Skip to content

[benchmark] add removeAll(keepingCapacity: true) benchmark for non-unique arrays #66087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ set(SWIFT_BENCH_MODULES
single-source/ArrayOfGenericRef
single-source/ArrayOfPOD
single-source/ArrayOfRef
single-source/ArrayRemoveAll
single-source/ArraySetElement
single-source/ArraySubscript
single-source/BinaryFloatingPointConversionFromBinaryInteger
Expand Down
55 changes: 55 additions & 0 deletions benchmark/single-source/ArrayRemoveAll.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This test checks the performance of removeAll
// on a non-uniquely referenced array.

import TestsUtils

public let benchmarks = [
BenchmarkInfo(
name: "Array.removeAll.keepingCapacity.Int",
runFunction: run_ArrayRemoveAll_Class,
tags: [.validation, .api, .Array],
setUpFunction: { blackHole(inputArray_Class) }
),
BenchmarkInfo(
name: "Array.removeAll.keepingCapacity.Object",
runFunction: run_ArrayRemoveAll_Int,
tags: [.validation, .api, .Array],
setUpFunction: { blackHole(inputArray_Int) }
)
]

class Slow {
public var num: Int

init(num: Int) {
self.num = num
}
}

let inputArray_Int: [Int] = Array(0..<500_000)
let inputArray_Class: [Slow] = (0..<50_000).map(Slow.init(num:))

@inline(never)
func removeAll<T>(_ arr: [T]) -> [T] {
var copy = arr
copy.removeAll(keepingCapacity: true)
return copy
}

@inline(never)
func run_ArrayRemoveAll_Class(_ n: Int) {
var copy = removeAll(inputArray_Class);
for _ in 1..<n {
copy = removeAll(inputArray_Class)
}
check(copy.capacity == inputArray_Class.capacity)
}

@inline(never)
func run_ArrayRemoveAll_Int(_ n: Int) {
var copy = removeAll(inputArray_Int);
for _ in 1..<n {
copy = removeAll(inputArray_Int)
}
check(copy.capacity == inputArray_Int.capacity)
}
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import ArrayOfGenericPOD
import ArrayOfGenericRef
import ArrayOfPOD
import ArrayOfRef
import ArrayRemoveAll
import ArraySetElement
import ArraySubscript
import BinaryFloatingPointConversionFromBinaryInteger
Expand Down Expand Up @@ -217,6 +218,7 @@ register(ArrayOfGenericPOD.benchmarks)
register(ArrayOfGenericRef.benchmarks)
register(ArrayOfPOD.benchmarks)
register(ArrayOfRef.benchmarks)
register(ArrayRemoveAll.benchmarks)
register(ArraySetElement.benchmarks)
register(ArraySubscript.benchmarks)
register(BinaryFloatingPointConversionFromBinaryInteger.benchmarks)
Expand Down