|
| 1 | +//===--- ProtocolDispatch.swift -------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 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 ProtocolConformance = BenchmarkInfo ( |
| 16 | + name: "ProtocolConformance", |
| 17 | + runFunction: run_ProtocolConformance, |
| 18 | + tags: [.validation, .runtime]) |
| 19 | + |
| 20 | +protocol P {} |
| 21 | + |
| 22 | +struct One: P {} |
| 23 | +struct Two {} |
| 24 | + |
| 25 | +struct Cat<T, U> {} |
| 26 | + |
| 27 | +extension Cat: P where T: P, U: P {} |
| 28 | + |
| 29 | +protocol Growable {} |
| 30 | +extension Growable { |
| 31 | + func grow() -> (Growable, Growable) { |
| 32 | + return (Cat<Self, One>(), Cat<Self, Two>()) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +extension One: Growable {} |
| 37 | +extension Two: Growable {} |
| 38 | +extension Cat: Growable {} |
| 39 | + |
| 40 | +@inline(never) |
| 41 | +public func run_ProtocolConformance(_ N: Int) { |
| 42 | + var array: [Growable] = [One(), Two()] |
| 43 | + var i = 0 |
| 44 | + var checks = 0 |
| 45 | + |
| 46 | + // The expected number of times we expect `elt is P` to be true. |
| 47 | + var expectedConforms = 0 |
| 48 | + |
| 49 | + // The expected number of times we expect `elt is P` to be true |
| 50 | + // per iteration, at the current time. |
| 51 | + var expectedConformsPerIter = 1 |
| 52 | + |
| 53 | + // The number of times we've actually seen `elt is P` be true. |
| 54 | + var conforms = 0 |
| 55 | + while checks < N * 500 { |
| 56 | + let (a, b) = array[i].grow() |
| 57 | + array.append(a) |
| 58 | + array.append(b) |
| 59 | + |
| 60 | + // The number of times `elt is P` is true per outer iteration |
| 61 | + // goes up by 1 when the array's count is a power of 2. |
| 62 | + if array.count & (array.count - 1) == 0 { |
| 63 | + expectedConformsPerIter += 1 |
| 64 | + } |
| 65 | + expectedConforms += expectedConformsPerIter |
| 66 | + |
| 67 | + for elt in array { |
| 68 | + if elt is P { |
| 69 | + conforms += 1 |
| 70 | + } |
| 71 | + checks += 1 |
| 72 | + } |
| 73 | + i += 1 |
| 74 | + } |
| 75 | + CheckResults(expectedConforms == conforms) |
| 76 | +} |
0 commit comments