Skip to content

Commit 0a49ba8

Browse files
authored
Merge pull request #22721 from palimondo/within-cells-interlinked
[benchmark] Janitor Duty: Sweep III
2 parents 40cce2d + 20365fb commit 0a49ba8

File tree

2 files changed

+65
-146
lines changed

2 files changed

+65
-146
lines changed

benchmark/single-source/DictionaryRemove.swift

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,18 @@ import TestsUtils
1616

1717
let t: [BenchmarkCategory] = [.validation, .api, .Dictionary]
1818

19+
let size = 100
20+
let numberMap = Dictionary(uniqueKeysWithValues: zip(1...size, 1...size))
21+
let boxedNums = (1...size).lazy.map { Box($0) }
22+
let boxedNumMap = Dictionary(uniqueKeysWithValues: zip(boxedNums, boxedNums))
23+
1924
public let DictionaryRemove = [
2025
BenchmarkInfo(name: "DictionaryRemove",
21-
runFunction: run_DictionaryRemove, tags: t, legacyFactor: 10),
26+
runFunction: remove, tags: t, legacyFactor: 10),
2227
BenchmarkInfo(name: "DictionaryRemoveOfObjects",
23-
runFunction: run_DictionaryRemoveOfObjects, tags: t, legacyFactor: 100),
28+
runFunction: removeObjects, tags: t, legacyFactor: 100),
2429
]
2530

26-
@inline(never)
27-
public func run_DictionaryRemove(_ N: Int) {
28-
let size = 100
29-
var dict = [Int: Int](minimumCapacity: size)
30-
31-
// Fill dictionary
32-
for i in 1...size {
33-
dict[i] = i
34-
}
35-
CheckResults(dict.count == size)
36-
37-
var tmpDict = dict
38-
for _ in 1...100*N {
39-
tmpDict = dict
40-
// Empty dictionary
41-
for i in 1...size {
42-
tmpDict.removeValue(forKey: i)
43-
}
44-
if !tmpDict.isEmpty {
45-
break
46-
}
47-
}
48-
49-
CheckResults(tmpDict.isEmpty)
50-
}
51-
5231
class Box<T : Hashable> : Hashable {
5332
var value: T
5433

@@ -65,28 +44,18 @@ class Box<T : Hashable> : Hashable {
6544
}
6645
}
6746

68-
@inline(never)
69-
public func run_DictionaryRemoveOfObjects(_ N: Int) {
70-
let size = 100
71-
var dict = Dictionary<Box<Int>, Box<Int>>(minimumCapacity: size)
72-
73-
// Fill dictionary
74-
for i in 1...size {
75-
dict[Box(i)] = Box(i)
76-
}
77-
CheckResults(dict.count == size)
78-
79-
var tmpDict = dict
80-
for _ in 1...10*N {
81-
tmpDict = dict
82-
// Empty dictionary
83-
for i in 1...size {
84-
tmpDict.removeValue(forKey: Box(i))
85-
}
86-
if !tmpDict.isEmpty {
87-
break
88-
}
89-
}
47+
func remove(N: Int) {
48+
for _ in 1...100*N {
49+
var dict = numberMap
50+
for i in 1...size { dict.removeValue(forKey: i) }
51+
CheckResults(dict.isEmpty)
52+
}
53+
}
9054

91-
CheckResults(tmpDict.isEmpty)
55+
func removeObjects(N: Int) {
56+
for _ in 1...10*N {
57+
var dict = boxedNumMap
58+
for i in 1...size { dict.removeValue(forKey: Box(i)) }
59+
CheckResults(dict.isEmpty)
60+
}
9261
}

benchmark/single-source/DictionarySwap.swift

Lines changed: 45 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -14,68 +14,24 @@
1414
// rdar://problem/19804127
1515
import TestsUtils
1616

17+
let size = 100
18+
let numberMap = Dictionary(uniqueKeysWithValues: zip(1...size, 1...size))
19+
let boxedNums = (1...size).lazy.map { Box($0) }
20+
let boxedNumMap = Dictionary(uniqueKeysWithValues: zip(boxedNums, boxedNums))
21+
1722
let t: [BenchmarkCategory] = [.validation, .api, .Dictionary]
1823

1924
public let DictionarySwap = [
2025
BenchmarkInfo(name: "DictionarySwap",
21-
runFunction: run_DictionarySwap, tags: t, legacyFactor: 4),
26+
runFunction: swap, tags: t, legacyFactor: 4),
2227
BenchmarkInfo(name: "DictionarySwapOfObjects",
23-
runFunction: run_DictionarySwapOfObjects, tags: t, legacyFactor: 40),
28+
runFunction: swapObjects, tags: t, legacyFactor: 40),
2429
BenchmarkInfo(name: "DictionarySwapAt",
25-
runFunction: run_DictionarySwapAt, tags: t, legacyFactor: 4),
30+
runFunction: swapAt, tags: t, legacyFactor: 4),
2631
BenchmarkInfo(name: "DictionarySwapAtOfObjects",
27-
runFunction: run_DictionarySwapAtOfObjects, tags: t, legacyFactor: 40),
32+
runFunction: swapAtObjects, tags: t, legacyFactor: 11),
2833
]
2934

