|
| 1 | +//===--- CxxSpanTests.swift ----------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 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 | +import CxxStdlibPerformance |
| 15 | + |
| 16 | +let iterRepeatFactor = 7 |
| 17 | + |
| 18 | +// FIXME swift-ci linux tests do not support std::span |
| 19 | +#if os(Linux) |
| 20 | +public let benchmarks = [BenchmarkInfo]() |
| 21 | +#else |
| 22 | + |
| 23 | +public let benchmarks = [ |
| 24 | + BenchmarkInfo( |
| 25 | + name: "CxxSpanTests.raw.iterator", |
| 26 | + runFunction: run_CxxSpanOfU32_RawIterator, |
| 27 | + tags: [.validation, .bridging, .cxxInterop], |
| 28 | + setUpFunction: makeSpanOnce), |
| 29 | + BenchmarkInfo( |
| 30 | + name: "CxxSpanTests.index.subscript", |
| 31 | + runFunction: run_CxxSpanOfU32_IndexAndSubscript, |
| 32 | + tags: [.validation, .bridging, .cxxInterop], |
| 33 | + setUpFunction: makeSpanOnce), |
| 34 | + BenchmarkInfo( |
| 35 | + name: "CxxSpanTests.for.loop", |
| 36 | + runFunction: run_CxxSpanOfU32_ForInLoop, |
| 37 | + tags: [.validation, .bridging, .cxxInterop], |
| 38 | + setUpFunction: makeSpanOnce), |
| 39 | + BenchmarkInfo( |
| 40 | + name: "CxxSpanTests.map", |
| 41 | + runFunction: run_CxxSpanOfU32_MapSpan, |
| 42 | + tags: [.validation, .bridging, .cxxInterop], |
| 43 | + setUpFunction: makeSpanOnce), |
| 44 | + BenchmarkInfo( |
| 45 | + name: "CxxSpanTests.filter", |
| 46 | + runFunction: run_CxxSpanOfU32_FilterSpan, |
| 47 | + tags: [.validation, .bridging, .cxxInterop], |
| 48 | + setUpFunction: makeSpanOnce), |
| 49 | + BenchmarkInfo( |
| 50 | + name: "CxxSpanTests.reduce", |
| 51 | + runFunction: run_CxxSpanOfU32_ReduceSpan, |
| 52 | + tags: [.validation, .bridging, .cxxInterop], |
| 53 | + setUpFunction: makeSpanOnce), |
| 54 | +] |
| 55 | + |
| 56 | +func makeSpanOnce() { |
| 57 | + initSpan() |
| 58 | +} |
| 59 | + |
| 60 | +@inline(never) |
| 61 | +public func run_CxxSpanOfU32_RawIterator(_ n: Int) { |
| 62 | + var sum: UInt32 = 0 |
| 63 | + for _ in 0..<(n * iterRepeatFactor * 2) { |
| 64 | + var b = span.__beginUnsafe() |
| 65 | + let e = span.__endUnsafe() |
| 66 | + while b != e { |
| 67 | + sum = sum &+ b.pointee |
| 68 | + b = b.successor() |
| 69 | + } |
| 70 | + } |
| 71 | + blackHole(sum) |
| 72 | +} |
| 73 | + |
| 74 | +@inline(never) |
| 75 | +public func run_CxxSpanOfU32_IndexAndSubscript(_ n: Int) { |
| 76 | + var sum: UInt32 = 0 |
| 77 | + for _ in 0..<(n * iterRepeatFactor * 2) { |
| 78 | + for i in 0..<span.size() { |
| 79 | + sum = sum &+ span[i] |
| 80 | + } |
| 81 | + } |
| 82 | + blackHole(sum) |
| 83 | +} |
| 84 | + |
| 85 | +@inline(never) |
| 86 | +public func run_CxxSpanOfU32_ForInLoop(_ n: Int) { |
| 87 | + var sum: UInt32 = 0 |
| 88 | + for _ in 0..<(n * iterRepeatFactor * 2) { |
| 89 | + for x in span { |
| 90 | + sum = sum &+ x |
| 91 | + } |
| 92 | + } |
| 93 | + blackHole(sum) |
| 94 | +} |
| 95 | + |
| 96 | +@inline(never) |
| 97 | +public func run_CxxSpanOfU32_MapSpan(_ n: Int) { |
| 98 | + for _ in 0..<(n * iterRepeatFactor) { |
| 99 | + let result = span.map { $0 &+ 5 } |
| 100 | + blackHole(result) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +@inline(never) |
| 105 | +public func run_CxxSpanOfU32_FilterSpan(_ n: Int) { |
| 106 | + for _ in 0..<(n * iterRepeatFactor) { |
| 107 | + let result = span.filter { $0 % 2 == 0 } |
| 108 | + blackHole(result) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +@inline(never) |
| 113 | +public func run_CxxSpanOfU32_ReduceSpan(_ n: Int) { |
| 114 | + var sum: UInt32 = 0 |
| 115 | + for _ in 0..<(n * iterRepeatFactor * 2) { |
| 116 | + sum = sum &+ span.reduce(sum, &+) |
| 117 | + } |
| 118 | + blackHole(sum) |
| 119 | +} |
| 120 | + |
| 121 | +#endif |
0 commit comments