Skip to content

Commit 6f103c6

Browse files
committed
Pre-construct indexPath, use setUpFunction
1 parent 55b3b34 commit 6f103c6

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

Diff for: benchmark/single-source/IndexPathTest.swift

+68-48
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,71 @@ import Foundation
1414
import TestsUtils
1515

1616
let size = 200
17+
let increasingIndexPath = indexPath(size)
18+
let decreasingIndexPath = indexPath(size, reversed: true)
19+
let increasingMaxMiddleIndexPath = indexPath(size, middle: size + 1)
20+
let increasingMinMiddleIndexPath = indexPath(size, middle: -1)
1721
let tags: [BenchmarkCategory] = [.validation, .api, .IndexPath]
1822

1923
public let IndexPathTest = [
2024
BenchmarkInfo(
2125
name: "IndexPath.Subscript.Mutation",
22-
runFunction: { n in run_IndexPathSubscriptMutation(n * 60, size) },
23-
tags: tags),
26+
runFunction: { n in
27+
run_IndexPathSubscriptMutation(n * 10, size, increasingIndexPath)
28+
},
29+
tags: tags,
30+
setUpFunction: { blackHole(increasingIndexPath) }),
2431
BenchmarkInfo(
2532
name: "IndexPath.Subscript.Range.Mutation",
26-
runFunction: { n in run_IndexPathSubscriptRangeMutation(n * 60, size) },
27-
tags: tags),
33+
runFunction: { n in
34+
run_IndexPathSubscriptRangeMutation(n, size, increasingIndexPath)
35+
},
36+
tags: tags,
37+
setUpFunction: { blackHole(increasingIndexPath) }),
2838

2939
BenchmarkInfo(
3040
name: "IndexPath.Max.Beginning",
31-
runFunction: { n in run_IndexPathMaxBeginning(n * 60) },
32-
tags: tags),
41+
runFunction: { n in
42+
run_IndexPathMaxBeginning(n * 25, decreasingIndexPath)
43+
},
44+
tags: tags,
45+
setUpFunction: { blackHole(decreasingIndexPath) }),
3346
BenchmarkInfo(
3447
name: "IndexPath.Max.Middle",
35-
runFunction: { n in run_IndexPathMaxMiddle(n * 60) },
36-
tags: tags),
48+
runFunction: { n in
49+
run_IndexPathMaxMiddle(n * 25, increasingMaxMiddleIndexPath)
50+
},
51+
tags: tags,
52+
setUpFunction: { blackHole(increasingMaxMiddleIndexPath) }),
3753
BenchmarkInfo(
3854
name: "IndexPath.Max.End",
39-
runFunction: { n in run_IndexPathMaxEnd(n * 60) },
40-
tags: tags),
55+
runFunction: { n in
56+
run_IndexPathMaxEnd(n * 25, increasingIndexPath)
57+
},
58+
tags: tags,
59+
setUpFunction: { blackHole(increasingIndexPath) }),
4160

4261
BenchmarkInfo(
4362
name: "IndexPath.Min.Beginning",
44-
runFunction: { n in run_IndexPathMinBeginning(n * 60) },
45-
tags: tags),
63+
runFunction: { n in
64+
run_IndexPathMinBeginning(n * 25, increasingIndexPath)
65+
},
66+
tags: tags,
67+
setUpFunction: { blackHole(increasingIndexPath) }),
4668
BenchmarkInfo(
4769
name: "IndexPath.Min.Middle",
48-
runFunction: { n in run_IndexPathMinMiddle(n * 60) },
49-
tags: tags),
70+
runFunction: { n in
71+
run_IndexPathMinMiddle(n * 25, increasingMinMiddleIndexPath)
72+
},
73+
tags: tags,
74+
setUpFunction: { blackHole(increasingMinMiddleIndexPath) }),
5075
BenchmarkInfo(
5176
name: "IndexPath.Min.End",
52-
runFunction: { n in run_IndexPathMinEnd(n * 60) },
53-
tags: tags),
77+
runFunction: { n in
78+
run_IndexPathMinEnd(n * 25, decreasingIndexPath)
79+
},
80+
tags: tags,
81+
setUpFunction: { blackHole(decreasingIndexPath) }),
5482
]
5583

