Skip to content
Open
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
14 changes: 13 additions & 1 deletion Sources/HTTPTypes/HTTPFields.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,21 @@ extension HTTPFields: Equatable {

extension HTTPFields: Hashable {
public func hash(into hasher: inout Hasher) {
// Equality ignores the order of differently named fields, so hashing
// must too. Combine each name's sequence (order of same-named fields
// still matters), then mix those group hashes commutatively.
var grouped = [String: Hasher]()

@nerdsupremacist nerdsupremacist Aug 28, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I wonder if there's a way to perform this change without a dictionary.

The dictionary will cause extra allocations, which might not be the fastest when doing basic hashing

That being said, I don't know off the top of my head, all the contexts in which we hash this struct and how critical performance is. So happy to continue with this implementation and we look for a performance improvement later. Correctness is the top priority

for field in self.fields {
hasher.combine(field)
let key = field.name.canonicalName
grouped[key, default: Hasher()].combine(field)
}
var combined = 0
for nameHasher in grouped.values {
// HTTPField already hashes its name, so XOR the per-name sequence hashes directly.
combined ^= nameHasher.finalize()
}
hasher.combine(combined)
hasher.combine(self.fields.count)
}
}

Expand Down
6 changes: 1 addition & 5 deletions Tests/HTTPTypesTests/HTTPTypesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,7 @@ extension HTTPField.Name {
.acceptEncoding: "gzip",
]
#expect(fields1 == fields2)

// Equal values must therefore hash equally.
withKnownIssue("HTTPFields.hash(into:) is order sensitive while == is not") {
#expect(fields1.hashValue == fields2.hashValue)
}
#expect(fields1.hashValue == fields2.hashValue)
}

@Test func sendable() {
Expand Down