Skip to content

Commit b954e60

Browse files
committed
[benchmark] Add NSDictionary benchmarks for dictionaries bridged from Swift
1 parent 6aec05a commit b954e60

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

benchmark/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ set(SWIFT_BENCH_MODULES
6565
single-source/DictTest4
6666
single-source/DictTest4Legacy
6767
single-source/DictionaryBridge
68+
single-source/DictionaryBridgeToObjC
6869
single-source/DictionaryCopy
6970
single-source/DictionaryGroup
7071
single-source/DictionaryKeysContains
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//===--- DictionaryBridgeToObjC.swift -------------------------------------===//
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+
// Performance benchmark for common operations on Dictionary values bridged to
14+
// NSDictionary.
15+
import TestsUtils
16+
#if _runtime(_ObjC)
17+
import Foundation
18+
19+
public let DictionaryBridgeToObjC = [
20+
BenchmarkInfo(
21+
name: "DictionaryBridgeToObjC_Bridge",
22+
runFunction: run_DictionaryBridgeToObjC_BridgeToObjC,
23+
tags: [.validation, .api, .Dictionary, .bridging]),
24+
BenchmarkInfo(
25+
name: "DictionaryBridgeToObjC_Access",
26+
runFunction: run_DictionaryBridgeToObjC_Access,
27+
tags: [.validation, .api, .Dictionary, .bridging]),
28+
BenchmarkInfo(
29+
name: "DictionaryBridgeToObjC_BulkAccess",
30+
runFunction: run_DictionaryBridgeToObjC_BulkAccess,
31+
tags: [.validation, .api, .Dictionary, .bridging])
32+
]
33+
34+
let numbers: [String: Int] = [
35+
"one": 1,
36+
"two": 2,
37+
"three": 3,
38+
"four": 4,
39+
"five": 5,
40+
"six": 6,
41+
"seven": 7,
42+
"eight": 8,
43+
"nine": 9,
44+
"ten": 10,
45+
"eleven": 11,
46+
"twelve": 12,
47+
"thirteen": 13,
48+
"fourteen": 14,
49+
"fifteen": 15,
50+
"sixteen": 16,
51+
"seventeen": 17,
52+
"eighteen": 18,
53+
"nineteen": 19,
54+
"twenty": 20
55+
]
56+
57+
@inline(never)
58+
public func run_DictionaryBridgeToObjC_BridgeToObjC(_ N: Int) {
59+
for _ in 1 ... 100 * N {
60+
blackHole(numbers as NSDictionary)
61+
}
62+
}
63+
64+
@inline(never)
65+
public func run_DictionaryBridgeToObjC_Access(_ N: Int) {
66+
let d = numbers as NSDictionary
67+
blackHole(d.object(forKey: "one")) // Force bridging of contents
68+
for _ in 1 ... 100 * N {
69+
for key in numbers.keys {
70+
CheckResults(identity(d).object(forKey: key) != nil)
71+
}
72+
}
73+
}
74+
75+
@inline(never)
76+
public func run_DictionaryBridgeToObjC_BulkAccess(_ N: Int) {
77+
let d = numbers as NSDictionary
78+
for _ in 1 ... 100 * N {
79+
let d2 = NSDictionary(dictionary: identity(d))
80+
CheckResults(d2.count == d.count)
81+
}
82+
}
83+
84+
#else // !_runtime(ObjC)
85+
public let DictionaryBridgeToObjC: [BenchmarkInfo] = []
86+
#endif

benchmark/utils/main.swift

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import DictTest3
5353
import DictTest4
5454
import DictTest4Legacy
5555
import DictionaryBridge
56+
import DictionaryBridgeToObjC
5657
import DictionaryCopy
5758
import DictionaryGroup
5859
import DictionaryKeysContains
@@ -215,6 +216,7 @@ registerBenchmark(Dictionary3)
215216
registerBenchmark(Dictionary4)
216217
registerBenchmark(Dictionary4Legacy)
217218
registerBenchmark(DictionaryBridge)
219+
registerBenchmark(DictionaryBridgeToObjC)
218220
registerBenchmark(DictionaryCopy)
219221
registerBenchmark(DictionaryGroup)
220222
registerBenchmark(DictionaryKeysContains)

0 commit comments

Comments
 (0)