Skip to content

Commit fe805ee

Browse files
authored
Merge pull request #44 from Joannis/jo/http-fields-dictionary-literal
Optimise initialization of HTTPFields through Dictionary Literal
2 parents 12358d5 + 363f7ae commit fe805ee

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Benchmarks/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import HTTPTypes
2+
import Benchmark
3+
4+
let benchmarks = {
5+
Benchmark(
6+
"Initialize HTTPFields from Dictionary Literal"
7+
) { benchmark in
8+
let fiels: HTTPFields = [
9+
.contentType: "application/json",
10+
.contentLength: "42",
11+
.connection: "keep-alive",
12+
.accept: "application/json",
13+
.acceptEncoding: "gzip, deflate, br",
14+
]
15+
}
16+
}

Benchmarks/Package.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// swift-tools-version: 5.7.1
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "Benchmarks",
7+
platforms: [
8+
.macOS(.v13)
9+
],
10+
dependencies: [
11+
.package(path: "../"),
12+
.package(url: "https://github.com/ordo-one/package-benchmark.git", from: "1.22.1"),
13+
],
14+
targets: [
15+
.executableTarget(
16+
name: "Benchmarks",
17+
dependencies: [
18+
.product(name: "Benchmark", package: "package-benchmark"),
19+
.product(name: "HTTPTypes", package: "swift-http-types"),
20+
],
21+
path: "Benchmarks/HTTPFieldsBenchmarks",
22+
plugins: [
23+
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
24+
]
25+
),
26+
]
27+
)

Sources/HTTPTypes/HTTPFields.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ public struct HTTPFields: Sendable, Hashable {
280280

281281
extension HTTPFields: ExpressibleByDictionaryLiteral {
282282
public init(dictionaryLiteral elements: (HTTPField.Name, String)...) {
283+
self.reserveCapacity(elements.count)
283284
for (name, value) in elements {
284285
precondition(!name.isPseudo, "Pseudo header field \"\(name)\" disallowed")
285286
self._storage.append(field: HTTPField(name: name, value: value))

0 commit comments

Comments
 (0)