Skip to content

Commit a723cf7

Browse files
authored
Merge pull request #34535 from micahbenn/main
[benchmark] Add benchmarks for IndexPath's subscripts, max, min
2 parents 4dab4c2 + 25bd5ea commit a723cf7

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-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: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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 Foundation
14+
import TestsUtils
15+
16+
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)
21+
let tags: [BenchmarkCategory] = [.validation, .api, .IndexPath]
22+
23+
public let IndexPathTest = [
24+
BenchmarkInfo(
25+
name: "IndexPath.Subscript.Mutation",
26+
runFunction: run_IndexPathSubscriptMutation,
27+
tags: tags,
28+
setUpFunction: { blackHole(increasingIndexPath) }),
29+
BenchmarkInfo(
30+
name: "IndexPath.Subscript.Range.Mutation",
31+
runFunction: run_IndexPathSubscriptRangeMutation,
32+
tags: tags,
33+
setUpFunction: { blackHole(increasingIndexPath) }),
34+
BenchmarkInfo(
35+
name: "IndexPath.Max",
36+
runFunction: run_IndexPathMax,
37+
tags: tags,
38+
setUpFunction: {
39+
blackHole(decreasingIndexPath)
40+
blackHole(increasingMaxMiddleIndexPath)
41+
blackHole(increasingIndexPath)
42+
}),
43+
BenchmarkInfo(
44+
name: "IndexPath.Min",
45+
runFunction: run_IndexPathMin,
46+
tags: tags,
47+
setUpFunction: {
48+
blackHole(increasingIndexPath)
49+
blackHole(increasingMinMiddleIndexPath)
50+
blackHole(decreasingIndexPath)
51+
}),
52+
]
53+
54+
func indexPath(_ size: Int, reversed: Bool = false) -> IndexPath {
55+
let indexes = Array(0..<size)
56+
return IndexPath(indexes: reversed ? indexes.reversed() : indexes)
57+
}
58+
59+
func indexPath(_ size: Int, middle: Int) -> IndexPath {
60+
var indexes = Array(0..<size)
61+
indexes.insert(middle, at: (indexes.count - 1) / 2)
62+
return IndexPath(indexes: indexes)
63+
}
64+
65+
// Subscript Mutations
66+
67+
@inline(__always)
68+
func subscriptMutation(
69+
n: Int,
70+
mutations: Int,
71+
indexPath: IndexPath,
72+
mutate: (inout IndexPath, Int) -> Void
73+
) {
74+
for _ in 0..<n {
75+
for i in 0..<mutations {
76+
var ip = indexPath
77+
mutate(&ip, i)
78+
}
79+
}
80+
}
81+
82+
@inline(never)
83+
public func run_IndexPathSubscriptMutation(_ n: Int) {
84+
subscriptMutation(
85+
n: n * 10, mutations: size, indexPath: increasingIndexPath,
86+
mutate: { ip, i in
87+
ip[i % 4] += 1
88+
})
89+
}
90+
91+
@inline(never)
92+
public func run_IndexPathSubscriptRangeMutation(_ n: Int) {
93+
subscriptMutation(
94+
n: n, mutations: size, indexPath: increasingIndexPath,
95+
mutate: { ip, i in
96+
ip[0..<i] += [i]
97+
})
98+
}
99+
100+
// Max
101+
102+
@inline(never)
103+
public func run_IndexPathMax(_ n: Int) {
104+
for _ in 0..<n * 10 {
105+
var val: Int?
106+
// Beginning max
107+
val = decreasingIndexPath.max()
108+
blackHole(val)
109+
// Middle max
110+
val = increasingMaxMiddleIndexPath.max()
111+
blackHole(val)
112+
// End max
113+
val = increasingIndexPath.max()
114+
blackHole(val)
115+
}
116+
}
117+
118+
// Min
119+
120+
@inline(never)
121+
public func run_IndexPathMin(_ n: Int) {
122+
for _ in 0..<n * 10 {
123+
var val: Int?
124+
// Beginning min
125+
val = increasingIndexPath.min()
126+
blackHole(val)
127+
// Middle min
128+
val = increasingMinMiddleIndexPath.min()
129+
blackHole(val)
130+
// End min
131+
val = decreasingIndexPath.min()
132+
blackHole(val)
133+
}
134+
}

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)