Skip to content

Commit 7178406

Browse files
committed
Add IndexPath benchmarks
Add benchmarks for subscripts, max, min
1 parent a8c48d1 commit 7178406

File tree

4 files changed

+171
-1
lines changed

4 files changed

+171
-1
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ set(SWIFT_BENCH_MODULES
9898
single-source/Hash
9999
single-source/Histogram
100100
single-source/HTTP2StateMachine
101+
single-source/IndexPathTest
101102
single-source/InsertCharacter
102103
single-source/IntegerParsing
103104
single-source/Integrate
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
//===--- IndexPathTest.swift -------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
import Foundation
15+
16+
let size = 200
17+
let tags: [BenchmarkCategory] = [.validation, .api, .IndexPath]
18+
19+
public let IndexPathTest = [
20+
BenchmarkInfo(name: "IndexPathSubscriptMutation",
21+
runFunction: { n in run_IndexPathSubscriptMutation(n * 1000, size)},
22+
tags: tags),
23+
BenchmarkInfo(name: "IndexPathSubscriptRangeMutation",
24+
runFunction: { n in run_IndexPathSubscriptRangeMutation(n * 1000, size)},
25+
tags: tags),
26+
27+
BenchmarkInfo(name: "IndexPathMaxBeginning",
28+
runFunction: { n in run_IndexPathMaxBeginning(n * 1000)},
29+
tags: tags),
30+
BenchmarkInfo(name: "IndexPathMaxMiddle",
31+
runFunction: { n in run_IndexPathMaxMiddle(n * 1000)},
32+
tags: tags),
33+
BenchmarkInfo(name: "IndexPathMaxEnd",
34+
runFunction: { n in run_IndexPathMaxEnd(n * 1000)},
35+
tags: tags),
36+
37+
BenchmarkInfo(name: "IndexPathMinBeginning",
38+
runFunction: { n in run_IndexPathMinBeginning(n * 1000)},
39+
tags: tags),
40+
BenchmarkInfo(name: "IndexPathMinMiddle",
41+
runFunction: { n in run_IndexPathMinMiddle(n * 1000)},
42+
tags: tags),
43+
BenchmarkInfo(name: "IndexPathMinEnd",
44+
runFunction: { n in run_IndexPathMinEnd(n * 1000)},
45+
tags: tags),
46+
]
47+
48+
@inline(__always)
49+
func indexPath(_ size: Int,
50+
reversed: Bool = false) -> IndexPath {
51+
let indexes = Array(0..<size)
52+
return IndexPath(indexes: reversed ? indexes.reversed() : indexes)
53+
}
54+
55+
@inline(__always)
56+
func indexPath(_ size: Int,
57+
middle: Int) -> IndexPath {
58+
var indexes = Array(0..<size)
59+
indexes.insert(middle, at: (indexes.count - 1) / 2)
60+
return IndexPath(indexes: indexes)
61+
}
62+
63+
// Subscript Mutations
64+
65+
@inline(__always)
66+
func subscriptMutation(n: Int,
67+
mutations: Int,
68+
mutate: (inout IndexPath, Int) -> ()) {
69+
for _ in 0..<n {
70+
var ip = indexPath(size)
71+
72+
for i in 0..<mutations {
73+
mutate(&ip, i)
74+
}
75+
76+
blackHole(ip)
77+
}
78+
}
79+
80+
@inline(never)
81+
public func run_IndexPathSubscriptMutation(_ n: Int, _ count: Int) {
82+
subscriptMutation(n: n,
83+
mutations: count,
84+
mutate: { ip, i in
85+
ip[i % 4] += 1
86+
})
87+
}
88+
89+
@inline(never)
90+
public func run_IndexPathSubscriptRangeMutation(_ n: Int, _ count: Int) {
91+
subscriptMutation(n: count,
92+
mutations: count,
93+
mutate: { ip, i in
94+
ip[0..<i] += [i]
95+
})
96+
}
97+
98+
// Max, Min
99+
100+
@inline(__always)
101+
func maxMin(n: Int,
102+
creator: () -> IndexPath,
103+
maxMinFunc: (inout IndexPath) -> Int?) {
104+
for _ in 0..<n {
105+
var ip = creator()
106+
let found = maxMinFunc(&ip) != nil
107+
blackHole(found)
108+
}
109+
}
110+
111+
// Max
112+
113+
@inline(never)
114+
public func run_IndexPathMaxBeginning(_ n: Int) {
115+
maxMin(n: n, creator: {
116+
indexPath(size, reversed: true)
117+
}, maxMinFunc: { ip in
118+
ip.max()
119+
})
120+
}
121+
122+
@inline(never)
123+
public func run_IndexPathMaxMiddle(_ n: Int) {
124+
maxMin(n: n, creator: {
125+
indexPath(size, middle: size + 1)
126+
}, maxMinFunc: { ip in
127+
ip.max()
128+
})
129+
}
130+
131+
@inline(never)
132+
public func run_IndexPathMaxEnd(_ n: Int) {
133+
maxMin(n: n, creator: {
134+
indexPath(size)
135+
}, maxMinFunc: { ip in
136+
ip.max()
137+
})
138+
}
139+
140+
// Min
141+
142+
@inline(never)
143+
public func run_IndexPathMinBeginning(_ n: Int) {
144+
maxMin(n: n, creator: {
145+
indexPath(size)
146+
}, maxMinFunc: { ip in
147+
ip.min()
148+
})
149+
}
150+
151+
@inline(never)
152+
public func run_IndexPathMinMiddle(_ n: Int) {
153+
maxMin(n: n, creator: {
154+
indexPath(size, middle: -1)
155+
}, maxMinFunc: { ip in
156+
ip.min()
157+
})
158+
}
159+
160+
@inline(never)
161+
public func run_IndexPathMinEnd(_ n: Int) {
162+
maxMin(n: n, creator: {
163+
indexPath(size, reversed: true)
164+
}, maxMinFunc: { ip in
165+
ip.min()
166+
})
167+
}

benchmark/utils/TestsUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public enum BenchmarkCategory : String {
2323
// we know is important to measure.
2424
case validation
2525
// subsystems to validate and their subcategories.
26-
case api, Array, String, Dictionary, Codable, Set, Data
26+
case api, Array, String, Dictionary, Codable, Set, Data, IndexPath
2727
case sdk
2828
case runtime, refcount, metadata
2929
// Other general areas of compiled code validation.

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import Hanoi
8686
import Hash
8787
import Histogram
8888
import HTTP2StateMachine
89+
import IndexPathTest
8990
import InsertCharacter
9091
import IntegerParsing
9192
import Integrate
@@ -274,6 +275,7 @@ registerBenchmark(Hanoi)
274275
registerBenchmark(HashTest)
275276
registerBenchmark(Histogram)
276277
registerBenchmark(HTTP2StateMachine)
278+
registerBenchmark(IndexPathTest)
277279
registerBenchmark(InsertCharacter)
278280
registerBenchmark(IntegerParsing)
279281
registerBenchmark(IntegrateTest)

0 commit comments

Comments
 (0)