|
| 1 | +//===--- DictTest4.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 | +import TestsUtils |
| 14 | + |
| 15 | +// This benchmark mostly measures lookups in dictionaries with complex keys, |
| 16 | +// using the legacy hashValue API. |
| 17 | + |
| 18 | +public let Dictionary4Legacy = [ |
| 19 | + BenchmarkInfo( |
| 20 | + name: "Dictionary4Legacy", |
| 21 | + runFunction: run_Dictionary4Legacy, |
| 22 | + tags: [.validation, .api, .Dictionary]), |
| 23 | + BenchmarkInfo( |
| 24 | + name: "Dictionary4OfObjectsLegacy", |
| 25 | + runFunction: run_Dictionary4OfObjectsLegacy, |
| 26 | + tags: [.validation, .api, .Dictionary]), |
| 27 | +] |
| 28 | + |
| 29 | +extension Int { |
| 30 | + mutating func combine(_ value: Int) { |
| 31 | + self = 16777619 &* self ^ value |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +struct LargeKey: Hashable { |
| 36 | + let i: Int |
| 37 | + let j: Int |
| 38 | + let k: Double |
| 39 | + let l: UInt32 |
| 40 | + let m: Bool |
| 41 | + let n: Bool |
| 42 | + let o: Bool |
| 43 | + let p: Bool |
| 44 | + let q: Bool |
| 45 | + |
| 46 | + var hashValue: Int { |
| 47 | + var hash = i.hashValue |
| 48 | + hash.combine(j.hashValue) |
| 49 | + hash.combine(k.hashValue) |
| 50 | + hash.combine(l.hashValue) |
| 51 | + hash.combine(m.hashValue) |
| 52 | + hash.combine(n.hashValue) |
| 53 | + hash.combine(o.hashValue) |
| 54 | + hash.combine(p.hashValue) |
| 55 | + hash.combine(q.hashValue) |
| 56 | + return hash |
| 57 | + } |
| 58 | + |
| 59 | + init(_ value: Int) { |
| 60 | + self.i = value |
| 61 | + self.j = 2 * value |
| 62 | + self.k = Double(value) / 7 |
| 63 | + self.l = UInt32(truncatingIfNeeded: value) |
| 64 | + self.m = value & 1 == 0 |
| 65 | + self.n = value & 2 == 0 |
| 66 | + self.o = value & 4 == 0 |
| 67 | + self.p = value & 8 == 0 |
| 68 | + self.q = value & 16 == 0 |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +@inline(never) |
| 73 | +public func run_Dictionary4Legacy(_ N: Int) { |
| 74 | + let size1 = 100 |
| 75 | + let reps = 20 |
| 76 | + let ref_result = "1 99 \(reps) \(reps * 99)" |
| 77 | + var hash1 = [LargeKey: Int]() |
| 78 | + var hash2 = [LargeKey: Int]() |
| 79 | + var res = "" |
| 80 | + |
| 81 | + for _ in 1...N { |
| 82 | + // Test insertions |
| 83 | + hash1 = [:] |
| 84 | + for i in 0..<size1 { |
| 85 | + hash1[LargeKey(i)] = i |
| 86 | + } |
| 87 | + |
| 88 | + hash2 = hash1 |
| 89 | + |
| 90 | + // Test lookups & value replacement |
| 91 | + for _ in 1..<reps { |
| 92 | + for (k, v) in hash1 { |
| 93 | + hash2[k] = hash2[k]! + v |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + res = "\(hash1[LargeKey(1)]!) \(hash1[LargeKey(99)]!)" + |
| 98 | + " \(hash2[LargeKey(1)]!) \(hash2[LargeKey(99)]!)" |
| 99 | + if res != ref_result { |
| 100 | + break |
| 101 | + } |
| 102 | + } |
| 103 | + CheckResults(res == ref_result) |
| 104 | +} |
| 105 | + |
| 106 | +class Box<T : Hashable> : Hashable { |
| 107 | + var value: T |
| 108 | + |
| 109 | + init(_ v: T) { |
| 110 | + value = v |
| 111 | + } |
| 112 | + |
| 113 | + func hash(into hasher: inout Hasher) { |
| 114 | + hasher.combine(value) |
| 115 | + } |
| 116 | + |
| 117 | + var hashValue: Int { |
| 118 | + return value.hashValue |
| 119 | + } |
| 120 | + |
| 121 | + static func ==(lhs: Box, rhs: Box) -> Bool { |
| 122 | + return lhs.value == rhs.value |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +@inline(never) |
| 127 | +public func run_Dictionary4OfObjectsLegacy(_ N: Int) { |
| 128 | + let size1 = 100 |
| 129 | + let reps = 20 |
| 130 | + let ref_result = "1 99 \(reps) \(reps * 99)" |
| 131 | + var hash1 = [Box<LargeKey>: Int]() |
| 132 | + var hash2 = [Box<LargeKey>: Int]() |
| 133 | + var res = "" |
| 134 | + |
| 135 | + for _ in 1...N { |
| 136 | + // Test insertions |
| 137 | + hash1 = [:] |
| 138 | + for i in 0..<size1 { |
| 139 | + hash1[Box(LargeKey(i))] = i |
| 140 | + } |
| 141 | + |
| 142 | + hash2 = hash1 |
| 143 | + |
| 144 | + // Test lookups & value replacement |
| 145 | + for _ in 1..<reps { |
| 146 | + for (k, v) in hash1 { |
| 147 | + hash2[k] = hash2[k]! + v |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + res = "\(hash1[Box(LargeKey(1))]!) \(hash1[Box(LargeKey(99))]!)" + |
| 152 | + " \(hash2[Box(LargeKey(1))]!) \(hash2[Box(LargeKey(99))]!)" |
| 153 | + if res != ref_result { |
| 154 | + break |
| 155 | + } |
| 156 | + } |
| 157 | + CheckResults(res == ref_result) |
| 158 | +} |
0 commit comments