Skip to content

Commit f6c24a0

Browse files
committed
benchmarks: extract setup code into the setUpFunction in some benchmarks where setup time is significant
1 parent 8876c24 commit f6c24a0

11 files changed

+160
-189
lines changed

benchmark/single-source/Array2D.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ import TestsUtils
1515
public let Array2D = BenchmarkInfo(
1616
name: "Array2D",
1717
runFunction: run_Array2D,
18-
tags: [.validation, .api, .Array])
18+
tags: [.validation, .api, .Array],
19+
setUpFunction: { blackHole(inputArray) },
20+
tearDownFunction: { inputArray = nil })
1921

20-
@inline(never)
21-
public func run_Array2D(_ N: Int) {
22+
var inputArray: [[Int]]! = {
2223
var A: [[Int]] = []
24+
A.reserveCapacity(1024)
2325
for _ in 0 ..< 1024 {
24-
var B: [Int] = []
25-
for y in 0 ..< 1024 {
26-
B.append(y)
27-
}
28-
A.append(B)
26+
A.append(Array(0 ..< 1024))
2927
}
28+
return A
29+
}()
30+
31+
@inline(never)
32+
func modifyArray(_ A: inout [[Int]], _ N: Int) {
3033
for _ in 0..<N {
3134
for i in 0 ..< 1024 {
3235
for y in 0 ..< 1024 {
@@ -36,3 +39,8 @@ public func run_Array2D(_ N: Int) {
3639
}
3740
}
3841
}
42+
43+
@inline(never)
44+
public func run_Array2D(_ N: Int) {
45+
modifyArray(&inputArray, N)
46+
}

benchmark/single-source/CString.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ public let CString = [
2121
BenchmarkInfo(name: "CStringLongAscii", runFunction: run_CStringLongAscii, tags: [.validation, .api, .String, .bridging]),
2222
BenchmarkInfo(name: "CStringLongNonAscii", runFunction: run_CStringLongNonAscii, tags: [.validation, .api, .String, .bridging]),
2323
BenchmarkInfo(name: "CStringShortAscii", runFunction: run_CStringShortAscii, tags: [.validation, .api, .String, .bridging]),
24-
BenchmarkInfo(name: "StringWithCString", runFunction: run_StringWithCString, tags: [.validation, .api, .String, .bridging]),
24+
BenchmarkInfo(name: "StringWithCString2", runFunction: run_StringWithCString, tags: [.validation, .api, .String, .bridging],
25+
setUpFunction: { blackHole(repeatedStr) }, tearDownFunction: { repeatedStr = nil })
2526
]
2627

2728
let ascii = "Swift is a multi-paradigm, compiled programming language created for iOS, OS X, watchOS, tvOS and Linux development by Apple Inc. Swift is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products. Swift is intended to be more resilient to erroneous code (\"safer\") than Objective-C and also more concise. It is built with the LLVM compiler framework included in Xcode 6 and later and uses the Objective-C runtime, which allows C, Objective-C, C++ and Swift code to run within a single program."
2829
let japanese = "日本語(にほんご、にっぽんご)は、主に日本国内や日本人同士の間で使われている言語である。"
2930

31+
var repeatedStr: String! = String(repeating: "x", count: 100 * (1 << 16))
32+
3033
@inline(never)
3134
public func run_StringWithCString(_ N: Int) {
32-
let str = String(repeating: "x", count: 100 * (1 << 16))
35+
let str: String = repeatedStr
3336
for _ in 0 ..< N {
3437
str.withCString { blackHole($0) }
3538
}

benchmark/single-source/Chars.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ import TestsUtils
1616
public let Chars = BenchmarkInfo(
1717
name: "Chars",
1818
runFunction: run_Chars,
19-
tags: [.validation, .api, .String])
19+
tags: [.validation, .api, .String],
20+
setUpFunction: { blackHole(alphabetInput) })
2021

21-
@inline(never)
22-
public func run_Chars(_ N: Int) {
23-
// Permute some characters.
24-
let alphabet: [Character] = [
22+
var alphabetInput: [Character] = [
2523
"A", "B", "C", "D", "E", "F", "G",
2624
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
2725
"S", "T", "U",
@@ -30,6 +28,11 @@ public func run_Chars(_ N: Int) {
3028
"2", "a", "t", "i", "o", "e", "q", "n", "X", "Y", "Z", "?", "m", "Z", ","
3129
]
3230

31+
@inline(never)
32+
public func run_Chars(_ N: Int) {
33+
// Permute some characters.
34+
let alphabet: [Character] = alphabetInput
35+
3336
for _ in 0..<N {
3437
for firstChar in alphabet {
3538
for middleChar in alphabet {

benchmark/single-source/ClassArrayGetter.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import TestsUtils
1515
public let ClassArrayGetter = BenchmarkInfo(
1616
name: "ClassArrayGetter",
1717
runFunction: run_ClassArrayGetter,
18-
tags: [.validation, .api, .Array])
18+
tags: [.validation, .api, .Array],
19+
setUpFunction: { blackHole(inputArray) },
20+
tearDownFunction: { inputArray = nil })
1921

2022
class Box {
2123
var v: Int
@@ -31,13 +33,18 @@ func sumArray(_ a: [Box]) -> Int {
3133
return s
3234
}
3335

34-
public func run_ClassArrayGetter(_ N: Int) {
36+
var inputArray: [Box]! = {
3537
let aSize = 10_000
3638
var a: [Box] = []
3739
a.reserveCapacity(aSize)
3840
for i in 1...aSize {
3941
a.append(Box(v:i))
4042
}
43+
return a
44+
}()
45+
46+
public func run_ClassArrayGetter(_ N: Int) {
47+
let a: [Box] = inputArray
4148
for _ in 1...N {
4249
_ = sumArray(a)
4350
}

benchmark/single-source/DictionaryGroup.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import TestsUtils
1414

1515
public let DictionaryGroup = [
1616
BenchmarkInfo(name: "DictionaryGroup", runFunction: run_DictionaryGroup, tags: [.validation, .api, .Dictionary]),
17-
BenchmarkInfo(name: "DictionaryGroupOfObjects", runFunction: run_DictionaryGroupOfObjects, tags: [.validation, .api, .Dictionary]),
17+
BenchmarkInfo(name: "DictionaryGroupOfObjects", runFunction: run_DictionaryGroupOfObjects, tags: [.validation, .api, .Dictionary],
18+
setUpFunction: { blackHole(inputObjects) },
19+
tearDownFunction: { inputObjects = nil }),
1820
]
1921

2022
let count = 10_000
@@ -45,9 +47,11 @@ class Box<T : Hashable> : Hashable {
4547
}
4648
}
4749

50+
var inputObjects: [Box<Int>]! = (0..<count).map { Box($0) }
51+
4852
@inline(never)
4953
public func run_DictionaryGroupOfObjects(_ N: Int) {
50-
let objects = (0..<count).map { Box($0) }
54+
let objects: [Box<Int>] = inputObjects
5155
for _ in 1...N {
5256
let dict = Dictionary(grouping: objects, by: { Box($0.value % 10) })
5357
CheckResults(dict.count == 10)

benchmark/single-source/LazyFilter.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import TestsUtils
1616

1717
public let LazyFilter = [
18-
BenchmarkInfo(name: "LazilyFilteredArrays", runFunction: run_LazilyFilteredArrays, tags: [.validation, .api, .Array]),
18+
BenchmarkInfo(name: "LazilyFilteredArrays", runFunction: run_LazilyFilteredArrays, tags: [.validation, .api, .Array],
19+
setUpFunction: { blackHole(filteredRange) }),
1920
BenchmarkInfo(name: "LazilyFilteredRange", runFunction: run_LazilyFilteredRange, tags: [.validation, .api, .Array]),
2021
BenchmarkInfo(
2122
name: "LazilyFilteredArrayContains",
@@ -36,10 +37,12 @@ public func run_LazilyFilteredRange(_ N: Int) {
3637
CheckResults(res == 123)
3738
}
3839

40+
let filteredRange = (1..<1_000_000).map({[$0]}).lazy.filter { $0.first! % 7 == 0 }
41+
3942
@inline(never)
4043
public func run_LazilyFilteredArrays(_ N: Int) {
4144
var res = 123
42-
let c = (1..<1_000_000).map({[$0]}).lazy.filter { $0.first! % 7 == 0 }
45+
let c = filteredRange
4346
for _ in 1...N {
4447
res += Array(c).count
4548
res -= Array(c).count

benchmark/single-source/RandomShuffle.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ import TestsUtils
1919

2020
public let RandomShuffle = [
2121
BenchmarkInfo(name: "RandomShuffleDef", runFunction: run_RandomShuffleDef,
22-
tags: [.api], setUpFunction: setup_RandomShuffle),
22+
tags: [.api],
23+
setUpFunction: { blackHole(numbersDef) },
24+
tearDownFunction: { numbersDef = nil }),
2325
BenchmarkInfo(name: "RandomShuffleLCG", runFunction: run_RandomShuffleLCG,
24-
tags: [.api], setUpFunction: setup_RandomShuffle),
26+
tags: [.api],
27+
setUpFunction: { blackHole(numbersLCG) },
28+
tearDownFunction: { numbersLCG = nil }),
2529
]
2630

2731
/// A linear congruential PRNG.
@@ -39,15 +43,12 @@ struct LCRNG: RandomNumberGenerator {
3943
}
4044
}
4145

42-
var numbers = Array(0...3_000_000)
43-
44-
@inline(never)
45-
func setup_RandomShuffle() {
46-
_ = numbers.count
47-
}
46+
var numbersDef: [Int]! = Array(0...3_000_000)
47+
var numbersLCG: [Int]! = Array(0...3_000_000)
4848

4949
@inline(never)
5050
public func run_RandomShuffleDef(_ N: Int) {
51+
var numbers: [Int] = numbersDef
5152
for _ in 0 ..< N {
5253
numbers.shuffle()
5354
blackHole(numbers.first!)
@@ -56,6 +57,7 @@ public func run_RandomShuffleDef(_ N: Int) {
5657

5758
@inline(never)
5859
public func run_RandomShuffleLCG(_ N: Int) {
60+
var numbers: [Int] = numbersLCG
5961
var generator = LCRNG(seed: 0)
6062
for _ in 0 ..< N {
6163
numbers.shuffle(using: &generator)

benchmark/single-source/ReversedCollections.swift

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,58 +13,65 @@
1313
import TestsUtils
1414

1515
public let ReversedCollections = [
16-
BenchmarkInfo(name: "ReversedArray", runFunction: run_ReversedArray, tags: [.validation, .api, .Array]),
16+
BenchmarkInfo(name: "ReversedArray2", runFunction: run_ReversedArray, tags: [.validation, .api, .Array],
17+
setUpFunction: { blackHole(arrayInput) },
18+
tearDownFunction: { arrayInput = nil }),
1719
BenchmarkInfo(name: "ReversedBidirectional", runFunction: run_ReversedBidirectional, tags: [.validation, .api]),
18-
BenchmarkInfo(name: "ReversedDictionary", runFunction: run_ReversedDictionary, tags: [.validation, .api, .Dictionary]),
20+
BenchmarkInfo(name: "ReversedDictionary2", runFunction: run_ReversedDictionary, tags: [.validation, .api, .Dictionary],
21+
setUpFunction: { blackHole(dictionaryInput) },
22+
tearDownFunction: { dictionaryInput = nil })
1923
]
2024

2125
// These benchmarks compare the performance of iteration through several
2226
// collection types after being reversed.
23-
var x = 0
2427
let length = 100_000
2528

29+
var arrayInput: [Int]! = Array(repeating: 1, count: length).reversed()
30+
2631
@inline(never)
2732
public func run_ReversedArray(_ N: Int) {
28-
let array = Array(repeating: 1, count: length)
29-
let reversedArray = array.reversed()
33+
let reversedArray: [Int] = arrayInput
3034

3135
// Iterate over the underlying type
3236
// ReversedRandomAccessCollection<Array<Int>>
3337
for _ in 1...N {
3438
for item in reversedArray {
35-
x = item
39+
blackHole(item)
3640
}
3741
}
3842
}
3943

4044
@inline(never)
4145
public func run_ReversedBidirectional(_ N: Int) {
42-
let bidirectional = AnyBidirectionalCollection(0..<length)
43-
let reversedBidirectional = bidirectional.reversed()
44-
4546
// Iterate over the underlying type
4647
// ReversedCollection<AnyBidirectionalCollection<Int>>
4748
for _ in 1...N {
49+
let bidirectional = AnyBidirectionalCollection(0..<length)
50+
let reversedBidirectional = bidirectional.reversed()
4851
for item in reversedBidirectional {
49-
x = item
52+
blackHole(item)
5053
}
5154
}
5255
}
5356

54-
@inline(never)
55-
public func run_ReversedDictionary(_ N: Int) {
57+
var dictionaryInput: [(Int, Int)]! = {
5658
var dictionary = [Int: Int]()
5759
for k in 0..<length {
58-
dictionary[k] = x
60+
dictionary[k] = k
5961
}
60-
let reversedDictionary = dictionary.reversed()
62+
return dictionary.reversed()
63+
}()
64+
65+
@inline(never)
66+
public func run_ReversedDictionary(_ N: Int) {
67+
let reversedDictionary: [(Int, Int)] = dictionaryInput
6168

6269
// Iterate over the underlying type
6370
// Array<(Int, Int)>
6471
for _ in 1...N {
6572
for (key, value) in reversedDictionary {
66-
x = key
67-
x = value
73+
blackHole(key)
74+
blackHole(value)
6875
}
6976
}
7077
}

0 commit comments

Comments
 (0)