Skip to content

Commit dbadd99

Browse files
committed
Add benchmark for devirtualization performance measurements
1 parent 727cd96 commit dbadd99

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ set(SWIFT_BENCH_MODULES
6262
single-source/Combos
6363
single-source/DataBenchmarks
6464
single-source/DeadArray
65+
single-source/DevirtualizeProtocolComposition
6566
single-source/DictOfArraysToArrayOfDicts
6667
single-source/DictTest
6768
single-source/DictTest2
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//===--- DevirtualizeProtocolComposition.swift -------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 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+
13+
import TestsUtils
14+
15+
public let DevirtualizeProtocolComposition = [
16+
BenchmarkInfo(name: "DevirtualizeProtocolComposition", runFunction: run_DevirtualizeProtocolComposition, tags: [.validation, .api]),
17+
]
18+
19+
public class ClassA<T> { }
20+
21+
protocol ProtocolA {
22+
func foo() -> Int
23+
}
24+
25+
protocol ProtocolB {
26+
func bar() -> Int
27+
}
28+
29+
public class ClassB: ClassA<String> {
30+
func foo() -> Int {
31+
return 10
32+
}
33+
}
34+
35+
extension ClassB: ProtocolA { }
36+
37+
func quadratic(a: Int) -> Int {
38+
var sum = 0
39+
for _ in 0..<a {
40+
for _ in 0..<a {
41+
sum += 1
42+
}
43+
}
44+
return sum
45+
}
46+
47+
func shouldOptimize1<T>(_ x: ClassA<T> & ProtocolA, count: Int) -> Int {
48+
var sum = 0
49+
for _ in 0..<count {
50+
sum += quadratic(a: x.foo())
51+
}
52+
return sum
53+
}
54+
55+
@inline(never)
56+
public func entryPoint1(c: ClassB) -> Int {
57+
return shouldOptimize1(c, count: 25)
58+
}
59+
60+
@inline(never)
61+
public func run_DevirtualizeProtocolComposition(N: Int) {
62+
for _ in 0..<N * 10_000 {
63+
entryPoint1(c: ClassB())
64+
}
65+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import Codable
5050
import Combos
5151
import DataBenchmarks
5252
import DeadArray
53+
import DevirtualizeProtocolComposition
5354
import DictOfArraysToArrayOfDicts
5455
import DictTest
5556
import DictTest2
@@ -231,6 +232,7 @@ registerBenchmark(Combos)
231232
registerBenchmark(ClassArrayGetter)
232233
registerBenchmark(DataBenchmarks)
233234
registerBenchmark(DeadArray)
235+
registerBenchmark(DevirtualizeProtocolComposition)
234236
registerBenchmark(DictOfArraysToArrayOfDicts)
235237
registerBenchmark(Dictionary)
236238
registerBenchmark(Dictionary2)

0 commit comments

Comments
 (0)