@@ -15,122 +15,127 @@ import Foundation
15
15
16
16
let d : [ BenchmarkCategory ] = [ . validation, . api, . Data]
17
17
18
+ /// Workload scaling: Inner loop multipliers
19
+ let M = 10_000 /// default
20
+ let I = 100_000 /// Data initialization
21
+ let S = 200 /// String conversions
22
+
18
23
public let DataBenchmarks = [
19
24
BenchmarkInfo ( name: " DataCreateEmpty " ,
20
- runFunction: { for _ in 0 ..< $0* 100_000 { blackHole ( Data ( ) ) } } , tags: d) ,
25
+ runFunction: { for _ in 0 ..< $0* I { blackHole ( Data ( ) ) } } , tags: d) ,
21
26
BenchmarkInfo ( name: " DataCreateSmall " ,
22
- runFunction: { for _ in 0 ..< $0* 100_000 { blackHole ( sampleData ( . small) ) } } ,
27
+ runFunction: { for _ in 0 ..< $0* I { blackHole ( sampleData ( . small) ) } } ,
23
28
tags: d) ,
24
29
BenchmarkInfo ( name: " DataCreateMedium " ,
25
- runFunction: { for _ in 0 ..< $0* 10_000 { blackHole ( sampleData ( . medium) ) } } ,
30
+ runFunction: { for _ in 0 ..< $0* M { blackHole ( sampleData ( . medium) ) } } ,
26
31
tags: d) ,
27
32
28
33
BenchmarkInfo ( name: " DataCreateEmptyArray " ,
29
- runFunction: { for _ in 0 ..< $0* 100_000 { blackHole ( Data ( [ ] ) ) } } , tags: d) ,
34
+ runFunction: { for _ in 0 ..< $0* I { blackHole ( Data ( [ ] ) ) } } , tags: d) ,
30
35
BenchmarkInfo ( name: " DataCreateSmallArray " ,
31
- runFunction: { for _ in 0 ..< $0* 100_000 { blackHole ( Data (
36
+ runFunction: { for _ in 0 ..< $0* I { blackHole ( Data (
32
37
[ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] ) ) } } , tags: d) ,
33
38
BenchmarkInfo ( name: " DataCreateMediumArray " ,
34
- runFunction: { for _ in 0 ..< $0* 10_000 { blackHole ( Data ( [
39
+ runFunction: { for _ in 0 ..< $0* M { blackHole ( Data ( [
35
40
0 , 1 , 2 , 3 , 4 , 5 , 6 ,
36
41
0 , 1 , 2 , 3 , 4 , 5 , 6 ,
37
42
0 , 1 , 2 , 3 , 4 , 5 , 6 ,
38
43
0 , 1 , 2 , 3 , 4 , 5 , 6 ,
39
44
] ) ) } } , tags: d) ,
40
45
41
46
BenchmarkInfo ( name: " DataSubscriptSmall " ,
42
- runFunction: { for _ in 0 ..< $0* 10_000 { blackHole ( small [ 1 ] ) } } , tags: d) ,
47
+ runFunction: { for _ in 0 ..< $0* M { blackHole ( small [ 1 ] ) } } , tags: d) ,
43
48
BenchmarkInfo ( name: " DataSubscriptMedium " ,
44
- runFunction: { for _ in 0 ..< $0* 10_000 { blackHole ( medium [ 521 ] ) } } , tags: d) ,
49
+ runFunction: { for _ in 0 ..< $0* M { blackHole ( medium [ 521 ] ) } } , tags: d) ,
45
50
46
51
BenchmarkInfo ( name: " DataCountSmall " ,
47
- runFunction: { count ( $0, data: small) } , tags: d) ,
52
+ runFunction: { count ( $0* M , data: small) } , tags: d) ,
48
53
BenchmarkInfo ( name: " DataCountMedium " ,
49
- runFunction: { count ( $0, data: medium) } , tags: d) ,
54
+ runFunction: { count ( $0* M , data: medium) } , tags: d) ,
50
55
51
56
BenchmarkInfo ( name: " DataSetCountSmall " ,
52
- runFunction: { setCount ( $0, data: small, extra: 3 ) } , tags: d) ,
57
+ runFunction: { setCount ( $0* M , data: small, extra: 3 ) } , tags: d) ,
53
58
BenchmarkInfo ( name: " DataSetCountMedium " ,
54
- runFunction: { setCount ( $0, data: medium, extra: 100 ) } , tags: d) ,
59
+ runFunction: { setCount ( $0* M , data: medium, extra: 100 ) } , tags: d) ,
55
60
56
61
BenchmarkInfo ( name: " DataAccessBytesSmall " ,
57
- runFunction: { withUnsafeBytes ( $0, data: small) } , tags: d) ,
62
+ runFunction: { withUnsafeBytes ( $0* M , data: small) } , tags: d) ,
58
63
BenchmarkInfo ( name: " DataAccessBytesMedium " ,
59
- runFunction: { withUnsafeBytes ( $0, data: medium) } , tags: d) ,
64
+ runFunction: { withUnsafeBytes ( $0* M , data: medium) } , tags: d) ,
60
65
61
66
BenchmarkInfo ( name: " DataMutateBytesSmall " ,
62
- runFunction: { withUnsafeMutableBytes ( $0, data: small) } , tags: d) ,
67
+ runFunction: { withUnsafeMutableBytes ( $0* M , data: small) } , tags: d) ,
63
68
BenchmarkInfo ( name: " DataMutateBytesMedium " ,
64
- runFunction: { withUnsafeMutableBytes ( $0, data: medium) } , tags: d) ,
69
+ runFunction: { withUnsafeMutableBytes ( $0* M , data: medium) } , tags: d) ,
65
70
66
71
BenchmarkInfo ( name: " DataCopyBytesSmall " ,
67
- runFunction: { copyBytes ( $0, data: small) } , tags: d) ,
72
+ runFunction: { copyBytes ( $0* M , data: small) } , tags: d) ,
68
73
BenchmarkInfo ( name: " DataCopyBytesMedium " ,
69
- runFunction: { copyBytes ( $0, data: medium) } , tags: d) ,
74
+ runFunction: { copyBytes ( $0* M , data: medium) } , tags: d) ,
70
75
71
76
BenchmarkInfo ( name: " DataAppendBytesSmall " ,
72
- runFunction: { append ( $0, bytes: 3 , to: small) } , tags: d) ,
77
+ runFunction: { append ( $0* M , bytes: 3 , to: small) } , tags: d) ,
73
78
BenchmarkInfo ( name: " DataAppendBytesMedium " ,
74
- runFunction: { append ( $0, bytes: 809 , to: medium) } , tags: d) ,
79
+ runFunction: { append ( $0* M , bytes: 809 , to: medium) } , tags: d) ,
75
80
76
81
BenchmarkInfo ( name: " DataAppendArray " ,
77
- runFunction: { append ( $0, arraySize: 809 , to: medium) } , tags: d) ,
82
+ runFunction: { append ( $0* M , arraySize: 809 , to: medium) } , tags: d) ,
78
83
79
84
BenchmarkInfo ( name: " DataReset " ,
80
- runFunction: { resetBytes ( $0, in: 431 ..< 809 , data: medium) } , tags: d) ,
85
+ runFunction: { resetBytes ( $0* M , in: 431 ..< 809 , data: medium) } , tags: d) ,
81
86
82
87
BenchmarkInfo ( name: " DataReplaceSmall " , runFunction: {
83
- replace ( $0, data: medium, subrange: 431 ..< 809 , with: small) } , tags: d) ,
88
+ replace ( $0* M , data: medium, subrange: 431 ..< 809 , with: small) } , tags: d) ,
84
89
BenchmarkInfo ( name: " DataReplaceMedium " , runFunction: {
85
- replace ( $0, data: medium, subrange: 431 ..< 809 , with: medium) } , tags: d) ,
90
+ replace ( $0* M , data: medium, subrange: 431 ..< 809 , with: medium) } , tags: d) ,
86
91
BenchmarkInfo ( name: " DataReplaceLarge " , runFunction: {
87
- replace ( $0, data: medium, subrange: 431 ..< 809 , with: large) } , tags: d) ,
92
+ replace ( $0* M , data: medium, subrange: 431 ..< 809 , with: large) } , tags: d) ,
88
93
89
94
BenchmarkInfo ( name: " DataReplaceSmallBuffer " , runFunction: {
90
- replaceBuffer ( $0, data: medium, subrange: 431 ..< 809 , with: small) } ,
95
+ replaceBuffer ( $0* M , data: medium, subrange: 431 ..< 809 , with: small) } ,
91
96
tags: d) ,
92
97
BenchmarkInfo ( name: " DataReplaceMediumBuffer " , runFunction: {
93
- replaceBuffer ( $0, data: medium, subrange: 431 ..< 809 , with: medium) } ,
98
+ replaceBuffer ( $0* M , data: medium, subrange: 431 ..< 809 , with: medium) } ,
94
99
tags: d) ,
95
100
BenchmarkInfo ( name: " DataReplaceLargeBuffer " , runFunction: {
96
- replaceBuffer ( $0, data: medium, subrange: 431 ..< 809 , with: large) } ,
101
+ replaceBuffer ( $0* M , data: medium, subrange: 431 ..< 809 , with: large) } ,
97
102
tags: d) ,
98
103
99
104
BenchmarkInfo ( name: " DataAppendSequence " ,
100
- runFunction: { append ( $0, sequenceLength: 809 , to: medium) } , tags: d) ,
105
+ runFunction: { append ( $0* M , sequenceLength: 809 , to: medium) } , tags: d) ,
101
106
102
107
BenchmarkInfo ( name: " DataAppendDataSmallToSmall " ,
103
- runFunction: { append ( $0, data: small, to: small) } , tags: d) ,
108
+ runFunction: { append ( $0* M , data: small, to: small) } , tags: d) ,
104
109
BenchmarkInfo ( name: " DataAppendDataSmallToMedium " ,
105
- runFunction: { append ( $0, data: small, to: medium) } , tags: d) ,
110
+ runFunction: { append ( $0* M , data: small, to: medium) } , tags: d) ,
106
111
BenchmarkInfo ( name: " DataAppendDataSmallToLarge " ,
107
- runFunction: { append ( $0, data: small, to: large) } , tags: d) ,
112
+ runFunction: { append ( $0* M , data: small, to: large) } , tags: d) ,
108
113
BenchmarkInfo ( name: " DataAppendDataMediumToSmall " ,
109
- runFunction: { append ( $0, data: medium, to: small) } , tags: d) ,
114
+ runFunction: { append ( $0* M , data: medium, to: small) } , tags: d) ,
110
115
BenchmarkInfo ( name: " DataAppendDataMediumToMedium " ,
111
- runFunction: { append ( $0, data: medium, to: medium) } , tags: d) ,
116
+ runFunction: { append ( $0* M , data: medium, to: medium) } , tags: d) ,
112
117
BenchmarkInfo ( name: " DataAppendDataMediumToLarge " ,
113
- runFunction: { append ( $0, data: medium, to: large) } , tags: d) ,
118
+ runFunction: { append ( $0* M , data: medium, to: large) } , tags: d) ,
114
119
BenchmarkInfo ( name: " DataAppendDataLargeToSmall " ,
115
- runFunction: { append ( $0, data: large, to: small) } , tags: d) ,
120
+ runFunction: { append ( $0* M , data: large, to: small) } , tags: d) ,
116
121
BenchmarkInfo ( name: " DataAppendDataLargeToMedium " ,
117
- runFunction: { append ( $0, data: large, to: medium) } , tags: d) ,
122
+ runFunction: { append ( $0* M , data: large, to: medium) } , tags: d) ,
118
123
BenchmarkInfo ( name: " DataAppendDataLargeToLarge " ,
119
- runFunction: { append ( $0, data: large, to: large) } , tags: d) ,
124
+ runFunction: { append ( $0* M , data: large, to: large) } , tags: d) ,
120
125
121
126
BenchmarkInfo ( name: " DataToStringEmpty " ,
122
- runFunction: { string ( $0, from: emptyData) } , tags: d, legacyFactor: 50 ) ,
127
+ runFunction: { string ( $0* S , from: emptyData) } , tags: d, legacyFactor: 50 ) ,
123
128
BenchmarkInfo ( name: " DataToStringSmall " ,
124
- runFunction: { string ( $0, from: smallData) } , tags: d, legacyFactor: 50 ) ,
129
+ runFunction: { string ( $0* S , from: smallData) } , tags: d, legacyFactor: 50 ) ,
125
130
BenchmarkInfo ( name: " DataToStringMedium " ,
126
- runFunction: { string ( $0, from: mediumData) } , tags: d, legacyFactor: 50 ) ,
131
+ runFunction: { string ( $0* S , from: mediumData) } , tags: d, legacyFactor: 50 ) ,
127
132
128
133
BenchmarkInfo ( name: " StringToDataEmpty " ,
129
- runFunction: { data ( $0, from: emptyString) } , tags: d, legacyFactor: 50 ) ,
134
+ runFunction: { data ( $0* S , from: emptyString) } , tags: d, legacyFactor: 50 ) ,
130
135
BenchmarkInfo ( name: " StringToDataSmall " ,
131
- runFunction: { data ( $0, from: smallString) } , tags: d, legacyFactor: 50 ) ,
136
+ runFunction: { data ( $0* S , from: smallString) } , tags: d, legacyFactor: 50 ) ,
132
137
BenchmarkInfo ( name: " StringToDataMedium " ,
133
- runFunction: { data ( $0, from: mediumString) } , tags: d, legacyFactor: 50 ) ,
138
+ runFunction: { data ( $0* S , from: mediumString) } , tags: d, legacyFactor: 50 ) ,
134
139
]
135
140
136
141
let emptyString = " "
@@ -191,7 +196,7 @@ func sampleData(_ type: SampleKind) -> Data {
191
196
192
197
@inline ( never)
193
198
func withUnsafeBytes( _ N: Int , data: Data ) {
194
- for _ in 0 ..< 10000 * N {
199
+ for _ in 1 ... N {
195
200
data. withUnsafeBytes { ( ptr: UnsafePointer < UInt8 > ) in
196
201
blackHole ( ptr. pointee)
197
202
}
@@ -200,7 +205,7 @@ func withUnsafeBytes(_ N: Int, data: Data) {
200
205
201
206
@inline ( never)
202
207
func withUnsafeMutableBytes( _ N: Int , data: Data ) {
203
- for _ in 0 ..< 10000 * N {
208
+ for _ in 1 ... N {
204
209
var copy = data
205
210
copy. withUnsafeMutableBytes { ( ptr: UnsafeMutablePointer < UInt8 > ) in
206
211
// Mutate a byte
@@ -214,7 +219,7 @@ func copyBytes(_ N: Int, data: Data) {
214
219
let amount = data. count
215
220
var buffer = UnsafeMutablePointer< UInt8> . allocate( capacity: amount)
216
221
defer { buffer. deallocate ( ) }
217
- for _ in 0 ..< 10000 * N {
222
+ for _ in 1 ... N {
218
223
data. copyBytes ( to: buffer, from: 0 ..< amount)
219
224
}
220
225
}
@@ -223,7 +228,7 @@ func copyBytes(_ N: Int, data: Data) {
223
228
func append( _ N: Int , bytes count: Int , to data: Data ) {
224
229
let bytes = malloc ( count) . assumingMemoryBound ( to: UInt8 . self)
225
230
defer { free ( bytes) }
226
- for _ in 0 ..< 10000 * N {
231
+ for _ in 1 ... N {
227
232
var copy = data
228
233
copy. append ( bytes, count: count)
229
234
}
@@ -235,7 +240,7 @@ func append(_ N: Int, arraySize: Int, to data: Data) {
235
240
bytes. withUnsafeMutableBufferPointer {
236
241
fillBuffer ( $0)
237
242
}
238
- for _ in 0 ..< 10000 * N {
243
+ for _ in 1 ... N {
239
244
var copy = data
240
245
copy. append ( contentsOf: bytes)
241
246
}
@@ -244,15 +249,15 @@ func append(_ N: Int, arraySize: Int, to data: Data) {
244
249
@inline ( never)
245
250
func append( _ N: Int , sequenceLength: Int , to data: Data ) {
246
251
let bytes = repeatElement ( UInt8 ( 0xA0 ) , count: sequenceLength)
247
- for _ in 0 ..< 10000 * N {
252
+ for _ in 1 ... N {
248
253
var copy = data
249
254
copy. append ( contentsOf: bytes)
250
255
}
251
256
}
252
257
253
258
@inline ( never)
254
259
func resetBytes( _ N: Int , in range: Range < Data . Index > , data: Data ) {
255
- for _ in 0 ..< 10000 * N {
260
+ for _ in 1 ... N {
256
261
var copy = data
257
262
copy. resetBytes ( in: range)
258
263
}
@@ -265,7 +270,7 @@ func replace(
265
270
subrange range: Range < Data . Index > ,
266
271
with replacement: Data
267
272
) {
268
- for _ in 0 ..< 10000 * N {
273
+ for _ in 1 ... N {
269
274
var copy = data
270
275
copy. replaceSubrange ( range, with: replacement)
271
276
}
@@ -281,7 +286,7 @@ func replaceBuffer(
281
286
replacement. withUnsafeBytes { ( bytes: UnsafePointer < UInt8 > ) in
282
287
let buffer = UnsafeBufferPointer ( start: bytes, count: replacement. count)
283
288
284
- for _ in 0 ..< 10000 * N {
289
+ for _ in 1 ... N {
285
290
var copy = data
286
291
copy. replaceSubrange ( range, with: buffer)
287
292
}
@@ -291,15 +296,15 @@ func replaceBuffer(
291
296
@inline ( never)
292
297
func append( _ N: Int , data: Data , to target: Data ) {
293
298
var copy : Data
294
- for _ in 0 ..< 10000 * N {
299
+ for _ in 1 ... N {
295
300
copy = target
296
301
copy. append ( data)
297
302
}
298
303
}
299
304
300
305
@inline ( never)
301
306
public func count( _ N: Int , data: Data ) {
302
- for _ in 0 ..< 10000 * N {
307
+ for _ in 1 ... N {
303
308
blackHole ( data. count)
304
309
}
305
310
}
@@ -309,22 +314,22 @@ public func setCount(_ N: Int, data: Data, extra: Int) {
309
314
var copy = data
310
315
let count = data. count + extra
311
316
let orig = data. count
312
- for _ in 0 ..< 10000 * N {
317
+ for _ in 1 ... N {
313
318
copy. count = count
314
319
copy. count = orig
315
320
}
316
321
}
317
322
318
323
@inline ( never)
319
324
public func string( _ N: Int , from data: Data ) {
320
- for _ in 0 ..< 200 * N {
325
+ for _ in 1 ... N {
321
326
blackHole ( String ( decoding: data, as: UTF8 . self) )
322
327
}
323
328
}
324
329
325
330
@inline ( never)
326
331
public func data( _ N: Int , from string: String ) {
327
- for _ in 0 ..< 200 * N {
332
+ for _ in 1 ... N {
328
333
blackHole ( Data ( string. utf8) )
329
334
}
330
335
}
0 commit comments