mirror of
https://github.com/apple/swift-nio
synced 2026-11-28 05:14:23 +00:00
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:
1 parent
e5298263ec
commit
fefd0c854f
3 files changed
+32
No files matched your search
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user