Skip to content

Commit 73dddfc

Browse files
authored
Merge pull request #61456 from hyp/eng/benchmark-sum1
[cxx-interop] Add initial benchmark to compare vector<uint32_t> sum i…
2 parents 5d90a69 + c4a9136 commit 73dddfc

File tree

7 files changed

+179
-1
lines changed

7 files changed

+179
-1
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ set(SWIFT_BENCH_MODULES
199199
single-source/WordCount
200200
single-source/XorLoop
201201
cxx-source/CreateObjects
202+
cxx-source/CxxVectorSum
202203
cxx-source/ReadAccessor
203204
)
204205

benchmark/Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ targets += cxxSingleSourceLibraries.map { name in
169169
swiftSettings: [.unsafeFlags(["-Xfrontend",
170170
"-enable-experimental-cxx-interop",
171171
"-I",
172-
"utils/CxxTests"])])
172+
"utils/CxxTests",
173+
// FIXME: https://github.com/apple/swift/issues/61453
174+
"-Xfrontend", "-validate-tbd-against-ir=none"])])
173175
}
174176

175177
targets += multiSourceLibraries.map { lib in

benchmark/cmake/modules/AddSwiftBenchmarkSuite.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ function (swift_benchmark_compile_archopts)
484484
set(cxx_options "")
485485
if ("${module_name_path}" MATCHES ".*cxx-source/.*")
486486
list(APPEND cxx_options "-Xfrontend" "-enable-experimental-cxx-interop" "-I" "${srcdir}/utils/CxxTests/")
487+
# FIXME: https://github.com/apple/swift/issues/61453
488+
list(APPEND cxx_options "-Xfrontend" "-validate-tbd-against-ir=none")
487489
endif()
488490

489491
if ("${bench_flags}" MATCHES "-whole-module.*")
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <vector>
5+
6+
using VectorOfU32 = std::vector<uint32_t>;
7+
8+
static inline VectorOfU32 vec;
9+
10+
inline void initVector(size_t size) {
11+
if (!vec.empty()) {
12+
return;
13+
}
14+
vec.reserve(size);
15+
for (size_t i = 0; i < size; ++i) {
16+
vec.push_back(uint32_t(i));
17+
}
18+
}
19+
20+
inline VectorOfU32 makeVector32(size_t size) {
21+
initVector(size);
22+
return vec;
23+
}
24+
25+
inline uint32_t testVector32Sum(size_t vectorSize, size_t iters) {
26+
auto vector = makeVector32(vectorSize);
27+
auto sum = uint32_t(0);
28+
for (size_t i = 0; i < iters; ++i) {
29+
for (auto x : vector) {
30+
sum += x;
31+
}
32+
}
33+
return sum;
34+
}
35+
36+
// FIXME: remove when the templated operator == is correctly bridged.
37+
inline bool operator ==(const VectorOfU32::const_iterator &lhs, const VectorOfU32::const_iterator &rhs) { return lhs.base() == rhs.base(); }

benchmark/utils/CxxTests/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ module CxxSubscripts {
77
header "Subscripts.h"
88
requires cplusplus
99
}
10+
11+
module CxxStdlibPerformance {
12+
header "CxxStdlibPerformance.h"
13+
requires cplusplus
14+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import ClassArrayGetter
5050
import CodableTest
5151
import Combos
5252
import CreateObjects
53+
import CxxVectorSum
5354
import DataBenchmarks
5455
import DeadArray
5556
import DevirtualizeProtocolComposition
@@ -233,6 +234,7 @@ register(CodableTest.benchmarks)
233234
register(Combos.benchmarks)
234235
register(ClassArrayGetter.benchmarks)
235236
register(CreateObjects.benchmarks)
237+
register(CxxVectorSum.benchmarks)
236238
register(DataBenchmarks.benchmarks)
237239
register(DeadArray.benchmarks)
238240
register(DevirtualizeProtocolComposition.benchmarks)

0 commit comments

Comments
 (0)