From 78e406c37673b9a5b592994343fd7509fef700ff Mon Sep 17 00:00:00 2001 From: David Smith Date: Tue, 9 May 2023 09:41:44 -0700 Subject: [PATCH 01/13] Add a few basic smoke test benchmarks for the default RangeReplaceableCollection append methods --- benchmark/CMakeLists.txt | 1 + ...angeReplaceableCollectionConformance.swift | 102 ++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index de4b8656b7820..d00ed7f4e6711 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -121,6 +121,7 @@ set(SWIFT_BENCH_MODULES single-source/NSDictionaryCastToSwift single-source/NSErrorTest single-source/NSStringConversion + single-source/NaiveRangeReplaceableCollectionConformance single-source/NibbleSort single-source/NIOChannelPipeline single-source/NopDeinit diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift new file mode 100644 index 0000000000000..e0a13824e163f --- /dev/null +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -0,0 +1,102 @@ +//===--- NaiveRangeReplaceableCollectionConformance.swift -----------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import TestsUtils + +var contiguous:[UInt8]? + +public let benchmarks = [ + BenchmarkInfo(name: "NaiveRRC.append.largeContiguous", + runFunction: runAppendLargeContiguous, + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000_000) }, + tags: [.validation, .api]), + BenchmarkInfo(name: "NaiveRRC.append.smallContiguousRepeatedly", + runFunction: runAppendLargeContiguous, + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1) }, + tags: [.validation, .api]), + BenchmarkInfo(name: "NaiveRRC.init.largeContiguous", + runFunction: runInitContiguous, + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000_000) }, + tags: [.validation, .api]), +] + +struct NaiveRRC : RangeReplaceableCollection { + + var storage:[UInt8] = [] + + init() {} + + func index(after i: Int) -> Int { + i + 1 + } + + func index(before i: Int) -> Int { + i - 1 + } + + var startIndex: Int { + 0 + } + + var endIndex: Int { + count + } + + var count: Int { + storage.count + } + + subscript(position: Int) -> UInt8 { + get { + storage[position] + } + set(newValue) { + storage[position] = newValue + } + } + + mutating func replaceSubrange(_ subrange: Range, with newElements: some Collection) { + storage.replaceSubrange(subrange, with: newElements) + } + + mutating func reserveCapacity(_ n: Int) { + storage.reserveCapacity(n) + } +} + +@inline(never) +public func runAppendLargeContiguous(N: Int) { + for _ in 1...N { + var rrc = NaiveRRC() + rrc.append(contentsOf: contiguous) + blackHole(rrc.count + rrc.last) + } +} + +@inline(never) +public func runAppendSmallContiguousRepeatedly(N: Int) { + for _ in 1...N { + var rrc = NaiveRRC() + for _ in 1...1_000_000 { + rrc.append(contentsOf: contiguous) + } + blackHole(rrc.count + rrc.last) + } +} + +@inline(never) +public func runInitLargeContiguous(N: Int) { + for _ in 1...N { + var rrc = NaiveRRC(contiguous) + blackHole(rrc.count + rrc.last) + } +} From 6cb9e5cfdd3c7c2c816b403969cf01a5520ea06d Mon Sep 17 00:00:00 2001 From: David Smith Date: Tue, 9 May 2023 11:27:04 -0700 Subject: [PATCH 02/13] Build fixes --- ...angeReplaceableCollectionConformance.swift | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift index e0a13824e163f..1a4da64519ec6 100644 --- a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -12,21 +12,21 @@ import TestsUtils -var contiguous:[UInt8]? +var contiguous:[UInt8] = [] public let benchmarks = [ - BenchmarkInfo(name: "NaiveRRC.append.largeContiguous", - runFunction: runAppendLargeContiguous, - setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000_000) }, - tags: [.validation, .api]), - BenchmarkInfo(name: "NaiveRRC.append.smallContiguousRepeatedly", - runFunction: runAppendLargeContiguous, - setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1) }, - tags: [.validation, .api]), - BenchmarkInfo(name: "NaiveRRC.init.largeContiguous", - runFunction: runInitContiguous, - setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000_000) }, - tags: [.validation, .api]), + BenchmarkInfo(name: "NaiveRRC.append.largeContiguous", + runFunction: runAppendLargeContiguous, + tags: [.validation, .api], + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000_000) }), + BenchmarkInfo(name: "NaiveRRC.append.smallContiguousRepeatedly", + runFunction: runAppendLargeContiguous, + tags: [.validation, .api], + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1) }), + BenchmarkInfo(name: "NaiveRRC.init.largeContiguous", + runFunction: runInitLargeContiguous, + tags: [.validation, .api], + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000_000) }) ] struct NaiveRRC : RangeReplaceableCollection { @@ -78,7 +78,7 @@ public func runAppendLargeContiguous(N: Int) { for _ in 1...N { var rrc = NaiveRRC() rrc.append(contentsOf: contiguous) - blackHole(rrc.count + rrc.last) + blackHole(rrc.count + rrc[0]) } } @@ -89,7 +89,7 @@ public func runAppendSmallContiguousRepeatedly(N: Int) { for _ in 1...1_000_000 { rrc.append(contentsOf: contiguous) } - blackHole(rrc.count + rrc.last) + blackHole(rrc.count + rrc[0]) } } @@ -97,6 +97,6 @@ public func runAppendSmallContiguousRepeatedly(N: Int) { public func runInitLargeContiguous(N: Int) { for _ in 1...N { var rrc = NaiveRRC(contiguous) - blackHole(rrc.count + rrc.last) + blackHole(rrc.count + rrc[0]) } } From 7d234d76ea9780dc19e7e1923300ac6aac2f964f Mon Sep 17 00:00:00 2001 From: David Smith Date: Tue, 9 May 2023 13:50:41 -0700 Subject: [PATCH 03/13] More build fixes --- .../NaiveRangeReplaceableCollectionConformance.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift index 1a4da64519ec6..142bc142c6989 100644 --- a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -78,7 +78,7 @@ public func runAppendLargeContiguous(N: Int) { for _ in 1...N { var rrc = NaiveRRC() rrc.append(contentsOf: contiguous) - blackHole(rrc.count + rrc[0]) + blackHole(rrc.count + Int(rrc[0])) } } @@ -89,7 +89,7 @@ public func runAppendSmallContiguousRepeatedly(N: Int) { for _ in 1...1_000_000 { rrc.append(contentsOf: contiguous) } - blackHole(rrc.count + rrc[0]) + blackHole(rrc.count + Int(rrc[0])) } } @@ -97,6 +97,6 @@ public func runAppendSmallContiguousRepeatedly(N: Int) { public func runInitLargeContiguous(N: Int) { for _ in 1...N { var rrc = NaiveRRC(contiguous) - blackHole(rrc.count + rrc[0]) + blackHole(rrc.count + Int(rrc[0])) } } From 9b3bd5ed0acd5ef2a32d9417ab7ec2255e17c5c6 Mon Sep 17 00:00:00 2001 From: David Smith Date: Tue, 9 May 2023 15:33:09 -0700 Subject: [PATCH 04/13] register new benchmarks --- benchmark/utils/main.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/benchmark/utils/main.swift b/benchmark/utils/main.swift index 2b0aba34c9658..93fa35ea02553 100644 --- a/benchmark/utils/main.swift +++ b/benchmark/utils/main.swift @@ -112,6 +112,7 @@ import Memset import MirrorTest import MonteCarloE import MonteCarloPi +import NaiveRangeReplaceableCollectionConformance import NibbleSort import NIOChannelPipeline import NSDictionaryCastToSwift @@ -303,6 +304,7 @@ register(Memset.benchmarks) register(MirrorTest.benchmarks) register(MonteCarloE.benchmarks) register(MonteCarloPi.benchmarks) +register(NaiveRangeReplaceableCollectionConformance.benchmarks) register(NSDictionaryCastToSwift.benchmarks) register(NSErrorTest.benchmarks) #if canImport(Darwin) From 71ed988d27d607742b35a03974b674323e4f5bd6 Mon Sep 17 00:00:00 2001 From: David Smith Date: Tue, 9 May 2023 16:29:56 -0700 Subject: [PATCH 05/13] Scale benchmarks --- .../NaiveRangeReplaceableCollectionConformance.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift index 142bc142c6989..9117cc2ea79c4 100644 --- a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -18,7 +18,7 @@ public let benchmarks = [ BenchmarkInfo(name: "NaiveRRC.append.largeContiguous", runFunction: runAppendLargeContiguous, tags: [.validation, .api], - setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000_000) }), + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 10_000) }), BenchmarkInfo(name: "NaiveRRC.append.smallContiguousRepeatedly", runFunction: runAppendLargeContiguous, tags: [.validation, .api], @@ -26,7 +26,7 @@ public let benchmarks = [ BenchmarkInfo(name: "NaiveRRC.init.largeContiguous", runFunction: runInitLargeContiguous, tags: [.validation, .api], - setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000_000) }) + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 10_000) }) ] struct NaiveRRC : RangeReplaceableCollection { @@ -86,7 +86,7 @@ public func runAppendLargeContiguous(N: Int) { public func runAppendSmallContiguousRepeatedly(N: Int) { for _ in 1...N { var rrc = NaiveRRC() - for _ in 1...1_000_000 { + for _ in 1...10_000_000 { rrc.append(contentsOf: contiguous) } blackHole(rrc.count + Int(rrc[0])) From 0ddc90b35578581c2996e32aeca19788231d25ca Mon Sep 17 00:00:00 2001 From: David Smith Date: Wed, 10 May 2023 01:25:38 -0700 Subject: [PATCH 06/13] Further benchmark scaling --- .../NaiveRangeReplaceableCollectionConformance.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift index 9117cc2ea79c4..95faedc10a59b 100644 --- a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -18,7 +18,7 @@ public let benchmarks = [ BenchmarkInfo(name: "NaiveRRC.append.largeContiguous", runFunction: runAppendLargeContiguous, tags: [.validation, .api], - setUpFunction: { contiguous = [UInt8](repeating: 7, count: 10_000) }), + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000) }), BenchmarkInfo(name: "NaiveRRC.append.smallContiguousRepeatedly", runFunction: runAppendLargeContiguous, tags: [.validation, .api], @@ -26,7 +26,7 @@ public let benchmarks = [ BenchmarkInfo(name: "NaiveRRC.init.largeContiguous", runFunction: runInitLargeContiguous, tags: [.validation, .api], - setUpFunction: { contiguous = [UInt8](repeating: 7, count: 10_000) }) + setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000) }) ] struct NaiveRRC : RangeReplaceableCollection { @@ -78,7 +78,7 @@ public func runAppendLargeContiguous(N: Int) { for _ in 1...N { var rrc = NaiveRRC() rrc.append(contentsOf: contiguous) - blackHole(rrc.count + Int(rrc[0])) + blackHole(rrc) } } @@ -89,7 +89,7 @@ public func runAppendSmallContiguousRepeatedly(N: Int) { for _ in 1...10_000_000 { rrc.append(contentsOf: contiguous) } - blackHole(rrc.count + Int(rrc[0])) + blackHole(rrc) } } @@ -97,6 +97,6 @@ public func runAppendSmallContiguousRepeatedly(N: Int) { public func runInitLargeContiguous(N: Int) { for _ in 1...N { var rrc = NaiveRRC(contiguous) - blackHole(rrc.count + Int(rrc[0])) + blackHole(rrc) } } From d5d97ad26e790186ade71ae9aaa4a3614ea4d52e Mon Sep 17 00:00:00 2001 From: David Smith Date: Wed, 10 May 2023 02:31:03 -0700 Subject: [PATCH 07/13] Further benchmark scaling --- .../NaiveRangeReplaceableCollectionConformance.swift | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift index 95faedc10a59b..a308191fc747a 100644 --- a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -19,7 +19,7 @@ public let benchmarks = [ runFunction: runAppendLargeContiguous, tags: [.validation, .api], setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000) }), - BenchmarkInfo(name: "NaiveRRC.append.smallContiguousRepeatedly", + BenchmarkInfo(name: "NaiveRRC.append.smallContiguousRepeated", runFunction: runAppendLargeContiguous, tags: [.validation, .api], setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1) }), @@ -86,7 +86,7 @@ public func runAppendLargeContiguous(N: Int) { public func runAppendSmallContiguousRepeatedly(N: Int) { for _ in 1...N { var rrc = NaiveRRC() - for _ in 1...10_000_000 { + for _ in 1...30_000_000 { rrc.append(contentsOf: contiguous) } blackHole(rrc) @@ -96,7 +96,6 @@ public func runAppendSmallContiguousRepeatedly(N: Int) { @inline(never) public func runInitLargeContiguous(N: Int) { for _ in 1...N { - var rrc = NaiveRRC(contiguous) - blackHole(rrc) + blackHole(NaiveRRC(contiguous)) } } From 4f363f72f13ddfe044c1251282a26e3f24d5ed1c Mon Sep 17 00:00:00 2001 From: David Smith Date: Wed, 10 May 2023 10:55:37 -0700 Subject: [PATCH 08/13] Try to thwart the optimizer --- .../NaiveRangeReplaceableCollectionConformance.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift index a308191fc747a..fe991423437dc 100644 --- a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -86,10 +86,10 @@ public func runAppendLargeContiguous(N: Int) { public func runAppendSmallContiguousRepeatedly(N: Int) { for _ in 1...N { var rrc = NaiveRRC() - for _ in 1...30_000_000 { + for _ in 1...10_000_000 { rrc.append(contentsOf: contiguous) + blackHole(rrc) } - blackHole(rrc) } } From b024a560edbd07bf077297f169a5bcf907bb6c6b Mon Sep 17 00:00:00 2001 From: David Smith Date: Fri, 12 May 2023 14:14:53 -0700 Subject: [PATCH 09/13] Fix thinko, thank you Nate! --- .../NaiveRangeReplaceableCollectionConformance.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift index fe991423437dc..76f80e12e01fc 100644 --- a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -20,7 +20,7 @@ public let benchmarks = [ tags: [.validation, .api], setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000) }), BenchmarkInfo(name: "NaiveRRC.append.smallContiguousRepeated", - runFunction: runAppendLargeContiguous, + runFunction: runAppendSmallContiguousRepeatedly, tags: [.validation, .api], setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1) }), BenchmarkInfo(name: "NaiveRRC.init.largeContiguous", @@ -88,8 +88,8 @@ public func runAppendSmallContiguousRepeatedly(N: Int) { var rrc = NaiveRRC() for _ in 1...10_000_000 { rrc.append(contentsOf: contiguous) - blackHole(rrc) } + blackHole(rrc) } } From a63031c636d9af9b4247003d17072d07298abafc Mon Sep 17 00:00:00 2001 From: David Smith Date: Fri, 12 May 2023 15:39:12 -0700 Subject: [PATCH 10/13] =?UTF-8?q?Scale=20benchmark=20down=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NaiveRangeReplaceableCollectionConformance.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift index 76f80e12e01fc..702a8f5a2c0e8 100644 --- a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -86,7 +86,7 @@ public func runAppendLargeContiguous(N: Int) { public func runAppendSmallContiguousRepeatedly(N: Int) { for _ in 1...N { var rrc = NaiveRRC() - for _ in 1...10_000_000 { + for _ in 1...1_000_000 { rrc.append(contentsOf: contiguous) } blackHole(rrc) From 92bba6315973344bdc1deb56c04372abba8159a1 Mon Sep 17 00:00:00 2001 From: David Smith Date: Fri, 12 May 2023 16:29:23 -0700 Subject: [PATCH 11/13] Ok I think this should be right --- .../NaiveRangeReplaceableCollectionConformance.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift index 702a8f5a2c0e8..fff987e81e83b 100644 --- a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -86,7 +86,7 @@ public func runAppendLargeContiguous(N: Int) { public func runAppendSmallContiguousRepeatedly(N: Int) { for _ in 1...N { var rrc = NaiveRRC() - for _ in 1...1_000_000 { + for _ in 1...100_000 { rrc.append(contentsOf: contiguous) } blackHole(rrc) From 708c5c43813aca3cbf669f63755603fc48254fe1 Mon Sep 17 00:00:00 2001 From: David Smith Date: Fri, 12 May 2023 16:31:08 -0700 Subject: [PATCH 12/13] Nah let's go a little lower --- .../NaiveRangeReplaceableCollectionConformance.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift index fff987e81e83b..88370688fd20b 100644 --- a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -86,7 +86,7 @@ public func runAppendLargeContiguous(N: Int) { public func runAppendSmallContiguousRepeatedly(N: Int) { for _ in 1...N { var rrc = NaiveRRC() - for _ in 1...100_000 { + for _ in 1...50_000 { rrc.append(contentsOf: contiguous) } blackHole(rrc) From 4d3c75631e126f22c81c0aa9be33301a0d83346d Mon Sep 17 00:00:00 2001 From: David Smith Date: Fri, 12 May 2023 17:22:08 -0700 Subject: [PATCH 13/13] Wow, even further I guess --- .../NaiveRangeReplaceableCollectionConformance.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift index 88370688fd20b..5f993a116c13d 100644 --- a/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift +++ b/benchmark/single-source/NaiveRangeReplaceableCollectionConformance.swift @@ -86,7 +86,7 @@ public func runAppendLargeContiguous(N: Int) { public func runAppendSmallContiguousRepeatedly(N: Int) { for _ in 1...N { var rrc = NaiveRRC() - for _ in 1...50_000 { + for _ in 1...5_000 { rrc.append(contentsOf: contiguous) } blackHole(rrc)