From 51c62aee1925a10f91a769293738e553af3a44eb Mon Sep 17 00:00:00 2001 From: Lucy Satheesan Date: Tue, 23 May 2023 10:52:43 -0700 Subject: [PATCH 1/5] [benchmark] add `removeAll(keepingCapacity: true)` non-unique test --- benchmark/CMakeLists.txt | 1 + benchmark/single-source/ArrayRemoveAll.swift | 82 ++++++++++++++++++++ benchmark/utils/main.swift | 2 + 3 files changed, 85 insertions(+) create mode 100644 benchmark/single-source/ArrayRemoveAll.swift 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..ed146bc4c2346 --- /dev/null +++ b/benchmark/single-source/ArrayRemoveAll.swift @@ -0,0 +1,82 @@ +// This test checks the performance of removeAll on a non-uniquely referenced array. +import TestsUtils + +public let benchmarks = [ + BenchmarkInfo( + name: "ArrayRemoveAll_Class", + runFunction: run_ArrayRemoveAll_Class, + tags: [.validation, .api, .Array], + setUpFunction: { blackHole(inputArray_Class) }, + tearDownFunction: { inputArray_Class = nil } + ), + BenchmarkInfo( + name: "ArrayRemoveAll_Int", + runFunction: run_ArrayRemoveAll_Int, + tags: [.validation, .api, .Array], + setUpFunction: { blackHole(inputArray_Int) }, + tearDownFunction: { inputArray_Int = nil } + ) +] + +let size_Int = 1_000_000 +let size_Class = 100_000 + +var inputArray_Int: [Int]! = { + var a: [Int] = [] + a.reserveCapacity(size_Int) + for i in 0 ..< size_Int { + a.append(i) + } + return a +}() + +var inputArray_Class: [Slow]! = { + var a: [Slow] = [] + a.reserveCapacity(size_Class) + for i in 0 ..< size_Class { + a.append(Slow(num: i)) + } + return a +}() + +class Slow { + public var num: Int + + init(num: Int) { + self.num = num + } +} + + +@inline(never) +func verifyCapacity(_ new: [T], orig: [T]) -> Bool { + return new.capacity == orig.capacity +} + +@inline(never) +func removeAll(_ arr: [T]) -> [T] { + var copy = arr + copy.removeAll(keepingCapacity: true) + return copy +} + +@inline(never) +func copyItem(_ item: T) -> T { + return item +} + +@inline(never) +func run_ArrayRemoveAll_Class(_ n: Int) { + for _ in 1...n { + let copy = removeAll(inputArray_Class) + check(verifyCapacity(copy, orig: inputArray_Class)) + } +} + +@inline(never) +func run_ArrayRemoveAll_Int(_ n: Int) { + for _ in 1...n { + let copy = removeAll(inputArray_Int) + check(verifyCapacity(copy, orig: inputArray_Int)) + } +} diff --git a/benchmark/utils/main.swift b/benchmark/utils/main.swift index 93fa35ea02553..d10da0d457837 100644 --- a/benchmark/utils/main.swift +++ b/benchmark/utils/main.swift @@ -25,6 +25,7 @@ import ArrayOfGenericPOD import ArrayOfGenericRef import ArrayOfPOD import ArrayOfRef +import ArrayRemoveAll import ArraySetElement import ArraySubscript import BinaryFloatingPointConversionFromBinaryInteger @@ -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) From d489ba2740bd692fee8e996a0d9c0de9746a9873 Mon Sep 17 00:00:00 2001 From: Lucy Satheesan Date: Tue, 23 May 2023 11:22:47 -0700 Subject: [PATCH 2/5] [benchmark] check results only once in ArrayRemoveAll --- benchmark/single-source/ArrayRemoveAll.swift | 41 ++++++++------------ 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/benchmark/single-source/ArrayRemoveAll.swift b/benchmark/single-source/ArrayRemoveAll.swift index ed146bc4c2346..0dd475e2eec24 100644 --- a/benchmark/single-source/ArrayRemoveAll.swift +++ b/benchmark/single-source/ArrayRemoveAll.swift @@ -18,6 +18,14 @@ public let benchmarks = [ ) ] +class Slow { + public var num: Int + + init(num: Int) { + self.num = num + } +} + let size_Int = 1_000_000 let size_Class = 100_000 @@ -39,20 +47,6 @@ var inputArray_Class: [Slow]! = { return a }() -class Slow { - public var num: Int - - init(num: Int) { - self.num = num - } -} - - -@inline(never) -func verifyCapacity(_ new: [T], orig: [T]) -> Bool { - return new.capacity == orig.capacity -} - @inline(never) func removeAll(_ arr: [T]) -> [T] { var copy = arr @@ -60,23 +54,20 @@ func removeAll(_ arr: [T]) -> [T] { return copy } -@inline(never) -func copyItem(_ item: T) -> T { - return item -} - @inline(never) func run_ArrayRemoveAll_Class(_ n: Int) { - for _ in 1...n { - let copy = removeAll(inputArray_Class) - check(verifyCapacity(copy, orig: inputArray_Class)) + var copy = removeAll(inputArray_Class); + for _ in 1.. Date: Tue, 23 May 2023 14:14:14 -0700 Subject: [PATCH 3/5] [benchmark] clean up setup for ArrayRemoveAll --- benchmark/single-source/ArrayRemoveAll.swift | 32 +++++--------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/benchmark/single-source/ArrayRemoveAll.swift b/benchmark/single-source/ArrayRemoveAll.swift index 0dd475e2eec24..fceb5224106b6 100644 --- a/benchmark/single-source/ArrayRemoveAll.swift +++ b/benchmark/single-source/ArrayRemoveAll.swift @@ -1,4 +1,6 @@ -// This test checks the performance of removeAll on a non-uniquely referenced array. +// This test checks the performance of removeAll +// on a non-uniquely referenced array. + import TestsUtils public let benchmarks = [ @@ -6,15 +8,13 @@ public let benchmarks = [ name: "ArrayRemoveAll_Class", runFunction: run_ArrayRemoveAll_Class, tags: [.validation, .api, .Array], - setUpFunction: { blackHole(inputArray_Class) }, - tearDownFunction: { inputArray_Class = nil } + setUpFunction: { blackHole(inputArray_Class) } ), BenchmarkInfo( name: "ArrayRemoveAll_Int", runFunction: run_ArrayRemoveAll_Int, tags: [.validation, .api, .Array], - setUpFunction: { blackHole(inputArray_Int) }, - tearDownFunction: { inputArray_Int = nil } + setUpFunction: { blackHole(inputArray_Int) } ) ] @@ -26,26 +26,8 @@ class Slow { } } -let size_Int = 1_000_000 -let size_Class = 100_000 - -var inputArray_Int: [Int]! = { - var a: [Int] = [] - a.reserveCapacity(size_Int) - for i in 0 ..< size_Int { - a.append(i) - } - return a -}() - -var inputArray_Class: [Slow]! = { - var a: [Slow] = [] - a.reserveCapacity(size_Class) - for i in 0 ..< size_Class { - a.append(Slow(num: i)) - } - return a -}() +let inputArray_Int: [Int] = Array(0..<1_000_000) +let inputArray_Class: [Slow] = (0..<100_000).map(Slow.init(num:)) @inline(never) func removeAll(_ arr: [T]) -> [T] { From 6a1129f68aa72a6890262e2a58c4a2a97fec9be1 Mon Sep 17 00:00:00 2001 From: Lucy Satheesan Date: Tue, 23 May 2023 17:10:32 -0700 Subject: [PATCH 4/5] [benchmark] rename `Array.removeAll` benchmarks --- benchmark/single-source/ArrayRemoveAll.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/single-source/ArrayRemoveAll.swift b/benchmark/single-source/ArrayRemoveAll.swift index fceb5224106b6..4543c2039926e 100644 --- a/benchmark/single-source/ArrayRemoveAll.swift +++ b/benchmark/single-source/ArrayRemoveAll.swift @@ -5,13 +5,13 @@ import TestsUtils public let benchmarks = [ BenchmarkInfo( - name: "ArrayRemoveAll_Class", + name: "Array.removeAll.keepingCapacity.Int", runFunction: run_ArrayRemoveAll_Class, tags: [.validation, .api, .Array], setUpFunction: { blackHole(inputArray_Class) } ), BenchmarkInfo( - name: "ArrayRemoveAll_Int", + name: "Array.removeAll.keepingCapacity.Object", runFunction: run_ArrayRemoveAll_Int, tags: [.validation, .api, .Array], setUpFunction: { blackHole(inputArray_Int) } From 32745c3c5610603b8b7433226d3594d6932de5b8 Mon Sep 17 00:00:00 2001 From: Lucy Satheesan Date: Tue, 23 May 2023 21:10:11 -0700 Subject: [PATCH 5/5] [benchmark] half `Array.removeAll` bench size --- benchmark/single-source/ArrayRemoveAll.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/single-source/ArrayRemoveAll.swift b/benchmark/single-source/ArrayRemoveAll.swift index 4543c2039926e..f2832329e2729 100644 --- a/benchmark/single-source/ArrayRemoveAll.swift +++ b/benchmark/single-source/ArrayRemoveAll.swift @@ -26,8 +26,8 @@ class Slow { } } -let inputArray_Int: [Int] = Array(0..<1_000_000) -let inputArray_Class: [Slow] = (0..<100_000).map(Slow.init(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] {