Skip to content

Commit a0670a2

Browse files
committed
benchmarks: add a benchmark for AnyHashable initialization
1 parent af591bc commit a0670a2

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(SWIFT_BENCH_MODULES
2121
single-source/unit-tests/StackPromo
2222
single-source/Ackermann
2323
single-source/AngryPhonebook
24+
single-source/AnyHashableWithAClass
2425
single-source/Array2D
2526
single-source/ArrayAppend
2627
single-source/ArrayInClass
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// This benchmark tests AnyHashable's initializer that needs to dynamically
14+
// upcast the instance to the type that introduces the Hashable
15+
// conformance.
16+
17+
class TestHashableBase : Hashable {
18+
var value: Int
19+
init(_ value: Int) {
20+
self.value = value
21+
}
22+
var hashValue: Int {
23+
return value
24+
}
25+
static func == (
26+
lhs: TestHashableBase,
27+
rhs: TestHashableBase
28+
) -> Bool {
29+
return lhs.value == rhs.value
30+
}
31+
}
32+
33+
class TestHashableDerived1 : TestHashableBase {}
34+
class TestHashableDerived2 : TestHashableDerived1 {}
35+
class TestHashableDerived3 : TestHashableDerived2 {}
36+
class TestHashableDerived4 : TestHashableDerived3 {}
37+
class TestHashableDerived5 : TestHashableDerived4 {}
38+
39+
@inline(never)
40+
public func run_AnyHashableWithAClass(_ N: Int) {
41+
let c = TestHashableDerived5(10)
42+
for _ in 0...(N*500000) {
43+
_ = AnyHashable(c)
44+
}
45+
}
46+

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import TestsUtils
2222
import DriverUtils
2323
import Ackermann
2424
import AngryPhonebook
25+
import AnyHashableWithAClass
2526
import Array2D
2627
import ArrayAppend
2728
import ArrayInClass
@@ -100,6 +101,7 @@ import XorLoop
100101

101102
precommitTests = [
102103
"AngryPhonebook": run_AngryPhonebook,
104+
"AnyHashableWithAClass": run_AnyHashableWithAClass,
103105
"Array2D": run_Array2D,
104106
"ArrayAppend": run_ArrayAppend,
105107
"ArrayAppendReserved": run_ArrayAppendReserved,

0 commit comments

Comments
 (0)