Improve equality check performance (#141)

### Motivation

#138 introduced code paths that scale quadratically when running an
equality check.

### Changes

- Introduce a new lock-step equality check that performs great for small
fields and fields that share roughly the same order
- Introduce a new lookup equality check that scales linear for vastly
different field orders

### Result

Faster equality checks
This commit is contained in:
Fabian Fett authored and GitHub committed 2026-08-18 14:46:30 +00:00
1 parent 43cf84fb56
commit e186752bbc
149 files changed
+684 -105

No files matched your search

@@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//
import Benchmark
import HTTPTypes
@_spi(HTTPTypesBenchmarking) import HTTPTypes
/// Benchmarks that run the same operation over field lists of 8, 16, 32, 64 and 128 fields.
///
@@ -35,7 +35,7 @@ func registerHTTPFieldsScalingBenchmarks() {
// MARK: contains
// Measured with a large scaling factor because a single call is too cheap to resolve against
// Measured with a `kilo` 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(
@@ -110,8 +110,6 @@ func registerHTTPFieldsScalingBenchmarks() {
// 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)",
@@ -132,6 +130,16 @@ func registerHTTPFieldsScalingBenchmarks() {
}
}
let equalLocallyDisplaced = scalingCase.equalLocallyDisplaced
Benchmark(
"HTTPFields.==-equal-locallyDisplaced-N=\(n)",
configuration: makeDefaultConfiguration()
) { benchmark in
for _ in benchmark.scaledIterations {
blackHole(equalLocallyDisplaced.lhs == equalLocallyDisplaced.rhs)
}
}
let mismatchSameOrder = scalingCase.mismatchSameOrder
Benchmark(
"HTTPFields.==-differsAt80%-sameOrder-N=\(n)",
@@ -204,4 +212,29 @@ func registerHTTPFieldsScalingBenchmarks() {
}
}
}
// MARK: - All distinct names, reversed
for distinctNameCase in distinctNameCases {
let n = distinctNameCase.n
let pair = distinctNameCase.sortedAgainstReversed
Benchmark(
"HTTPFields.==-equal-allDistinctNamesReversed-N=\(n)",
configuration: makeDefaultConfiguration()
) { benchmark in
for _ in benchmark.scaledIterations {
blackHole(pair.lhs == pair.rhs)
}
}
Benchmark(
"HTTPFields.isEqualByNameIndex-equal-allDistinctNamesReversed-N=\(n)",
configuration: makeDefaultConfiguration()
) { benchmark in
for _ in benchmark.scaledIterations {
blackHole(HTTPFields.isEqualByNameIndex(pair.lhs, pair.rhs))
}
}
}
}
@@ -12,21 +12,16 @@
//
//===----------------------------------------------------------------------===//
import HTTPTypes
@_spi(HTTPTypesBenchmarking) 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.
/// sizes is only ever caused by the fields that were added.
///
/// 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.
/// Real field lists grow past ~16 fields almost exclusively because of cookies.
/// `Cookie` fields with distinct values, so that no two fields in a list compare equal.
private func cookieFields(_ range: Range<Int>) -> [HTTPField] {
@@ -77,19 +72,14 @@ 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 {
/// Builds an `HTTPFields` by appending, to ensure the resulting `HTTPFields` backing array
/// is uniquely referenced.
private func makeFieldsByRebuild(_ fields: [HTTPField]) -> HTTPFields {
var result = HTTPFields()
result.reserveCapacity(fields.count)
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
}
@@ -110,6 +100,16 @@ private func reorderingUniqueNames(_ fields: [HTTPField]) -> [HTTPField] {
return fields.map { $0.name == .cookie ? $0 : reversed.next()! }
}
private let localDisplacementDistance = 3
private func displacingOneField(_ fields: [HTTPField]) -> [HTTPField] {
precondition(fields.count > localDisplacementDistance, "list too short to displace a field within")
var fields = fields
let field = fields.removeFirst()
fields.insert(field, at: localDisplacementDistance)
return fields
}
/// 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 {
@@ -143,6 +143,8 @@ struct ScalingCase: Sendable {
let equalSameOrder: FieldsPair
/// Equal, but with the uniquely named fields appended in the opposite order.
let equalDifferentOrder: FieldsPair
/// Equal, but with one field appended a few slots away from where the other side has it.
let equalLocallyDisplaced: 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
@@ -159,18 +161,22 @@ struct ScalingCase: Sendable {
self.n = fields.count
self.cookieCount = fields.filter { $0.name == .cookie }.count
self.fields = fields
self.readFields = makeFields(fields)
self.readFields = makeFieldsByRebuild(fields)
let mismatched = withLateMismatch(fields)
self.equalSameOrder = FieldsPair(lhs: makeFields(fields), rhs: makeFields(fields))
self.equalSameOrder = FieldsPair(lhs: makeFieldsByRebuild(fields), rhs: makeFieldsByRebuild(fields))
self.equalDifferentOrder = FieldsPair(
lhs: makeFields(fields),
rhs: makeFields(reorderingUniqueNames(fields))
lhs: makeFieldsByRebuild(fields),
rhs: makeFieldsByRebuild(reorderingUniqueNames(fields))
)
self.mismatchSameOrder = FieldsPair(lhs: makeFields(fields), rhs: makeFields(mismatched))
self.equalLocallyDisplaced = FieldsPair(
lhs: makeFieldsByRebuild(fields),
rhs: makeFieldsByRebuild(displacingOneField(fields))
)
self.mismatchSameOrder = FieldsPair(lhs: makeFieldsByRebuild(fields), rhs: makeFieldsByRebuild(mismatched))
self.mismatchDifferentOrder = FieldsPair(
lhs: makeFields(fields),
rhs: makeFields(reorderingUniqueNames(mismatched))
lhs: makeFieldsByRebuild(fields),
rhs: makeFieldsByRebuild(reorderingUniqueNames(mismatched))
)
}
}
@@ -183,6 +189,33 @@ let scalingCases: [ScalingCase] = [
ScalingCase(scalingFields128),
]
// MARK: - All distinct names
/// A field list of `count` fields whose names are all distinct: `n1` through `n<count>`.
private func distinctNameFields(_ count: Int) -> [HTTPField] {
(1...count).map { HTTPField(name: name("n\($0)"), value: "value\($0)-aBcDeF0123456789") }
}
struct DistinctNameCase: Sendable {
/// The number of fields, which is also the number of distinct names.
let n: Int
/// One list ordered by name against the same fields reversed.
let sortedAgainstReversed: FieldsPair
init(_ count: Int) {
let fields = distinctNameFields(count)
self.n = count
self.sortedAgainstReversed = FieldsPair(
lhs: makeFieldsByRebuild(fields),
rhs: makeFieldsByRebuild(fields.reversed())
)
}
}
/// Runs past the 128 the other lists stop at, to cover both sides of the point where `==` hands off
/// to the by-name index.
let distinctNameCases: [DistinctNameCase] = [8, 16, 32, 64, 128, 256, 512].map(DistinctNameCase.init)
/// A name that is present in every field list, for the lookup and mutation benchmarks.
let scalingPresentName: HTTPField.Name = .userAgent
@@ -212,18 +245,53 @@ func validateScalingFixtures() {
let pairs = [
("equal, same order", scalingCase.equalSameOrder, true),
("equal, different order", scalingCase.equalDifferentOrder, true),
("equal, locally displaced", scalingCase.equalLocallyDisplaced, 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)")
precondition(
HTTPFields.isEqualByNameIndex(pair.lhs, pair.rhs) == expected,
"N=\(n): \(description): isEqualByNameIndex disagrees with =="
)
}
let differentOrder = scalingCase.equalDifferentOrder
let reorderedPairs = [
("equal, different order", scalingCase.equalDifferentOrder),
("equal, locally displaced", scalingCase.equalLocallyDisplaced),
]
for (description, pair) in reorderedPairs {
precondition(
n < 2 || !Array(pair.lhs).elementsEqual(pair.rhs),
"N=\(n): \(description) is actually in the same order"
)
}
let displaced = scalingCase.equalLocallyDisplaced
let movedPositions = zip(Array(displaced.lhs), Array(displaced.rhs)).enumerated()
.filter { $0.element.0 != $0.element.1 }
.map(\.offset)
precondition(
n < 2 || !Array(differentOrder.lhs).elementsEqual(differentOrder.rhs),
"N=\(n): equal, different order is actually in the same order"
movedPositions == Array(0...localDisplacementDistance),
"N=\(n): locally displaced pair disturbs \(movedPositions.count) positions, not \(localDisplacementDistance + 1)"
)
}
for distinctNameCase in distinctNameCases {
let n = distinctNameCase.n
let pair = distinctNameCase.sortedAgainstReversed
precondition(pair.lhs.count == n && pair.rhs.count == n, "distinct N=\(n): wrong field count")
precondition(Set(pair.lhs.map(\.name)).count == n, "distinct N=\(n): names are not all distinct")
precondition(pair.lhs == pair.rhs, "distinct N=\(n): not equal")
precondition(
HTTPFields.isEqualByNameIndex(pair.lhs, pair.rhs) == true,
"distinct N=\(n): isEqualByNameIndex disagrees with =="
)
precondition(
!Array(pair.lhs).elementsEqual(pair.rhs),
"distinct N=\(n): actually in the same order"
)
}
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 147
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 275
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 531
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 147
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 275
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 531
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 16
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 149
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 277
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 533
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 18
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 149
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 277
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 533
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 20
}
Loaded 100 of 149 files, more files were not shown because too many files have changed in this diff. Show more