Improve Performance for HTTPField.Name Initializers (#144)

# Motivation

Currently when initializing a Field Name, we create a canonical
lowercased version of the field name. However, it is reasonable to
expect that a significant amount of the time the users will insert a
name string that is already lowercased, so turning it into lowercase
creates an extra unnecessary allocation

# Changes

This changes the validation step for the name to compute in a single
pass whether the name is valid and if it's already in the canonical
lowercased format. If it's already lowercased, then we can skip the
extra allocation and just reuse the same string. if it's valid but not
lowercased we can default to the previous logic.

For ultimate performance this iterates over Spans whenever we use Swift
6.4. For other Swift versions, we default to iterating over the UTF8View
instead, which is slightly slower, but keeps us backwards compatible.

# Result

More performance
This commit is contained in:
Mathias Quintero authored and GitHub committed 2026-08-25 15:51:28 +02:00
1 parent e186752bbc
commit c6bd833124
7 files changed
+129 -33

No files matched your search

+4 -17
View File
@@ -42,11 +42,12 @@ extension HTTPField {
/// - Parameter name: The name of the HTTP field. It can be accessed from the `rawName`
/// property.
public init?(_ name: String) {
guard HTTPField.isValidToken(name) else {
guard let canonicalName = HTTPField.validatedCanonicalName(name) else {
return nil
}
self.rawName = name
self.canonicalName = name.lowercased()
self.canonicalName = canonicalName
}
/// Create an HTTP field name from a string produced by HPACK or QPACK decoders used in
@@ -59,27 +60,13 @@ extension HTTPField {
/// be lowercased.
@available(HTTPTypes 1.2, *)
public init?(parsed name: String) {
guard !name.isEmpty else {
return nil
}
let token: Substring
if name.utf8.first == UInt8(ascii: ":") {
token = name.dropFirst()
} else {
token = Substring(name)
}
guard
token.utf8.allSatisfy({
switch $0 {
case 0x21, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2A, 0x2B, 0x2D, 0x2E, 0x5E, 0x5F, 0x60, 0x7C, 0x7E:
return true
case 0x30...0x39, 0x61...0x7A: // DIGHT, ALPHA
return true
default:
return false
}
})
else {
guard HTTPField.isValidToken(token) else {
return nil
}
self.rawName = name