Skip to content

Commit a486e49

Browse files
authored
Merge pull request #20067 from lorentey/anyhashable-benchmark
[benchmark] Add benchmark for [AnyHashable: Any] with String keys
2 parents 22d411c + f93dcf3 commit a486e49

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ set(SWIFT_BENCH_MODULES
7272
single-source/DictionaryGroup
7373
single-source/DictionaryKeysContains
7474
single-source/DictionaryLiteral
75+
single-source/DictionaryOfAnyHashableStrings
7576
single-source/DictionaryRemove
7677
single-source/DictionarySubscriptDefault
7778
single-source/DictionarySwap
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
15+
// This benchmark tests the performance of Dictionary<AnyHashable, Any> with
16+
// small ASCII String keys. Untyped NSDictionary values get imported as this
17+
// type, so it occurs relatively often in practice.
18+
19+
public var DictionaryOfAnyHashableStrings = [
20+
BenchmarkInfo(
21+
name: "DictionaryOfAnyHashableStrings_insert",
22+
runFunction: run_DictionaryOfAnyHashableStrings_insert,
23+
tags: [.abstraction, .runtime, .cpubench],
24+
setUpFunction: {
25+
keys = buildKeys(500)
26+
}
27+
),
28+
BenchmarkInfo(
29+
name: "DictionaryOfAnyHashableStrings_lookup",
30+
runFunction: run_DictionaryOfAnyHashableStrings_lookup,
31+
tags: [.abstraction, .runtime, .cpubench],
32+
setUpFunction: {
33+
keys = buildKeys(500)
34+
workload = buildWorkload()
35+
}
36+
),
37+
]
38+
39+
var keys: [String] = []
40+
var workload: [AnyHashable: Any] = [:]
41+
42+
func buildKeys(_ size: Int) -> [String] {
43+
var result: [String] = []
44+
let keyPrefixes = ["font", "bgcolor", "fgcolor", "blink", "marquee"]
45+
for key in keyPrefixes {
46+
for i in 0 ..< size {
47+
result.append(key + "\(i)")
48+
}
49+
}
50+
return result
51+
}
52+
53+
func buildWorkload() -> [AnyHashable: Any] {
54+
precondition(keys.count > 0)
55+
var result: [AnyHashable: Any] = [:]
56+
var i = 0
57+
for key in keys {
58+
result[key] = i
59+
i += 1
60+
}
61+
return result
62+
}
63+
64+
65+
@inline(never)
66+
public func run_DictionaryOfAnyHashableStrings_insert(_ n: Int) {
67+
precondition(keys.count > 0)
68+
for _ in 0 ... n {
69+
blackHole(buildWorkload())
70+
}
71+
}
72+
73+
@inline(never)
74+
public func run_DictionaryOfAnyHashableStrings_lookup(_ n: Int) {
75+
precondition(workload.count > 0)
76+
precondition(keys.count > 0)
77+
for _ in 0 ... n {
78+
for i in 0 ..< keys.count {
79+
let key = keys[i]
80+
CheckResults((workload[key] as! Int) == i)
81+
}
82+
}
83+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import DictionaryCopy
6060
import DictionaryGroup
6161
import DictionaryKeysContains
6262
import DictionaryLiteral
63+
import DictionaryOfAnyHashableStrings
6364
import DictionaryRemove
6465
import DictionarySubscriptDefault
6566
import DictionarySwap
@@ -226,6 +227,7 @@ registerBenchmark(DictionaryCopy)
226227
registerBenchmark(DictionaryGroup)
227228
registerBenchmark(DictionaryKeysContains)
228229
registerBenchmark(DictionaryLiteral)
230+
registerBenchmark(DictionaryOfAnyHashableStrings)
229231
registerBenchmark(DictionaryRemove)
230232
registerBenchmark(DictionarySubscriptDefault)
231233
registerBenchmark(DictionarySwap)

0 commit comments

Comments
 (0)