Create ReadAccessor benchmark

Create a benchmark to test the
performance of synthesized read
accessors generated to interop
with C++ [] operators.
This commit is contained in:
Nuri Amari
2022-03-01 12:47:06 -05:00
parent 9e787022a6
commit cda2c26d8c
5 changed files with 70 additions and 0 deletions

View File

@@ -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

View File

@@ -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)
}
}
}
}

View File

@@ -0,0 +1,10 @@
#ifndef BENCHMARK_SUBSCRIPTS_H
#define BENCHMARK_SUBSCRIPTS_H
#include <vector>
using TwoDimensionalVector = std::vector<std::vector<int>>;
inline TwoDimensionalVector initVector() { return {100, std::vector<int>{1000, 0}}; }
#endif

View File

@@ -2,3 +2,8 @@ module CxxCreateObjects {
header "CreateObjects.h"
requires cplusplus
}
module CxxSubscripts {
header "Subscripts.h"
requires cplusplus
}

View File

@@ -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)