Skip to content

Commit fc817ba

Browse files
authored
Merge pull request #21848 from palimondo/and-dreadfully-distinct
[benchmark] Data.[init,append].Sequence various sizes
2 parents fae040a + d366105 commit fc817ba

File tree

1 file changed

+145
-10
lines changed

1 file changed

+145
-10
lines changed

benchmark/single-source/DataBenchmarks.swift

Lines changed: 145 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,76 @@ 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: {
43+
BenchmarkInfo(name: "Data.init.Sequence.809B.Count", runFunction: {
44+
_init($0*100, sequence: Bytes(count: 809, exact: true)) }, tags: d),
45+
BenchmarkInfo(name: "Data.init.Sequence.809B.Count0", runFunction: {
46+
_init($0*100, sequence: Bytes(count: 809, exact: false)) }, tags: d),
47+
BenchmarkInfo(name: "Data.init.Sequence.809B.Count.I", runFunction: {
48+
for _ in 0..<$0*100 {
49+
blackHole(Data(Bytes(count: 809, exact: true))) } }, tags: d),
50+
BenchmarkInfo(name: "Data.init.Sequence.809B.Count0.I", runFunction: {
51+
for _ in 0..<$0*100 {
52+
blackHole(Data(Bytes(count: 809, exact: false))) } }, tags: d),
53+
BenchmarkInfo(name: "Data.init.Sequence.2047B.Count.I", runFunction: {
54+
for _ in 0..<$0*50 {
55+
blackHole(Data(Bytes(count: 2047, exact: true))) } }, tags: d),
56+
BenchmarkInfo(name: "Data.init.Sequence.2047B.Count0.I", runFunction: {
57+
for _ in 0..<$0*50 {
58+
blackHole(Data(Bytes(count: 2047, exact: false))) } }, tags: d),
59+
BenchmarkInfo(name: "Data.init.Sequence.2049B.Count.I", runFunction: {
60+
for _ in 0..<$0*50 {
61+
blackHole(Data(Bytes(count: 2049, exact: true))) } }, tags: d),
62+
BenchmarkInfo(name: "Data.init.Sequence.2049B.Count0.I", runFunction: {
63+
for _ in 0..<$0*50 {
64+
blackHole(Data(Bytes(count: 2049, exact: false))) } }, tags: d),
65+
BenchmarkInfo(name: "Data.init.Sequence.511B.Count.I", runFunction: {
66+
for _ in 0..<$0*150 {
67+
blackHole(Data(Bytes(count: 511, exact: true))) } }, tags: d),
68+
BenchmarkInfo(name: "Data.init.Sequence.511B.Count0.I", runFunction: {
69+
for _ in 0..<$0*150 {
70+
blackHole(Data(Bytes(count: 511, exact: false))) } }, tags: d),
71+
BenchmarkInfo(name: "Data.init.Sequence.513B.Count.I", runFunction: {
72+
for _ in 0..<$0*150 {
73+
blackHole(Data(Bytes(count: 513, exact: true))) } }, tags: d),
74+
BenchmarkInfo(name: "Data.init.Sequence.513B.Count0.I", runFunction: {
75+
for _ in 0..<$0*150 {
76+
blackHole(Data(Bytes(count: 513, exact: false))) } }, tags: d),
77+
BenchmarkInfo(name: "Data.init.Sequence.64kB.Count", runFunction: {
78+
_init($0, sequence: Bytes(count: 2<<15, exact: true)) }, tags: d),
79+
BenchmarkInfo(name: "Data.init.Sequence.64kB.Count0", runFunction: {
80+
_init($0, sequence: Bytes(count: 2<<15, exact: false)) }, tags: d),
81+
BenchmarkInfo(name: "Data.init.Sequence.64kB.Count.I", runFunction: {
82+
for _ in 0..<$0 {
83+
blackHole(Data(Bytes(count: 2<<15, exact: true))) } }, tags: d),
84+
BenchmarkInfo(name: "Data.init.Sequence.64kB.Count0.I", runFunction: {
85+
for _ in 0..<$0 {
86+
blackHole(Data(Bytes(count: 2<<15, exact: false))) } }, tags: d),
87+
BenchmarkInfo(name: "Data.init.Sequence.809B.Count.RE", runFunction: {
88+
_init($0*100, sequence: repeatElement(UInt8(0xA0), count: 809)) }, tags: d),
89+
BenchmarkInfo(name: "Data.init.Sequence.809B.Count0.RE", runFunction: {
90+
_init($0*100, sequence: Count0(repeatElement(UInt8(0xA0), count: 809))) },
91+
tags: d),
92+
BenchmarkInfo(name: "Data.init.Sequence.809B.Count.RE.I", runFunction: {
4493
for _ in 0..<$0*100 {
4594
blackHole(Data(repeatElement(UInt8(0xA0), count: 809)))
4695
} }, tags: d),
47-
BenchmarkInfo(name: "Data.init.Sequence.UnderestimatedCount", runFunction: {
48-
for _ in 0..<$0*100 { blackHole(Data(repeatElementSeq(809))) } }, tags: d),
96+
BenchmarkInfo(name: "Data.init.Sequence.809B.Count0.RE.I", runFunction: {
97+
for _ in 0..<$0*100 {
98+
blackHole(Data(Count0(repeatElement(UInt8(0xA0), count: 809))))
99+
} }, tags: d),
100+
BenchmarkInfo(name: "Data.init.Sequence.64kB.Count.RE", runFunction: {
101+
_init($0, sequence: repeatElement(UInt8(0xA0), count: 2<<15)) }, tags: d),
102+
BenchmarkInfo(name: "Data.init.Sequence.64kB.Count0.RE", runFunction: {
103+
_init($0, sequence: Count0(repeatElement(UInt8(0xA0), count: 2<<15))) },
104+
tags: d),
105+
BenchmarkInfo(name: "Data.init.Sequence.64kB.Count.RE.I", runFunction: {
106+
for _ in 0..<$0 {
107+
blackHole(Data(repeatElement(UInt8(0xA0), count: 2<<15)))
108+
} }, tags: d),
109+
BenchmarkInfo(name: "Data.init.Sequence.64kB.Count0.RE.I", runFunction: {
110+
for _ in 0..<$0 {
111+
blackHole(Data(Count0(repeatElement(UInt8(0xA0), count: 2<<15))))
112+
} }, tags: d),
49113

50114
BenchmarkInfo(name: "DataSubscriptSmall",
51115
runFunction: { let data = small
@@ -120,12 +184,58 @@ public let DataBenchmarks = [
120184
BenchmarkInfo(name: "DataAppendSequence",
121185
runFunction: { append($0*100, sequenceLength: 809, to: medium) },
122186
tags: d, legacyFactor: 100),
123-
BenchmarkInfo(name: "Data.append.Sequence.ExactCount", runFunction: {
187+
BenchmarkInfo(name: "Data.append.Sequence.809B.Count", runFunction: {
188+
append($0*100, sequence: Bytes(count: 809, exact: true), to: medium) },
189+
tags: d),
190+
BenchmarkInfo(name: "Data.append.Sequence.809B.Count0", runFunction: {
191+
append($0*100, sequence: Bytes(count: 809, exact: false) , to: medium) },
192+
tags: d),
193+
BenchmarkInfo(name: "Data.append.Sequence.809B.Count.I",
194+
runFunction: { for _ in 1...$0*100 { var copy = medium
195+
copy.append(contentsOf: Bytes(count: 809, exact: true)) } }, tags: d),
196+
BenchmarkInfo(name: "Data.append.Sequence.809B.Count0.I",
197+
runFunction: { for _ in 1...$0*100 { var copy = medium
198+
copy.append(contentsOf: Bytes(count: 809, exact: false)) } }, tags: d),
199+
BenchmarkInfo(name: "Data.append.Sequence.64kB.Count", runFunction: {
200+
append($0, sequence: Bytes(count: 2<<15, exact: true), to: medium) },
201+
tags: d),
202+
BenchmarkInfo(name: "Data.append.Sequence.64kB.Count0", runFunction: {
203+
append($0, sequence: Bytes(count: 2<<15, exact: false), to: medium) },
204+
tags: d),
205+
BenchmarkInfo(name: "Data.append.Sequence.64kB.Count.I", runFunction: {
206+
for _ in 1...$0 { var copy = medium
207+
copy.append(contentsOf: Bytes(count: 2<<15, exact: true)) } }, tags: d),
208+
BenchmarkInfo(name: "Data.append.Sequence.64kB.Count0.I", runFunction: {
209+
for _ in 1...$0 { var copy = medium
210+
copy.append(contentsOf: Bytes(count: 2<<15, exact: false)) } }, tags: d),
211+
BenchmarkInfo(name: "Data.append.Sequence.809B.Count.RE.I", runFunction: {
212+
for _ in 1...$0*100 { var copy = medium
213+
copy.append(contentsOf: repeatElement(UInt8(0xA0), count: 809)) } },
214+
tags: d),
215+
BenchmarkInfo(name: "Data.append.Sequence.809B.Count0.RE.I", runFunction: {
216+
for _ in 1...$0*100 { var copy = medium
217+
copy.append(contentsOf: Count0(repeatElement(UInt8(0xA0), count: 809))) } },
218+
tags: d),
219+
BenchmarkInfo(name: "Data.append.Sequence.809B.Count.RE", runFunction: {
124220
append($0*100, sequence: repeatElement(UInt8(0xA0), count: 809),
125221
to: medium) }, tags: d),
126-
BenchmarkInfo(name: "Data.append.Sequence.UnderestimatedCount", runFunction: {
127-
append($0*100, sequence: repeatElementSeq(809), to: medium) },
222+
BenchmarkInfo(name: "Data.append.Sequence.809B.Count0.RE", runFunction: {
223+
append($0*100, sequence: Count0(repeatElement(UInt8(0xA0), count: 809)),
224+
to: medium) }, tags: d),
225+
BenchmarkInfo(name: "Data.append.Sequence.64kB.Count.RE.I", runFunction: {
226+
for _ in 1...$0 { var copy = medium
227+
copy.append(contentsOf: repeatElement(UInt8(0xA0), count: 2<<15)) } },
128228
tags: d),
229+
BenchmarkInfo(name: "Data.append.Sequence.64kB.Count0.RE.I", runFunction: {
230+
for _ in 1...$0 { var copy = medium
231+
copy.append(contentsOf: Count0(repeatElement(UInt8(0xA0), count: 2<<15))) }
232+
}, tags: d),
233+
BenchmarkInfo(name: "Data.append.Sequence.64kB.Count.RE", runFunction: {
234+
append($0, sequence: repeatElement(UInt8(0xA0), count: 2<<15),
235+
to: medium) }, tags: d),
236+
BenchmarkInfo(name: "Data.append.Sequence.64kB.Count0.RE", runFunction: {
237+
append($0, sequence: Count0(repeatElement(UInt8(0xA0), count: 2<<15)),
238+
to: medium) }, tags: d),
129239

130240
BenchmarkInfo(name: "DataAppendDataSmallToSmall",
131241
runFunction: { append($0*500, data: small, to: small) }, tags: d,
@@ -205,10 +315,28 @@ let large = sampleData(.large)
205315

206316
let array809 = byteArray(size: 809)
207317

208-
let repeatElementSeq = { count in
209-
return sequence(state: count) { (i: inout Int) -> UInt8? in
210-
defer { i = i &- 1 }; return i > 0 ? UInt8(0xA0) : nil
211-
}
318+
struct Count0<S: Sequence> : Sequence {
319+
let base: S
320+
init (_ base:S) { self.base = base }
321+
func makeIterator() -> S.Iterator { return base.makeIterator() }
322+
var underestimatedCount: Int { return 0 }
323+
}
324+
325+
struct Bytes: Sequence, IteratorProtocol {
326+
let count: Int
327+
let exact: Bool
328+
var i: Int = 0
329+
init(count: Int, exact: Bool) {
330+
self.count = count
331+
self.exact = exact
332+
}
333+
mutating func next() -> UInt8? {
334+
defer { i = i &+ 1 }
335+
return (i < count) ? UInt8(truncatingIfNeeded: i) : nil
336+
}
337+
var underestimatedCount: Int {
338+
return exact ? count : 0
339+
}
212340
}
213341

214342
enum SampleKind {
@@ -322,6 +450,13 @@ where S.Element == UInt8 {
322450
}
323451
}
324452

453+
@inline(never)
454+
func _init<S: Sequence>(_ N: Int, sequence: S) where S.Element == UInt8 {
455+
for _ in 1...N {
456+
blackHole(Data(sequence))
457+
}
458+
}
459+
325460
@inline(never)
326461
func resetBytes(_ N: Int, in range: Range<Data.Index>, data: Data) {
327462
for _ in 1...N {

0 commit comments

Comments
 (0)