Skip to content

Commit fae040a

Browse files
authored
Merge pull request #22690 from palimondo/against-the-dark
[benchmark] Janitor Duty: Sweep II
2 parents 4ca08a5 + 311ddce commit fae040a

File tree

2 files changed

+45
-71
lines changed

2 files changed

+45
-71
lines changed
Lines changed: 32 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,47 @@
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],
28+
runFunction: compactMapValues, tags: t,
29+
setUpFunction: { blackHole(smallOddNumMap); blackHole(compactOddNums)},
830
legacyFactor: 50),
931
BenchmarkInfo(name: "DictionaryCompactMapValuesOfCastValue",
10-
runFunction: run_DictionaryCompactMapValuesOfCastValue,
11-
tags: [.validation, .api, .Dictionary],
12-
legacyFactor: 50),
32+
runFunction: compactMapValuesInt, tags: t,
33+
setUpFunction: { blackHole(oddStringMap); blackHole(compactOddNums)},
34+
legacyFactor: 54),
1335
]
1436

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]()
37+
func compactMapValues(N: Int) {
3638
for _ in 1...20*N {
37-
newDict = dict.compactMapValues({$0})
38-
if newDict != refDict {
39-
break
40-
}
39+
CheckResults(smallOddNumMap.compactMapValues({$0}) == compactOddNums)
4140
}
42-
43-
CheckResults(newDict == refDict)
4441
}
4542

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]()
43+
func compactMapValuesInt(N: Int) {
6844
for _ in 1...20*N {
69-
newDict = dict.compactMapValues(Int.init)
70-
if newDict != refDict {
71-
break
72-
}
45+
CheckResults(oddStringMap.compactMapValues(Int.init) == compactOddNums)
7346
}
74-
75-
CheckResults(newDict == refDict)
7647
}

benchmark/single-source/DictionaryGroup.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,24 @@
1313
import TestsUtils
1414

1515
public let DictionaryGroup = [
16-
BenchmarkInfo(name: "DictionaryGroup", runFunction: run_DictionaryGroup, tags: [.validation, .api, .Dictionary]),
17-
BenchmarkInfo(name: "DictionaryGroupOfObjects", runFunction: run_DictionaryGroupOfObjects, tags: [.validation, .api, .Dictionary],
16+
BenchmarkInfo(name: "DictionaryGroup",
17+
runFunction: run_DictionaryGroup,
18+
tags: [.validation, .api, .Dictionary]),
19+
BenchmarkInfo(name: "DictionaryGroupOfObjects",
20+
runFunction: run_DictionaryGroupOfObjects,
21+
tags: [.validation, .api, .Dictionary],
1822
setUpFunction: { blackHole(inputObjects) },
19-
tearDownFunction: { inputObjects = nil }),
23+
tearDownFunction: { inputObjects = nil },
24+
legacyFactor: 9
25+
),
2026
]
2127

22-
let count = 10_000
23-
let result = count / 10
24-
2528
@inline(never)
2629
public func run_DictionaryGroup(_ N: Int) {
2730
for _ in 1...N {
28-
let dict = Dictionary(grouping: 0..<count, by: { $0 % 10 })
31+
let dict = Dictionary(grouping: 0..<10_000, by: { $0 % 10 })
2932
CheckResults(dict.count == 10)
30-
CheckResults(dict[0]!.count == result)
33+
CheckResults(dict[0]!.count == 1_000)
3134
}
3235
}
3336

@@ -47,14 +50,14 @@ class Box<T : Hashable> : Hashable {
4750
}
4851
}
4952

50-
var inputObjects: [Box<Int>]! = (0..<count).map { Box($0) }
53+
var inputObjects: [Box<Int>]! = (0..<1_000).lazy.map { Box($0) }
5154

5255
@inline(never)
5356
public func run_DictionaryGroupOfObjects(_ N: Int) {
5457
let objects: [Box<Int>] = inputObjects
5558
for _ in 1...N {
5659
let dict = Dictionary(grouping: objects, by: { Box($0.value % 10) })
5760
CheckResults(dict.count == 10)
58-
CheckResults(dict[Box(0)]!.count == result)
61+
CheckResults(dict[Box(0)]!.count == 100)
5962
}
6063
}

0 commit comments

Comments
 (0)