Skip to content

Commit ff5074d

Browse files
committed
[benchmark] add removeAll(keepingCapacity: true) non-unique test
1 parent 282f15e commit ff5074d

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set(SWIFT_BENCH_MODULES
3737
single-source/ArrayOfGenericRef
3838
single-source/ArrayOfPOD
3939
single-source/ArrayOfRef
40+
single-source/ArrayRemoveAll
4041
single-source/ArraySetElement
4142
single-source/ArraySubscript
4243
single-source/BinaryFloatingPointConversionFromBinaryInteger
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// This test checks the performance of removeAll on a non-uniquely referenced array.
2+
import TestsUtils
3+
4+
public let benchmarks = [
5+
BenchmarkInfo(
6+
name: "ArrayRemoveAll_Class",
7+
runFunction: run_ArrayRemoveAll_Class,
8+
tags: [.validation, .api, .Array],
9+
setUpFunction: { blackHole(inputArray_Class) },
10+
tearDownFunction: { inputArray_Class = nil }
11+
),
12+
BenchmarkInfo(
13+
name: "ArrayRemoveAll_Int",
14+
runFunction: run_ArrayRemoveAll_Int,
15+
tags: [.validation, .api, .Array],
16+
setUpFunction: { blackHole(inputArray_Int) },
17+
tearDownFunction: { inputArray_Int = nil }
18+
)
19+
]
20+
21+
let size_Int = 1_000_000
22+
let size_Class = 100_000
23+
24+
var inputArray_Int: [Int]! = {
25+
var a: [Int] = []
26+
a.reserveCapacity(size_Int)
27+
for i in 0 ..< size_Int {
28+
a.append(i)
29+
}
30+
return a
31+
}()
32+
33+
var inputArray_Class: [Slow]! = {
34+
var a: [Slow] = []
35+
a.reserveCapacity(size_Class)
36+
for i in 0 ..< size_Class {
37+
a.append(Slow(num: i))
38+
}
39+
return a
40+
}()
41+
42+
class Slow {
43+
public var num: Int
44+
45+
init(num: Int) {
46+
self.num = num
47+
}
48+
}
49+
50+
51+
@inline(never)
52+
func verifyCapacity<T>(_ new: [T], orig: [T]) -> Bool {
53+
return new.capacity == orig.capacity
54+
}
55+
56+
@inline(never)
57+
func removeAll<T>(_ arr: [T]) -> [T] {
58+
var copy = arr
59+
copy.removeAll(keepingCapacity: true)
60+
return copy
61+
}
62+
63+
@inline(never)
64+
func copyItem<T>(_ item: T) -> T {
65+
return item
66+
}
67+
68+
@inline(never)
69+
func run_ArrayRemoveAll_Class(_ n: Int) {
70+
for _ in 1...n {
71+
let copy = removeAll(inputArray_Class)
72+
check(verifyCapacity(copy, orig: inputArray_Class))
73+
}
74+
}
75+
76+
@inline(never)
77+
func run_ArrayRemoveAll_Int(_ n: Int) {
78+
for _ in 1...n {
79+
let copy = removeAll(inputArray_Int)
80+
check(verifyCapacity(copy, orig: inputArray_Int))
81+
}
82+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import ArrayOfGenericPOD
2525
import ArrayOfGenericRef
2626
import ArrayOfPOD
2727
import ArrayOfRef
28+
import ArrayRemoveAll
2829
import ArraySetElement
2930
import ArraySubscript
3031
import BinaryFloatingPointConversionFromBinaryInteger
@@ -217,6 +218,7 @@ register(ArrayOfGenericPOD.benchmarks)
217218
register(ArrayOfGenericRef.benchmarks)
218219
register(ArrayOfPOD.benchmarks)
219220
register(ArrayOfRef.benchmarks)
221+
register(ArrayRemoveAll.benchmarks)
220222
register(ArraySetElement.benchmarks)
221223
register(ArraySubscript.benchmarks)
222224
register(BinaryFloatingPointConversionFromBinaryInteger.benchmarks)

0 commit comments

Comments
 (0)