Skip to content

Commit 4238b8f

Browse files
committed
[cxx-interop] Create benchmarks for using std::span in Swift
* swift-ci linux tests do not support std::span
1 parent 4fc85d7 commit 4238b8f

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ set(SWIFT_BENCH_MODULES
214214
cxx-source/CxxVectorSum
215215
# TODO: rdar://92120528
216216
# cxx-source/ReadAccessor
217+
cxx-source/CxxSpanTests
217218
)
218219

219220
set(SWIFT_MULTISOURCE_SWIFT_BENCHES
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//===--- CxxSpanTests.swift ----------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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+
import TestsUtils
14+
import CxxStdlibPerformance
15+
import CxxStdlib
16+
17+
let iterRepeatFactor = 7
18+
19+
// FIXME swift-ci linux tests do not support std::span
20+
#if os(Linux)
21+
public let benchmarks = [BenchmarkInfo]()
22+
23+
#else
24+
25+
public let benchmarks = [
26+
BenchmarkInfo(
27+
name: "CxxSpanTests.raw.iterator",
28+
runFunction: rawIterator,
29+
tags: [.validation, .bridging, .cxxInterop],
30+
setUpFunction: makeSpanOnce),
31+
BenchmarkInfo(
32+
name: "CxxSpanTests.index.subscript",
33+
runFunction: indexAndSubscript,
34+
tags: [.validation, .bridging, .cxxInterop],
35+
setUpFunction: makeSpanOnce),
36+
BenchmarkInfo(
37+
name: "CxxSpanTests.for.loop",
38+
runFunction: forInLoop,
39+
tags: [.validation, .bridging, .cxxInterop],
40+
setUpFunction: makeSpanOnce),
41+
BenchmarkInfo(
42+
name: "CxxSpanTests.map",
43+
runFunction: mapSpan,
44+
tags: [.validation, .bridging, .cxxInterop],
45+
setUpFunction: makeSpanOnce),
46+
BenchmarkInfo(
47+
name: "CxxSpanTests.filter",
48+
runFunction: filterSpan,
49+
tags: [.validation, .bridging, .cxxInterop],
50+
setUpFunction: makeSpanOnce),
51+
BenchmarkInfo(
52+
name: "CxxSpanTests.reduce",
53+
runFunction: reduceSpan,
54+
tags: [.validation, .bridging, .cxxInterop],
55+
setUpFunction: makeSpanOnce)
56+
]
57+
58+
func makeSpanOnce() {
59+
initSpan()
60+
}
61+
62+
@inline(never)
63+
public func rawIterator(_ n: Int) {
64+
var sum: UInt32 = 0
65+
for _ in 0..<(n * iterRepeatFactor * 2) {
66+
var b = span.__beginUnsafe()
67+
let e = span.__endUnsafe()
68+
while b != e {
69+
sum = sum &+ b.pointee
70+
b = b.successor()
71+
}
72+
}
73+
blackHole(sum)
74+
}
75+
76+
@inline(never)
77+
public func indexAndSubscript(_ n: Int) {
78+
var sum: UInt32 = 0
79+
for _ in 0..<(n * iterRepeatFactor * 2) {
80+
for i in 0..<span.size() {
81+
sum = sum &+ span[i]
82+
}
83+
}
84+
blackHole(sum)
85+
}
86+
87+
@inline(never)
88+
public func forInLoop(_ n: Int) {
89+
var sum: UInt32 = 0
90+
for _ in 0..<(n * iterRepeatFactor * 2) {
91+
for x in span {
92+
sum = sum &+ x
93+
}
94+
}
95+
blackHole(sum)
96+
}
97+
98+
@inline(never)
99+
public func mapSpan(_ n: Int) {
100+
for _ in 0..<(n * iterRepeatFactor) {
101+
let result = span.map { $0 + 5 }
102+
blackHole(result)
103+
}
104+
}
105+
106+
@inline(never)
107+
public func filterSpan(_ n: Int) {
108+
for _ in 0..<(n * iterRepeatFactor) {
109+
let result = span.filter { $0 % 2 == 0 }
110+
blackHole(result)
111+
}
112+
}
113+
114+
@inline(never)
115+
public func reduceSpan(_ n: Int) {
116+
var sum: UInt32 = 0
117+
for _ in 0..<(n * iterRepeatFactor * 2) {
118+
sum = sum &+ span.reduce(sum, &+)
119+
}
120+
blackHole(sum)
121+
}
122+
123+
#endif

benchmark/utils/CxxTests/CxxStdlibPerformance.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,26 @@
44
#include <vector>
55
#include <set>
66

7+
// FIXME swift-ci linux tests do not support std::span
8+
#if !defined(__linux__)
9+
#include <span>
10+
#endif
11+
12+
static const size_t spanSize = 50000;
13+
14+
using ArrayOfU32 = uint32_t[spanSize];
715
using VectorOfU32 = std::vector<uint32_t>;
816
using SetOfU32 = std::set<uint32_t>;
17+
#if !defined(__linux__)
18+
using SpanOfU32 = std::span<uint32_t>;
19+
#endif
920

21+
static inline ArrayOfU32 array;
1022
static inline VectorOfU32 vec;
1123
static inline SetOfU32 set;
24+
#if !defined(__linux__)
25+
static inline SpanOfU32 span;
26+
#endif
1227

1328
inline void initVector(size_t size) {
1429
if (!vec.empty()) {
@@ -29,6 +44,18 @@ inline void initSet(size_t size) {
2944
}
3045
}
3146

47+
#if !defined(__linux__)
48+
inline void initSpan() {
49+
if (!span.empty()) {
50+
return;
51+
}
52+
for (size_t i = 0; i < spanSize; ++i) {
53+
array[i] = uint32_t(i);
54+
}
55+
span = SpanOfU32(array);
56+
}
57+
#endif
58+
3259
inline VectorOfU32 makeVector32(size_t size) {
3360
initVector(size);
3461
return vec;

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import CreateObjects
6060
import CxxStringConversion
6161
// rdar://128520766
6262
// import CxxVectorSum
63+
import CxxSpanTests
6364
import DataBenchmarks
6465
import DeadArray
6566
import DevirtualizeProtocolComposition
@@ -258,6 +259,7 @@ register(CreateObjects.benchmarks)
258259
register(CxxStringConversion.benchmarks)
259260
// rdar://128520766
260261
// register(CxxVectorSum.benchmarks)
262+
register(CxxSpanTests.benchmarks)
261263
register(DataBenchmarks.benchmarks)
262264
register(DeadArray.benchmarks)
263265
register(DevirtualizeProtocolComposition.benchmarks)

0 commit comments

Comments
 (0)