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