mirror of
https://github.com/apple/swift-http-types
synced 2026-11-22 04:30:40 +00:00
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:
1 parent
8ec4cbcfa3
commit
37b3778072
48 files changed
+54
-50
No files matched your search
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 25
|
||||
"mallocCountTotal" : 21
|
||||
}
|
||||
+1
-1
@@ -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
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 22
|
||||
"mallocCountTotal" : 20
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 42
|
||||
"mallocCountTotal" : 26
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 50
|
||||
"mallocCountTotal" : 28
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 37
|
||||
"mallocCountTotal" : 23
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 41
|
||||
"mallocCountTotal" : 27
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 25
|
||||
"mallocCountTotal" : 21
|
||||
}
|
||||
+1
-1
@@ -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
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 22
|
||||
"mallocCountTotal" : 20
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 42
|
||||
"mallocCountTotal" : 26
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 50
|
||||
"mallocCountTotal" : 28
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 37
|
||||
"mallocCountTotal" : 23
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 41
|
||||
"mallocCountTotal" : 27
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 21
|
||||
"mallocCountTotal" : 23
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 27
|
||||
"mallocCountTotal" : 23
|
||||
}
|
||||
+1
-1
@@ -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
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 24
|
||||
"mallocCountTotal" : 22
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 44
|
||||
"mallocCountTotal" : 28
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 52
|
||||
"mallocCountTotal" : 30
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 53
|
||||
"mallocCountTotal" : 55
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 39
|
||||
"mallocCountTotal" : 25
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 42
|
||||
"mallocCountTotal" : 28
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 27
|
||||
"mallocCountTotal" : 23
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 50
|
||||
"mallocCountTotal" : 28
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 31
|
||||
"mallocCountTotal" : 21
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 38
|
||||
"mallocCountTotal" : 30
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 24
|
||||
"mallocCountTotal" : 22
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 44
|
||||
"mallocCountTotal" : 28
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 52
|
||||
"mallocCountTotal" : 30
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 39
|
||||
"mallocCountTotal" : 25
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 42
|
||||
"mallocCountTotal" : 28
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 27
|
||||
"mallocCountTotal" : 23
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 50
|
||||
"mallocCountTotal" : 28
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 31
|
||||
"mallocCountTotal" : 21
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 38
|
||||
"mallocCountTotal" : 30
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 24
|
||||
"mallocCountTotal" : 22
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 44
|
||||
"mallocCountTotal" : 28
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 52
|
||||
"mallocCountTotal" : 30
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 39
|
||||
"mallocCountTotal" : 25
|
||||
}
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"mallocCountTotal" : 42
|
||||
"mallocCountTotal" : 28
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user