Skip to content

Commit 50db6f1

Browse files
committed
benchmarks: fix the setup functions of the CharacterProperties benchmark.
Force all globals to be initialized in the setup functions
1 parent f1afba1 commit 50db6f1

File tree

2 files changed

+75
-12
lines changed

2 files changed

+75
-12
lines changed

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!

benchmark/single-source/CharacterProperties.swift.gyb

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

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

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

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

4747
extension Character {
4848
var firstScalar: UnicodeScalar { return unicodeScalars.first! }
@@ -75,6 +75,13 @@ func is${Property}Stashed(_ c: Character) -> Bool {
7575
}
7676
% end
7777

78+
func setupStash() {
79+
blackHole(workload)
80+
% for Property, Set in Properties.items():
81+
blackHole(${Set})
82+
% end
83+
}
84+
7885
// Memoize the stashed set
7986
% for Property, Set in Properties.items():
8087
var ${Set}Memo = Set<UInt32>()
@@ -89,6 +96,13 @@ func is${Property}StashedMemo(_ c: Character) -> Bool {
8996
}
9097
% end
9198

99+
func setupMemo() {
100+
blackHole(workload)
101+
% for Property, Set in Properties.items():
102+
blackHole(${Set}Memo)
103+
% end
104+
}
105+
92106
// Precompute whole scalar set
93107
% for Property, Set in Properties.items():
94108
var ${Set}Precomputed: Set<UInt32> = {
@@ -106,6 +120,13 @@ func is${Property}Precomputed(_ c: Character) -> Bool {
106120
}
107121
% end
108122

123+
func setupPrecomputed() {
124+
blackHole(workload)
125+
% for Property, Set in Properties.items():
126+
blackHole(${Set}Precomputed)
127+
% end
128+
}
129+
109130
// Compute on the fly
110131
//
111132
// TODO: If UnicodeScalars ever exposes category, etc., implement the others!

0 commit comments

Comments
 (0)