Skip to content

Commit baa3a7d

Browse files
[benchmarks] Add a benchmark for printing using mirrors (#32150)
* Add a benchmark for mirrors and typename->string conversion
1 parent 651503b commit baa3a7d

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ set(SWIFT_BENCH_MODULES
108108
single-source/LuhnAlgoLazy
109109
single-source/MapReduce
110110
single-source/Memset
111+
single-source/Mirror
111112
single-source/MonteCarloE
112113
single-source/MonteCarloPi
113114
single-source/NSDictionaryCastToSwift

benchmark/single-source/Mirror.swift

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//===--- Mirror.swift ------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 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+
// This test measures performance of Mirror and related things.
14+
import TestsUtils
15+
16+
public let TypeName = BenchmarkInfo(
17+
name: "TypeName",
18+
runFunction: run_TypeName,
19+
tags: [.api, .String])
20+
21+
public let MirrorDefault = BenchmarkInfo(
22+
name: "MirrorDefault",
23+
runFunction: run_MirrorDefault,
24+
tags: [.api, .String])
25+
26+
struct S1 { var s: String; var d: Double }
27+
struct S2 { var i: Int; var a: [Range<Int>] }
28+
29+
class C { var i: Int = 0 }
30+
class D: C { var s: String = "" }
31+
32+
enum E {
33+
case a,b(Int)
34+
}
35+
36+
struct G<T> { var t: T }
37+
class H<T>: C { var t: T; init(_ t: T) { self.t = t }}
38+
39+
public func run_MirrorDefault(scale: Int) {
40+
let N = 100*scale
41+
42+
let s1 = S1(s: "foo", d: 3.14)
43+
let s2 = S2(i: 42, a: [0..<4])
44+
let c = C()
45+
let d = D()
46+
let e = E.a
47+
let f = E.b(99)
48+
let g = G(t: 12.3)
49+
let h = H<[Int]>([1,2,3])
50+
51+
var str = ""
52+
53+
for _ in 0..<N {
54+
str = "\(s1),\(s2),\(c),\(d),\(e),\(f),\(g),\(h)"
55+
blackHole(str)
56+
}
57+
58+
CheckResults(str ==
59+
"S1(s: \"foo\", d: 3.14),S2(i: 42, a: [Range(0..<4)]),Mirror.C,Mirror.D,a,b(99),G<Double>(t: 12.3),Mirror.H<Swift.Array<Swift.Int>>")
60+
}
61+
62+
func typename<T>(of: T.Type) -> String {
63+
"\(T.self)"
64+
}
65+
66+
public func run_TypeName(scale: Int) {
67+
let N = 1_000*scale
68+
var a: [String] = []
69+
a.reserveCapacity(16)
70+
71+
for _ in 0..<N {
72+
a = []
73+
a.removeAll(keepingCapacity: true)
74+
a.append(typename(of: S1.self))
75+
a.append(typename(of: S2.self))
76+
a.append(typename(of: C.self))
77+
a.append(typename(of: D.self))
78+
a.append(typename(of: G<S1>.self))
79+
a.append(typename(of: G<C>.self))
80+
a.append(typename(of: G<String>.self))
81+
a.append(typename(of: H<Int>.self))
82+
a.append(typename(of: [S1].self))
83+
a.append(typename(of: [G<Int>].self))
84+
a.append(typename(of: [H<S1>].self))
85+
a.append(typename(of: S1?.self))
86+
a.append(typename(of: C?.self))
87+
blackHole(a)
88+
}
89+
90+
let expected = ["S1",
91+
"S2",
92+
"C",
93+
"D",
94+
"G<S1>",
95+
"G<C>",
96+
"G<String>",
97+
"H<Int>",
98+
"Array<S1>",
99+
"Array<G<Int>>",
100+
"Array<H<S1>>",
101+
"Optional<S1>",
102+
"Optional<C>",
103+
]
104+
CheckResults(a == expected)
105+
}

benchmark/utils/main.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import LuhnAlgoEager
9696
import LuhnAlgoLazy
9797
import MapReduce
9898
import Memset
99+
import Mirror
99100
import MonteCarloE
100101
import MonteCarloPi
101102
import NibbleSort
@@ -281,6 +282,7 @@ registerBenchmark(LuhnAlgoEager)
281282
registerBenchmark(LuhnAlgoLazy)
282283
registerBenchmark(MapReduce)
283284
registerBenchmark(Memset)
285+
registerBenchmark(MirrorDefault)
284286
registerBenchmark(MonteCarloE)
285287
registerBenchmark(MonteCarloPi)
286288
registerBenchmark(NSDictionaryCastToSwift)
@@ -366,6 +368,7 @@ registerBenchmark(Suffix)
366368
registerBenchmark(SuperChars)
367369
registerBenchmark(TwoSum)
368370
registerBenchmark(TypeFlood)
371+
registerBenchmark(TypeName)
369372
registerBenchmark(UTF8Decode)
370373
registerBenchmark(Walsh)
371374
registerBenchmark(WordCount)

0 commit comments

Comments
 (0)