From f9fe9f44f7a38464348fd31384600f1f71b59330 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 18 Nov 2026 16:29:40 -0800 Subject: [PATCH] [Benchmarks] Add basic lookup benchmarks for hashed containers --- .../Libraries/DictionaryContainers.json | 62 +++++++++++++++++++ Benchmarks/Libraries/SetContainers.json | 62 +++++++++++++++++++ .../RigidDictionaryBenchmarks.swift | 54 ++++++++++++++++ .../Benchmarks/RigidSetBenchmarks.swift | 52 ++++++++++++++++ .../UniqueDictionaryBenchmarks.swift | 54 ++++++++++++++++ .../Benchmarks/UniqueSetBenchmarks.swift | 52 ++++++++++++++++ Benchmarks/Sources/benchmark-tool/main.swift | 33 +++++++--- 7 files changed, 360 insertions(+), 9 deletions(-) create mode 100644 Benchmarks/Libraries/DictionaryContainers.json create mode 100644 Benchmarks/Libraries/SetContainers.json create mode 100644 Benchmarks/Sources/Benchmarks/RigidDictionaryBenchmarks.swift create mode 100644 Benchmarks/Sources/Benchmarks/RigidSetBenchmarks.swift create mode 100644 Benchmarks/Sources/Benchmarks/UniqueDictionaryBenchmarks.swift create mode 100644 Benchmarks/Sources/Benchmarks/UniqueSetBenchmarks.swift diff --git a/Benchmarks/Libraries/DictionaryContainers.json b/Benchmarks/Libraries/DictionaryContainers.json new file mode 100644 index 00000000..adef4d4b --- /dev/null +++ b/Benchmarks/Libraries/DictionaryContainers.json @@ -0,0 +1,62 @@ +{ + "kind": "group", + "title": "Dictionary Container Benchmarks", + "directory": "DictionaryContainers", + "contents": [ + { + "kind": "group", + "title": "RigidDictionary Operations", + "directory": "RigidDictionary", + "contents": [ + { + "kind": "chart", + "title": "lookups", + "tasks": [ + "RigidDictionary value(forKey:), successful lookups", + "RigidDictionary value(forKey:), unsuccessful lookups", + ] + } + ] + }, + { + "kind": "group", + "title": "UniqueDictionary Operations", + "directory": "UniqueDictionary", + "contents": [ + { + "kind": "chart", + "title": "lookups", + "tasks": [ + "UniqueDictionary value(forKey:), successful lookups", + "UniqueDictionary value(forKey:), unsuccessful lookups", + ] + } + ] + }, + { + "kind": "group", + "title": "UniqueDictionary vs RigidDictionary vs Dictionary", + "directory": "comparative", + "contents": [ + { + "kind": "chart", + "title": "lookups-successful", + "tasks": [ + "Dictionary subscript, successful lookups", + "RigidDictionary value(forKey:), successful lookups", + "UniqueDictionary value(forKey:), successful lookups", + ] + }, + { + "kind": "chart", + "title": "lookups-unsuccessful", + "tasks": [ + "Dictionary subscript, unsuccessful lookups", + "RigidDictionary value(forKey:), unsuccessful lookups", + "UniqueDictionary value(forKey:), unsuccessful lookups", + ] + } + ] + } + ] +} diff --git a/Benchmarks/Libraries/SetContainers.json b/Benchmarks/Libraries/SetContainers.json new file mode 100644 index 00000000..5c80e3fe --- /dev/null +++ b/Benchmarks/Libraries/SetContainers.json @@ -0,0 +1,62 @@ +{ + "kind": "group", + "title": "Set Container Benchmarks", + "directory": "SetContainers", + "contents": [ + { + "kind": "group", + "title": "RigidSet Operations", + "directory": "RigidSet", + "contents": [ + { + "kind": "chart", + "title": "lookups", + "tasks": [ + "RigidSet successful contains", + "RigidSet unsuccessful contains", + ] + } + ] + }, + { + "kind": "group", + "title": "UniqueSet Operations", + "directory": "UniqueSet", + "contents": [ + { + "kind": "chart", + "title": "lookups", + "tasks": [ + "UniqueSet successful contains", + "UniqueSet unsuccessful contains", + ] + } + ] + }, + { + "kind": "group", + "title": "UniqueSet vs RigidSet vs Set", + "directory": "comparative", + "contents": [ + { + "kind": "chart", + "title": "contains-successful", + "tasks": [ + "Set successful contains", + "RigidSet successful contains", + "UniqueSet successful contains", + ] + }, + { + "kind": "chart", + "title": "contains-unsuccessful", + "tasks": [ + "Set unsuccessful contains", + "RigidSet unsuccessful contains", + "UniqueSet unsuccessful contains", + ] + } + ] + } + ] +} diff --git a/Benchmarks/Sources/Benchmarks/RigidDictionaryBenchmarks.swift b/Benchmarks/Sources/Benchmarks/RigidDictionaryBenchmarks.swift new file mode 100644 index 00000000..45a15e9b --- /dev/null +++ b/Benchmarks/Sources/Benchmarks/RigidDictionaryBenchmarks.swift @@ -0,0 +1,54 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception +// +//===----------------------------------------------------------------------===// + +import CollectionsBenchmark +import BasicContainers + +extension Benchmark { + public mutating func addRigidDictionaryBenchmarks() { +#if compiler(>=6.4) && UnstableHashedContainers + guard #available(SwiftStdlib 6.4, *) else { return } + + self.add( + title: "RigidDictionary value(forKey:), successful lookups", + input: ([Int], [Int]).self + ) { input, lookups in + var d = RigidDictionary(capacity: input.count) + for i in input { + d.insertValue(2 * i, forKey: i) + } + return { timer in + for i in lookups { + precondition(d.value(forKey: i)?.value == 2 * i) + } + } + } + + self.add( + title: "RigidDictionary value(forKey:), unsuccessful lookups", + input: ([Int], [Int]).self + ) { input, lookups in + var d = RigidDictionary(capacity: input.count) + for i in input { + d.insertValue(2 * i, forKey: i) + } + let c = input.count + return { timer in + for i in lookups { + precondition(d.value(forKey: i + c) == nil) + } + } + } +#endif + } +} diff --git a/Benchmarks/Sources/Benchmarks/RigidSetBenchmarks.swift b/Benchmarks/Sources/Benchmarks/RigidSetBenchmarks.swift new file mode 100644 index 00000000..3e46324c --- /dev/null +++ b/Benchmarks/Sources/Benchmarks/RigidSetBenchmarks.swift @@ -0,0 +1,52 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception +// +//===----------------------------------------------------------------------===// + +import CollectionsBenchmark +import BasicContainers + +extension Benchmark { + public mutating func addRigidSetBenchmarks() { +#if compiler(>=6.4) && UnstableHashedContainers + self.add( + title: "RigidSet successful contains", + input: ([Int], [Int]).self + ) { input, lookups in + var set = RigidSet(capacity: input.count) + for i in input { + set.insert(i) + } + return { timer in + for i in lookups { + precondition(set.contains(i)) + } + } + } + + self.add( + title: "RigidSet unsuccessful contains", + input: ([Int], [Int]).self + ) { input, lookups in + var set = RigidSet(capacity: input.count) + for i in input { + set.insert(i) + } + let lookups = lookups.map { $0 + input.count } + return { timer in + for i in lookups { + precondition(!set.contains(i)) + } + } + } +#endif + } +} diff --git a/Benchmarks/Sources/Benchmarks/UniqueDictionaryBenchmarks.swift b/Benchmarks/Sources/Benchmarks/UniqueDictionaryBenchmarks.swift new file mode 100644 index 00000000..35220809 --- /dev/null +++ b/Benchmarks/Sources/Benchmarks/UniqueDictionaryBenchmarks.swift @@ -0,0 +1,54 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception +// +//===----------------------------------------------------------------------===// + +import CollectionsBenchmark +import BasicContainers + +extension Benchmark { + public mutating func addUniqueDictionaryBenchmarks() { +#if compiler(>=6.4) && UnstableHashedContainers + guard #available(SwiftStdlib 6.4, *) else { return } + + self.add( + title: "UniqueDictionary value(forKey:), successful lookups", + input: ([Int], [Int]).self + ) { input, lookups in + var d = UniqueDictionary(minimumCapacity: input.count) + for i in input { + d.insertValue(2 * i, forKey: i) + } + return { timer in + for i in lookups { + precondition(d.value(forKey: i)?.value == 2 * i) + } + } + } + + self.add( + title: "UniqueDictionary value(forKey:), unsuccessful lookups", + input: ([Int], [Int]).self + ) { input, lookups in + var d = UniqueDictionary(minimumCapacity: input.count) + for i in input { + d.insertValue(2 * i, forKey: i) + } + let c = input.count + return { timer in + for i in lookups { + precondition(d.value(forKey: i + c) == nil) + } + } + } +#endif + } +} diff --git a/Benchmarks/Sources/Benchmarks/UniqueSetBenchmarks.swift b/Benchmarks/Sources/Benchmarks/UniqueSetBenchmarks.swift new file mode 100644 index 00000000..2676b0f5 --- /dev/null +++ b/Benchmarks/Sources/Benchmarks/UniqueSetBenchmarks.swift @@ -0,0 +1,52 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception +// +//===----------------------------------------------------------------------===// + +import CollectionsBenchmark +import BasicContainers + +extension Benchmark { + public mutating func addUniqueSetBenchmarks() { +#if compiler(>=6.4) && UnstableHashedContainers + self.add( + title: "UniqueSet successful contains", + input: ([Int], [Int]).self + ) { input, lookups in + var set = UniqueSet(minimumCapacity: input.count) + for i in input { + set.insert(i) + } + return { timer in + for i in lookups { + precondition(set.contains(i)) + } + } + } + + self.add( + title: "UniqueSet unsuccessful contains", + input: ([Int], [Int]).self + ) { input, lookups in + var set = UniqueSet(minimumCapacity: input.count) + for i in input { + set.insert(i) + } + let lookups = lookups.map { $0 + input.count } + return { timer in + for i in lookups { + precondition(!set.contains(i)) + } + } + } +#endif + } +} diff --git a/Benchmarks/Sources/benchmark-tool/main.swift b/Benchmarks/Sources/benchmark-tool/main.swift index 1234a362..b1e46ea2 100644 --- a/Benchmarks/Sources/benchmark-tool/main.swift +++ b/Benchmarks/Sources/benchmark-tool/main.swift @@ -31,27 +31,42 @@ benchmark.registerCustomGenerators() benchmark.addArrayBenchmarks() benchmark.addSetBenchmarks() benchmark.addDictionaryBenchmarks() -benchmark.addTreeDictionaryBenchmarks() + benchmark.addDequeBenchmarks() benchmark.addRigidDequeBenchmarks() benchmark.addUniqueDequeBenchmarks() + benchmark.addOrderedSetBenchmarks() benchmark.addOrderedDictionaryBenchmarks() + +benchmark.addHeapBenchmarks() + +benchmark.addBitSetBenchmarks() +benchmark.addBitArrayBenchmarks() + +#if UnstableHashedContainers +benchmark.addRigidSetBenchmarks() +benchmark.addUniqueSetBenchmarks() +benchmark.addRigidDictionaryBenchmarks() +benchmark.addUniqueDictionaryBenchmarks() +#endif + +benchmark.addTreeSetBenchmarks() +benchmark.addTreeDictionaryBenchmarks() + #if UnstableSortedCollections benchmark.addSortedSetBenchmarks() benchmark.addSortedDictionaryBenchmarks() #endif -benchmark.addHeapBenchmarks() -benchmark.addBitSetBenchmarks() -benchmark.addBitArrayBenchmarks() -benchmark.addTreeSetBenchmarks() -benchmark.addCppBenchmarks() -#if EnableRustBenchmarks -benchmark.addRustBenchmarks() -#endif + benchmark.addBigStringBenchmarks() #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) benchmark.addFoundationBenchmarks() #endif +benchmark.addCppBenchmarks() +#if EnableRustBenchmarks +benchmark.addRustBenchmarks() +#endif + benchmark.main()