Skip to content

Commit bff5d52

Browse files
authored
Merge pull request #41618 from NuriAmari/read-accessor-benchmark
Create ReadAccessor benchmark
2 parents a72afb3 + cda2c26 commit bff5d52

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ set(SWIFT_BENCH_MODULES
196196
single-source/WordCount
197197
single-source/XorLoop
198198
cxx-source/CreateObjects
199+
cxx-source/ReadAccessor
199200
)
200201

201202
set(SWIFT_MULTISOURCE_SWIFT_BENCHES
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Subscripts.swift - Very brief description
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 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 simple test that reads a non trivial C++ struct using an imported
14+
/// subscript thousands of times.
15+
///
16+
// -----------------------------------------------------------------------------
17+
18+
import TestsUtils
19+
import CxxSubscripts
20+
21+
var vec : TwoDimensionalVector?
22+
23+
public let benchmarks = [
24+
BenchmarkInfo(
25+
name: "ReadAccessor",
26+
runFunction: run_ReadAccessor,
27+
tags: [.validation, .bridging],
28+
setUpFunction: {
29+
vec = initVector()
30+
})
31+
]
32+
33+
@inline(never)
34+
public func run_ReadAccessor(_ N: Int) {
35+
for i in 0...N {
36+
for j in 0..<100 {
37+
#if os(Linux)
38+
let row = vec![UInt(j)];
39+
#else
40+
let row = vec![j];
41+
#endif
42+
for k in 0..<1_000 {
43+
#if os(Linux)
44+
let element = row[UInt(k)];
45+
#else
46+
let element = row[k];
47+
#endif
48+
blackHole(element)
49+
}
50+
}
51+
}
52+
}

benchmark/utils/CxxTests/Subscripts.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef BENCHMARK_SUBSCRIPTS_H
2+
#define BENCHMARK_SUBSCRIPTS_H
3+
#include <vector>
4+
5+
using TwoDimensionalVector = std::vector<std::vector<int>>;
6+
7+
inline TwoDimensionalVector initVector() { return {100, std::vector<int>{1000, 0}}; }
8+
9+
#endif
10+

benchmark/utils/CxxTests/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ module CxxCreateObjects {
22
header "CreateObjects.h"
33
requires cplusplus
44
}
5+
6+
module CxxSubscripts {
7+
header "Subscripts.h"
8+
requires cplusplus
9+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ import RangeAssignment
152152
import RangeIteration
153153
import RangeOverlaps
154154
import RangeReplaceableCollectionPlusDefault
155+
import ReadAccessor
155156
import RecursiveOwnedParameter
156157
import ReduceInto
157158
import RemoveWhere
@@ -332,6 +333,7 @@ register(RangeAssignment.benchmarks)
332333
register(RangeIteration.benchmarks)
333334
register(RangeOverlaps.benchmarks)
334335
register(RangeReplaceableCollectionPlusDefault.benchmarks)
336+
register(ReadAccessor.benchmarks)
335337
register(RecursiveOwnedParameter.benchmarks)
336338
register(ReduceInto.benchmarks)
337339
register(RemoveWhere.benchmarks)

0 commit comments

Comments
 (0)