Skip to content

Commit fbaaa75

Browse files
authored
Merge pull request #14483 from lorentey/largekey-benchmark
2 parents a455db6 + ba978d3 commit fbaaa75

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ set(SWIFT_BENCH_MODULES
5959
single-source/DictTest
6060
single-source/DictTest2
6161
single-source/DictTest3
62+
single-source/DictTest4
6263
single-source/DictionaryBridge
6364
single-source/DictionaryGroup
6465
single-source/DictionaryLiteral
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
// exercising the default hash compression function.
17+
18+
public let Dictionary4 = [
19+
BenchmarkInfo(name: "Dictionary4", runFunction: run_Dictionary4, tags: [.validation, .api, .Dictionary]),
20+
BenchmarkInfo(name: "Dictionary4OfObjects", runFunction: run_Dictionary4OfObjects, tags: [.validation, .api, .Dictionary]),
21+
]
22+
23+
struct LargeKey: Hashable {
24+
let i: Int
25+
let j: Int
26+
let k: Double
27+
let l: UInt32
28+
let m: Bool
29+
let n: Bool
30+
let o: Bool
31+
let p: Bool
32+
let q: Bool
33+
34+
init(_ value: Int) {
35+
self.i = value
36+
self.j = 2 * value
37+
self.k = Double(value) / 7
38+
self.l = UInt32(truncatingIfNeeded: value)
39+
self.m = value & 1 == 0
40+
self.n = value & 2 == 0
41+
self.o = value & 4 == 0
42+
self.p = value & 8 == 0
43+
self.q = value & 16 == 0
44+
}
45+
}
46+
47+
@inline(never)
48+
public func run_Dictionary4(_ N: Int) {
49+
let size1 = 100
50+
let reps = 20
51+
let ref_result = "1 99 \(reps) \(reps * 99)"
52+
var hash1 = [LargeKey: Int]()
53+
var hash2 = [LargeKey: Int]()
54+
var res = ""
55+
56+
for _ in 1...N {
57+
// Test insertions
58+
hash1 = [:]
59+
for i in 0..<size1 {
60+
hash1[LargeKey(i)] = i
61+
}
62+
63+
hash2 = hash1
64+
65+
// Test lookups & value replacement
66+
for _ in 1..<reps {
67+
for (k, v) in hash1 {
68+
hash2[k] = hash2[k]! + v
69+
}
70+
}
71+
72+
res = "\(hash1[LargeKey(1)]!) \(hash1[LargeKey(99)]!)" +
73+
" \(hash2[LargeKey(1)]!) \(hash2[LargeKey(99)]!)"
74+
if res != ref_result {
75+
break
76+
}
77+
}
78+
CheckResults(res == ref_result)
79+
}
80+
81+
class Box<T : Hashable> : Hashable {
82+
var value: T
83+
84+
init(_ v: T) {
85+
value = v
86+
}
87+
88+
var hashValue: Int {
89+
return value.hashValue
90+
}
91+
92+
static func ==(lhs: Box, rhs: Box) -> Bool {
93+
return lhs.value == rhs.value
94+
}
95+
}
96+
97+
@inline(never)
98+
public func run_Dictionary4OfObjects(_ N: Int) {
99+
let size1 = 100
100+
let reps = 20
101+
let ref_result = "1 99 \(reps) \(reps * 99)"
102+
var hash1 = [Box<LargeKey>: Int]()
103+
var hash2 = [Box<LargeKey>: Int]()
104+
var res = ""
105+
106+
for _ in 1...N {
107+
// Test insertions
108+
hash1 = [:]
109+
for i in 0..<size1 {
110+
hash1[Box(LargeKey(i))] = i
111+
}
112+
113+
hash2 = hash1
114+
115+
// Test lookups & value replacement
116+
for _ in 1..<reps {
117+
for (k, v) in hash1 {
118+
hash2[k] = hash2[k]! + v
119+
}
120+
}
121+
122+
res = "\(hash1[Box(LargeKey(1))]!) \(hash1[Box(LargeKey(99))]!)" +
123+
" \(hash2[Box(LargeKey(1))]!) \(hash2[Box(LargeKey(99))]!)"
124+
if res != ref_result {
125+
break
126+
}
127+
}
128+
CheckResults(res == ref_result)
129+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import DictOfArraysToArrayOfDicts
4646
import DictTest
4747
import DictTest2
4848
import DictTest3
49+
import DictTest4
4950
import DictionaryBridge
5051
import DictionaryGroup
5152
import DictionaryLiteral
@@ -189,6 +190,7 @@ registerBenchmark(DictOfArraysToArrayOfDicts)
189190
registerBenchmark(Dictionary)
190191
registerBenchmark(Dictionary2)
191192
registerBenchmark(Dictionary3)
193+
registerBenchmark(Dictionary4)
192194
registerBenchmark(DictionaryBridge)
193195
registerBenchmark(DictionaryGroup)
194196
registerBenchmark(DictionaryLiteral)

0 commit comments

Comments
 (0)