Skip to content

Commit ae9f5f1

Browse files
committed
[benchmark] Extract setup from DataBenchmarks
DataCount had setup overhead of 18 μs (20%). DataSubscript had setup overhead of 18 μs (2%). SetUpFunction wasn’t necessary, because of short initialization (18 μs for `sampleData(.medium)`), which will inflate only the initial measurement. Runtimes of other benchmarks hide the sampleData initialization in their artificially high runtimes — most use internal multiplier of 10 000 iterations — but were changed to use the same constant data, since it was already available. The overhead will already be extracted if we go for more precise measurement with lower multipliers in the future.
1 parent 03d9841 commit ae9f5f1

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

benchmark/single-source/DataBenchmarks.swift

+35-33
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public let DataBenchmarks = [
4141
BenchmarkInfo(name: "DataAppendDataLargeToLarge", runFunction: run_AppendDataLargeToLarge, tags: [.validation, .api, .Data]),
4242
]
4343

44+
let small = sampleData(.small)
45+
let medium = sampleData(.medium)
46+
4447
enum SampleKind {
4548
case small
4649
case medium
@@ -138,7 +141,6 @@ func sampleData(_ type: SampleKind) -> Data {
138141
case .string: return sampleString()
139142
case .immutableBacking: return sampleBridgedNSData()
140143
}
141-
142144
}
143145

