mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-28 05:14:25 +00:00
Add HPACKHeader.removeAll(keepingCapacity:) (#393)
In scenarios, in which we want to send lots of small HTTP/2 requests, we want to reuse a single HPACKHeaders object as often as we can. To support this we should have a removeAll(keepingCapacity:) method.
This commit is contained in:
1 parent
428b0f3551
commit
145eec4e4f
3 files changed
+38
-3
No files matched your search
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2023 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
@@ -425,6 +425,15 @@ extension HPACKHeaders {
|
||||
public mutating func reserveCapacity(_ minimumCapacity: Int) {
|
||||
self.headers.reserveCapacity(minimumCapacity)
|
||||
}
|
||||
|
||||
/// Removes all headers whilst optionally keeping the capacity.
|
||||
///
|
||||
/// - Parameter keepingCapacity: Whether to keep the underlying memory or not. Use this flag to
|
||||
/// reduce allocations when reusing the header collection.
|
||||
@inlinable
|
||||
public mutating func removeAll(keepingCapacity: Bool = false) {
|
||||
self.headers.removeAll(keepingCapacity: keepingCapacity)
|
||||
}
|
||||
}
|
||||
|
||||
extension HPACKHeaders: RandomAccessCollection {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2022-2023 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
@@ -180,4 +180,30 @@ final class HPACKHeadersTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testRemoveAll() {
|
||||
let original: HPACKHeaders = [
|
||||
"foo": "bar",
|
||||
"bar": "foo",
|
||||
"foo": "baz",
|
||||
"foo": "bar,baz",
|
||||
"foo": " bar ,baz ,,",
|
||||
"bar": "foo"
|
||||
]
|
||||
|
||||
XCTAssertEqual(original.capacity, 6)
|
||||
var keepCapacity = original
|
||||
XCTAssertEqual(keepCapacity.count, 6)
|
||||
XCTAssertEqual(keepCapacity.capacity, 6)
|
||||
keepCapacity.removeAll(keepingCapacity: true)
|
||||
XCTAssertEqual(keepCapacity.count, 0)
|
||||
XCTAssertEqual(keepCapacity.capacity, 6)
|
||||
|
||||
var lowerCapacity = original
|
||||
XCTAssertEqual(lowerCapacity.count, 6)
|
||||
XCTAssertEqual(lowerCapacity.capacity, 6)
|
||||
lowerCapacity.removeAll(keepingCapacity: false)
|
||||
XCTAssertEqual(lowerCapacity.count, 0)
|
||||
XCTAssertEqual(lowerCapacity.capacity, 0)
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
function replace_acceptable_years() {
|
||||
# this needs to replace all acceptable forms with 'YEARS'
|
||||
sed -e 's/20[12][78901]-20[12][890123]/YEARS/' -e 's/20[12][890123]/YEARS/'
|
||||
sed -e 's/20[12][789012]-20[12][890123]/YEARS/' -e 's/20[12][890123]/YEARS/'
|
||||
}
|
||||
|
||||
printf "=> Checking for unacceptable language... "
|
||||
|
||||
Reference in new issue
Block a user