mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[interop] rewrite the C++ vector sum benchmark to have clear naming and take iter into account
This commit is contained in:
@@ -19,54 +19,94 @@ import Cxx
|
||||
|
||||
public let benchmarks = [
|
||||
BenchmarkInfo(
|
||||
name: "CxxVectorOfU32SumInCxx",
|
||||
runFunction: run_CxxVectorOfU32SumInCxx,
|
||||
tags: [.validation, .bridging, .cxxInterop],
|
||||
setUpFunction: create_CxxVectorOfU32),
|
||||
name: "CxxVectorOfU32.Sum.Cxx.RangedForLoop",
|
||||
runFunction: run_CxxVectorOfU32_Sum_Cxx_RangedForLoop,
|
||||
tags: [.validation, .bridging, .cxxInterop]),
|
||||
BenchmarkInfo(
|
||||
name: "CxxVectorOfU32SumInSwift_Fastest",
|
||||
runFunction: run_CxxVectorOfU32SumInSwift_Fastest,
|
||||
tags: [.validation, .bridging, .cxxInterop],
|
||||
setUpFunction: create_CxxVectorOfU32),
|
||||
name: "CxxVectorOfU32.Sum.Swift.ForInLoop",
|
||||
runFunction: run_CxxVectorOfU32_Sum_Swift_ForInLoop,
|
||||
tags: [.validation, .bridging, .cxxInterop]),
|
||||
BenchmarkInfo(
|
||||
name: "CxxVectorOfU32SumInSwift",
|
||||
runFunction: run_CxxVectorOfU32SumInSwift,
|
||||
tags: [.validation, .bridging, .cxxInterop],
|
||||
setUpFunction: create_CxxVectorOfU32)
|
||||
name: "CxxVectorOfU32.Sum.Swift.RawIteratorLoop",
|
||||
runFunction: run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop,
|
||||
tags: [.validation, .bridging, .cxxInterop]),
|
||||
BenchmarkInfo(
|
||||
name: "CxxVectorOfU32.Sum.Swift.IndexAndSubscriptLoop",
|
||||
runFunction: run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop,
|
||||
tags: [.validation, .bridging, .cxxInterop]),
|
||||
BenchmarkInfo(
|
||||
name: "CxxVectorOfU32.Sum.Swift.Reduce",
|
||||
runFunction: run_CxxVectorOfU32_Sum_Swift_Reduce,
|
||||
tags: [.validation, .bridging, .cxxInterop])
|
||||
]
|
||||
|
||||
// FIXME: compare CxxVectorOfU32SumInCxx to CxxVectorOfU32SumInSwift and
|
||||
// establish an expected threshold of performance, which when exceeded should
|
||||
// fail the benchmark.
|
||||
|
||||
var vectorOfU32: VectorOfU32!
|
||||
|
||||
func create_CxxVectorOfU32() {
|
||||
vectorOfU32 = makeVector32(1_000_000)
|
||||
}
|
||||
let vectorSize = 100_000
|
||||
let iterRepeatFactor = 10
|
||||
|
||||
@inline(never)
|
||||
public func run_CxxVectorOfU32SumInCxx(_ n: Int) {
|
||||
// FIXME: Use no implicitly copyable, immutable borrow instead.
|
||||
// https://github.com/apple/swift/issues/61454
|
||||
let sum = testVector32Sum(&vectorOfU32)
|
||||
public func run_CxxVectorOfU32_Sum_Cxx_RangedForLoop(_ n: Int) {
|
||||
let sum = testVector32Sum(vectorSize, n * iterRepeatFactor)
|
||||
blackHole(sum)
|
||||
}
|
||||
|
||||
// This function should have comparable performance to `run_CxxVectorOfU32SumInCxx`.
|
||||
@inline(never)
|
||||
public func run_CxxVectorOfU32SumInSwift_Fastest(_ n: Int) {
|
||||
public func run_CxxVectorOfU32_Sum_Swift_ForInLoop(_ n: Int) {
|
||||
let vectorOfU32 = makeVector32(vectorSize)
|
||||
var sum: UInt32 = 0
|
||||
for _ in 0..<(n * iterRepeatFactor) {
|
||||
for x in vectorOfU32 {
|
||||
sum = sum &+ x
|
||||
}
|
||||
}
|
||||
blackHole(sum)
|
||||
}
|
||||
|
||||
// This function should have comparable performance to
|
||||
// `run_CxxVectorOfU32_Sum_Cxx_RangedForLoop`.
|
||||
@inline(never)
|
||||
public func run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop(_ n: Int) {
|
||||
let vectorOfU32 = makeVector32(vectorSize)
|
||||
var sum: UInt32 = 0
|
||||
// begin mutating, end mutating needed to avoid copies right now.
|
||||
var b = vectorOfU32.beginMutating()
|
||||
let e = vectorOfU32.endMutating()
|
||||
while !cmp(b, e) {
|
||||
sum = sum &+ b.pointee
|
||||
b = next(b)
|
||||
for _ in 0..<(n * iterRepeatFactor) {
|
||||
var b = vectorOfU32.__beginUnsafe()
|
||||
let e = vectorOfU32.__endUnsafe()
|
||||
while !cmp(b, e) {
|
||||
sum = sum &+ b.pointee
|
||||
b = next(b)
|
||||
}
|
||||
}
|
||||
blackHole(sum)
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
public func run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop(_ n: Int) {
|
||||
let vectorOfU32 = makeVector32(vectorSize)
|
||||
var sum: UInt32 = 0
|
||||
for _ in 0..<(n * iterRepeatFactor) {
|
||||
var i = 0
|
||||
let e = vectorOfU32.size()
|
||||
while i != e {
|
||||
sum = sum &+ vectorOfU32[i]
|
||||
i = i &+ 1
|
||||
}
|
||||
}
|
||||
blackHole(sum)
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
public func run_CxxVectorOfU32_Sum_Swift_Reduce(_ n: Int) {
|
||||
let vectorOfU32 = makeVector32(vectorSize)
|
||||
var sum: UInt32 = 0
|
||||
for _ in 0..<(n * iterRepeatFactor) {
|
||||
sum = vectorOfU32.reduce(sum, &+)
|
||||
}
|
||||
blackHole(sum)
|
||||
}
|
||||
|
||||
public func !=(_ y: VectorOfU32.const_iterator, _ x: VectorOfU32.const_iterator) -> Bool {
|
||||
return y.__baseUnsafe() != x.__baseUnsafe()
|
||||
}
|
||||
@@ -78,12 +118,3 @@ public func ==(_ y: VectorOfU32.const_iterator, _ x: VectorOfU32.const_iterator)
|
||||
extension VectorOfU32.const_iterator : Equatable, UnsafeCxxInputIterator { }
|
||||
|
||||
extension VectorOfU32: CxxSequence {}
|
||||
|
||||
@inline(never)
|
||||
public func run_CxxVectorOfU32SumInSwift(_ n: Int) {
|
||||
var sum: UInt32 = 0
|
||||
for i in vectorOfU32 {
|
||||
sum = sum &+ i
|
||||
}
|
||||
blackHole(sum)
|
||||
}
|
||||
|
||||
@@ -14,10 +14,13 @@ inline VectorOfU32 makeVector32(size_t size) {
|
||||
return result;
|
||||
}
|
||||
|
||||
inline uint32_t testVector32Sum(VectorOfU32 &vector) {
|
||||
inline uint32_t testVector32Sum(size_t vectorSize, size_t iters) {
|
||||
auto vector = makeVector32(vectorSize);
|
||||
auto sum = uint32_t(0);
|
||||
for (auto i : vector) {
|
||||
sum += i;
|
||||
for (size_t i = 0; i < iters; ++i) {
|
||||
for (auto x : vector) {
|
||||
sum += x;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user