mirror of
https://github.com/apple/swift-http-types
synced 2026-11-22 04:30:40 +00:00
[Benchmark] Rewrite benchmarks to be scaling (#136)
### Motivation As in #133 stated, we want to make performance improvements based on numbers. However in #133 we only introduced benchmarks for `HeaderFields` that are a fixed size long. This PR introduces scaling benchmarks so that we can test `HeaderFields` with different lengths (N=8, 16, 32, 64, 128) ### Changes - Add scaling `HeaderFields` benchmarks - Remove fixed size `HeaderFields` benchmarks ### Result A clearer view into performance.
This commit is contained in:
1 parent
7d65507593
commit
e84a4ff0bc
434 files changed
+1468
-164
No files matched your search
@@ -25,12 +25,17 @@ let defaultMetrics: [BenchmarkMetric] = [
|
||||
.instructions,
|
||||
]
|
||||
|
||||
var defaultConfiguration: Benchmark.Configuration {
|
||||
.init(
|
||||
metrics: defaultMetrics,
|
||||
scalingFactor: .one,
|
||||
maxDuration: .seconds(10),
|
||||
maxIterations: 10_000
|
||||
func makeDefaultConfiguration(
|
||||
metrics: [BenchmarkMetric] = defaultMetrics,
|
||||
scalingFactor: BenchmarkScalingFactor = .one,
|
||||
maxDuration: Duration = .seconds(3),
|
||||
maxIterations: Int = 10_000
|
||||
) -> Benchmark.Configuration {
|
||||
Benchmark.Configuration(
|
||||
metrics: metrics,
|
||||
scalingFactor: scalingFactor,
|
||||
maxDuration: maxDuration,
|
||||
maxIterations: maxIterations
|
||||
)
|
||||
}
|
||||
|
||||
@@ -115,115 +120,12 @@ extension HTTPField.Name {
|
||||
// MARK: - Benchmarks
|
||||
|
||||
let benchmarks: @Sendable () -> Void = {
|
||||
|
||||
// MARK: Construction
|
||||
|
||||
Benchmark("HTTPFields.init(dictionaryLiteral)", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
let fields: HTTPFields = [
|
||||
.contentType: "application/json",
|
||||
.contentLength: "42",
|
||||
.connection: "keep-alive",
|
||||
.accept: "application/json",
|
||||
.acceptEncoding: "gzip, deflate, br",
|
||||
]
|
||||
blackHole(fields)
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark("HTTPFields.append - full response header set", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
var fields = HTTPFields()
|
||||
for field in parsedResponseFields where !field.name.isPseudoName {
|
||||
fields.append(field)
|
||||
}
|
||||
blackHole(fields)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Decoding — the HTTP/2 & HTTP/3 receive path
|
||||
|
||||
Benchmark("HTTPRequest(parsed) - decode request header block", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(try HTTPRequest(parsed: parsedRequestFields))
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark("HTTPResponse(parsed) - decode response header block", configuration: defaultConfiguration) {
|
||||
benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(try HTTPResponse(parsed: parsedResponseFields))
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark("HTTPFields(parsedTrailerFields) - decode trailers", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(try HTTPFields(parsedTrailerFields: parsedTrailerFieldList))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Encoding — the send path, i.e. reading every field back out
|
||||
|
||||
Benchmark("HTTPFields - iterate all fields (serialize)", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
for field in sampleResponseFields {
|
||||
blackHole(field.name.canonicalName)
|
||||
blackHole(field.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Lookup
|
||||
|
||||
Benchmark("HTTPFields - lookup single-valued fields by name", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(sampleResponseFields[.contentType])
|
||||
blackHole(sampleResponseFields[.contentLength])
|
||||
blackHole(sampleResponseFields[.cacheControl])
|
||||
blackHole(sampleResponseFields[.eTag])
|
||||
blackHole(sampleResponseFields[.location])
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark("HTTPFields - lookup multi-valued field by name", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(sampleResponseFields[.setCookie])
|
||||
blackHole(sampleResponseFields[values: .setCookie])
|
||||
blackHole(sampleResponseFields[fields: .setCookie])
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark("HTTPFields.contains", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(sampleResponseFields.contains(.contentType))
|
||||
blackHole(sampleResponseFields.contains(.transferEncoding))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Mutation
|
||||
|
||||
Benchmark("HTTPFields - set and overwrite field values", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
var fields = sampleResponseFields
|
||||
fields[.contentLength] = "512"
|
||||
fields[.contentType] = "text/plain"
|
||||
fields[.location] = "https://www.example.com/moved"
|
||||
blackHole(fields)
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark("HTTPFields - cookie round trip", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
var fields = HTTPFields()
|
||||
fields[.cookie] = "session=abc123; theme=dark; locale=en_US; consent=1"
|
||||
blackHole(fields[.cookie])
|
||||
blackHole(fields)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Field names
|
||||
|
||||
Benchmark("HTTPField.Name.init - mixed case names", configuration: defaultConfiguration) { benchmark in
|
||||
Benchmark(
|
||||
"HTTPField.Name.init-mixedCaseNames",
|
||||
configuration: makeDefaultConfiguration(scalingFactor: .kilo)
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
for name in rawFieldNames {
|
||||
blackHole(HTTPField.Name(name))
|
||||
@@ -231,7 +133,10 @@ let benchmarks: @Sendable () -> Void = {
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark("HTTPField.Name.init - already lowercase names", configuration: defaultConfiguration) { benchmark in
|
||||
Benchmark(
|
||||
"HTTPField.Name.init-alreadyLowercaseNames",
|
||||
configuration: makeDefaultConfiguration(scalingFactor: .kilo)
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
for name in lowercaseFieldNames {
|
||||
blackHole(HTTPField.Name(name))
|
||||
@@ -241,7 +146,7 @@ let benchmarks: @Sendable () -> Void = {
|
||||
|
||||
// MARK: ISO-Latin-1 values
|
||||
|
||||
Benchmark("HTTPField - non-ASCII value round trip", configuration: defaultConfiguration) { benchmark in
|
||||
Benchmark("HTTPField-nonASCIIValueRoundTrip", configuration: makeDefaultConfiguration()) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
let field = HTTPField(name: .contentDisposition, value: latin1FieldValue)
|
||||
blackHole(field.value)
|
||||
@@ -251,16 +156,22 @@ let benchmarks: @Sendable () -> Void = {
|
||||
|
||||
// MARK: URL conversion
|
||||
|
||||
Benchmark("HTTPRequest(method, url) - request from URL", configuration: defaultConfiguration) { benchmark in
|
||||
Benchmark("HTTPRequest(method,url)-requestFromURL", configuration: makeDefaultConfiguration()) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(HTTPRequest(method: .get, url: sampleURL, headerFields: sampleRequestFields))
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark("HTTPRequest.url - synthesize URL from pseudo fields", configuration: defaultConfiguration) {
|
||||
Benchmark("HTTPRequest.url-synthesizeURLFromPseudoFields", configuration: makeDefaultConfiguration()) {
|
||||
benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(sampleRequest.url)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Scaling
|
||||
|
||||
// The same operations over field lists of 8, 16, 32, 64 and 128 fields. See
|
||||
// `HTTPFieldsScalingBenchmarks.swift`.
|
||||
registerHTTPFieldsScalingBenchmarks()
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift open source project
|
||||
//
|
||||
// Copyright (c) 2026 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Benchmark
|
||||
import HTTPTypes
|
||||
|
||||
/// Benchmarks that run the same operation over field lists of 8, 16, 32, 64 and 128 fields.
|
||||
///
|
||||
/// The point of these is the *shape of the curve*, not the individual number: operations that cost
|
||||
/// the same on a small field list can cost wildly different amounts on a large one, and that
|
||||
/// difference is what decides whether a change to `HTTPFields` is worth making. A single measurement
|
||||
/// at a single size cannot show it.
|
||||
///
|
||||
/// See `HTTPFieldsScalingFixtures.swift` for the field lists.
|
||||
///
|
||||
/// Registered from the `benchmarks` closure in `Benchmarks.swift`, because package-benchmark only
|
||||
/// picks up one such closure per target.
|
||||
func registerHTTPFieldsScalingBenchmarks() {
|
||||
validateScalingFixtures()
|
||||
|
||||
for scalingCase in scalingCases {
|
||||
let n = scalingCase.n
|
||||
let fields = scalingCase.readFields
|
||||
|
||||
// MARK: contains
|
||||
|
||||
// Measured with a large scaling factor because a single call is too cheap to resolve against
|
||||
// the measurement overhead. Note that package-benchmark divides the reported numbers by the
|
||||
// scaling factor, so they stay comparable to the benchmarks below.
|
||||
Benchmark(
|
||||
"HTTPFields.contains-hit-N=\(n)",
|
||||
configuration: makeDefaultConfiguration(scalingFactor: .kilo)
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(fields.contains(scalingPresentName))
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark(
|
||||
"HTTPFields.contains-miss-N=\(n)",
|
||||
configuration: makeDefaultConfiguration(scalingFactor: .kilo)
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(fields.contains(scalingAbsentName))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Lookup
|
||||
|
||||
// Exactly one field carries this name at every size.
|
||||
Benchmark(
|
||||
"HTTPFields[name]-singleValuedField-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(fields[scalingPresentName])
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark(
|
||||
"HTTPFields[name]-miss-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(fields[scalingAbsentName])
|
||||
}
|
||||
}
|
||||
|
||||
// Cookies are what makes a field list large, so these are the lookups that have to retrieve
|
||||
// a growing number of fields. The three of them are separate benchmarks rather than one,
|
||||
// because they return different things — one joined string, the values, the fields — and a
|
||||
// combined number would not say which of them scales.
|
||||
Benchmark(
|
||||
"HTTPFields[name]-allCookieFieldsJoined-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(fields[.cookie])
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark(
|
||||
"HTTPFields[values]-allCookieFields-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(fields[values: .cookie])
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark(
|
||||
"HTTPFields[fields]-allCookieFields-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(fields[fields: .cookie])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Equality
|
||||
|
||||
// Both sides of every pair were built independently rather than copied from one another, so
|
||||
// equality always has to do the real comparison.
|
||||
let equalSameOrder = scalingCase.equalSameOrder
|
||||
Benchmark(
|
||||
"HTTPFields.==-equal-sameOrder-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(equalSameOrder.lhs == equalSameOrder.rhs)
|
||||
}
|
||||
}
|
||||
|
||||
let equalDifferentOrder = scalingCase.equalDifferentOrder
|
||||
Benchmark(
|
||||
"HTTPFields.==-equal-differentOrder-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(equalDifferentOrder.lhs == equalDifferentOrder.rhs)
|
||||
}
|
||||
}
|
||||
|
||||
let mismatchSameOrder = scalingCase.mismatchSameOrder
|
||||
Benchmark(
|
||||
"HTTPFields.==-differsAt80%-sameOrder-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(mismatchSameOrder.lhs == mismatchSameOrder.rhs)
|
||||
}
|
||||
}
|
||||
|
||||
let mismatchDifferentOrder = scalingCase.mismatchDifferentOrder
|
||||
Benchmark(
|
||||
"HTTPFields.==-differsAt80%-differentOrder-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(mismatchDifferentOrder.lhs == mismatchDifferentOrder.rhs)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Building
|
||||
|
||||
let sourceFields = scalingCase.fields
|
||||
Benchmark(
|
||||
"HTTPFields.append-buildFromNFields-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
var built = HTTPFields()
|
||||
for field in sourceFields {
|
||||
built.append(field)
|
||||
}
|
||||
blackHole(built)
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark(
|
||||
"HTTPFields(parsedTrailerFields)-decodeNFields-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(try HTTPFields(parsedTrailerFields: sourceFields))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Copy-on-write mutation
|
||||
|
||||
// Writing through a second reference, which is what a middleware that adds or rewrites a
|
||||
// header does. Both of these pay for whatever the copy costs; they differ in whether the
|
||||
// write then replaces an existing field or adds a new one.
|
||||
Benchmark(
|
||||
"HTTPFields-copyThenOverwriteExistingField-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
var copy = fields
|
||||
copy[scalingPresentName] = "benchmark"
|
||||
blackHole(copy)
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark(
|
||||
"HTTPFields-copyThenInsertNewField-N=\(n)",
|
||||
configuration: makeDefaultConfiguration()
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
var copy = fields
|
||||
copy[scalingAbsentName] = "chunked"
|
||||
blackHole(copy)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift open source project
|
||||
//
|
||||
// Copyright (c) 2026 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import HTTPTypes
|
||||
|
||||
// MARK: - Field lists
|
||||
|
||||
/// Field lists of increasing size, used to measure how `HTTPFields` operations scale.
|
||||
///
|
||||
/// The lists are nested: every list is a prefix of the next larger one, so a difference between two
|
||||
/// sizes is only ever caused by the fields that were added, never by the fields that were already
|
||||
/// there.
|
||||
///
|
||||
/// Real field lists grow past ~16 fields almost exclusively because of cookies, so that is how these
|
||||
/// grow too: the ordinary headers stop at 16 and everything beyond that is a `Cookie` field. The two
|
||||
/// sizes therefore vary independently — the total number of fields keeps growing while the number of
|
||||
/// distinct names does not — and comparing the small sizes against the large ones shows which of the
|
||||
/// two a given operation is sensitive to.
|
||||
|
||||
/// `Cookie` fields with distinct values, so that no two fields in a list compare equal.
|
||||
private func cookieFields(_ range: Range<Int>) -> [HTTPField] {
|
||||
range.map { HTTPField(name: .cookie, value: "cookie\($0)=aBcDeF0123456789-\($0)") }
|
||||
}
|
||||
|
||||
/// A name that has no static member on `HTTPField.Name`.
|
||||
private func name(_ string: String) -> HTTPField.Name {
|
||||
HTTPField.Name(string)!
|
||||
}
|
||||
|
||||
/// 6 ordinary headers and 2 cookies.
|
||||
let scalingFields8: [HTTPField] =
|
||||
[
|
||||
HTTPField(name: .connection, value: "keep-alive"),
|
||||
HTTPField(name: .userAgent, value: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)"),
|
||||
HTTPField(name: .accept, value: "text/html,application/xhtml+xml,application/xml;q=0.9"),
|
||||
HTTPField(name: .acceptEncoding, value: "gzip, deflate, br"),
|
||||
HTTPField(name: .acceptLanguage, value: "en-US,en;q=0.9"),
|
||||
HTTPField(name: .referer, value: "https://www.example.com/home"),
|
||||
] + cookieFields(0..<2)
|
||||
|
||||
/// 12 ordinary headers and 4 cookies.
|
||||
let scalingFields16: [HTTPField] =
|
||||
scalingFields8 + [
|
||||
HTTPField(name: .origin, value: "https://www.example.com"),
|
||||
HTTPField(name: .cacheControl, value: "no-cache"),
|
||||
HTTPField(name: name("sec-fetch-dest"), value: "document"),
|
||||
HTTPField(name: name("sec-fetch-mode"), value: "navigate"),
|
||||
HTTPField(name: name("sec-fetch-site"), value: "same-origin"),
|
||||
HTTPField(name: .te, value: "trailers"),
|
||||
] + cookieFields(2..<4)
|
||||
|
||||
/// 16 ordinary headers and 16 cookies.
|
||||
let scalingFields32: [HTTPField] =
|
||||
scalingFields16 + [
|
||||
HTTPField(name: .authorization, value: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"),
|
||||
HTTPField(name: .ifNoneMatch, value: "\"686897696a7c876b7e\""),
|
||||
HTTPField(name: .ifModifiedSince, value: "Mon, 03 Aug 2026 12:00:00 GMT"),
|
||||
HTTPField(name: .priority, value: "u=0, i"),
|
||||
] + cookieFields(4..<16)
|
||||
|
||||
/// 16 ordinary headers and 48 cookies.
|
||||
let scalingFields64: [HTTPField] = scalingFields32 + cookieFields(16..<48)
|
||||
|
||||
/// 16 ordinary headers and 112 cookies.
|
||||
let scalingFields128: [HTTPField] = scalingFields64 + cookieFields(48..<112)
|
||||
|
||||
// MARK: - Building
|
||||
|
||||
/// Builds an `HTTPFields` by appending, which is how one is built on a receive path.
|
||||
///
|
||||
/// Fixtures that are compared against each other are always built by two separate calls to this
|
||||
/// function, never by copying one into the other, so that the two values are genuinely distinct and
|
||||
/// equality cannot take a shortcut for two copies of the same value.
|
||||
private func makeFields(_ fields: [HTTPField]) -> HTTPFields {
|
||||
var result = HTTPFields()
|
||||
for field in fields {
|
||||
result.append(field)
|
||||
}
|
||||
// Read the value once before handing it out, so that any work `HTTPFields` defers until its
|
||||
// first read is not charged to whichever benchmark happens to run first.
|
||||
precondition(result.contains(scalingPresentName))
|
||||
return result
|
||||
}
|
||||
|
||||
/// The two sides of an equality benchmark.
|
||||
struct FieldsPair: Sendable {
|
||||
let lhs: HTTPFields
|
||||
let rhs: HTTPFields
|
||||
}
|
||||
|
||||
/// Reverses the order of all non-`Cookie` fields, leaving every `Cookie` field in its original slot.
|
||||
///
|
||||
/// Two `HTTPFields` are equal regardless of the order of their differently named fields, but *not*
|
||||
/// regardless of the order of the fields sharing one name. So only the fields with a unique name may
|
||||
/// be permuted here; permuting the cookies would produce a value that is unequal rather than one
|
||||
/// that is equal but differently ordered.
|
||||
private func reorderingUniqueNames(_ fields: [HTTPField]) -> [HTTPField] {
|
||||
var reversed = fields.filter { $0.name != .cookie }.reversed().makeIterator()
|
||||
return fields.map { $0.name == .cookie ? $0 : reversed.next()! }
|
||||
}
|
||||
|
||||
/// The position of the one field that differs between the two sides of a "late mismatch" pair: 80%
|
||||
/// into the list, so that the leading 80% is identical.
|
||||
private func lateMismatchIndex(_ count: Int) -> Int {
|
||||
count * 4 / 5
|
||||
}
|
||||
|
||||
/// Changes the value of the field 80% into the list, leaving everything else alone.
|
||||
private func withLateMismatch(_ fields: [HTTPField]) -> [HTTPField] {
|
||||
var fields = fields
|
||||
let index = lateMismatchIndex(fields.count)
|
||||
let field = fields[index]
|
||||
fields[index] = HTTPField(name: field.name, value: field.value + "-mismatch")
|
||||
return fields
|
||||
}
|
||||
|
||||
// MARK: - Cases
|
||||
|
||||
/// Everything the benchmarks need for one field count, precomputed so that no fixture construction
|
||||
/// is ever measured.
|
||||
struct ScalingCase: Sendable {
|
||||
/// The number of fields, i.e. the N the benchmark names refer to.
|
||||
let n: Int
|
||||
/// How many of the `n` fields are `Cookie` fields, i.e. how many of them share a single name.
|
||||
let cookieCount: Int
|
||||
/// The fields as a decoder would hand them over.
|
||||
let fields: [HTTPField]
|
||||
/// A prebuilt value for the read-only benchmarks.
|
||||
let readFields: HTTPFields
|
||||
|
||||
/// Equal, and appended in the same order.
|
||||
let equalSameOrder: FieldsPair
|
||||
/// Equal, but with the uniquely named fields appended in the opposite order.
|
||||
let equalDifferentOrder: FieldsPair
|
||||
/// Unequal: the leading 80% is identical and in the same order, the field at 80% differs.
|
||||
///
|
||||
/// Note what this does and does not pin down. It guarantees the shape — two field lists that
|
||||
/// differ in exactly one field, late in the list — which is the realistic way an inequality
|
||||
/// shows up. It does *not* guarantee that only 80% of the comparison work happens before the
|
||||
/// difference is found: the order in which `==` looks at fields is not part of its contract, so
|
||||
/// how much of the list it gets through first is not something a benchmark can fix.
|
||||
let mismatchSameOrder: FieldsPair
|
||||
/// Unequal in the same single field as `mismatchSameOrder`, with the uniquely named fields
|
||||
/// appended in the opposite order. The same caveat applies.
|
||||
let mismatchDifferentOrder: FieldsPair
|
||||
|
||||
init(_ fields: [HTTPField]) {
|
||||
self.n = fields.count
|
||||
self.cookieCount = fields.filter { $0.name == .cookie }.count
|
||||
self.fields = fields
|
||||
self.readFields = makeFields(fields)
|
||||
|
||||
let mismatched = withLateMismatch(fields)
|
||||
self.equalSameOrder = FieldsPair(lhs: makeFields(fields), rhs: makeFields(fields))
|
||||
self.equalDifferentOrder = FieldsPair(
|
||||
lhs: makeFields(fields),
|
||||
rhs: makeFields(reorderingUniqueNames(fields))
|
||||
)
|
||||
self.mismatchSameOrder = FieldsPair(lhs: makeFields(fields), rhs: makeFields(mismatched))
|
||||
self.mismatchDifferentOrder = FieldsPair(
|
||||
lhs: makeFields(fields),
|
||||
rhs: makeFields(reorderingUniqueNames(mismatched))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
let scalingCases: [ScalingCase] = [
|
||||
ScalingCase(scalingFields8),
|
||||
ScalingCase(scalingFields16),
|
||||
ScalingCase(scalingFields32),
|
||||
ScalingCase(scalingFields64),
|
||||
ScalingCase(scalingFields128),
|
||||
]
|
||||
|
||||
/// A name that is present in every field list, for the lookup and mutation benchmarks.
|
||||
let scalingPresentName: HTTPField.Name = .userAgent
|
||||
|
||||
/// A name that is present in no field list, for the failing lookup and the insertion benchmarks.
|
||||
let scalingAbsentName: HTTPField.Name = .transferEncoding
|
||||
|
||||
/// Asserts that the fixtures mean what their names claim.
|
||||
///
|
||||
/// A fixture that is silently equal when it should be unequal, or that turns out to be a copy of the
|
||||
/// value it is compared against, produces a plausible-looking but meaningless curve, so this runs on
|
||||
/// every benchmark invocation rather than being a one-off check.
|
||||
func validateScalingFixtures() {
|
||||
let expectedSizes = [8, 16, 32, 64, 128]
|
||||
precondition(scalingCases.map(\.n) == expectedSizes, "unexpected field counts")
|
||||
|
||||
for (smaller, larger) in zip(scalingCases, scalingCases.dropFirst()) {
|
||||
precondition(Array(larger.fields.prefix(smaller.n)) == smaller.fields, "N=\(larger.n) is not nested")
|
||||
}
|
||||
|
||||
for scalingCase in scalingCases {
|
||||
let n = scalingCase.n
|
||||
precondition(scalingCase.readFields.count == n, "N=\(n): wrong field count")
|
||||
precondition(scalingCase.readFields.contains(scalingPresentName), "N=\(n): missing present name")
|
||||
precondition(!scalingCase.readFields.contains(scalingAbsentName), "N=\(n): absent name is present")
|
||||
precondition(scalingCase.readFields[values: .cookie].count == scalingCase.cookieCount, "N=\(n): cookie count")
|
||||
|
||||
let pairs = [
|
||||
("equal, same order", scalingCase.equalSameOrder, true),
|
||||
("equal, different order", scalingCase.equalDifferentOrder, true),
|
||||
("differs at 80%, same order", scalingCase.mismatchSameOrder, false),
|
||||
("differs at 80%, different order", scalingCase.mismatchDifferentOrder, false),
|
||||
]
|
||||
for (description, pair, expected) in pairs {
|
||||
precondition(pair.lhs.count == n && pair.rhs.count == n, "N=\(n): \(description) has wrong field count")
|
||||
precondition((pair.lhs == pair.rhs) == expected, "N=\(n): \(description) is not \(expected)")
|
||||
}
|
||||
|
||||
let differentOrder = scalingCase.equalDifferentOrder
|
||||
precondition(
|
||||
n < 2 || !Array(differentOrder.lhs).elementsEqual(differentOrder.rhs),
|
||||
"N=\(n): equal, different order is actually in the same order"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -82,31 +82,28 @@ let sampleHTTPURLResponse = HTTPURLResponse(httpResponse: sampleHTTPResponse, ur
|
||||
|
||||
let benchmarks: @Sendable () -> Void = {
|
||||
|
||||
Benchmark("URLRequest(httpRequest) - HTTPRequest to URLRequest", configuration: defaultConfiguration) {
|
||||
benchmark in
|
||||
Benchmark("URLRequest(httpRequest)-HTTPRequestToURLRequest", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(URLRequest(httpRequest: sampleHTTPRequest))
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark("URLRequest.httpRequest - URLRequest to HTTPRequest", configuration: defaultConfiguration) {
|
||||
benchmark in
|
||||
Benchmark("URLRequest.httpRequest-URLRequestToHTTPRequest", configuration: defaultConfiguration) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(sampleURLRequest.httpRequest)
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark(
|
||||
"HTTPURLResponse(httpResponse, url) - HTTPResponse to HTTPURLResponse",
|
||||
"HTTPURLResponse(httpResponse,url)-HTTPResponseToHTTPURLResponse",
|
||||
configuration: defaultConfiguration
|
||||
) {
|
||||
benchmark in
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(HTTPURLResponse(httpResponse: sampleHTTPResponse, url: sampleURL))
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark("HTTPURLResponse.httpResponse - HTTPURLResponse to HTTPResponse", configuration: defaultConfiguration) {
|
||||
Benchmark("HTTPURLResponse.httpResponse-HTTPURLResponseToHTTPResponse", configuration: defaultConfiguration) {
|
||||
benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
blackHole(sampleHTTPURLResponse.httpResponse)
|
||||
|
||||
File renamed without changes.
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 1016
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 1016
|
||||
}
|
||||
-3
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"mallocCountTotal" : 17
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"mallocCountTotal" : 17
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 29
|
||||
}
|
||||
File renamed without changes.
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 19
|
||||
}
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 16
|
||||
}
|
||||
File renamed without changes.
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 19
|
||||
}
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 18
|
||||
}
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 29
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 27
|
||||
}
|
||||
}
|
||||
File renamed without changes.
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 1016
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 1016
|
||||
}
|
||||
-3
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"mallocCountTotal" : 17
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"mallocCountTotal" : 17
|
||||
}
|
||||
File renamed without changes.
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 27
|
||||
}
|
||||
File renamed without changes.
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 29
|
||||
}
|
||||
File renamed without changes.
Loaded 100 of 434 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user