Improve allocations in Large Field Equality Checks (#150)

### Motivation

Checking for equality of multiple http fields creates an array per field
name. However, not every field will actually have multiple values, which
means we can save array allocations for fields that are only set once.

### Modifications

- Added an enum to handle single values without extra allocations

### Result

Faster equality checks.
This commit is contained in:
Mathias Quintero authored and GitHub committed 2026-09-03 15:00:23 +00:00
1 parent dfebed0b4e
commit 6c8ca82973
51 files changed
+93 -54

No files matched your search

@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 147
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 275
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 531
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 145
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 33
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 273
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 49
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 529
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 81
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 25
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 147
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 275
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 531
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 145
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 33
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 273
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 49
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 529
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 81
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 25
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 149
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 277
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 533
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 147
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 35
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 275
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 51
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 531
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 83
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 27
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 149
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 277
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 533
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 147
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 35
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 275
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 51
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 531
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 83
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 27
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 149
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 277
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 533
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 147
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 35
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 275
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 51
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 531
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 83
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 27
"mallocCountTotal" : 19
}
+43 -4
View File
@@ -286,6 +286,46 @@ extension HTTPFields: Equatable {
/// compare in lock-step.
private static var minFieldsToIndexByName: Int { 64 }
private enum FieldEqualityGroup {
case empty
case single(HTTPField)
case multiple([HTTPField])
mutating func append(_ newElement: HTTPField) {
switch self {
case .empty:
self = .single(newElement)
case .single(let element):
self = .multiple([element, newElement])
case .multiple(var elements):
elements.append(newElement)
self = .multiple(elements)
}
}
mutating func popLast() -> HTTPField? {
switch self {
case .empty:
return nil
case .single(let field):
self = .empty
return field
case .multiple(var fields):
let field = fields.removeLast()
guard let first = fields.first else {
self = .empty
return field
}
if fields.count < 2 {
self = .single(first)
} else {
self = .multiple(fields)
}
return field
}
}
}
/// Answers the same question as `==` in linear time, by indexing `rhs` by name and draining that
/// index while walking `lhs`.
@_spi(HTTPTypesBenchmarking)
@@ -298,20 +338,19 @@ extension HTTPFields: Equatable {
// another way to create a FIFO structure: By adding the fields to the dictionary in reverse
// order, we'll add later values first to the values array. This allows us, when iterating
// the lhs fields, to remove values from the end of the values array.
var remaining = [String: [HTTPField]](minimumCapacity: lhs._fields.count)
var remaining = [String: FieldEqualityGroup](minimumCapacity: lhs._fields.count)
for field in rhs._fields.reversed() {
remaining[field.name.canonicalName, default: []].append(field)
remaining[field.name.canonicalName, default: .empty].append(field)
}
for field in lhs._fields {
guard let group = remaining.index(forKey: field.name.canonicalName),
let candidate = remaining.values[group].last
let candidate = remaining.values[group].popLast()
else {
return false
}
if candidate != field {
return false
}
remaining.values[group].removeLast()
}
return true
}