|
| 1 | +//===--- CxxVectorSum.swift -----------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2012 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 is a benchmark that tracks how quickly Swift can sum up a C++ vector |
| 14 | +// as compared to the C++ implementation of such sum. |
| 15 | + |
| 16 | +import TestsUtils |
| 17 | +import CxxStdlibPerformance |
| 18 | +import Cxx |
| 19 | + |
| 20 | +// FIXME: Linux needs fix for https://github.com/apple/swift/issues/61547. |
| 21 | +#if os(Linux) |
| 22 | +public let benchmarks: [BenchmarkInfo] = [] |
| 23 | +#else |
| 24 | +public let benchmarks = [ |
| 25 | + BenchmarkInfo( |
| 26 | + name: "CxxVecU32.sum.Cxx.rangedForLoop", |
| 27 | + runFunction: run_CxxVectorOfU32_Sum_Cxx_RangedForLoop, |
| 28 | + tags: [.validation, .bridging, .cxxInterop], |
| 29 | + setUpFunction: makeVectorOnce), |
| 30 | + BenchmarkInfo( |
| 31 | + name: "CxxVecU32.sum.Swift.forInLoop", |
| 32 | + runFunction: run_CxxVectorOfU32_Sum_Swift_ForInLoop, |
| 33 | + tags: [.validation, .bridging, .cxxInterop], |
| 34 | + setUpFunction: makeVectorOnce), |
| 35 | + BenchmarkInfo( |
| 36 | + name: "CxxVecU32.sum.Swift.iteratorLoop", |
| 37 | + runFunction: run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop, |
| 38 | + tags: [.validation, .bridging, .cxxInterop], |
| 39 | + setUpFunction: makeVectorOnce), |
| 40 | + BenchmarkInfo( |
| 41 | + name: "CxxVecU32.sum.Swift.subscriptLoop", |
| 42 | + runFunction: run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop, |
| 43 | + tags: [.validation, .bridging, .cxxInterop], |
| 44 | + setUpFunction: makeVectorOnce), |
| 45 | + BenchmarkInfo( |
| 46 | + name: "CxxVecU32.sum.Swift.reduce", |
| 47 | + runFunction: run_CxxVectorOfU32_Sum_Swift_Reduce, |
| 48 | + tags: [.validation, .bridging, .cxxInterop], |
| 49 | + setUpFunction: makeVectorOnce) |
| 50 | +] |
| 51 | + |
| 52 | +func makeVectorOnce() { |
| 53 | + initVector(vectorSize) |
| 54 | +} |
| 55 | + |
| 56 | +// FIXME: compare CxxVectorOfU32SumInCxx to CxxVectorOfU32SumInSwift and |
| 57 | +// establish an expected threshold of performance, which when exceeded should |
| 58 | +// fail the benchmark. |
| 59 | + |
| 60 | +// FIXME: Bump up to 50k and 10 once the sequence is faster. |
| 61 | +let vectorSize = 25_000 |
| 62 | +let iterRepeatFactor = 7 |
| 63 | + |
| 64 | +@inline(never) |
| 65 | +public func run_CxxVectorOfU32_Sum_Cxx_RangedForLoop(_ n: Int) { |
| 66 | + let sum = testVector32Sum(vectorSize, n * iterRepeatFactor) |
| 67 | + blackHole(sum) |
| 68 | +} |
| 69 | + |
| 70 | +@inline(never) |
| 71 | +public func run_CxxVectorOfU32_Sum_Swift_ForInLoop(_ n: Int) { |
| 72 | + let vectorOfU32 = makeVector32(vectorSize) |
| 73 | + var sum: UInt32 = 0 |
| 74 | + for _ in 0..<(n * iterRepeatFactor) { |
| 75 | + for x in vectorOfU32 { |
| 76 | + sum = sum &+ x |
| 77 | + } |
| 78 | + } |
| 79 | + blackHole(sum) |
| 80 | +} |
| 81 | + |
| 82 | +// This function should have comparable performance to |
| 83 | +// `run_CxxVectorOfU32_Sum_Cxx_RangedForLoop`. |
| 84 | +@inline(never) |
| 85 | +public func run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop(_ n: Int) { |
| 86 | + let vectorOfU32 = makeVector32(vectorSize) |
| 87 | + var sum: UInt32 = 0 |
| 88 | + for _ in 0..<(n * iterRepeatFactor) { |
| 89 | + var b = vectorOfU32.__beginUnsafe() |
| 90 | + let e = vectorOfU32.__endUnsafe() |
| 91 | + while b != e { |
| 92 | + sum = sum &+ b.pointee |
| 93 | + b = b.successor() |
| 94 | + } |
| 95 | + } |
| 96 | + blackHole(sum) |
| 97 | +} |
| 98 | + |
| 99 | +// Need to wait for https://github.com/apple/swift/issues/61499 |
| 100 | +@inline(never) |
| 101 | +public func run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop(_ n: Int) { |
| 102 | +#if FIXED_61499 |
| 103 | + let vectorOfU32 = makeVector32(vectorSize) |
| 104 | + var sum: UInt32 = 0 |
| 105 | + for _ in 0..<(n * iterRepeatFactor) { |
| 106 | + for i in 0..<vectorOfU32.size() { |
| 107 | + sum = sum &+ vectorOfU32[i] |
| 108 | + } |
| 109 | + } |
| 110 | + blackHole(sum) |
| 111 | +#else |
| 112 | + run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop(n) |
| 113 | +#endif |
| 114 | +} |
| 115 | + |
| 116 | +@inline(never) |
| 117 | +public func run_CxxVectorOfU32_Sum_Swift_Reduce(_ n: Int) { |
| 118 | + let vectorOfU32 = makeVector32(vectorSize) |
| 119 | + var sum: UInt32 = 0 |
| 120 | + for _ in 0..<(n * iterRepeatFactor) { |
| 121 | + sum = vectorOfU32.reduce(sum, &+) |
| 122 | + } |
| 123 | + blackHole(sum) |
| 124 | +} |
| 125 | + |
| 126 | +extension VectorOfU32.const_iterator : Equatable, UnsafeCxxInputIterator { } |
| 127 | + |
| 128 | +extension VectorOfU32: CxxSequence {} |
| 129 | +#endif |
0 commit comments