Skip to content

Commit 106ea29

Browse files
committed
[interop] rewrite the C++ vector sum benchmark to have clear naming and take iter into account
1 parent 367a0f3 commit 106ea29

File tree

2 files changed

+73
-39
lines changed

2 files changed

+73
-39
lines changed

benchmark/cxx-source/CxxVectorSum.swift

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,94 @@ import Cxx
1919

2020
public let benchmarks = [
2121
BenchmarkInfo(
22-
name: "CxxVectorOfU32SumInCxx",
23-
runFunction: run_CxxVectorOfU32SumInCxx,
24-
tags: [.validation, .bridging, .cxxInterop],
25-
setUpFunction: create_CxxVectorOfU32),
22+
name: "CxxVectorOfU32.Sum.Cxx.RangedForLoop",
23+
runFunction: run_CxxVectorOfU32_Sum_Cxx_RangedForLoop,
24+
tags: [.validation, .bridging, .cxxInterop]),
2625
BenchmarkInfo(
27-
name: "CxxVectorOfU32SumInSwift_Fastest",
28-
runFunction: run_CxxVectorOfU32SumInSwift_Fastest,
29-
tags: [.validation, .bridging, .cxxInterop],
30-
setUpFunction: create_CxxVectorOfU32),
26+
name: "CxxVectorOfU32.Sum.Swift.ForInLoop",
27+
runFunction: run_CxxVectorOfU32_Sum_Swift_ForInLoop,
28+
tags: [.validation, .bridging, .cxxInterop]),
3129
BenchmarkInfo(
32-
name: "CxxVectorOfU32SumInSwift",
33-
runFunction: run_CxxVectorOfU32SumInSwift,
34-
tags: [.validation, .bridging, .cxxInterop],
35-
setUpFunction: create_CxxVectorOfU32)
30+
name: "CxxVectorOfU32.Sum.Swift.RawIteratorLoop",
31+
runFunction: run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop,
32+
tags: [.validation, .bridging, .cxxInterop]),
33+
BenchmarkInfo(
34+
name: "CxxVectorOfU32.Sum.Swift.IndexAndSubscriptLoop",
35+
runFunction: run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop,
36+
tags: [.validation, .bridging, .cxxInterop]),
37+
BenchmarkInfo(
38+
name: "CxxVectorOfU32.Sum.Swift.Reduce",
39+
runFunction: run_CxxVectorOfU32_Sum_Swift_Reduce,
40+
tags: [.validation, .bridging, .cxxInterop])
3641
]
3742

3843
// FIXME: compare CxxVectorOfU32SumInCxx to CxxVectorOfU32SumInSwift and
3944
// establish an expected threshold of performance, which when exceeded should
4045
// fail the benchmark.
4146

42-
var vectorOfU32: VectorOfU32!
47+
let vectorSize = 100_000
48+
let iterRepeatFactor = 10
4349

44-
func create_CxxVectorOfU32() {
45-
vectorOfU32 = makeVector32(1_000_000)
50+
@inline(never)
51+
public func run_CxxVectorOfU32_Sum_Cxx_RangedForLoop(_ n: Int) {
52+
let sum = testVector32Sum(vectorSize, n * iterRepeatFactor)
53+
blackHole(sum)
4654
}
4755

4856
@inline(never)
49-
public func run_CxxVectorOfU32SumInCxx(_ n: Int) {
50-
// FIXME: Use no implicitly copyable, immutable borrow instead.
51-
// https://github.com/apple/swift/issues/61454
52-
let sum = testVector32Sum(&vectorOfU32)
57+
public func run_CxxVectorOfU32_Sum_Swift_ForInLoop(_ n: Int) {
58+
let vectorOfU32 = makeVector32(vectorSize)
59+
var sum: UInt32 = 0
60+
for _ in 0..<(n * iterRepeatFactor) {
61+
for x in vectorOfU32 {
62+
sum = sum &+ x
63+
}
64+
}
65+
blackHole(sum)
66+
}
67+
68+
// This function should have comparable performance to
69+
// `run_CxxVectorOfU32_Sum_Cxx_RangedForLoop`.
70+
@inline(never)
71+
public func run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop(_ n: Int) {
72+
let vectorOfU32 = makeVector32(vectorSize)
73+
var sum: UInt32 = 0
74+
for _ in 0..<(n * iterRepeatFactor) {
75+
var b = vectorOfU32.__beginUnsafe()
76+
let e = vectorOfU32.__endUnsafe()
77+
while !cmp(b, e) {
78+
sum = sum &+ b.pointee
79+
b = next(b)
80+
}
81+
}
5382
blackHole(sum)
5483
}
5584

56-
// This function should have comparable performance to `run_CxxVectorOfU32SumInCxx`.
5785
@inline(never)
58-
public func run_CxxVectorOfU32SumInSwift_Fastest(_ n: Int) {
86+
public func run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop(_ n: Int) {
87+
let vectorOfU32 = makeVector32(vectorSize)
5988
var sum: UInt32 = 0
60-
// begin mutating, end mutating needed to avoid copies right now.
61-
var b = vectorOfU32.beginMutating()
62-
let e = vectorOfU32.endMutating()
63-
while !cmp(b, e) {
64-
sum = sum &+ b.pointee
65-
b = next(b)
89+
for _ in 0..<(n * iterRepeatFactor) {
90+
var i = 0
91+
let e = vectorOfU32.size()
92+
while i != e {
93+
sum = sum &+ vectorOfU32[i]
94+
i = i &+ 1
95+
}
6696
}
6797
blackHole(sum)
6898
}
6999

100+
@inline(never)
101+
public func run_CxxVectorOfU32_Sum_Swift_Reduce(_ n: Int) {
102+
let vectorOfU32 = makeVector32(vectorSize)
103+
var sum: UInt32 = 0
104+
for _ in 0..<(n * iterRepeatFactor) {
105+
sum = vectorOfU32.reduce(sum, &+)
106+
}
107+
blackHole(sum)
108+
}
109+
70110
public func !=(_ y: VectorOfU32.const_iterator, _ x: VectorOfU32.const_iterator) -> Bool {
71111
return y.__baseUnsafe() != x.__baseUnsafe()
72112
}
@@ -78,12 +118,3 @@ public func ==(_ y: VectorOfU32.const_iterator, _ x: VectorOfU32.const_iterator)
78118
extension VectorOfU32.const_iterator : Equatable, UnsafeCxxInputIterator { }
79119

80120
extension VectorOfU32: CxxSequence {}
81-
82-
@inline(never)
83-
public func run_CxxVectorOfU32SumInSwift(_ n: Int) {
84-
var sum: UInt32 = 0
85-
for i in vectorOfU32 {
86-
sum = sum &+ i
87-
}
88-
blackHole(sum)
89-
}

benchmark/utils/CxxTests/CxxStdlibPerformance.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ inline VectorOfU32 makeVector32(size_t size) {
1414
return result;
1515
}
1616

17-
inline uint32_t testVector32Sum(VectorOfU32 &vector) {
17+
inline uint32_t testVector32Sum(size_t vectorSize, size_t iters) {
18+
auto vector = makeVector32(vectorSize);
1819
auto sum = uint32_t(0);
19-
for (auto i : vector) {
20-
sum += i;
20+
for (size_t i = 0; i < iters; ++i) {
21+
for (auto x : vector) {
22+
sum += x;
23+
}
2124
}
2225
return sum;
2326
}

0 commit comments

Comments
 (0)