5684
@inline(__always)
@@ -72,30 +100,34 @@ func indexPath(_ size: Int, middle: Int) -> IndexPath {
72100
func subscriptMutation(
73101
n: Int,
74102
mutations: Int,
103+
indexPath: IndexPath,
75104
mutate: (inout IndexPath, Int) -> Void
76105
) {
77106
for _ in 0..<n {
78-
var ip = indexPath(size)
79107
for i in 0..<mutations {
108+
var ip = indexPath
80109
mutate(&ip, i)
81110
}
82-
blackHole(ip)
83111
}
84112
}
85113

86114
@inline(never)
87-
public func run_IndexPathSubscriptMutation(_ n: Int, _ count: Int) {
115+
public func run_IndexPathSubscriptMutation(
116+
_ n: Int, _ count: Int, _ indexPath: IndexPath
117+
) {
88118
subscriptMutation(
89-
n: n, mutations: count,
119+
n: n, mutations: count, indexPath: indexPath,
90120
mutate: { ip, i in
91121
ip[i % 4] += 1
92122
})
93123
}
94124

95125
@inline(never)
96-
public func run_IndexPathSubscriptRangeMutation(_ n: Int, _ count: Int) {
126+
public func run_IndexPathSubscriptRangeMutation(
127+
_ n: Int, _ count: Int, _ indexPath: IndexPath
128+
) {
97129
subscriptMutation(
98-
n: n, mutations: count,
130+
n: n, mutations: count, indexPath: indexPath,
99131
mutate: { ip, i in
100132
ip[0..<i] += [i]
101133
})
@@ -106,11 +138,11 @@ public func run_IndexPathSubscriptRangeMutation(_ n: Int, _ count: Int) {
106138
@inline(__always)
107139
func maxMin(
108140
n: Int,
109-
creator: () -> IndexPath,
141+
indexPath: IndexPath,
110142
maxMinFunc: (inout IndexPath) -> Int?
111143
) {
112144
for _ in 0..<n {
113-
var ip = creator()
145+
var ip = indexPath
114146
let found = maxMinFunc(&ip) != nil
115147
blackHole(found)
116148
}
@@ -119,36 +151,30 @@ func maxMin(
119151
// Max
120152

121153
@inline(never)
122-
public func run_IndexPathMaxBeginning(_ n: Int) {
154+
public func run_IndexPathMaxBeginning(_ n: Int, _ indexPath: IndexPath) {
123155
maxMin(
124156
n: n,
125-
creator: {
126-
indexPath(size, reversed: true)
127-
},
157+
indexPath: indexPath,
128158
maxMinFunc: { ip in
129159
ip.max()
130160
})
131161
}
132162

133163
@inline(never)
134-
public func run_IndexPathMaxMiddle(_ n: Int) {
164+
public func run_IndexPathMaxMiddle(_ n: Int, _ indexPath: IndexPath) {
135165
maxMin(
136166
n: n,
137-
creator: {
138-
indexPath(size, middle: size + 1)
139-
},
167+
indexPath: indexPath,
140168
maxMinFunc: { ip in
141169
ip.max()
142170
})
143171
}
144172

145173
@inline(never)
146-
public func run_IndexPathMaxEnd(_ n: Int) {
174+
public func run_IndexPathMaxEnd(_ n: Int, _ indexPath: IndexPath) {
147175
maxMin(
148176
n: n,
149-
creator: {
150-
indexPath(size)
151-
},
177+
indexPath: indexPath,
152178
maxMinFunc: { ip in
153179
ip.max()
154180
})
@@ -157,36 +183,30 @@ public func run_IndexPathMaxEnd(_ n: Int) {
157183
// Min
158184

159185
@inline(never)
160-
public func run_IndexPathMinBeginning(_ n: Int) {
186+
public func run_IndexPathMinBeginning(_ n: Int, _ indexPath: IndexPath) {
161187
maxMin(
162188
n: n,
163-
creator: {
164-
indexPath(size)
165-
},
189+
indexPath: indexPath,
166190
maxMinFunc: { ip in
167191
ip.min()
168192
})
169193
}
170194

171195
@inline(never)
172-
public func run_IndexPathMinMiddle(_ n: Int) {
196+
public func run_IndexPathMinMiddle(_ n: Int, _ indexPath: IndexPath) {
173197
maxMin(
174198
n: n,
175-
creator: {
176-
indexPath(size, middle: -1)
177-
},
199+
indexPath: indexPath,
178200
maxMinFunc: { ip in
179201
ip.min()
180202
})
181203
}
182204

183205
@inline(never)
184-
public func run_IndexPathMinEnd(_ n: Int) {
206+
public func run_IndexPathMinEnd(_ n: Int, _ indexPath: IndexPath) {
185207
maxMin(
186208
n: n,
187-
creator: {
188-
indexPath(size, reversed: true)
189-
},
209+
indexPath: indexPath,
190210
maxMinFunc: { ip in
191211
ip.min()
192212
})

0 commit comments

Comments
 (0)