Skip to content

Commit 53653de

Browse files
committed
[benchmark] Extract setup from ArrayAppend*
ArrayAppendStrings had setup overhead of 10ms (42%). ArrayAppendLazyMap had setup overhead of 24 μs (1%). ArrayAppendOptionals and ArrayAppendArrayOfInt also had barely visible, small overhead of ~18μs, that was mostly hidden in measurement noise, but I’ve extracted the setup from all places that had 10 000 element array initializations, in preparation for more precise measurement in the future.
1 parent 8617745 commit 53653de

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

benchmark/single-source/ArrayAppend.swift

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,50 @@ import TestsUtils
1717
let t: [BenchmarkCategory] = [.validation, .api, .Array]
1818
public let ArrayAppend = [
1919
BenchmarkInfo(name: "ArrayAppend", runFunction: run_ArrayAppend, tags: t),
20-
BenchmarkInfo(name: "ArrayAppendArrayOfInt", runFunction: run_ArrayAppendArrayOfInt, tags: t),
20+
BenchmarkInfo(name: "ArrayAppendArrayOfInt", runFunction: run_ArrayAppendArrayOfInt, tags: t,
21+
setUpFunction: ones, tearDownFunction: releaseOnes),
2122
BenchmarkInfo(name: "ArrayAppendAscii", runFunction: run_ArrayAppendAscii, tags: t),
2223
BenchmarkInfo(name: "ArrayAppendAsciiSubstring", runFunction: run_ArrayAppendAsciiSubstring, tags: t),
23-
BenchmarkInfo(name: "ArrayAppendFromGeneric", runFunction: run_ArrayAppendFromGeneric, tags: t),
24-
BenchmarkInfo(name: "ArrayAppendGenericStructs", runFunction: run_ArrayAppendGenericStructs, 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 } ),
2529
BenchmarkInfo(name: "ArrayAppendLatin1", runFunction: run_ArrayAppendLatin1, tags: t),
2630
BenchmarkInfo(name: "ArrayAppendLatin1Substring", runFunction: run_ArrayAppendLatin1Substring, tags: t),
27-
BenchmarkInfo(name: "ArrayAppendLazyMap", runFunction: run_ArrayAppendLazyMap, tags: t),
28-
BenchmarkInfo(name: "ArrayAppendOptionals", runFunction: run_ArrayAppendOptionals, 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 } ),
2936
BenchmarkInfo(name: "ArrayAppendRepeatCol", runFunction: run_ArrayAppendRepeatCol, tags: t),
3037
BenchmarkInfo(name: "ArrayAppendReserved", runFunction: run_ArrayAppendReserved, tags: t),
3138
BenchmarkInfo(name: "ArrayAppendSequence", runFunction: run_ArrayAppendSequence, tags: t),
32-
BenchmarkInfo(name: "ArrayAppendStrings", runFunction: run_ArrayAppendStrings, tags: t),
33-
BenchmarkInfo(name: "ArrayAppendToFromGeneric", runFunction: run_ArrayAppendToFromGeneric, tags: t),
34-
BenchmarkInfo(name: "ArrayAppendToGeneric", runFunction: run_ArrayAppendToGeneric, 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),
3546
BenchmarkInfo(name: "ArrayAppendUTF16", runFunction: run_ArrayAppendUTF16, tags: t),
3647
BenchmarkInfo(name: "ArrayAppendUTF16Substring", runFunction: run_ArrayAppendUTF16Substring, tags: t),
37-
BenchmarkInfo(name: "ArrayPlusEqualArrayOfInt", runFunction: run_ArrayPlusEqualArrayOfInt, tags: t),
48+
BenchmarkInfo(name: "ArrayPlusEqualArrayOfInt", runFunction: run_ArrayPlusEqualArrayOfInt, tags: t,
49+
setUpFunction: ones, tearDownFunction: releaseOnes),
3850
BenchmarkInfo(name: "ArrayPlusEqualFiveElementCollection", runFunction: run_ArrayPlusEqualFiveElementCollection, tags: t),
3951
BenchmarkInfo(name: "ArrayPlusEqualSingleElementCollection", runFunction: run_ArrayPlusEqualSingleElementCollection, tags: t),
4052
BenchmarkInfo(name: "ArrayPlusEqualThreeElements", runFunction: run_ArrayPlusEqualThreeElements, tags: t),
4153
]
4254

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+
4364
// Append single element
4465
@inline(never)
4566
public func run_ArrayAppend(_ N: Int) {
@@ -87,7 +108,8 @@ public func run_ArrayAppendSequence(_ N: Int) {
87108
// can pre-reserve capacity.
88109
@inline(never)
89110
public func run_ArrayAppendArrayOfInt(_ N: Int) {
90-
let other = Array(repeating: 1, count: 10_000)
111+
let other: [Int] = otherOnes
112+
91113
for _ in 0..<N {
92114
for _ in 0..<10 {
93115
var nums = [Int]()
@@ -102,7 +124,8 @@ public func run_ArrayAppendArrayOfInt(_ N: Int) {
102124
// except +=, to check equally performant.
103125
@inline(never)
104126
public func run_ArrayPlusEqualArrayOfInt(_ N: Int) {
105-
let other = Array(repeating: 1, count: 10_000)
127+
let other: [Int] = otherOnes
128+
106129
for _ in 0..<N {
107130
for _ in 0..<10 {
108131
var nums = [Int]()
@@ -117,7 +140,8 @@ public func run_ArrayPlusEqualArrayOfInt(_ N: Int) {
117140
// can pre-reserve capacity.
118141
@inline(never)
119142
public func run_ArrayAppendStrings(_ N: Int) {
120-
let other = stride(from: 0, to: 10_000, by: 1).map { "\($0)" }
143+
let other: [String] = otherStrings
144+
121145
for _ in 0..<N {
122146
for _ in 0..<10 {
123147
var nums = [String]()
@@ -138,7 +162,8 @@ struct S<T,U> {
138162
// can pre-reserve capacity.
139163
@inline(never)
140164
public func run_ArrayAppendGenericStructs(_ N: Int) {
141-
let other = Array(repeating: S(x: 3, y: 4.2), count: 10_000)
165+
let other: [S<Int, Double>] = otherStructs
166+
142167
for _ in 0..<N {
143168
for _ in 0..<10 {
144169
var nums = [S<Int, Double>]()
@@ -153,7 +178,7 @@ public func run_ArrayAppendGenericStructs(_ N: Int) {
153178
// can pre-reserve capacity.
154179
@inline(never)
155180
public func run_ArrayAppendOptionals(_ N: Int) {
156-
let other: [Int?] = Array(repeating: 1, count: 10_000)
181+
let other: [Int?] = otherOptionalOnes
157182

158183
for _ in 0..<N {
159184
for _ in 0..<10 {
@@ -170,7 +195,7 @@ public func run_ArrayAppendOptionals(_ N: Int) {
170195
// can pre-reserve capacity, but no optimization points used.
171196
@inline(never)
172197
public func run_ArrayAppendLazyMap(_ N: Int) {
173-
let other = Array(0..<10_000).lazy.map { $0 * 2 }
198+
let other = array.lazy.map { $0 * 2 }
174199

175200
for _ in 0..<N {
176201
for _ in 0..<10 {
@@ -210,7 +235,7 @@ public func appendFromGeneric<
210235

211236
@inline(never)
212237
public func run_ArrayAppendFromGeneric(_ N: Int) {
213-
let other = Array(repeating: 1, count: 10_000)
238+
let other: [Int] = otherOnes
214239

215240
for _ in 0..<N {
216241
for _ in 0..<10 {
@@ -232,7 +257,7 @@ public func appendToGeneric<
232257

233258
@inline(never)
234259
public func run_ArrayAppendToGeneric(_ N: Int) {
235-
let other = Array(repeating: 1, count: 10_000)
260+
let other: [Int] = otherOnes
236261

237262
for _ in 0..<N {
238263
for _ in 0..<10 {
@@ -256,7 +281,7 @@ where R.Element == S.Element {
256281

257282
@inline(never)
258283
public func run_ArrayAppendToFromGeneric(_ N: Int) {
259-
let other = Array(repeating: 1, count: 10_000)
284+
let other: [Int] = otherOnes
260285

261286
for _ in 0..<N {
262287
for _ in 0..<10 {

0 commit comments

Comments
 (0)