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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
//===--- DictionarySubscriptDefault.swift ---------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 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
//
//===----------------------------------------------------------------------===//
import TestsUtils
public let benchmarks = [
BenchmarkInfo(name: "DictionarySubscriptDefaultMutation",
runFunction: run_DictionarySubscriptDefaultMutation,
tags: [.validation, .api, .Dictionary]),
BenchmarkInfo(name: "DictionarySubscriptDefaultMutationArray",
runFunction: run_DictionarySubscriptDefaultMutationArray,
tags: [.validation, .api, .Dictionary]),
BenchmarkInfo(name: "DictionarySubscriptDefaultMutationOfObjects",
runFunction: run_DictionarySubscriptDefaultMutationOfObjects,
tags: [.validation, .api, .Dictionary], legacyFactor: 20),
BenchmarkInfo(name: "DictionarySubscriptDefaultMutationArrayOfObjects",
runFunction:
run_DictionarySubscriptDefaultMutationArrayOfObjects,
tags: [.validation, .api, .Dictionary], legacyFactor: 20),
]
@inline(never)
public func run_DictionarySubscriptDefaultMutation(_ n: Int) {
for _ in 1...n {
var dict = [Int: Int]()
for i in 0..<10_000 {
dict[i % 100, default: 0] += 1
}
check(dict.count == 100)
check(dict[0]! == 100)
}
}
@inline(never)
public func run_DictionarySubscriptDefaultMutationArray(_ n: Int) {
for _ in 1...n {
var dict = [Int: [Int]]()
for i in 0..<10_000 {
dict[i % 100, default: []].append(i)
}
check(dict.count == 100)
check(dict[0]!.count == 100)
}
}
// Hack to workaround the fact that if we attempt to increment the Box's value
// from the subscript, the compiler will just call the subscript's getter (and
// therefore not insert the instance) as it's dealing with a reference type.
// By using a mutating method in a protocol extension, the compiler is forced to
// treat this an actual mutation, so cannot call the getter.
protocol P {
associatedtype T
var value: T { get set }
}
extension P {
mutating func mutateValue(_ mutations: (inout T) -> Void) {
mutations(&value)
}
}
class Box<T : Hashable> : Hashable, P {
var value: T
init(_ v: T) {
value = v
}
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
static func ==(lhs: Box, rhs: Box) -> Bool {
return lhs.value == rhs.value
}
}
@inline(never)
public func run_DictionarySubscriptDefaultMutationOfObjects(_ n: Int) {
for _ in 1...n {
var dict = [Box<Int>: Box<Int>]()
for i in 0..<500 {
dict[Box(i % 5), default: Box(0)].mutateValue { $0 += 1 }
}
check(dict.count == 5)
check(dict[Box(0)]!.value == 100)
}
}
@inline(never)
public func run_DictionarySubscriptDefaultMutationArrayOfObjects(_ n: Int) {
for _ in 1...n {
var dict = [Box<Int>: [Box<Int>]]()
for i in 0..<500 {
dict[Box(i % 5), default: []].append(Box(i))
}
check(dict.count == 5)
check(dict[Box(0)]!.count == 100)
}
}
|