Skip to content

Commit 5f21c12

Browse files
authored
Merge pull request #20048 from palimondo/i-just-do-eyes
[benchmark] Extract Setup from Benchmarks
2 parents 6636815 + d8adfc7 commit 5f21c12

24 files changed

+355
-339
lines changed

benchmark/single-source/ArrayAppend.swift

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,53 @@
1414

1515
import TestsUtils
1616

17+
let t: [BenchmarkCategory] = [.validation, .api, .Array]
1718
public let ArrayAppend = [
18-
BenchmarkInfo(name: "ArrayAppend", runFunction: run_ArrayAppend, tags: [.validation, .api, .Array]),
19-
BenchmarkInfo(name: "ArrayAppendArrayOfInt", runFunction: run_ArrayAppendArrayOfInt, tags: [.validation, .api, .Array]),
20-
BenchmarkInfo(name: "ArrayAppendAscii", runFunction: run_ArrayAppendAscii, tags: [.validation, .api, .Array]),
21-
BenchmarkInfo(name: "ArrayAppendAsciiSubstring", runFunction: run_ArrayAppendAsciiSubstring, tags: [.validation, .api, .Array]),
22-
BenchmarkInfo(name: "ArrayAppendFromGeneric", runFunction: run_ArrayAppendFromGeneric, tags: [.validation, .api, .Array]),
23-
BenchmarkInfo(name: "ArrayAppendGenericStructs", runFunction: run_ArrayAppendGenericStructs, tags: [.validation, .api, .Array]),
24-
BenchmarkInfo(name: "ArrayAppendLatin1", runFunction: run_ArrayAppendLatin1, tags: [.validation, .api, .Array]),
25-
BenchmarkInfo(name: "ArrayAppendLatin1Substring", runFunction: run_ArrayAppendLatin1Substring, tags: [.validation, .api, .Array]),
26-
BenchmarkInfo(name: "ArrayAppendLazyMap", runFunction: run_ArrayAppendLazyMap, tags: [.validation, .api, .Array]),
27-
BenchmarkInfo(name: "ArrayAppendOptionals", runFunction: run_ArrayAppendOptionals, tags: [.validation, .api, .Array]),
28-
BenchmarkInfo(name: "ArrayAppendRepeatCol", runFunction: run_ArrayAppendRepeatCol, tags: [.validation, .api, .Array]),
29-
BenchmarkInfo(name: "ArrayAppendReserved", runFunction: run_ArrayAppendReserved, tags: [.validation, .api, .Array]),
30-
BenchmarkInfo(name: "ArrayAppendSequence", runFunction: run_ArrayAppendSequence, tags: [.validation, .api, .Array]),
31-
BenchmarkInfo(name: "ArrayAppendStrings", runFunction: run_ArrayAppendStrings, tags: [.validation, .api, .Array]),
32-
BenchmarkInfo(name: "ArrayAppendToFromGeneric", runFunction: run_ArrayAppendToFromGeneric, tags: [.validation, .api, .Array]),
33-
BenchmarkInfo(name: "ArrayAppendToGeneric", runFunction: run_ArrayAppendToGeneric, tags: [.validation, .api, .Array]),
34-
BenchmarkInfo(name: "ArrayAppendUTF16", runFunction: run_ArrayAppendUTF16, tags: [.validation, .api, .Array]),
35-
BenchmarkInfo(name: "ArrayAppendUTF16Substring", runFunction: run_ArrayAppendUTF16Substring, tags: [.validation, .api, .Array]),
36-
BenchmarkInfo(name: "ArrayPlusEqualArrayOfInt", runFunction: run_ArrayPlusEqualArrayOfInt, tags: [.validation, .api, .Array]),
37-
BenchmarkInfo(name: "ArrayPlusEqualFiveElementCollection", runFunction: run_ArrayPlusEqualFiveElementCollection, tags: [.validation, .api, .Array]),
38-
BenchmarkInfo(name: "ArrayPlusEqualSingleElementCollection", runFunction: run_ArrayPlusEqualSingleElementCollection, tags: [.validation, .api, .Array]),
39-
BenchmarkInfo(name: "ArrayPlusEqualThreeElements", runFunction: run_ArrayPlusEqualThreeElements, tags: [.validation, .api, .Array]),
19+
BenchmarkInfo(name: "ArrayAppend", runFunction: run_ArrayAppend, tags: t),
20+
BenchmarkInfo(name: "ArrayAppendArrayOfInt", runFunction: run_ArrayAppendArrayOfInt, tags: t,
21+
setUpFunction: ones, tearDownFunction: releaseOnes),
22+
BenchmarkInfo(name: "ArrayAppendAscii", runFunction: run_ArrayAppendAscii, tags: t),
23+
BenchmarkInfo(name: "ArrayAppendAsciiSubstring", runFunction: run_ArrayAppendAsciiSubstring, tags: t),
24+
BenchmarkInfo(name: "ArrayAppendFromGeneric", runFunction: run_ArrayAppendFromGeneric, tags: t,
25+
setUpFunction: ones, tearDownFunction: releaseOnes),
26+
BenchmarkInfo(name: "ArrayAppendGenericStructs", runFunction: run_ArrayAppendGenericStructs, tags: t,
27+
setUpFunction: { otherStructs = Array(repeating: S(x: 3, y: 4.2), count: 10_000) },
28+
tearDownFunction: { otherStructs = nil } ),
29+
BenchmarkInfo(name: "ArrayAppendLatin1", runFunction: run_ArrayAppendLatin1, tags: t),
30+
BenchmarkInfo(name: "ArrayAppendLatin1Substring", runFunction: run_ArrayAppendLatin1Substring, tags: t),
31+
BenchmarkInfo(name: "ArrayAppendLazyMap", runFunction: run_ArrayAppendLazyMap, tags: t,
32+
setUpFunction: { blackHole(array) }),
33+
BenchmarkInfo(name: "ArrayAppendOptionals", runFunction: run_ArrayAppendOptionals, tags: t,
34+
setUpFunction: { otherOptionalOnes = Array(repeating: 1, count: 10_000) },
35+
tearDownFunction: { otherOptionalOnes = nil } ),
36+
BenchmarkInfo(name: "ArrayAppendRepeatCol", runFunction: run_ArrayAppendRepeatCol, tags: t),
37+
BenchmarkInfo(name: "ArrayAppendReserved", runFunction: run_ArrayAppendReserved, tags: t),
38+
BenchmarkInfo(name: "ArrayAppendSequence", runFunction: run_ArrayAppendSequence, tags: t),
39+
BenchmarkInfo(name: "ArrayAppendStrings", runFunction: run_ArrayAppendStrings, tags: t,
40+
setUpFunction: { otherStrings = stride(from: 0, to: 10_000, by: 1).map { "\($0)" } },
41+
tearDownFunction: { otherStrings = nil } ),
42+
BenchmarkInfo(name: "ArrayAppendToFromGeneric", runFunction: run_ArrayAppendToFromGeneric, tags: t,
43+
setUpFunction: ones, tearDownFunction: releaseOnes),
44+
BenchmarkInfo(name: "ArrayAppendToGeneric", runFunction: run_ArrayAppendToGeneric, tags: t,
45+
setUpFunction: ones, tearDownFunction: releaseOnes),
46+
BenchmarkInfo(name: "ArrayAppendUTF16", runFunction: run_ArrayAppendUTF16, tags: t),
47+
BenchmarkInfo(name: "ArrayAppendUTF16Substring", runFunction: run_ArrayAppendUTF16Substring, tags: t),
48+
BenchmarkInfo(name: "ArrayPlusEqualArrayOfInt", runFunction: run_ArrayPlusEqualArrayOfInt, tags: t,
49+
setUpFunction: ones, tearDownFunction: releaseOnes),
50+
BenchmarkInfo(name: "ArrayPlusEqualFiveElementCollection", runFunction: run_ArrayPlusEqualFiveElementCollection, tags: t),
51+
BenchmarkInfo(name: "ArrayPlusEqualSingleElementCollection", runFunction: run_ArrayPlusEqualSingleElementCollection, tags: t),
52+
BenchmarkInfo(name: "ArrayPlusEqualThreeElements", runFunction: run_ArrayPlusEqualThreeElements, tags: t),
4053
]
4154

