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:
Fabian Fett authored and GitHub committed 2023-05-17 02:09:06 -07:00
1 parent 428b0f3551
commit 145eec4e4f
3 files changed
+38 -3

No files matched your search

+10 -1
View File
@@ -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 {
+27 -1
View File
@@ -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)
}
}
+1 -1
View File
@@ -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... "