Provides a means to control the underlying header storage capacity. (#1451)

Motivation:

It's not always possible to initialize `HTTPHeaders` with all the headers one needs to set. In such case, the underlying array will be expanded as and when needed upon headers being appended. `Array` provides the means to reserve capacity to optimize such use case. This PR exposes this functionality in `HTTPHeaders`.

Modifications:

Expose method `reserveCapacity(_ minimumCapacity: Int)` and computed variable `capacity: Int` in `HTTPHeaders` and added some tests.

Result:

Developers will be able to control the capacity of the underlying headers array as such:

```
var headers = HTTPHeaders()
headers.reserveCapacity(5)
```

Co-authored-by: Ludovic Dewailly <ldewailly@apple.com>
This commit is contained in:
Ludovic DewaillyandLudovic Dewailly authored and GitHub committed 2020-03-16 17:47:34 +00:00
1 parent e5298263ec
commit fefd0c854f
3 files changed
+32

No files matched your search

+15
View File
@@ -476,6 +476,21 @@ public struct HTTPHeaders: CustomStringConvertible, ExpressibleByDictionaryLiter
}
}
extension HTTPHeaders {
/// The total number of headers that can be contained without allocating new storage.
public var capacity: Int {
return self.headers.capacity
}
/// Reserves enough space to store the specified number of headers.
///
/// - Parameter minimumCapacity: The requested number of headers to store.
public mutating func reserveCapacity(_ minimumCapacity: Int) {
self.headers.reserveCapacity(minimumCapacity)
}
}
extension ByteBuffer {
/// Serializes this HTTP header block to bytes suitable for writing to the wire.