diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index 4204843bbc3e9..0dae33fa639ba 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -196,6 +196,7 @@ set(SWIFT_BENCH_MODULES single-source/WordCount single-source/XorLoop cxx-source/CreateObjects + cxx-source/ReadAccessor ) set(SWIFT_MULTISOURCE_SWIFT_BENCHES diff --git a/benchmark/cxx-source/ReadAccessor.swift b/benchmark/cxx-source/ReadAccessor.swift new file mode 100644 index 0000000000000..d71d838d03790 --- /dev/null +++ b/benchmark/cxx-source/ReadAccessor.swift @@ -0,0 +1,52 @@ +// Subscripts.swift - Very brief description +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +// ----------------------------------------------------------------------------- +/// +/// This is a simple test that reads a non trivial C++ struct using an imported +/// subscript thousands of times. +/// +// ----------------------------------------------------------------------------- + +import TestsUtils +import CxxSubscripts + +var vec : TwoDimensionalVector? + +public let benchmarks = [ + BenchmarkInfo( + name: "ReadAccessor", + runFunction: run_ReadAccessor, + tags: [.validation, .bridging], + setUpFunction: { + vec = initVector() + }) +] + +@inline(never) +public func run_ReadAccessor(_ N: Int) { + for i in 0...N { + for j in 0..<100 { +#if os(Linux) + let row = vec![UInt(j)]; +#else + let row = vec![j]; +#endif + for k in 0..<1_000 { +#if os(Linux) + let element = row[UInt(k)]; +#else + let element = row[k]; +#endif + blackHole(element) + } + } + } +} diff --git a/benchmark/utils/CxxTests/Subscripts.h b/benchmark/utils/CxxTests/Subscripts.h new file mode 100644 index 0000000000000..ab15c4a4e6f76 --- /dev/null +++ b/benchmark/utils/CxxTests/Subscripts.h @@ -0,0 +1,10 @@ +#ifndef BENCHMARK_SUBSCRIPTS_H +#define BENCHMARK_SUBSCRIPTS_H +#include + +using TwoDimensionalVector = std::vector>; + +inline TwoDimensionalVector initVector() { return {100, std::vector{1000, 0}}; } + +#endif + diff --git a/benchmark/utils/CxxTests/module.modulemap b/benchmark/utils/CxxTests/module.modulemap index 15dbcf2c4cc5d..3edf70afbd71a 100644 --- a/benchmark/utils/CxxTests/module.modulemap +++ b/benchmark/utils/CxxTests/module.modulemap @@ -2,3 +2,8 @@ module CxxCreateObjects { header "CreateObjects.h" requires cplusplus } + +module CxxSubscripts { + header "Subscripts.h" + requires cplusplus +} diff --git a/benchmark/utils/main.swift b/benchmark/utils/main.swift index d2af8a20357b8..9f634c3755ea2 100644 --- a/benchmark/utils/main.swift +++ b/benchmark/utils/main.swift @@ -152,6 +152,7 @@ import RangeAssignment import RangeIteration import RangeOverlaps import RangeReplaceableCollectionPlusDefault +import ReadAccessor import RecursiveOwnedParameter import ReduceInto import RemoveWhere @@ -332,6 +333,7 @@ register(RangeAssignment.benchmarks) register(RangeIteration.benchmarks) register(RangeOverlaps.benchmarks) register(RangeReplaceableCollectionPlusDefault.benchmarks) +register(ReadAccessor.benchmarks) register(RecursiveOwnedParameter.benchmarks) register(ReduceInto.benchmarks) register(RemoveWhere.benchmarks)