Skip to content

Commit d72b592

Browse files
committed
[cxx-interop] Add initial benchmark to compare vector<uint32_t> sum in C++ vs Swift
1 parent d977dd1 commit d72b592

File tree

7 files changed

+131
-1
lines changed

7 files changed

+131
-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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
public let benchmarks = [
21+
BenchmarkInfo(
22+
name: "CxxVectorOfU32SumInCxx",
23+
runFunction: run_CxxVectorOfU32SumInCxx,
24+
tags: [.validation, .bridging, .cxxInterop],
25+
setUpFunction: create_CxxVectorOfU32),
26+
BenchmarkInfo(
27+
name: "CxxVectorOfU32SumInSwift_Fastest",
28+
runFunction: run_CxxVectorOfU32SumInSwift_Fastest,
29+
tags: [.validation, .bridging, .cxxInterop],
30+
setUpFunction: create_CxxVectorOfU32),
31+
BenchmarkInfo(
32+
name: "CxxVectorOfU32SumInSwift",
33+
runFunction: run_CxxVectorOfU32SumInSwift,
34+
tags: [.validation, .bridging, .cxxInterop],
35+
setUpFunction: create_CxxVectorOfU32)
36+
]
37+
38+
// FIXME: compare CxxVectorOfU32SumInCxx to CxxVectorOfU32SumInSwift and
39+
// establish an expected threshold of performance, which when exceeded should
40+
// fail the benchmark.
41+
42+
var vectorOfU32: VectorOfU32!
43+
44+
func create_CxxVectorOfU32() {
45+
vectorOfU32 = makeVector32(1_000_000)
46+
}
47+
48+
@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)
53+
blackHole(sum)
54+
}
55+
56+
// This function should have comparable performance to `run_CxxVectorOfU32SumInCxx`.
57+
@inline(never)
58+
public func run_CxxVectorOfU32SumInSwift_Fastest(_ n: Int) {
59+
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)
66+
}
67+
blackHole(sum)
68+
}
69+
70+
public func !=(_ y: VectorOfU32.const_iterator, _ x: VectorOfU32.const_iterator) -> Bool {
71+
return y.__baseUnsafe() != x.__baseUnsafe()
72+
}
73+
74+
public func ==(_ y: VectorOfU32.const_iterator, _ x: VectorOfU32.const_iterator) -> Bool {
75+
return y.__baseUnsafe() == x.__baseUnsafe()
76+
}
77+
78+
extension VectorOfU32.const_iterator : Equatable, UnsafeCxxInputIterator { }
79+
80+
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <vector>
5+
6+
using VectorOfU32 = std::vector<uint32_t>;
7+
8+
inline VectorOfU32 makeVector32(size_t size) {
9+
auto result = VectorOfU32();
10+
result.reserve(size);
11+
for (size_t i = 0; i < size; ++i) {
12+
result.push_back(uint32_t(i));
13+
}
14+
return result;
15+
}
16+
17+
inline uint32_t testVector32Sum(VectorOfU32 &vector) {
18+
auto sum = uint32_t(0);
19+
for (auto i : vector) {
20+
sum += i;
21+
}
22+
return sum;
23+
}
24+
25+
template<class T>
26+
inline T next(const T& i) { return i + 1; }
27+
28+
template<class T>
29+
inline bool cmp(const T &lhs, const T &rhs) { return lhs == rhs; }

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)