From af7e9d67a303e9b3244e128780bb513ca70ca658 Mon Sep 17 00:00:00 2001 From: Mathias Quintero Date: Wed, 26 Aug 2026 16:14:36 +0100 Subject: [PATCH] Improve Performance of HTTP Request and Response inits (#145) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### 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 --- .../HTTPFieldsBenchmarks/Benchmarks.swift | 14 ++++ ...railerFields)-decodeNFields-N=128.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=16.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=32.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=64.p90.json | 2 +- ...dTrailerFields)-decodeNFields-N=8.p90.json | 2 +- ...nchmarks.HTTPRequest.init(parsed).p90.json | 3 + ...chmarks.HTTPResponse.init(parsed).p90.json | 3 + ...railerFields)-decodeNFields-N=128.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=16.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=32.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=64.p90.json | 2 +- ...dTrailerFields)-decodeNFields-N=8.p90.json | 2 +- ...nchmarks.HTTPRequest.init(parsed).p90.json | 3 + ...chmarks.HTTPResponse.init(parsed).p90.json | 3 + ...railerFields)-decodeNFields-N=128.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=16.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=32.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=64.p90.json | 2 +- ...dTrailerFields)-decodeNFields-N=8.p90.json | 2 +- ...nchmarks.HTTPRequest.init(parsed).p90.json | 3 + ...chmarks.HTTPResponse.init(parsed).p90.json | 3 + ...railerFields)-decodeNFields-N=128.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=16.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=32.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=64.p90.json | 2 +- ...dTrailerFields)-decodeNFields-N=8.p90.json | 2 +- ...nchmarks.HTTPRequest.init(parsed).p90.json | 3 + ...chmarks.HTTPResponse.init(parsed).p90.json | 3 + ...railerFields)-decodeNFields-N=128.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=16.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=32.p90.json | 2 +- ...TrailerFields)-decodeNFields-N=64.p90.json | 2 +- ...dTrailerFields)-decodeNFields-N=8.p90.json | 2 +- ...nchmarks.HTTPRequest.init(parsed).p90.json | 3 + ...chmarks.HTTPResponse.init(parsed).p90.json | 3 + Sources/HTTPTypes/HTTPParsedFields.swift | 77 ++++++++++--------- 37 files changed, 109 insertions(+), 62 deletions(-) create mode 100644 Benchmarks/Thresholds/6.1/Benchmarks.HTTPRequest.init(parsed).p90.json create mode 100644 Benchmarks/Thresholds/6.1/Benchmarks.HTTPResponse.init(parsed).p90.json create mode 100644 Benchmarks/Thresholds/6.2/Benchmarks.HTTPRequest.init(parsed).p90.json create mode 100644 Benchmarks/Thresholds/6.2/Benchmarks.HTTPResponse.init(parsed).p90.json create mode 100644 Benchmarks/Thresholds/6.3/Benchmarks.HTTPRequest.init(parsed).p90.json create mode 100644 Benchmarks/Thresholds/6.3/Benchmarks.HTTPResponse.init(parsed).p90.json create mode 100644 Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPRequest.init(parsed).p90.json create mode 100644 Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPResponse.init(parsed).p90.json create mode 100644 Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPRequest.init(parsed).p90.json create mode 100644 Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPResponse.init(parsed).p90.json diff --git a/Benchmarks/Benchmarks/HTTPFieldsBenchmarks/Benchmarks.swift b/Benchmarks/Benchmarks/HTTPFieldsBenchmarks/Benchmarks.swift index 401e5ed..4836463 100644 --- a/Benchmarks/Benchmarks/HTTPFieldsBenchmarks/Benchmarks.swift +++ b/Benchmarks/Benchmarks/HTTPFieldsBenchmarks/Benchmarks.swift @@ -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 diff --git a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json index 7d1a075..106b3a6 100644 --- a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json +++ b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 24 + "mallocCountTotal" : 17 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json index c3df4ca..106b3a6 100644 --- a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json +++ b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 21 + "mallocCountTotal" : 17 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json index 6b8e738..106b3a6 100644 --- a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json +++ b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 22 + "mallocCountTotal" : 17 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json index 2311442..106b3a6 100644 --- a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json +++ b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 23 + "mallocCountTotal" : 17 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json index d49021a..106b3a6 100644 --- a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json +++ b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 20 + "mallocCountTotal" : 17 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPRequest.init(parsed).p90.json b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPRequest.init(parsed).p90.json new file mode 100644 index 0000000..ff64d7f --- /dev/null +++ b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPRequest.init(parsed).p90.json @@ -0,0 +1,3 @@ +{ + "mallocCountTotal" : 18 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.1/Benchmarks.HTTPResponse.init(parsed).p90.json b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPResponse.init(parsed).p90.json new file mode 100644 index 0000000..ff64d7f --- /dev/null +++ b/Benchmarks/Thresholds/6.1/Benchmarks.HTTPResponse.init(parsed).p90.json @@ -0,0 +1,3 @@ +{ + "mallocCountTotal" : 18 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json index 7d1a075..106b3a6 100644 --- a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json +++ b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 24 + "mallocCountTotal" : 17 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json index c3df4ca..106b3a6 100644 --- a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json +++ b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 21 + "mallocCountTotal" : 17 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json index 6b8e738..106b3a6 100644 --- a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json +++ b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 22 + "mallocCountTotal" : 17 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json index 2311442..106b3a6 100644 --- a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json +++ b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 23 + "mallocCountTotal" : 17 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json index d49021a..106b3a6 100644 --- a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json +++ b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 20 + "mallocCountTotal" : 17 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPRequest.init(parsed).p90.json b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPRequest.init(parsed).p90.json new file mode 100644 index 0000000..ff64d7f --- /dev/null +++ b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPRequest.init(parsed).p90.json @@ -0,0 +1,3 @@ +{ + "mallocCountTotal" : 18 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.2/Benchmarks.HTTPResponse.init(parsed).p90.json b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPResponse.init(parsed).p90.json new file mode 100644 index 0000000..ff64d7f --- /dev/null +++ b/Benchmarks/Thresholds/6.2/Benchmarks.HTTPResponse.init(parsed).p90.json @@ -0,0 +1,3 @@ +{ + "mallocCountTotal" : 18 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json index 41ab8be..1726cdd 100644 --- a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json +++ b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 26 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json index 2311442..1726cdd 100644 --- a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json +++ b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 23 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json index 7d1a075..1726cdd 100644 --- a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json +++ b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 24 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json index fcde1ab..1726cdd 100644 --- a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json +++ b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 25 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json index 6b8e738..1726cdd 100644 --- a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json +++ b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 22 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPRequest.init(parsed).p90.json b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPRequest.init(parsed).p90.json new file mode 100644 index 0000000..d49021a --- /dev/null +++ b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPRequest.init(parsed).p90.json @@ -0,0 +1,3 @@ +{ + "mallocCountTotal" : 20 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/6.3/Benchmarks.HTTPResponse.init(parsed).p90.json b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPResponse.init(parsed).p90.json new file mode 100644 index 0000000..d49021a --- /dev/null +++ b/Benchmarks/Thresholds/6.3/Benchmarks.HTTPResponse.init(parsed).p90.json @@ -0,0 +1,3 @@ +{ + "mallocCountTotal" : 20 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json index 41ab8be..1726cdd 100644 --- a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json +++ b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 26 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json index 2311442..1726cdd 100644 --- a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json +++ b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 23 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json index 7d1a075..1726cdd 100644 --- a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json +++ b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 24 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json index fcde1ab..1726cdd 100644 --- a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json +++ b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 25 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json index 6b8e738..1726cdd 100644 --- a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json +++ b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 22 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPRequest.init(parsed).p90.json b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPRequest.init(parsed).p90.json new file mode 100644 index 0000000..d49021a --- /dev/null +++ b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPRequest.init(parsed).p90.json @@ -0,0 +1,3 @@ +{ + "mallocCountTotal" : 20 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPResponse.init(parsed).p90.json b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPResponse.init(parsed).p90.json new file mode 100644 index 0000000..d49021a --- /dev/null +++ b/Benchmarks/Thresholds/nightly-main/Benchmarks.HTTPResponse.init(parsed).p90.json @@ -0,0 +1,3 @@ +{ + "mallocCountTotal" : 20 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json index 41ab8be..1726cdd 100644 --- a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json +++ b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=128.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 26 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json index 2311442..1726cdd 100644 --- a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json +++ b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=16.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 23 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json index 7d1a075..1726cdd 100644 --- a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json +++ b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=32.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 24 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json index fcde1ab..1726cdd 100644 --- a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json +++ b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=64.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 25 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json index 6b8e738..1726cdd 100644 --- a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json +++ b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPFields(parsedTrailerFields)-decodeNFields-N=8.p90.json @@ -1,3 +1,3 @@ { - "mallocCountTotal" : 22 + "mallocCountTotal" : 19 } \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPRequest.init(parsed).p90.json b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPRequest.init(parsed).p90.json new file mode 100644 index 0000000..d49021a --- /dev/null +++ b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPRequest.init(parsed).p90.json @@ -0,0 +1,3 @@ +{ + "mallocCountTotal" : 20 +} \ No newline at end of file diff --git a/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPResponse.init(parsed).p90.json b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPResponse.init(parsed).p90.json new file mode 100644 index 0000000..d49021a --- /dev/null +++ b/Benchmarks/Thresholds/nightly-next/Benchmarks.HTTPResponse.init(parsed).p90.json @@ -0,0 +1,3 @@ +{ + "mallocCountTotal" : 20 +} \ No newline at end of file diff --git a/Sources/HTTPTypes/HTTPParsedFields.swift b/Sources/HTTPTypes/HTTPParsedFields.swift index 984e43c..5fcc16c 100644 --- a/Sources/HTTPTypes/HTTPParsedFields.swift +++ b/Sources/HTTPTypes/HTTPParsedFields.swift @@ -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 } }