Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce ARC in HPACK closestMatch #268

Merged
merged 1 commit into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions Sources/NIOHPACK/HPACKHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,12 @@ private extension Substring {
}


extension Sequence where Self.Element == UInt8 {
/// Compares the collection of `UInt8`s to a case insensitive collection.
///
/// This collection could be get from applying the `UTF8View`
/// property on the string protocol.
extension String.UTF8View {
/// Compares two UTF8 strings as case insensitive ASCII bytes.
///
/// - Parameter bytes: The string constant in the form of a collection of `UInt8`
/// - Returns: Whether the collection contains **EXACTLY** this array or no, but by ignoring case.
fileprivate func compareCaseInsensitiveASCIIBytes<T: Sequence>(to other: T) -> Bool
where T.Element == UInt8 {
fileprivate func compareCaseInsensitiveASCIIBytes(to other: String.UTF8View) -> Bool {
// fast path: we can get the underlying bytes of both
let maybeMaybeResult = self.withContiguousStorageIfAvailable { lhsBuffer -> Bool? in
other.withContiguousStorageIfAvailable { rhsBuffer in
Expand All @@ -491,7 +487,7 @@ extension Sequence where Self.Element == UInt8 {
}

@inline(never)
private func _compareCaseInsensitiveASCIIBytesSlowPath<T: Sequence>(to other: T) -> Bool where T.Element == UInt8 {
private func _compareCaseInsensitiveASCIIBytesSlowPath(to other: String.UTF8View) -> Bool {
return self.elementsEqual(other, by: { return (($0 & 0xdf) == ($1 & 0xdf) && $0.isASCII) })
}
}
Expand Down
24 changes: 19 additions & 5 deletions Sources/NIOHPACK/HeaderTables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,32 @@ struct HeaderTableStorage {
func closestMatch(name: String, value: String) -> MatchType {
var partialIndex: Int? = nil

for (index, header) in self.headers.enumerated() {
// Yes, I'm manually reimplementing IndexingIterator here. This is because
// the excess ARC in this loop shows up in our profiles pretty substantially,
// and it's triggered by https://bugs.swift.org/browse/SR-13931.
//
// Working around this until the above is resolved.
var offset = 0
var index = self.headers.startIndex

while index < self.headers.endIndex {
defer {
// Unchecked arithmetic is safe here, we can't overflow as offset can never exceed count.
offset &+= 1
self.headers.formIndex(after: &index)
}

// Check if the header name matches.
guard header.name.isEqualCaseInsensitiveASCIIBytes(to: name) else {
guard self.headers[index].name.isEqualCaseInsensitiveASCIIBytes(to: name) else {
continue
}

if partialIndex == nil {
partialIndex = index
partialIndex = offset
}

if value == header.value {
return .full(index)
if value == self.headers[index].value {
return .full(offset)
}
}

Expand Down