Skip to content

Commit bfbff45

Browse files
committed
[benchmark] Downsized DictionaryKeysContains
The DictionaryKeysContains used unreasonably large dictionary to demonstrate the failed O(n) instead of O(1) performance in case of Cocoa Dictionary. The setup of 1M element dictionary took 8 seconds on my old machine! The old pathological behavior can be equaly well demonstrated with a much smaller dictionary. (Validated by modifying `public func _customContainsEquatableElement` in `Dictionary.swift` to `return _variant.index(forKey: element) != nil`) The reported performance with correct O(1) behavior is unchanged.
1 parent c71d863 commit bfbff45

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

benchmark/single-source/DictionaryKeysContains.swift

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,50 @@ public let DictionaryKeysContains = [
2121
name: "DictionaryKeysContainsNative",
2222
runFunction: run_DictionaryKeysContains,
2323
tags: [.validation, .api, .Dictionary],
24-
setUpFunction: setup_DictionaryKeysContainsNative,
25-
tearDownFunction: teardown_DictionaryKeysContains),
24+
setUpFunction: setupNativeDictionary,
25+
tearDownFunction: teardownDictionary,
26+
unsupportedPlatforms: [.linux]),
2627
BenchmarkInfo(
2728
name: "DictionaryKeysContainsCocoa",
2829
runFunction: run_DictionaryKeysContains,
2930
tags: [.validation, .api, .Dictionary],
30-
setUpFunction: setup_DictionaryKeysContainsCocoa,
31-
tearDownFunction: teardown_DictionaryKeysContains),
31+
setUpFunction: setupBridgedDictionary,
32+
tearDownFunction: teardownDictionary),
3233
]
3334
#else
3435
public let DictionaryKeysContains = [
3536
BenchmarkInfo(
3637
name: "DictionaryKeysContainsNative",
3738
runFunction: run_DictionaryKeysContains,
3839
tags: [.validation, .api, .Dictionary],
39-
setUpFunction: setup_DictionaryKeysContainsNative,
40-
tearDownFunction: teardown_DictionaryKeysContains,
40+
setUpFunction: setupNativeDictionary,
41+
tearDownFunction: teardownDictionary,
4142
unsupportedPlatforms: [.linux]),
4243
]
4344
#endif
4445

4546
private var dictionary: [NSString: NSString]!
4647

47-
private func setup_DictionaryKeysContainsNative() {
48+
private func setupNativeDictionary() {
4849
#if os(Linux)
4950
fatalError("Unsupported benchmark")
5051
#else
51-
let keyValuePairs = (1...1_000_000).map {
52-
("\($0)" as NSString, "\($0)" as NSString)
52+
let keyValuePair: (Int) -> (NSString, NSString) = {
53+
let n = "\($0)" as NSString; return (n, n)
5354
}
54-
dictionary = [NSString: NSString](uniqueKeysWithValues: keyValuePairs)
55+
dictionary = [NSString: NSString](uniqueKeysWithValues:
56+
(1...10_000).lazy.map(keyValuePair))
5557
#endif
5658
}
5759

5860
#if _runtime(_ObjC)
59-
private func setup_DictionaryKeysContainsCocoa() {
60-
let keyValuePairs = (1...1_000_000).map {
61-
("\($0)" as NSString, "\($0)" as NSString)
62-
}
63-
let nativeDictionary = [NSString: NSString](
64-
uniqueKeysWithValues: keyValuePairs)
65-
dictionary = (NSDictionary(dictionary: nativeDictionary)
66-
as! [NSString: NSString])
61+
private func setupBridgedDictionary() {
62+
setupNativeDictionary()
63+
dictionary = (NSDictionary(dictionary: dictionary) as! [NSString: NSString])
6764
}
6865
#endif
6966

70-
private func teardown_DictionaryKeysContains() {
67+
private func teardownDictionary() {
7168
dictionary = nil
7269
}
7370

@@ -82,4 +79,3 @@ public func run_DictionaryKeysContains(_ N: Int) {
8279
}
8380
#endif
8481
}
85-

0 commit comments

Comments
 (0)