Mathias QuinteroandFabian Fett f41c097554 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>
2026-08-31 11:30:56 +02:00
2026-06-05 10:54:44 -07:00
2026-06-01 11:57:33 -07:00
2024-11-14 13:33:27 +00:00
2024-07-16 17:10:23 -07:00
2024-10-24 22:30:18 +02:00
2023-06-30 10:06:35 -07:00
2024-10-24 22:30:18 +02:00
2023-06-30 10:06:35 -07:00
2024-07-16 17:10:23 -07:00
2023-06-30 10:06:35 -07:00
2023-06-30 10:06:35 -07:00
2026-06-22 15:33:26 -07:00

Swift HTTP Types

Swift HTTP Types are version-independent HTTP currency types designed for both clients and servers. They provide a common set of representations for HTTP requests and responses, focusing on modern HTTP features.

Getting Started

Add the following dependency clause to your Package.swift:

dependencies: [
    .package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.0")
]

The HTTPTypes library exposes the core HTTP currency types, including HTTPRequest, HTTPResponse, and HTTPFields, as well as the HTTP/2 and HTTP/3 error code types HTTP2ErrorCode and HTTP3ErrorCode.

The HTTPTypesFoundation library provides conveniences for using new HTTP types with Foundation, including bidirectional convertors between the new types and Foundation URL types, and URLSession convenience methods with the new types.

The NIOHTTPTypes, NIOHTTPTypesHTTP1, and NIOHTTPTypesHTTP2 libraries provide channel handlers for translating the version-specific NIO HTTP types with the new HTTP types. They can be found in swift-nio-extras.

Usage

Create a request

let request = HTTPRequest(method: .get, scheme: "https", authority: "www.example.com", path: "/")

Create a request from a Foundation URL

var request = HTTPRequest(method: .get, url: URL(string: "https://www.example.com/")!)
request.method = .post
request.path = "/upload"

Create a response

let response = HTTPResponse(status: .ok)

Access and modify header fields

extension HTTPField.Name {
    static let myCustomHeader = Self("My-Custom-Header")!
}

// Set
request.headerFields[.userAgent] = "MyApp/1.0"
request.headerFields[.myCustomHeader] = "custom-value"
request.headerFields[values: .acceptLanguage] = ["en-US", "zh-Hans-CN"]

// Get
request.headerFields[.userAgent] // "MyApp/1.0"
request.headerFields[.myCustomHeader] // "custom-value"
request.headerFields[.acceptLanguage] // "en-US, zh-Hans-CN"
request.headerFields[values: .acceptLanguage] // ["en-US", "zh-Hans-CN"]

Use with URLSession

var request = HTTPRequest(method: .post, url: URL(string: "https://www.example.com/upload")!)
request.headerFields[.userAgent] = "MyApp/1.0"
let (responseBody, response) = try await URLSession.shared.upload(for: request, from: requestBody)
guard response.status == .created else {
    // Handle error
}

Use with SwiftNIO

channel.configureHTTP2Pipeline(mode: .server) { channel in
    channel.pipeline.addHandlers([
        HTTP2FramePayloadToHTTPServerCodec(),
        ExampleChannelHandler()
    ])
}.map { _ in () }
final class ExampleChannelHandler: ChannelDuplexHandler {
    typealias InboundIn = HTTPTypeServerRequestPart
    typealias OutboundOut = HTTPTypeServerResponsePart

    func channelRead(context: ChannelHandlerContext, data: NIOAny) {
        switch unwrapInboundIn(data) {
        case .head(let request):
            // Handle request headers
        case .body(let body):
            // Handle request body
        case .end(let trailers):
            // Handle complete request
            let response = HTTPResponse(status: .ok)
            context.write(wrapOutboundOut(.head(response)), promise: nil)
            context.writeAndFlush(wrapOutboundOut(.end(nil)), promise: nil)
        }
    }
}

Developing HTTP Types

For the most part, HTTP Types development is as straightforward as any other SwiftPM project. With that said, we do have a few processes that are worth understanding before you contribute. For details, please see CONTRIBUTING.md in this repository.

Please note that all work on HTTP Types is covered by the Swift HTTP Types Code of Conduct.

S
Description
Mirror of github.com/apple/swift-http-types
Readme Apache-2.0
675 KiB
0 Stars 1 Watchers 0 Forks
Languages
Swift 99%
Shell 1%