Skip to content

Commit ca927dc

Browse files
committed
[interop] cxx vector benchmark: iterator is fast with operator == in C++, and subscript is fast with #61499 fixed
1 parent 4634230 commit ca927dc

File tree

2 files changed

+9
-44
lines changed

2 files changed

+9
-44
lines changed

benchmark/cxx-source/CxxVectorSum.swift

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,23 @@ import Cxx
1919

2020
public let benchmarks = [
2121
BenchmarkInfo(
22-
name: "CxxVectorOfU32.Sum.Cxx.RangedForLoop",
22+
name: "CxxVecU32.Sum.Cxx.RangedForLoop",
2323
runFunction: run_CxxVectorOfU32_Sum_Cxx_RangedForLoop,
2424
tags: [.validation, .bridging, .cxxInterop]),
2525
BenchmarkInfo(
26-
name: "CxxVectorOfU32.Sum.Swift.ForInLoop",
26+
name: "CxxVecU32.Sum.Swift.ForInLoop",
2727
runFunction: run_CxxVectorOfU32_Sum_Swift_ForInLoop,
2828
tags: [.validation, .bridging, .cxxInterop]),
2929
BenchmarkInfo(
30-
name: "CxxVectorOfU32.Sum.Swift.RawIteratorLoop",
30+
name: "CxxVecU32.Sum.Swift.IteratorLoop",
3131
runFunction: run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop,
3232
tags: [.validation, .bridging, .cxxInterop]),
3333
BenchmarkInfo(
34-
name: "CxxVectorOfU32.Sum.Swift.RawIteratorLoop.WithCxxInlineHelpers",
35-
runFunction: run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop_WithCxxInlineHelpers,
36-
tags: [.validation, .bridging, .cxxInterop]),
37-
BenchmarkInfo(
38-
name: "CxxVectorOfU32.Sum.Swift.IndexAndSubscriptLoop",
34+
name: "CxxVecU32.Sum.Swift.SubscriptLoop",
3935
runFunction: run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop,
4036
tags: [.validation, .bridging, .cxxInterop]),
4137
BenchmarkInfo(
42-
name: "CxxVectorOfU32.Sum.Swift.Reduce",
38+
name: "CxxVecU32.Sum.Swift.Reduce",
4339
runFunction: run_CxxVectorOfU32_Sum_Swift_Reduce,
4440
tags: [.validation, .bridging, .cxxInterop])
4541
]
@@ -69,23 +65,6 @@ public func run_CxxVectorOfU32_Sum_Swift_ForInLoop(_ n: Int) {
6965
blackHole(sum)
7066
}
7167

72-
// This function should have comparable performance to
73-
// `run_CxxVectorOfU32_Sum_Cxx_RangedForLoop`.
74-
@inline(never)
75-
public func run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop_WithCxxInlineHelpers(_ n: Int) {
76-
let vectorOfU32 = makeVector32(vectorSize)
77-
var sum: UInt32 = 0
78-
for _ in 0..<(n * iterRepeatFactor) {
79-
var b = vectorOfU32.__beginUnsafe()
80-
let e = vectorOfU32.__endUnsafe()
81-
while !cmp(b, e) {
82-
sum = sum &+ b.pointee
83-
b = next(b)
84-
}
85-
}
86-
blackHole(sum)
87-
}
88-
8968
// This function should have comparable performance to
9069
// `run_CxxVectorOfU32_Sum_Cxx_RangedForLoop`.
9170
@inline(never)
@@ -108,11 +87,8 @@ public func run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop(_ n: Int) {
10887
let vectorOfU32 = makeVector32(vectorSize)
10988
var sum: UInt32 = 0
11089
for _ in 0..<(n * iterRepeatFactor) {
111-
var i = 0
112-
let e = vectorOfU32.size()
113-
while i != e {
114-
sum = sum &+ vectorOfU32[i]
115-
i = i &+ 1
90+
for i in 0..<vectorOfU32.size() {
91+
sum = sum &+ vectorOfU32[i]
11692
}
11793
}
11894
blackHole(sum)
@@ -128,14 +104,6 @@ public func run_CxxVectorOfU32_Sum_Swift_Reduce(_ n: Int) {
128104
blackHole(sum)
129105
}
130106

131-
public func !=(_ y: VectorOfU32.const_iterator, _ x: VectorOfU32.const_iterator) -> Bool {
132-
return y.__baseUnsafe() != x.__baseUnsafe()
133-
}
134-
135-
public func ==(_ y: VectorOfU32.const_iterator, _ x: VectorOfU32.const_iterator) -> Bool {
136-
return y.__baseUnsafe() == x.__baseUnsafe()
137-
}
138-
139107
extension VectorOfU32.const_iterator : Equatable, UnsafeCxxInputIterator { }
140108

141109
extension VectorOfU32: CxxSequence {}

benchmark/utils/CxxTests/CxxStdlibPerformance.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,5 @@ inline uint32_t testVector32Sum(size_t vectorSize, size_t iters) {
2525
return sum;
2626
}
2727

28-
template<class T>
29-
inline T next(const T& i) { return i + 1; }
30-
31-
template<class T>
32-
inline bool cmp(const T &lhs, const T &rhs) { return lhs == rhs; }
28+
// FIXME: remove when the templated operator == is correctly bridged.
29+
inline bool operator ==(const VectorOfU32::const_iterator &lhs, const VectorOfU32::const_iterator &rhs) { return lhs.base() == rhs.base(); }

0 commit comments

Comments
 (0)