|
| 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 | +} |
0 commit comments