Skip to content

Commit a50939b

Browse files
committed
[interop] cxx vec benchmark - initialize vector before benchmark
1 parent 0e95c75 commit a50939b

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

benchmark/cxx-source/CxxVectorSum.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,34 @@ public let benchmarks = [
2121
BenchmarkInfo(
2222
name: "CxxVecU32.Sum.Cxx.RangedForLoop",
2323
runFunction: run_CxxVectorOfU32_Sum_Cxx_RangedForLoop,
24-
tags: [.validation, .bridging, .cxxInterop]),
24+
tags: [.validation, .bridging, .cxxInterop],
25+
setUpFunction: makeVectorOnce),
2526
BenchmarkInfo(
2627
name: "CxxVecU32.Sum.Swift.ForInLoop",
2728
runFunction: run_CxxVectorOfU32_Sum_Swift_ForInLoop,
28-
tags: [.validation, .bridging, .cxxInterop]),
29+
tags: [.validation, .bridging, .cxxInterop],
30+
setUpFunction: makeVectorOnce),
2931
BenchmarkInfo(
3032
name: "CxxVecU32.Sum.Swift.IteratorLoop",
3133
runFunction: run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop,
32-
tags: [.validation, .bridging, .cxxInterop]),
34+
tags: [.validation, .bridging, .cxxInterop],
35+
setUpFunction: makeVectorOnce),
3336
BenchmarkInfo(
3437
name: "CxxVecU32.Sum.Swift.SubscriptLoop",
3538
runFunction: run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop,
36-
tags: [.validation, .bridging, .cxxInterop]),
39+
tags: [.validation, .bridging, .cxxInterop],
40+
setUpFunction: makeVectorOnce),
3741
BenchmarkInfo(
3842
name: "CxxVecU32.Sum.Swift.Reduce",
3943
runFunction: run_CxxVectorOfU32_Sum_Swift_Reduce,
40-
tags: [.validation, .bridging, .cxxInterop])
44+
tags: [.validation, .bridging, .cxxInterop],
45+
setUpFunction: makeVectorOnce)
4146
]
4247

48+
func makeVectorOnce() {
49+
initVector(vectorSize)
50+
}
51+
4352
// FIXME: compare CxxVectorOfU32SumInCxx to CxxVectorOfU32SumInSwift and
4453
// establish an expected threshold of performance, which when exceeded should
4554
// fail the benchmark.

benchmark/utils/CxxTests/CxxStdlibPerformance.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
55

66
using VectorOfU32 = std::vector<uint32_t>;
77

8-
inline VectorOfU32 makeVector32(size_t size) {
9-
auto result = VectorOfU32();
10-
result.reserve(size);
8+
static inline VectorOfU32 vec;
9+
10+
inline void initVector(size_t size) {
11+
if (!vec.empty()) {
12+
return;
13+
}
14+
vec.reserve(size);
1115
for (size_t i = 0; i < size; ++i) {
12-
result.push_back(uint32_t(i));
16+
vec.push_back(uint32_t(i));
1317
}
14-
return result;
18+
}
19+
20+
inline VectorOfU32 makeVector32(size_t size) {
21+
initVector(size);
22+
return vec;
1523
}
1624

1725
inline uint32_t testVector32Sum(size_t vectorSize, size_t iters) {

0 commit comments

Comments
 (0)