Improve Performance of HTTP Request and Response inits (#145)

### Motivation

Currently constructing a request or response from parsed http fields
requires multiple unnecessary allocations.
Mainly due to:
1. Allocations from resizing the array as we keep appending more values
to it
2. When running validation checks, we temporarily use an array of values
to represent the values for a given field name. When those fields are
set this requires an extra array allocation just to run the check

### Modifications

- Reserve needed capacity during initial of parsed fields to prevent
multiple subsequent allocations
- Avoid allocating an array while validating content length, content
disposition and location headers by validating them at insertion time
and therefore only iterating once

### Result

Reduction in allocations and instructions in benchmarks

Co-authored-by: Fabian Fett <fabianfett@apple.com>
This commit is contained in:
Mathias QuinteroandFabian Fett authored and GitHub committed 2026-08-26 17:14:36 +02:00
1 parent c6bd833124
commit af7e9d67a3
37 files changed
+109 -62

No files matched your search

@@ -169,6 +169,20 @@ let benchmarks: @Sendable () -> Void = {
}
}
Benchmark("HTTPRequest.init(parsed)", configuration: makeDefaultConfiguration()) {
benchmark in
for _ in benchmark.scaledIterations {
blackHole(try HTTPRequest(parsed: parsedRequestFields))
}
}
Benchmark("HTTPResponse.init(parsed)", configuration: makeDefaultConfiguration()) {
benchmark in
for _ in benchmark.scaledIterations {
blackHole(try HTTPResponse(parsed: parsedResponseFields))
}
}
// MARK: Scaling
// The same operations over field lists of 8, 16, 32, 64 and 128 fields. See
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 24
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 21
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 22
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 23
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 20
"mallocCountTotal" : 17
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 24
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 21
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 22
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 23
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 20
"mallocCountTotal" : 17
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 26
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 23
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 24
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 25
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 22
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 26
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 23
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 24
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 25
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 22
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 26
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 23
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 24
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 25
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 22
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
+40 -37
View File
@@ -21,7 +21,12 @@ struct HTTPParsedFields {
private var path: ISOLatin1String?
private var extendedConnectProtocol: ISOLatin1String?
private var status: ISOLatin1String?
private var fields: HTTPFields = .init()
private var contentLength: ISOLatin1String?
private var contentDisposition: ISOLatin1String?
private var location: ISOLatin1String?
private var fields: HTTPFields
enum ParsingError: Error {
case invalidName
@@ -45,6 +50,18 @@ struct HTTPParsedFields {
case multipleLocation
}
init() {
self.fields = .init()
}
init(parsed: [HTTPFields.Element]) throws {
self.fields = .init()
self.fields.reserveCapacity(parsed.count)
for field in parsed {
try self.add(field: field)
}
}
mutating func add(field: HTTPField) throws {
if field.name.isPseudo {
if !self.fields.isEmpty {
@@ -85,22 +102,29 @@ struct HTTPParsedFields {
throw ParsingError.invalidPseudoName
}
} else {
switch field.name {
case .contentLength:
if let contentLength = self.contentLength, contentLength != field.rawValue {
throw ParsingError.multipleContentLength
}
self.contentLength = field.rawValue
case .contentDisposition:
if let contentDisposition = self.contentDisposition, contentDisposition != field.rawValue {
throw ParsingError.multipleContentDisposition
}
self.contentDisposition = field.rawValue
case .location:
if let location = self.location, location != field.rawValue {
throw ParsingError.multipleLocation
}
self.location = field.rawValue
default:
break
}
self.fields.append(field)
}
}
private func validateFields() throws {
guard self.fields[values: .contentLength].allElementsSame else {
throw ParsingError.multipleContentLength
}
guard self.fields[values: .contentDisposition].allElementsSame else {
throw ParsingError.multipleContentDisposition
}
guard self.fields[values: .location].allElementsSame else {
throw ParsingError.multipleLocation
}
}
var request: HTTPRequest {
get throws {
guard let method = self.method else {
@@ -112,7 +136,6 @@ struct HTTPParsedFields {
if self.status != nil {
throw ParsingError.requestWithResponsePseudo
}
try self.validateFields()
var request = HTTPRequest(
method: requestMethod,
scheme: self.scheme,
@@ -143,7 +166,6 @@ struct HTTPParsedFields {
if !HTTPResponse.Status.isValidStatus(statusString) {
throw ParsingError.invalidStatus
}
try self.validateFields()
return HTTPResponse(status: .init(code: Int(statusString)!), headerFields: self.fields)
}
}
@@ -155,7 +177,6 @@ struct HTTPParsedFields {
{
throw ParsingError.trailerFieldsWithPseudo
}
try self.validateFields()
return self.fields
}
}
@@ -183,15 +204,6 @@ extension HTTPRequest {
}
}
extension Array where Element: Equatable {
fileprivate var allElementsSame: Bool {
guard let first = self.first else {
return true
}
return dropFirst().allSatisfy { $0 == first }
}
}
@available(HTTPTypes 1.2, *)
extension HTTPRequest {
/// Create an HTTP request with an array of parsed `HTTPField`. The fields must include the
@@ -200,10 +212,7 @@ extension HTTPRequest {
/// - Parameter fields: The array of parsed `HTTPField` produced by HPACK or QPACK decoders
/// used in modern HTTP versions.
public init(parsed fields: [HTTPField]) throws {
var parsedFields = HTTPParsedFields()
for field in fields {
try parsedFields.add(field: field)
}
let parsedFields = try HTTPParsedFields(parsed: fields)
self = try parsedFields.request
}
}
@@ -216,10 +225,7 @@ extension HTTPResponse {
/// - Parameter fields: The array of parsed `HTTPField` produced by HPACK or QPACK decoders
/// used in modern HTTP versions.
public init(parsed fields: [HTTPField]) throws {
var parsedFields = HTTPParsedFields()
for field in fields {
try parsedFields.add(field: field)
}
let parsedFields = try HTTPParsedFields(parsed: fields)
self = try parsedFields.response
}
}
@@ -232,10 +238,7 @@ extension HTTPFields {
/// - Parameter fields: The array of parsed `HTTPField` produced by HPACK or QPACK decoders
/// used in modern HTTP versions.
public init(parsedTrailerFields fields: [HTTPField]) throws {
var parsedFields = HTTPParsedFields()
for field in fields {
try parsedFields.add(field: field)
}
let parsedFields = try HTTPParsedFields(parsed: fields)
self = try parsedFields.trailerFields
}
}