Skip to content

Commit

Permalink
Create ReadAccessor benchmark
Browse files Browse the repository at this point in the history
Create a benchmark to test the
performance of synthesized read
accessors generated to interop
with C++ [] operators.
  • Loading branch information
NuriAmari committed Mar 7, 2022
1 parent 9e78702 commit cda2c26
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions benchmark/cxx-source/ReadAccessor.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
10 changes: 10 additions & 0 deletions benchmark/utils/CxxTests/Subscripts.h
Original file line number Diff line number Diff line change
@@ -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

5 changes: 5 additions & 0 deletions benchmark/utils/CxxTests/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ module CxxCreateObjects {
header "CreateObjects.h"
requires cplusplus
}

module CxxSubscripts {
header "Subscripts.h"
requires cplusplus
}
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ import RangeAssignment
import RangeIteration
import RangeOverlaps
import RangeReplaceableCollectionPlusDefault
import ReadAccessor
import RecursiveOwnedParameter
import ReduceInto
import RemoveWhere
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cda2c26

Please sign in to comment.