Stop copying the name index dictionary on every HTTPFields.append (#134)

### Motivation

`_Storage.append(field:)` updated the name index through optional
chaining:

  self.index?[name, default: (first: location, last: 0)].last = location

The optional chaining self.index? is expanded into

  if var index = self.index {
index[name, default: (first: location, last: 0)].last = location // this
allocates – as dict is referenced by self.index and index
    self.index = index
  }

Because of this we have an allocation on every append.

### Changes

Nil-check `self.index` once, then mutate through the force-unwrapped
`self.index![name] = ...`, which is an addressable projection of the
stored property and mutates in place.

### Result

Allocations drop on every append-driven path.
This commit is contained in:
Fabian Fett authored and GitHub committed 2026-08-12 16:28:16 +02:00
1 parent 8ec4cbcfa3
commit 37b3778072
48 files changed
+54 -50

No files matched your search

@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 25
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 48
"mallocCountTotal" : 26
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 29
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 36
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 22
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 42
"mallocCountTotal" : 26
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 50
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 37
"mallocCountTotal" : 23
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 41
"mallocCountTotal" : 27
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 25
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 48
"mallocCountTotal" : 26
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 29
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 36
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 22
"mallocCountTotal" : 20
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 42
"mallocCountTotal" : 26
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 50
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 37
"mallocCountTotal" : 23
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 41
"mallocCountTotal" : 27
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 21
"mallocCountTotal" : 23
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 27
"mallocCountTotal" : 23
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 50
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 31
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 36
"mallocCountTotal" : 30
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 24
"mallocCountTotal" : 22
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 44
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 52
"mallocCountTotal" : 30
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 53
"mallocCountTotal" : 55
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 39
"mallocCountTotal" : 25
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 42
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 27
"mallocCountTotal" : 23
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 50
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 31
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 38
"mallocCountTotal" : 30
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 24
"mallocCountTotal" : 22
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 44
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 52
"mallocCountTotal" : 30
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 39
"mallocCountTotal" : 25
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 42
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 27
"mallocCountTotal" : 23
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 50
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 31
"mallocCountTotal" : 21
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 38
"mallocCountTotal" : 30
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 24
"mallocCountTotal" : 22
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 44
"mallocCountTotal" : 28
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 52
"mallocCountTotal" : 30
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 39
"mallocCountTotal" : 25
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 42
"mallocCountTotal" : 28
}
+7 -3
View File
@@ -116,10 +116,14 @@ public struct HTTPFields: Sendable, Hashable {
precondition(!field.name.isPseudo, "Pseudo header field \"\(field.name)\" disallowed")
let name = field.name.canonicalName
let location = UInt16(self.fields.count)
if let index = self.index?[name] {
self.fields[Int(index.last)].next = location
if self.index != nil {
if let entry = self.index![name] {
self.fields[Int(entry.last)].next = location
self.index![name] = (first: entry.first, last: location)
} else {
self.index![name] = (first: location, last: location)
}
}
self.index?[name, default: (first: location, last: 0)].last = location
self.fields.append((field, .max))
precondition(self.fields.count < UInt16.max, "Too many fields")
}