Skip to content

Commit 1d10a6a

Browse files
authored
Merge pull request #21766 from itaiferber/data-additional-benchmarks
Additional Data benchmarks
2 parents 81c5a67 + 04eaf07 commit 1d10a6a

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

benchmark/single-source/DataBenchmarks.swift

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ public let DataBenchmarks = [
4040
0, 1, 2, 3, 4, 5, 6,
4141
])) } }, tags: d, legacyFactor: 20),
4242

43+
BenchmarkInfo(name: "Data.init.Sequence.ExactCount", runFunction: {
44+
for _ in 0..<$0*100 {
45+
blackHole(Data(repeatElement(UInt8(0xA0), count: 809)))
46+
} }, tags: d),
47+
BenchmarkInfo(name: "Data.init.Sequence.UnderestimatedCount", runFunction: {
48+
for _ in 0..<$0*100 { blackHole(Data(repeatElementSeq(809))) } }, tags: d),
49+
4350
BenchmarkInfo(name: "DataSubscriptSmall",
4451
runFunction: { let data = small
4552
for _ in 0..<$0*10_000 { blackHole(data[1]) } }, tags: d),
@@ -112,7 +119,13 @@ public let DataBenchmarks = [
112119

113120
BenchmarkInfo(name: "DataAppendSequence",
114121
runFunction: { append($0*100, sequenceLength: 809, to: medium) },
115-
tags: d, legacyFactor: 100),
122+
tags: d, legacyFactor: 100),
123+
BenchmarkInfo(name: "Data.append.Sequence.ExactCount", runFunction: {
124+
append($0*100, sequence: repeatElement(UInt8(0xA0), count: 809),
125+
to: medium) }, tags: d),
126+
BenchmarkInfo(name: "Data.append.Sequence.UnderestimatedCount", runFunction: {
127+
append($0*100, sequence: repeatElementSeq(809), to: medium) },
128+
tags: d),
116129

117130
BenchmarkInfo(name: "DataAppendDataSmallToSmall",
118131
runFunction: { append($0*500, data: small, to: small) }, tags: d,
@@ -161,6 +174,13 @@ public let DataBenchmarks = [
161174
BenchmarkInfo(name: "StringToDataMedium",
162175
runFunction: { data($0*200, from: mediumString) }, tags: d,
163176
legacyFactor: 50),
177+
178+
BenchmarkInfo(name: "Data.hash.Empty",
179+
runFunction: { hash($0*10_000, data: Data()) }, tags: d),
180+
BenchmarkInfo(name: "Data.hash.Small",
181+
runFunction: { hash($0*10_000, data: small) }, tags: d),
182+
BenchmarkInfo(name: "Data.hash.Medium",
183+
runFunction: { hash($0*1_000, data: medium) }, tags: d),
164184
]
165185

166186
let emptyString = ""
@@ -174,6 +194,12 @@ let small = sampleData(.small)
174194
let medium = sampleData(.medium)
175195
let large = sampleData(.large)
176196

197+
let repeatElementSeq = { count in
198+
return sequence(state: count) { (i: inout Int) -> UInt8? in
199+
defer { i = i &- 1 }; return i > 0 ? UInt8(0xA0) : nil
200+
}
201+
}
202+
177203
enum SampleKind {
178204
case small
179205
case medium
@@ -280,6 +306,15 @@ func append(_ N: Int, sequenceLength: Int, to data: Data) {
280306
}
281307
}
282308

309+
@inline(never)
310+
func append<S: Sequence>(_ N: Int, sequence: S, to data: Data)
311+
where S.Element == UInt8 {
312+
for _ in 1...N {
313+
var copy = data
314+
copy.append(contentsOf: sequence)
315+
}
316+
}
317+
283318
@inline(never)
284319
func resetBytes(_ N: Int, in range: Range<Data.Index>, data: Data) {
285320
for _ in 1...N {
@@ -358,3 +393,13 @@ public func data(_ N: Int, from string: String) {
358393
blackHole(Data(string.utf8))
359394
}
360395
}
396+
397+
@inline(never)
398+
public func hash(_ N: Int, data: Data) {
399+
var hasher = Hasher()
400+
for _ in 0 ..< N {
401+
hasher.combine(data)
402+
}
403+
404+
let _ = hasher.finalize()
405+
}

0 commit comments

Comments
 (0)