Improve Performance of HTTPField.value (#146)

### Motivation

When instantiating new HTTPFields, the values need to be validated,
legalized and turned into the internal representation.
We use this representation to expose a string in `HTTPField.value` as
well as to iterate over raw bytes using
`HTTPField.withUnsafeBytesOfValue`.

Currently the internal representation is to use a ISO Latin 1 String to
store the bytes only once. This however means that we need to transcode
back and forth for non ascii values, which causes a lot of extra
computations and allocations.

### Modifications

1. Moved logic to a standalone structure for `HTTPField.Value` rather
than `ISOLatin1String`
2. Added Unit Tests and Benchmarks
3. Swapped internal storage of values to be an enum of either a string
or a value
4. Whenever the bytes stored are valid utf8, we will not always store
the value as a raw string. This means that we need the least amount of
allocations and transcoding when operating with the value through our
public API
5. We only store a raw array of bytes whenever the bytes are not valid
utf8. This speeds up `withUnsafeBytesOfValue` massively and
`HTTPField.value` a little bit
6. During legalization, store the modified bytes in a temporary buffer
instead of using lazy collections to avoid allocating an array and speed
up utf8 validation
7. Use spans whenever possible to speed up access to bytes without
boundary checks

### Result

These are the changes on the benchmarks added in this PR

```
HTTPField-asciiBytesValueRoundTrip
Instructions: 28K -> 6847 (-75%)
Malloc: 1 -> 1

HTTPField-asciiValueRoundTrip
Instructions: 7367 -> 3081 (-58%)
Malloc: 0 -> 0

HTTPField-invalidAsciiBytesValueRoundTrip
Instructions: 133K -> 7255 (-94%)
Malloc: 6 -> 1 (-83%)

HTTPField-invalidAsciiBytesValueRoundTripAsLenientValue
Instructions: 62K -> 6623 (-89%)
Malloc: 3 -> 1 (-66%)

HTTPField-invalidAsciiValueRoundTrip
Instructions: 93K -> 6959 (-92%)
Malloc: 5 -> 1 (-80%)

HTTPField-invalidNonASCIIValueRoundTrip
Instructions: 393K -> 8735 (-97%)
Malloc: 11 -> 1 (-91%)

HTTPField-nonASCIIValueRoundTrip
Instructions: 131K -> 3219 (-97%)
Malloc: 4 -> 0 (-100%)
```

---------

Co-authored-by: Fabian Fett <fabianfett@apple.com>
This commit is contained in:
Mathias QuinteroandFabian Fett authored and GitHub committed 2026-08-31 11:30:56 +02:00
1 parent 6226da4bb9
commit f41c097554
43 files changed
+730 -211

No files matched your search

+7 -7
View File
@@ -58,10 +58,10 @@ public struct HTTPRequest: Sendable, Hashable {
/// A convenient way to access the value of the ":method" pseudo header field.
public var method: Method {
get {
Method(unchecked: self.pseudoHeaderFields.method.rawValue._storage)
Method(unchecked: self.pseudoHeaderFields.method.value)
}
set {
self.pseudoHeaderFields.method.rawValue = ISOLatin1String(unchecked: newValue.rawValue)
self.pseudoHeaderFields.method.rawValue = HTTPField.Value(unchecked: newValue.rawValue)
}
}
@@ -205,7 +205,7 @@ public struct HTTPRequest: Sendable, Hashable {
}
set {
precondition(newValue.name == .method, "Cannot change pseudo-header field name")
precondition(HTTPField.isValidToken(newValue.rawValue._storage), "Invalid character in method field")
precondition(newValue.rawValue.isValidToken, "Invalid character in method field")
if !isKnownUniquelyReferenced(&self._storage) {
self._storage = self._storage.copy()
@@ -313,7 +313,7 @@ public struct HTTPRequest: Sendable, Hashable {
/// - path: The value of the ":path" pseudo header field.
/// - headerFields: The request header fields.
public init(method: Method, scheme: String?, authority: String?, path: String?, headerFields: HTTPFields = [:]) {
let methodField = HTTPField(name: .method, uncheckedValue: ISOLatin1String(unchecked: method.rawValue))
let methodField = HTTPField(name: .method, uncheckedValue: HTTPField.Value(unchecked: method.rawValue))
let schemeField = scheme.map { HTTPField(name: .scheme, value: $0) }
let authorityField = authority.map { HTTPField(name: .authority, value: $0) }
let pathField = path.map { HTTPField(name: .path, value: $0) }
@@ -330,7 +330,7 @@ public struct HTTPRequest: Sendable, Hashable {
@available(HTTPTypes 1.0, *)
extension HTTPRequest: CustomDebugStringConvertible {
public var debugDescription: String {
"(\(self.pseudoHeaderFields.method.rawValue._storage)) \((self.pseudoHeaderFields.scheme?.value).map { "\($0)://" } ?? "")\(self.pseudoHeaderFields.authority?.value ?? "")\(self.pseudoHeaderFields.path?.value ?? "")"
"(\(self.pseudoHeaderFields.method.value)) \((self.pseudoHeaderFields.scheme?.value).map { "\($0)://" } ?? "")\(self.pseudoHeaderFields.authority?.value ?? "")\(self.pseudoHeaderFields.path?.value ?? "")"
}
}
@@ -420,10 +420,10 @@ extension HTTPRequest.PseudoHeaderFields: Codable {
debugDescription: "\":method\" pseudo header field is missing"
)
}
guard HTTPField.isValidToken(method.rawValue._storage) else {
guard method.rawValue.isValidToken else {
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "\"\(method.rawValue._storage)\" is not a valid method"
debugDescription: "\"\(method.value)\" is not a valid method"
)
}
self.init(