|
| 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 |
0 commit comments