55+
var otherOnes: [Int]!
56+
var otherOptionalOnes: [Int?]!
57+
var otherStrings: [String]!
58+
var otherStructs: [S<Int, Double>]!
59+
let array = Array(0..<10_000)
60+
61+
func ones() { otherOnes = Array(repeating: 1, count: 10_000) }
62+
func releaseOnes() { otherOnes = nil }
63+
4264
// Append single element
4365
@inline(never)
4466
public func run_ArrayAppend(_ N: Int) {
@@ -67,10 +89,11 @@ public func run_ArrayAppendReserved(_ N: Int) {
6789
}
6890

6991
// Append a sequence. Length of sequence unknown so
70-
// can't pre-reserve capacity.
92+
// can't pre-reserve capacity.
7193
@inline(never)
7294
public func run_ArrayAppendSequence(_ N: Int) {
7395
let seq = stride(from: 0, to: 10_000, by: 1)
96+
7497
for _ in 0..<N {
7598
for _ in 0..<10 {
7699
var nums = [Int]()
@@ -85,7 +108,8 @@ public func run_ArrayAppendSequence(_ N: Int) {
85108
// can pre-reserve capacity.
86109
@inline(never)
87110
public func run_ArrayAppendArrayOfInt(_ N: Int) {
88-
let other = Array(repeating: 1, count: 10_000)
111+
let other: [Int] = otherOnes
112+
89113
for _ in 0..<N {
90114
for _ in 0..<10 {
91115
var nums = [Int]()
@@ -100,7 +124,8 @@ public func run_ArrayAppendArrayOfInt(_ N: Int) {
100124
// except +=, to check equally performant.
101125
@inline(never)
102126
public func run_ArrayPlusEqualArrayOfInt(_ N: Int) {
103-
let other = Array(repeating: 1, count: 10_000)
127+
let other: [Int] = otherOnes
128+
104129
for _ in 0..<N {
105130
for _ in 0..<10 {
106131
var nums = [Int]()
@@ -115,7 +140,8 @@ public func run_ArrayPlusEqualArrayOfInt(_ N: Int) {
115140
// can pre-reserve capacity.
116141
@inline(never)
117142
public func run_ArrayAppendStrings(_ N: Int) {
118-
let other = stride(from: 0, to: 10_000, by: 1).map { "\($0)" }
143+
let other: [String] = otherStrings
144+
119145
for _ in 0..<N {
120146
for _ in 0..<10 {
121147
var nums = [String]()
@@ -136,10 +162,11 @@ struct S<T,U> {
136162
// can pre-reserve capacity.
137163
@inline(never)
138164
public func run_ArrayAppendGenericStructs(_ N: Int) {
139-
let other = Array(repeating: S(x: 3, y: 4.2), count: 10_000)
165+
let other: [S<Int, Double>] = otherStructs
166+
140167
for _ in 0..<N {
141168
for _ in 0..<10 {
142-
var nums = [S<Int,Double>]()
169+
var nums = [S<Int, Double>]()
143170
for _ in 0..<8 {
144171
nums += other
145172
}
@@ -151,7 +178,7 @@ public func run_ArrayAppendGenericStructs(_ N: Int) {
151178
// can pre-reserve capacity.
152179
@inline(never)
153180
public func run_ArrayAppendOptionals(_ N: Int) {
154-
let other: [Int?] = Array(repeating: 1, count: 10_000)
181+
let other: [Int?] = otherOptionalOnes
155182

156183
for _ in 0..<N {
157184
for _ in 0..<10 {
@@ -168,7 +195,7 @@ public func run_ArrayAppendOptionals(_ N: Int) {
168195
// can pre-reserve capacity, but no optimization points used.
169196
@inline(never)
170197
public func run_ArrayAppendLazyMap(_ N: Int) {
171-
let other = Array(0..<10_000).lazy.map { $0 * 2 }
198+
let other = array.lazy.map { $0 * 2 }
172199

173200
for _ in 0..<N {
174201
for _ in 0..<10 {
@@ -208,7 +235,7 @@ public func appendFromGeneric<
208235

209236
@inline(never)
210237
public func run_ArrayAppendFromGeneric(_ N: Int) {
211-
let other = Array(repeating: 1, count: 10_000)
238+
let other: [Int] = otherOnes
212239

213240
for _ in 0..<N {
214241
for _ in 0..<10 {
@@ -230,7 +257,7 @@ public func appendToGeneric<
230257

231258
@inline(never)
232259
public func run_ArrayAppendToGeneric(_ N: Int) {
233-
let other = Array(repeating: 1, count: 10_000)
260+
let other: [Int] = otherOnes
234261

235262
for _ in 0..<N {
236263
for _ in 0..<10 {
@@ -247,14 +274,14 @@ public func run_ArrayAppendToGeneric(_ N: Int) {
247274
@inline(never)
248275
public func appendToFromGeneric<
249276
R: RangeReplaceableCollection, S: Sequence
250-
>(collection: inout R, sequence: S)
277+
>(collection: inout R, sequence: S)
251278
where R.Element == S.Element {
252279
collection.append(contentsOf: sequence)
253280
}
254281

255282
@inline(never)
256283
public func run_ArrayAppendToFromGeneric(_ N: Int) {
257-
let other = Array(repeating: 1, count: 10_000)
284+
let other: [Int] = otherOnes
258285

259286
for _ in 0..<N {
260287
for _ in 0..<10 {
@@ -319,7 +346,7 @@ public func run_ArrayAppendAscii(_ N: Int) {
319346
}
320347
}
321348

322-
// Append the utf8 elements of an ascii string to a [UInt8]
349+
// Append the utf8 elements of a latin1 string to a [UInt8]
323350
@inline(never)
324351
public func run_ArrayAppendLatin1(_ N: Int) {
325352
let s = "the quick brown fox jumps over the lazy dog\u{00A1}"
@@ -333,7 +360,7 @@ public func run_ArrayAppendLatin1(_ N: Int) {
333360
}
334361
}
335362

336-
// Append the utf8 elements of an ascii string to a [UInt8]
363+
// Append the utf8 elements of an utf16 string to a [UInt8]
337364
@inline(never)
338365
public func run_ArrayAppendUTF16(_ N: Int) {
339366
let s = "the quick brown 🦊 jumps over the lazy dog"
@@ -347,7 +374,7 @@ public func run_ArrayAppendUTF16(_ N: Int) {
347374
}
348375
}
349376

350-
// Append the utf8 elements of an ascii string to a [UInt8]
377+
// Append the utf8 elements of an ascii substring to a [UInt8]
351378
@inline(never)
352379
public func run_ArrayAppendAsciiSubstring(_ N: Int) {
353380
let s = "the quick brown fox jumps over the lazy dog!"[...]
@@ -361,7 +388,7 @@ public func run_ArrayAppendAsciiSubstring(_ N: Int) {
361388
}
362389
}
363390

364-
// Append the utf8 elements of an ascii string to a [UInt8]
391+
// Append the utf8 elements of a latin1 substring to a [UInt8]
365392
@inline(never)
366393
public func run_ArrayAppendLatin1Substring(_ N: Int) {
367394
let s = "the quick brown fox jumps over the lazy dog\u{00A1}"[...]
@@ -375,7 +402,7 @@ public func run_ArrayAppendLatin1Substring(_ N: Int) {
375402
}
376403
}
377404

378-
// Append the utf8 elements of an ascii string to a [UInt8]
405+
// Append the utf8 elements of an utf16 substring to a [UInt8]
379406
@inline(never)
380407
public func run_ArrayAppendUTF16Substring(_ N: Int) {
381408
let s = "the quick brown 🦊 jumps over the lazy dog"[...]
@@ -388,4 +415,3 @@ public func run_ArrayAppendUTF16Substring(_ N: Int) {
388415
}
389416
}
390417
}
391-

benchmark/single-source/ArrayInClass.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import TestsUtils
1414
public let ArrayInClass = BenchmarkInfo(
1515
name: "ArrayInClass",
1616
runFunction: run_ArrayInClass,
17-
tags: [.validation, .api, .Array])
17+
tags: [.validation, .api, .Array],
18+
setUpFunction: { ac = ArrayContainer() },
19+
tearDownFunction: { ac = nil })
20+
21+
var ac: ArrayContainer!
1822

1923
class ArrayContainer {
2024
final var arr : [Int]
@@ -32,13 +36,8 @@ class ArrayContainer {
3236
}
3337
}
3438

35-
@inline(never)
36-
func getArrayContainer() -> ArrayContainer {
37-
return ArrayContainer()
38-
}
39-
4039
@inline(never)
4140
public func run_ArrayInClass(_ N: Int) {
42-
let a = getArrayContainer()
41+
let a = ac!
4342
a.runLoop(N)
4443
}

0 commit comments

Comments
 (0)