30-
@inline(never)
31-
public func run_DictionarySwap(_ N: Int) {
32-
let size = 100
33-
var dict = [Int: Int](minimumCapacity: size)
34-
35-
// Fill dictionary
36-
for i in 1...size {
37-
dict[i] = i
38-
}
39-
CheckResults(dict.count == size)
40-
41-
var swapped = false
42-
for _ in 1...2500*N {
43-
(dict[25], dict[75]) = (dict[75]!, dict[25]!)
44-
swapped = !swapped
45-
if !swappedCorrectly(swapped, dict[25]!, dict[75]!) {
46-
break
47-
}
48-
}
49-
50-
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
51-
}
52-
53-
@inline(never)
54-
public func run_DictionarySwapAt(_ N: Int) {
55-
let size = 100
56-
var dict = [Int: Int](minimumCapacity: size)
57-
58-
// Fill dictionary
59-
for i in 1...size {
60-
dict[i] = i
61-
}
62-
CheckResults(dict.count == size)
63-
64-
var swapped = false
65-
for _ in 1...2500*N {
66-
let i25 = dict.index(forKey: 25)!
67-
let i75 = dict.index(forKey: 75)!
68-
69-
dict.values.swapAt(i25, i75)
70-
swapped = !swapped
71-
if !swappedCorrectly(swapped, dict[25]!, dict[75]!) {
72-
break
73-
}
74-
}
75-
76-
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
77-
}
78-
7935
// Return true if correctly swapped, false otherwise
8036
func swappedCorrectly(_ swapped: Bool, _ p25: Int, _ p75: Int) -> Bool {
8137
return swapped && (p25 == 75 && p75 == 25) ||
@@ -98,55 +54,49 @@ class Box<T : Hashable> : Hashable {
9854
}
9955
}
10056

101-
@inline(never)
102-
public func run_DictionarySwapOfObjects(_ N: Int) {
103-
let size = 100
104-
var dict = Dictionary<Box<Int>, Box<Int>>(minimumCapacity: size)
105-
106-
// Fill dictionary
107-
for i in 1...size {
108-
dict[Box(i)] = Box(i)
109-
}
110-
CheckResults(dict.count == size)
111-
112-
var swapped = false
113-
for _ in 1...250*N {
114-
let b1 = Box(25)
115-
let b2 = Box(75)
116-
(dict[b1], dict[b2]) = (dict[b2]!, dict[b1]!)
117-
swapped = !swapped
118-
if !swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value) {
119-
break
120-
}
57+
func swap(N: Int) {
58+
var dict = numberMap
59+
var swapped = false
60+
for _ in 1...2500*N {
61+
(dict[25], dict[75]) = (dict[75]!, dict[25]!)
62+
swapped = !swapped
63+
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
12164
}
122-
123-
CheckResults(swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value))
12465
}
12566

126-
@inline(never)
127-
public func run_DictionarySwapAtOfObjects(_ N: Int) {
128-
let size = 100
129-
var dict = [Box<Int>: Box<Int>](minimumCapacity: size)
130-
131-
// Fill dictionary
132-
for i in 1...size {
133-
dict[Box(i)] = Box(i)
134-
}
135-
CheckResults(dict.count == size)
136-
67+
func swapObjects(N: Int) {
68+
var dict = boxedNumMap
13769
var swapped = false
13870
for _ in 1...250*N {
139-
let b25 = Box(25)
140-
let b75 = Box(75)
141-
let i25 = dict.index(forKey: b25)!
142-
let i75 = dict.index(forKey: b75)!
71+
let b1 = Box(25)
72+
let b2 = Box(75)
73+
(dict[b1], dict[b2]) = (dict[b2]!, dict[b1]!)
74+
swapped = !swapped
75+
CheckResults(swappedCorrectly(swapped,
76+
dict[Box(25)]!.value, dict[Box(75)]!.value))
77+
}
78+
}
14379

80+
func swapAt(N: Int) {
81+
var dict = numberMap
82+
var swapped = false
83+
for _ in 1...2500*N {
84+
let i25 = dict.index(forKey: 25)!
85+
let i75 = dict.index(forKey: 75)!
14486
dict.values.swapAt(i25, i75)
14587
swapped = !swapped
146-
if !swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value) {
147-
break
148-
}
88+
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
14989
}
150-
151-
CheckResults(swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value))
15290
}
91+
92+
func swapAtObjects(N: Int) {
93+
var dict = boxedNumMap
94+
var swapped = false
95+
for _ in 1...1000*N {
96+
let i25 = dict.index(forKey: Box(25))!
97+
let i75 = dict.index(forKey: Box(75))!
98+
dict.values.swapAt(i25, i75)
99+
swapped = !swapped
100+
CheckResults(swappedCorrectly(swapped,
101+
dict[Box(25)]!.value, dict[Box(75)]!.value))
102+
}}

0 commit comments

Comments
 (0)