-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathDictionaryCopy.swift
70 lines (60 loc) · 2.44 KB
/
DictionaryCopy.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//===--- DictionaryCopy.swift ---------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// This benchmark checks for quadratic behavior while copying elements in hash
// order between Dictionaries of decreasing capacity
//
// https://github.com/apple/swift/issues/45856
import TestsUtils
let t: [BenchmarkCategory] = [.validation, .api, .Dictionary]
// We run the test at a spread of sizes between 1*x and 2*x, because the
// quadratic behavior only happens at certain load factors.
public let benchmarks = [
BenchmarkInfo(name:"Dict.CopyKeyValue.16k",
runFunction: copyKeyValue, tags: t, setUpFunction: { dict(16_000) }),
BenchmarkInfo(name:"Dict.CopyKeyValue.20k",
runFunction: copyKeyValue, tags: t, setUpFunction: { dict(20_000) }),
BenchmarkInfo(name:"Dict.CopyKeyValue.24k",
runFunction: copyKeyValue, tags: t, setUpFunction: { dict(24_000) }),
BenchmarkInfo(name:"Dict.CopyKeyValue.28k",
runFunction: copyKeyValue, tags: t, setUpFunction: { dict(28_000) }),
BenchmarkInfo(name:"Dict.FilterAllMatch.16k",
runFunction: filterAllMatch, tags: t, setUpFunction: { dict(16_000) }),
BenchmarkInfo(name:"Dict.FilterAllMatch.20k",
runFunction: filterAllMatch, tags: t, setUpFunction: { dict(20_000) }),
BenchmarkInfo(name:"Dict.FilterAllMatch.24k",
runFunction: filterAllMatch, tags: t, setUpFunction: { dict(24_000) }),
BenchmarkInfo(name:"Dict.FilterAllMatch.28k",
runFunction: filterAllMatch, tags: t, setUpFunction: { dict(28_000) }),
]
var dict: [Int: Int]?
func dict(_ size: Int) {
dict = Dictionary(uniqueKeysWithValues: zip(1...size, 1...size))
}
@inline(never)
func copyKeyValue(n: Int) {
for _ in 1...n {
var copy = [Int: Int]()
for (key, value) in dict! {
copy[key] = value
}
check(copy.count == dict!.count)
}
}
// Filter with a predicate returning true is essentially the same loop as the
// one in copyKeyValue above.
@inline(never)
func filterAllMatch(n: Int) {
for _ in 1...n {
let copy = dict!.filter { _ in true }
check(copy.count == dict!.count)
}
}