[Benchmarks] Add basic lookup benchmarks for hashed containers

This commit is contained in:
Karoy Lorentey committed 2026-11-18 16:29:40 -08:00
1 parent 29fd239474
commit f9fe9f44f7
7 files changed
+360 -9

No files matched your search

@@ -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<Int, Int> value(forKey:), successful lookups",
"RigidDictionary<Int, Int> value(forKey:), unsuccessful lookups",
]
}
]
},
{
"kind": "group",
"title": "UniqueDictionary Operations",
"directory": "UniqueDictionary",
"contents": [
{
"kind": "chart",
"title": "lookups",
"tasks": [
"UniqueDictionary<Int, Int> value(forKey:), successful lookups",
"UniqueDictionary<Int, Int> value(forKey:), unsuccessful lookups",
]
}
]
},
{
"kind": "group",
"title": "UniqueDictionary vs RigidDictionary vs Dictionary",
"directory": "comparative",
"contents": [
{
"kind": "chart",
"title": "lookups-successful",
"tasks": [
"Dictionary<Int, Int> subscript, successful lookups",
"RigidDictionary<Int, Int> value(forKey:), successful lookups",
"UniqueDictionary<Int, Int> value(forKey:), successful lookups",
]
},
{
"kind": "chart",
"title": "lookups-unsuccessful",
"tasks": [
"Dictionary<Int, Int> subscript, unsuccessful lookups",
"RigidDictionary<Int, Int> value(forKey:), unsuccessful lookups",
"UniqueDictionary<Int, Int> value(forKey:), unsuccessful lookups",
]
}
]
}
]
}
+62
View File
@@ -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<Int> successful contains",
"RigidSet<Int> unsuccessful contains",
]
}
]
},
{
"kind": "group",
"title": "UniqueSet Operations",
"directory": "UniqueSet",
"contents": [
{
"kind": "chart",
"title": "lookups",
"tasks": [
"UniqueSet<Int> successful contains",
"UniqueSet<Int> unsuccessful contains",
]
}
]
},
{
"kind": "group",
"title": "UniqueSet vs RigidSet vs Set",
"directory": "comparative",
"contents": [
{
"kind": "chart",
"title": "contains-successful",
"tasks": [
"Set<Int> successful contains",
"RigidSet<Int> successful contains",
"UniqueSet<Int> successful contains",
]
},
{
"kind": "chart",
"title": "contains-unsuccessful",
"tasks": [
"Set<Int> unsuccessful contains",
"RigidSet<Int> unsuccessful contains",
"UniqueSet<Int> unsuccessful contains",
]
}
]
}
]
}
@@ -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<Int, Int> value(forKey:), successful lookups",
input: ([Int], [Int]).self
) { input, lookups in
var d = RigidDictionary<Int, Int>(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<Int, Int> value(forKey:), unsuccessful lookups",
input: ([Int], [Int]).self
) { input, lookups in
var d = RigidDictionary<Int, Int>(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
}
}
@@ -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<Int> successful contains",
input: ([Int], [Int]).self
) { input, lookups in
var set = RigidSet<Int>(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<Int> unsuccessful contains",
input: ([Int], [Int]).self
) { input, lookups in
var set = RigidSet<Int>(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
}
}
@@ -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<Int, Int> value(forKey:), successful lookups",
input: ([Int], [Int]).self
) { input, lookups in
var d = UniqueDictionary<Int, Int>(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<Int, Int> value(forKey:), unsuccessful lookups",
input: ([Int], [Int]).self
) { input, lookups in
var d = UniqueDictionary<Int, Int>(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
}
}
@@ -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<Int> successful contains",
input: ([Int], [Int]).self
) { input, lookups in
var set = UniqueSet<Int>(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<Int> unsuccessful contains",
input: ([Int], [Int]).self
) { input, lookups in
var set = UniqueSet<Int>(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
}
}
+24 -9
View File
@@ -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()