Skip to content

Commit 98f3961

Browse files
authored
Merge pull request #18096 from eeckstein/fix-benchmark-iteration-times
benchmarks: make benchmarks more stable
2 parents dd5bfac + bd43d54 commit 98f3961

22 files changed

+274
-240
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/ArrayOfGenericPOD.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RefArray<T> {
4040
// elements should be a nop.
4141
@inline(never)
4242
func genEnumArray() {
43-
_ = RefArray<Int?>(3)
43+
blackHole(RefArray<Int?>(3))
4444
// should be a nop
4545
}
4646

@@ -53,13 +53,13 @@ struct S<T> {
5353
}
5454
@inline(never)
5555
func genStructArray() {
56-
_ = RefArray<S<Int>>(S(x:3, y:4))
56+
blackHole(RefArray<S<Int>>(S(x:3, y:4)))
5757
// should be a nop
5858
}
5959

6060
@inline(never)
6161
public func run_ArrayOfGenericPOD(_ N: Int) {
62-
for _ in 0...N {
62+
for _ in 0..<N {
6363
genEnumArray()
6464
genStructArray()
6565
}

benchmark/single-source/ArrayOfGenericRef.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class GenericRef<T> : Constructible {
4747
// Reference to a POD class.
4848
@inline(never)
4949
func genPODRefArray() {
50-
_ = ConstructibleArray<GenericRef<Int>>(3)
50+
blackHole(ConstructibleArray<GenericRef<Int>>(3))
5151
// should be a nop
5252
}
5353

@@ -57,7 +57,7 @@ class Dummy {}
5757
@inline(never)
5858
func genCommonRefArray() {
5959
let d = Dummy()
60-
_ = ConstructibleArray<GenericRef<Dummy>>(d)
60+
blackHole(ConstructibleArray<GenericRef<Dummy>>(d))
6161
// should be a nop
6262
}
6363

@@ -74,7 +74,7 @@ class RefArray<T> {
7474
@inline(never)
7575
func genRefEnumArray() {
7676
let d = Dummy()
77-
_ = RefArray<Dummy?>(d)
77+
blackHole(RefArray<Dummy?>(d))
7878
// should be a nop
7979
}
8080

@@ -88,13 +88,13 @@ struct GenericVal<T> : Constructible {
8888
@inline(never)
8989
func genRefStructArray() {
9090
let d = Dummy()
91-
_ = ConstructibleArray<GenericVal<Dummy>>(d)
91+
blackHole(ConstructibleArray<GenericVal<Dummy>>(d))
9292
// should be a nop
9393
}
9494

9595
@inline(never)
9696
public func run_ArrayOfGenericRef(_ N: Int) {
97-
for _ in 0...N {
97+
for _ in 0..<N {
9898
genPODRefArray()
9999
genCommonRefArray()
100100
genRefEnumArray()

benchmark/single-source/ArrayOfPOD.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class RefArray<T> {
3333

3434
@inline(never)
3535
func genIntArray() {
36-
_ = RefArray<Int>(3, count:200_000)
36+
blackHole(RefArray<Int>(3, count:200_000))
3737
// should be a nop
3838
}
3939

@@ -45,7 +45,7 @@ enum PODEnum {
4545

4646
@inline(never)
4747
func genEnumArray() {
48-
_ = RefArray<PODEnum>(PODEnum.Some(3))
48+
blackHole(RefArray<PODEnum>(PODEnum.Some(3)))
4949
// should be a nop
5050
}
5151

@@ -55,13 +55,13 @@ struct S {
5555
}
5656
@inline(never)
5757
func genStructArray() {
58-
_ = RefArray<S>(S(x:3, y:4))
58+
blackHole(RefArray<S>(S(x:3, y:4)))
5959
// should be a nop
6060
}
6161

6262
@inline(never)
6363
public func run_ArrayOfPOD(_ N: Int) {
64-
for _ in 0...N {
64+
for _ in 0..<N {
6565
genIntArray()
6666
genEnumArray()
6767
genStructArray()

benchmark/single-source/ArrayOfRef.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class POD : Constructible {
4848

4949
@inline(never)
5050
func genPODRefArray() {
51-
_ = ConstructibleArray<POD>(3)
51+
blackHole(ConstructibleArray<POD>(3))
5252
// should be a nop
5353
}
5454

@@ -64,7 +64,7 @@ class CommonRef : Constructible {
6464
@inline(never)
6565
func genCommonRefArray() {
6666
let d = Dummy()
67-
_ = ConstructibleArray<CommonRef>(d)
67+
blackHole(ConstructibleArray<CommonRef>(d))
6868
// should be a nop
6969
}
7070

@@ -85,7 +85,7 @@ class RefArray<T> {
8585
@inline(never)
8686
func genRefEnumArray() {
8787
let e = RefEnum.Some(Dummy())
88-
_ = RefArray<RefEnum>(e)
88+
blackHole(RefArray<RefEnum>(e))
8989
// should be a nop
9090
}
9191

@@ -99,13 +99,13 @@ struct S : Constructible {
9999
@inline(never)
100100
func genRefStructArray() {
101101
let d = Dummy()
102-
_ = ConstructibleArray<S>(d)
102+
blackHole(ConstructibleArray<S>(d))
103103
// should be a nop
104104
}
105105

106106
@inline(never)
107107
public func run_ArrayOfRef(_ N: Int) {
108-
for _ in 0...N {
108+
for _ in 0..<N {
109109
genPODRefArray()
110110
genCommonRefArray()
111111
genRefEnumArray()

benchmark/single-source/CSVParsing.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import TestsUtils
22
public let CSVParsing = BenchmarkInfo(
3-
name: "CSVParsing",
3+
name: "CSVParsing2",
44
runFunction: run_CSVParsing,
55
tags: [.miniapplication, .api, .String],
66
setUpFunction: { buildWorkload() },
77
tearDownFunction: nil)
88
public let CSVParsingAlt = BenchmarkInfo(
9-
name: "CSVParsingAlt",
9+
name: "CSVParsingAlt2",
1010
runFunction: run_CSVParsingAlt,
1111
tags: [.miniapplication, .api, .String],
1212
setUpFunction: { buildWorkload() },
1313
tearDownFunction: nil)
1414
public let CSVParsingAltIndices = BenchmarkInfo(
15-
name: "CSVParsingAltIndices",
15+
name: "CSVParsingAltIndices2",
1616
runFunction: run_CSVParsingAltIndices,
1717
tags: [.miniapplication, .api, .String],
1818
setUpFunction: { buildWorkload() },
@@ -198,7 +198,7 @@ let workloadBase = """
198198
一,二,三,四,五,六,七,
199199
saquui,ta'lo,tso'i,nvgi,hisgi,sudali,galiquogi
200200
"""
201-
let targetRowNumber = 200_000
201+
let targetRowNumber = 500
202202
let repeatCount = targetRowNumber / workloadBase.split(separator: "\n").count
203203
let workload: String = repeatElement(workloadBase, count: repeatCount).joined()
204204

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: 5 * (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/CharacterProperties.swift

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,26 @@ import Foundation
2222
public let CharacterPropertiesFetch = BenchmarkInfo(
2323
name: "CharacterPropertiesFetch",
2424
runFunction: run_CharacterPropertiesFetch,
25-
tags: [.validation, .api, .String])
25+
tags: [.validation, .api, .String],
26+
setUpFunction: { blackHole(workload) })
2627

2728
public let CharacterPropertiesStashed = BenchmarkInfo(
2829
name: "CharacterPropertiesStashed",
2930
runFunction: run_CharacterPropertiesStashed,
3031
tags: [.validation, .api, .String],
31-
setUpFunction: { run_CharacterPropertiesStashed(1) },
32-
tearDownFunction: nil)
32+
setUpFunction: { setupStash() })
3333

3434
public let CharacterPropertiesStashedMemo = BenchmarkInfo(
3535
name: "CharacterPropertiesStashedMemo",
3636
runFunction: run_CharacterPropertiesStashedMemo,
37-
tags: [.validation, .api, .String])
37+
tags: [.validation, .api, .String],
38+
setUpFunction: { setupMemo() })
3839

3940
public let CharacterPropertiesPrecomputed = BenchmarkInfo(
4041
name: "CharacterPropertiesPrecomputed",
4142
runFunction: run_CharacterPropertiesPrecomputed,
4243
tags: [.validation, .api, .String],
43-
setUpFunction: { run_CharacterPropertiesPrecomputed(1) },
44-
tearDownFunction: nil)
44+
setUpFunction: { setupPrecomputed() })
4545

4646
extension Character {
4747
var firstScalar: UnicodeScalar { return unicodeScalars.first! }
@@ -122,6 +122,20 @@ func isCapitalizedStashed(_ c: Character) -> Bool {
122122
return capitalizedLetters.contains(c.firstScalar)
123123
}
124124

125+
func setupStash() {
126+
blackHole(workload)
127+
blackHole(controlCharacters)
128+
blackHole(alphanumerics)
129+
blackHole(lowercaseLetters)
130+
blackHole(punctuationCharacters)
131+
blackHole(whitespaces)
132+
blackHole(letters)
133+
blackHole(uppercaseLetters)
134+
blackHole(decimalDigits)
135+
blackHole(newlines)
136+
blackHole(capitalizedLetters)
137+
}
138+
125139
// Memoize the stashed set
126140
var controlCharactersMemo = Set<UInt32>()
127141
func isControlStashedMemo(_ c: Character) -> Bool {
@@ -224,6 +238,20 @@ func isCapitalizedStashedMemo(_ c: Character) -> Bool {
224238
return false
225239
}
226240

241+
func setupMemo() {
242+
blackHole(workload)
243+
blackHole(controlCharactersMemo)
244+
blackHole(alphanumericsMemo)
245+
blackHole(lowercaseLettersMemo)
246+
blackHole(punctuationCharactersMemo)
247+
blackHole(whitespacesMemo)
248+
blackHole(lettersMemo)
249+
blackHole(uppercaseLettersMemo)
250+
blackHole(decimalDigitsMemo)
251+
blackHole(newlinesMemo)
252+
blackHole(capitalizedLettersMemo)
253+
}
254+
227255
// Precompute whole scalar set
228256
var controlCharactersPrecomputed: Set<UInt32> = {
229257
var result = Set<UInt32>()
@@ -356,6 +384,20 @@ func isCapitalizedPrecomputed(_ c: Character) -> Bool {
356384
return capitalizedLettersPrecomputed.contains(c.firstScalar.value)
357385
}
358386

387+
func setupPrecomputed() {
388+
blackHole(workload)
389+
blackHole(controlCharactersPrecomputed)
390+
blackHole(alphanumericsPrecomputed)
391+
blackHole(lowercaseLettersPrecomputed)
392+
blackHole(punctuationCharactersPrecomputed)
393+
blackHole(whitespacesPrecomputed)
394+
blackHole(lettersPrecomputed)
395+
blackHole(uppercaseLettersPrecomputed)
396+
blackHole(decimalDigitsPrecomputed)
397+
blackHole(newlinesPrecomputed)
398+
blackHole(capitalizedLettersPrecomputed)
399+
}
400+
359401
// Compute on the fly
360402
//
361403
// TODO: If UnicodeScalars ever exposes category, etc., implement the others!

0 commit comments

Comments
 (0)