Skip to content

Commit 2704a3e

Browse files
committed
[benchmark] DictionaryCompactMapValues Setup Overhead
Refactored to extract setup overhead. Input refference dictionaries are extracted to lazily initialized constants (only first sample includes the overhead, so I’m also skipping the `setUpFunction` with `blackHole`).
1 parent bf69971 commit 2704a3e

File tree

1 file changed

+29
-70
lines changed

1 file changed

+29
-70
lines changed
Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,35 @@
1-
// Dictionary compact map values benchmark
1+
//===--- DictionaryCompactMapValues.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+
213
import TestsUtils
314

15+
let size = 100
16+
let oddNumbers = stride(from: 1, to: size, by: 2)
17+
let smallOddNumMap: [Int: Int?] =
18+
Dictionary(uniqueKeysWithValues: zip(oddNumbers, oddNumbers))
19+
let compactOddNums: [Int: Int] =
20+
Dictionary(uniqueKeysWithValues: zip(oddNumbers, oddNumbers))
21+
let oddStringMap: [Int: String] = Dictionary(uniqueKeysWithValues:
22+
(1...size).lazy.map { ($0, $0 % 2 == 0 ? "dummy" : "\($0)") })
23+
24+
let t: [BenchmarkCategory] = [.validation, .api, .Dictionary]
25+
426
public let DictionaryCompactMapValues = [
527
BenchmarkInfo(name: "DictionaryCompactMapValuesOfNilValue",
6-
runFunction: run_DictionaryCompactMapValuesOfNilValue,
7-
tags: [.validation, .api, .Dictionary],
8-
legacyFactor: 50),
28+
runFunction: { for _ in 1...$0*20 {
29+
CheckResults(smallOddNumMap.compactMapValues({$0}) == compactOddNums) }},
30+
tags: t, legacyFactor: 50),
931
BenchmarkInfo(name: "DictionaryCompactMapValuesOfCastValue",
10-
runFunction: run_DictionaryCompactMapValuesOfCastValue,
11-
tags: [.validation, .api, .Dictionary],
12-
legacyFactor: 50),
32+
runFunction: { for _ in 1...$0*20 {
33+
CheckResults(oddStringMap.compactMapValues(Int.init) == compactOddNums) }
34+
}, tags: t, legacyFactor: 54),
1335
]
14-
15-
@inline(never)
16-
public func run_DictionaryCompactMapValuesOfNilValue(_ N: Int) {
17-
let size = 100
18-
var dict = [Int: Int?](minimumCapacity: size)
19-
20-
// Fill Dictionary
21-
for i in 1...size {
22-
if i % 2 == 0 {
23-
dict[i] = nil
24-
} else {
25-
dict[i] = i
26-
}
27-
}
28-
CheckResults(dict.count == size / 2)
29-
30-
var refDict = [Int: Int]()
31-
for i in stride(from: 1, to: 100, by: 2) {
32-
refDict[i] = i
33-
}
34-
35-
var newDict = [Int: Int]()
36-
for _ in 1...20*N {
37-
newDict = dict.compactMapValues({$0})
38-
if newDict != refDict {
39-
break
40-
}
41-
}
42-
43-
CheckResults(newDict == refDict)
44-
}
45-
46-
@inline(never)
47-
public func run_DictionaryCompactMapValuesOfCastValue(_ N: Int) {
48-
let size = 100
49-
var dict = [Int: String](minimumCapacity: size)
50-
51-
// Fill Dictionary
52-
for i in 1...size {
53-
if i % 2 == 0 {
54-
dict[i] = "dummy"
55-
} else {
56-
dict[i] = "\(i)"
57-
}
58-
}
59-
60-
CheckResults(dict.count == size)
61-
62-
var refDict = [Int: Int]()
63-
for i in stride(from: 1, to: 100, by: 2) {
64-
refDict[i] = i
65-
}
66-
67-
var newDict = [Int: Int]()
68-
for _ in 1...20*N {
69-
newDict = dict.compactMapValues(Int.init)
70-
if newDict != refDict {
71-
break
72-
}
73-
}
74-
75-
CheckResults(newDict == refDict)
76-
}

0 commit comments

Comments
 (0)