diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index d00ed7f4e6711..3d5ebb922e57b 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -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 diff --git a/benchmark/single-source/ArrayRemoveAll.swift b/benchmark/single-source/ArrayRemoveAll.swift new file mode 100644 index 0000000000000..f2832329e2729 --- /dev/null +++ b/benchmark/single-source/ArrayRemoveAll.swift @@ -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(_ 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..