Skip to content

Commit a457b0d

Browse files
author
Itai Ferber
committed
Data.init<S>/append<S> worst-case benchmarks
1 parent 05e5eff commit a457b0d

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

benchmark/single-source/DataBenchmarks.swift

Lines changed: 27 additions & 2 deletions
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: "DataCreateSequenceExactCount", runFunction: {
44+
for _ in 0..<$0*500 {
45+
blackHole(Data(repeatElement(UInt8(0xA0), count: 809)))
46+
} }, tags: d),
47+
BenchmarkInfo(name: "DataCreateSequenceUnderestimatedCount", runFunction: {
48+
for _ in 0..<$0*500 { 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),
@@ -110,9 +117,12 @@ public let DataBenchmarks = [
110117
replaceBuffer($0*10, data: medium, subrange:431..<809, with: large) },
111118
tags: d),
112119

113-
BenchmarkInfo(name: "DataAppendSequence",
120+
BenchmarkInfo(name: "DataAppendSequenceExactCount",
114121
runFunction: { append($0*100, sequenceLength: 809, to: medium) },
115-
tags: d, legacyFactor: 100),
122+
tags: d, legacyFactor: 100),
123+
BenchmarkInfo(name: "DataAppendSequenceUnderestimatedCount", runFunction: {
124+
append($0*100, sequence: repeatElementSeq(809), to: medium) },
125+
tags: d, legacyFactor: 100),
116126

117127
BenchmarkInfo(name: "DataAppendDataSmallToSmall",
118128
runFunction: { append($0*500, data: small, to: small) }, tags: d,
@@ -174,6 +184,12 @@ let small = sampleData(.small)
174184
let medium = sampleData(.medium)
175185
let large = sampleData(.large)
176186

187+
let repeatElementSeq = { count in
188+
return sequence(state: count) { (i: inout Int) -> UInt8? in
189+
defer { i = i &- 1 }; return i > 0 ? UInt8(0xA0) : nil
190+
}
191+
}
192+
177193
enum SampleKind {
178194
case small
179195
case medium
@@ -280,6 +296,15 @@ func append(_ N: Int, sequenceLength: Int, to data: Data) {
280296
}
281297
}
282298

299+
@inline(never)
300+
func append<S: Sequence>(_ N: Int, sequence: S, to data: Data)
301+
where S.Element == UInt8 {
302+
for _ in 1...N {
303+
var copy = data
304+
copy.append(contentsOf: sequence)
305+
}
306+
}
307+
283308
@inline(never)
284309
func resetBytes(_ N: Int, in range: Range<Data.Index>, data: Data) {
285310
for _ in 1...N {

0 commit comments

Comments
 (0)