Skip to content

Commit caa497a

Browse files
authored
Merge pull request #66087 from oxy/gh56321-benchmark
[benchmark] add `removeAll(keepingCapacity: true)` benchmark for non-unique arrays
2 parents 631a3ef + 32745c3 commit caa497a

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// This test checks the performance of removeAll
2+
// on a non-uniquely referenced array.
3+
4+
import TestsUtils
5+
6+
public let benchmarks = [
7+
BenchmarkInfo(
8+
name: "Array.removeAll.keepingCapacity.Int",
9+
runFunction: run_ArrayRemoveAll_Class,
10+
tags: [.validation, .api, .Array],
11+
setUpFunction: { blackHole(inputArray_Class) }
12+
),
13+
BenchmarkInfo(
14+
name: "Array.removeAll.keepingCapacity.Object",
15+
runFunction: run_ArrayRemoveAll_Int,
16+
tags: [.validation, .api, .Array],
17+
setUpFunction: { blackHole(inputArray_Int) }
18+
)
19+
]
20+
21+
class Slow {
22+
public var num: Int
23+
24+
init(num: Int) {
25+
self.num = num
26+
}
27+
}
28+
29+
let inputArray_Int: [Int] = Array(0..<500_000)
30+
let inputArray_Class: [Slow] = (0..<50_000).map(Slow.init(num:))
31+
32+
@inline(never)
33+
func removeAll<T>(_ arr: [T]) -> [T] {
34+
var copy = arr
35+
copy.removeAll(keepingCapacity: true)
36+
return copy
37+
}
38+
39+
@inline(never)
40+
func run_ArrayRemoveAll_Class(_ n: Int) {
41+
var copy = removeAll(inputArray_Class);
42+
for _ in 1..<n {
43+
copy = removeAll(inputArray_Class)
44+
}
45+
check(copy.capacity == inputArray_Class.capacity)
46+
}
47+
48+
@inline(never)
49+
func run_ArrayRemoveAll_Int(_ n: Int) {
50+
var copy = removeAll(inputArray_Int);
51+
for _ in 1..<n {
52+
copy = removeAll(inputArray_Int)
53+
}
54+
check(copy.capacity == inputArray_Int.capacity)
55+
}

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)