144146
func benchmark_AccessBytes(_ N: Int, _ data: Data) {
@@ -228,7 +230,7 @@ func benchmark_AppendData(_ N: Int, _ lhs: Data, _ rhs: Data) {
228230

229231
@inline(never)
230232
public func run_Subscript(_ N: Int) {
231-
let data = sampleData(.medium)
233+
let data = medium
232234
let index = 521
233235
for _ in 1...10000*N {
234236
// Ensure that the compiler does not optimize away this call
@@ -238,7 +240,7 @@ public func run_Subscript(_ N: Int) {
238240

239241
@inline(never)
240242
public func run_Count(_ N: Int) {
241-
let data = sampleData(.medium)
243+
let data = medium
242244
for _ in 1...10000*N {
243245
// Ensure that the compiler does not optimize away this call
244246
blackHole(data.count)
@@ -247,7 +249,7 @@ public func run_Count(_ N: Int) {
247249

248250
@inline(never)
249251
public func run_SetCount(_ N: Int) {
250-
let data = sampleData(.medium)
252+
let data = medium
251253
let count = data.count + 100
252254
var otherData = data
253255
let orig = data.count
@@ -259,65 +261,65 @@ public func run_SetCount(_ N: Int) {
259261

260262
@inline(never)
261263
public func run_AccessBytes(_ N: Int) {
262-
let data = sampleData(.medium)
264+
let data = medium
263265
benchmark_AccessBytes(N, data)
264266
}
265267

266268
@inline(never)
267269
public func run_MutateBytes(_ N: Int) {
268-
let data = sampleData(.medium)
270+
let data = medium
269271
benchmark_MutateBytes(N, data)
270272
}
271273

272274
@inline(never)
273275
public func run_CopyBytes(_ N: Int) {
274-
let data = sampleData(.medium)
276+
let data = medium
275277
benchmark_CopyBytes(N, data)
276278
}
277279

278280
@inline(never)
279281
public func run_AppendBytes(_ N: Int) {
280-
let data = sampleData(.medium)
282+
let data = medium
281283
benchmark_AppendBytes(N, 809, data)
282284
}
283285

284286
@inline(never)
285287
public func run_AppendArray(_ N: Int) {
286-
let data = sampleData(.medium)
288+
let data = medium
287289
benchmark_AppendArray(N, 809, data)
288290
}
289291

290292
@inline(never)
291293
public func run_Reset(_ N: Int) {
292-
let data = sampleData(.medium)
294+
let data = medium
293295
benchmark_Reset(N, 431..<809, data)
294296
}
295297

296298
@inline(never)
297299
public func run_ReplaceSmall(_ N: Int) {
298-
let data = sampleData(.medium)
299-
let replacement = sampleData(.small)
300+
let data = medium
301+
let replacement = small
300302
benchmark_Replace(N, 431..<809, data, replacement)
301303
}
302304

303305
@inline(never)
304306
public func run_ReplaceMedium(_ N: Int) {
305-
let data = sampleData(.medium)
306-
let replacement = sampleData(.medium)
307+
let data = medium
308+
let replacement = medium
307309
benchmark_Replace(N, 431..<809, data, replacement)
308310
}
309311

310312
@inline(never)
311313
public func run_ReplaceLarge(_ N: Int) {
312-
let data = sampleData(.medium)
314+
let data = medium
313315
let replacement = sampleData(.large)
314316
benchmark_Replace(N, 431..<809, data, replacement)
315317
}
316318

317319
@inline(never)
318320
public func run_ReplaceSmallBuffer(_ N: Int) {
319-
let data = sampleData(.medium)
320-
let replacement = sampleData(.small)
321+
let data = medium
322+
let replacement = small
321323
let sz = replacement.count
322324
replacement.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) in
323325
benchmark_ReplaceBuffer(N, 431..<809, data, UnsafeBufferPointer(start: ptr, count: sz))
@@ -326,8 +328,8 @@ public func run_ReplaceSmallBuffer(_ N: Int) {
326328

327329
@inline(never)
328330
public func run_ReplaceMediumBuffer(_ N: Int) {
329-
let data = sampleData(.medium)
330-
let replacement = sampleData(.medium)
331+
let data = medium
332+
let replacement = medium
331333
let sz = replacement.count
332334
replacement.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) in
333335
benchmark_ReplaceBuffer(N, 431..<809, data, UnsafeBufferPointer(start: ptr, count: sz))
@@ -336,7 +338,7 @@ public func run_ReplaceMediumBuffer(_ N: Int) {
336338

337339
@inline(never)
338340
public func run_ReplaceLargeBuffer(_ N: Int) {
339-
let data = sampleData(.medium)
341+
let data = medium
340342
let replacement = sampleData(.large)
341343
let sz = replacement.count
342344
replacement.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) in
@@ -346,62 +348,62 @@ public func run_ReplaceLargeBuffer(_ N: Int) {
346348

347349
@inline(never)
348350
public func run_AppendSequence(_ N: Int) {
349-
let data = sampleData(.medium)
351+
let data = medium
350352
benchmark_AppendSequence(N, 809, data)
351353
}
352354

353355
@inline(never)
354356
public func run_AppendDataSmallToSmall(_ N: Int) {
355-
let data = sampleData(.small)
356-
let other = sampleData(.small)
357+
let data = small
358+
let other = small
357359
benchmark_AppendData(N, data, other)
358360
}
359361

360362
@inline(never)
361363
public func run_AppendDataSmallToMedium(_ N: Int) {
362-
let data = sampleData(.medium)
363-
let other = sampleData(.small)
364+
let data = medium
365+
let other = small
364366
benchmark_AppendData(N, data, other)
365367
}
366368

367369
@inline(never)
368370
public func run_AppendDataSmallToLarge(_ N: Int) {
369371
let data = sampleData(.large)
370-
let other = sampleData(.small)
372+
let other = small
371373
benchmark_AppendData(N, data, other)
372374
}
373375

374376
@inline(never)
375377
public func run_AppendDataMediumToSmall(_ N: Int) {
376-
let data = sampleData(.small)
377-
let other = sampleData(.medium)
378+
let data = small
379+
let other = medium
378380
benchmark_AppendData(N, data, other)
379381
}
380382

381383
@inline(never)
382384
public func run_AppendDataMediumToMedium(_ N: Int) {
383-
let data = sampleData(.medium)
384-
let other = sampleData(.medium)
385+
let data = medium
386+
let other = medium
385387
benchmark_AppendData(N, data, other)
386388
}
387389

388390
@inline(never)
389391
public func run_AppendDataMediumToLarge(_ N: Int) {
390392
let data = sampleData(.large)
391-
let other = sampleData(.medium)
393+
let other = medium
392394
benchmark_AppendData(N, data, other)
393395
}
394396

395397
@inline(never)
396398
public func run_AppendDataLargeToSmall(_ N: Int) {
397-
let data = sampleData(.small)
399+
let data = small
398400
let other = sampleData(.large)
399401
benchmark_AppendData(N, data, other)
400402
}
401403

402404
@inline(never)
403405
public func run_AppendDataLargeToMedium(_ N: Int) {
404-
let data = sampleData(.medium)
406+
let data = medium
405407
let other = sampleData(.large)
406408
benchmark_AppendData(N, data, other)
407409
}

0 commit comments

Comments
 (0)