@@ -40,6 +40,13 @@ public let DataBenchmarks = [
40
40
0 , 1 , 2 , 3 , 4 , 5 , 6 ,
41
41
] ) ) } } , tags: d, legacyFactor: 20 ) ,
42
42
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
+
43
50
BenchmarkInfo ( name: " DataSubscriptSmall " ,
44
51
runFunction: { let data = small
45
52
for _ in 0 ..< $0* 10_000 { blackHole ( data [ 1 ] ) } } , tags: d) ,
@@ -112,7 +119,13 @@ public let DataBenchmarks = [
112
119
113
120
BenchmarkInfo ( name: " DataAppendSequence " ,
114
121
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) ,
116
129
117
130
BenchmarkInfo ( name: " DataAppendDataSmallToSmall " ,
118
131
runFunction: { append ( $0* 500 , data: small, to: small) } , tags: d,
@@ -161,6 +174,13 @@ public let DataBenchmarks = [
161
174
BenchmarkInfo ( name: " StringToDataMedium " ,
162
175
runFunction: { data ( $0* 200 , from: mediumString) } , tags: d,
163
176
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) ,
164
184
]
165
185
166
186
let emptyString = " "
@@ -174,6 +194,12 @@ let small = sampleData(.small)
174
194
let medium = sampleData ( . medium)
175
195
let large = sampleData ( . large)
176
196
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
+
177
203
enum SampleKind {
178
204
case small
179
205
case medium
@@ -280,6 +306,15 @@ func append(_ N: Int, sequenceLength: Int, to data: Data) {
280
306
}
281
307
}
282
308
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
+
283
318
@inline ( never)
284
319
func resetBytes( _ N: Int , in range: Range < Data . Index > , data: Data ) {
285
320
for _ in 1 ... N {
@@ -358,3 +393,13 @@ public func data(_ N: Int, from string: String) {
358
393
blackHole ( Data ( string. utf8) )
359
394
}
360
395
}
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