[cxx-interop] Add initial benchmark to compare vector<uint32_t> sum in C++ vs Swift

This commit is contained in:
Alex Lorenz
2022-10-05 11:41:52 -07:00
parent d977dd101f
commit d72b592eeb
7 changed files with 131 additions and 1 deletions

View File

@@ -199,6 +199,7 @@ set(SWIFT_BENCH_MODULES
single-source/WordCount
single-source/XorLoop
cxx-source/CreateObjects
cxx-source/CxxVectorSum
cxx-source/ReadAccessor
)

View File

@@ -169,7 +169,9 @@ targets += cxxSingleSourceLibraries.map { name in
swiftSettings: [.unsafeFlags(["-Xfrontend",
"-enable-experimental-cxx-interop",
"-I",
"utils/CxxTests"])])
"utils/CxxTests",
// FIXME: https://github.com/apple/swift/issues/61453
"-Xfrontend", "-validate-tbd-against-ir=none"])])
}
targets += multiSourceLibraries.map { lib in

View File

@@ -484,6 +484,8 @@ function (swift_benchmark_compile_archopts)
set(cxx_options "")
if ("${module_name_path}" MATCHES ".*cxx-source/.*")
list(APPEND cxx_options "-Xfrontend" "-enable-experimental-cxx-interop" "-I" "${srcdir}/utils/CxxTests/")
# FIXME: https://github.com/apple/swift/issues/61453
list(APPEND cxx_options "-Xfrontend" "-validate-tbd-against-ir=none")
endif()
if ("${bench_flags}" MATCHES "-whole-module.*")

View File

@@ -0,0 +1,89 @@
//===--- CxxVectorSum.swift -----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2012 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 benchmark that tracks how quickly Swift can sum up a C++ vector
// as compared to the C++ implementation of such sum.
import TestsUtils
import CxxStdlibPerformance
import Cxx
public let benchmarks = [
BenchmarkInfo(
name: "CxxVectorOfU32SumInCxx",
runFunction: run_CxxVectorOfU32SumInCxx,
tags: [.validation, .bridging, .cxxInterop],
setUpFunction: create_CxxVectorOfU32),
BenchmarkInfo(
name: "CxxVectorOfU32SumInSwift_Fastest",
runFunction: run_CxxVectorOfU32SumInSwift_Fastest,
tags: [.validation, .bridging, .cxxInterop],
setUpFunction: create_CxxVectorOfU32),
BenchmarkInfo(
name: "CxxVectorOfU32SumInSwift",
runFunction: run_CxxVectorOfU32SumInSwift,
tags: [.validation, .bridging, .cxxInterop],
setUpFunction: create_CxxVectorOfU32)
]
// FIXME: compare CxxVectorOfU32SumInCxx to CxxVectorOfU32SumInSwift and
// establish an expected threshold of performance, which when exceeded should
// fail the benchmark.
var vectorOfU32: VectorOfU32!
func create_CxxVectorOfU32() {
vectorOfU32 = makeVector32(1_000_000)
}
@inline(never)
public func run_CxxVectorOfU32SumInCxx(_ n: Int) {
// FIXME: Use no implicitly copyable, immutable borrow instead.
// https://github.com/apple/swift/issues/61454
let sum = testVector32Sum(&vectorOfU32)
blackHole(sum)
}
// This function should have comparable performance to `run_CxxVectorOfU32SumInCxx`.
@inline(never)
public func run_CxxVectorOfU32SumInSwift_Fastest(_ n: Int) {
var sum: UInt32 = 0
// begin mutating, end mutating needed to avoid copies right now.
var b = vectorOfU32.beginMutating()
let e = vectorOfU32.endMutating()
while !cmp(b, e) {
sum = sum &+ b.pointee
b = next(b)
}
blackHole(sum)
}
public func !=(_ y: VectorOfU32.const_iterator, _ x: VectorOfU32.const_iterator) -> Bool {
return y.__baseUnsafe() != x.__baseUnsafe()
}
public func ==(_ y: VectorOfU32.const_iterator, _ x: VectorOfU32.const_iterator) -> Bool {
return y.__baseUnsafe() == x.__baseUnsafe()
}
extension VectorOfU32.const_iterator : Equatable, UnsafeCxxInputIterator { }
extension VectorOfU32: CxxSequence {}
@inline(never)
public func run_CxxVectorOfU32SumInSwift(_ n: Int) {
var sum: UInt32 = 0
for i in vectorOfU32 {
sum = sum &+ i
}
blackHole(sum)
}

View File

@@ -0,0 +1,29 @@
#pragma once
#include <cstdint>
#include <vector>
using VectorOfU32 = std::vector<uint32_t>;
inline VectorOfU32 makeVector32(size_t size) {
auto result = VectorOfU32();
result.reserve(size);
for (size_t i = 0; i < size; ++i) {
result.push_back(uint32_t(i));
}
return result;
}
inline uint32_t testVector32Sum(VectorOfU32 &vector) {
auto sum = uint32_t(0);
for (auto i : vector) {
sum += i;
}
return sum;
}
template<class T>
inline T next(const T& i) { return i + 1; }
template<class T>
inline bool cmp(const T &lhs, const T &rhs) { return lhs == rhs; }

View File

@@ -7,3 +7,8 @@ module CxxSubscripts {
header "Subscripts.h"
requires cplusplus
}
module CxxStdlibPerformance {
header "CxxStdlibPerformance.h"
requires cplusplus
}

View File

@@ -50,6 +50,7 @@ import ClassArrayGetter
import CodableTest
import Combos
import CreateObjects
import CxxVectorSum
import DataBenchmarks
import DeadArray
import DevirtualizeProtocolComposition
@@ -233,6 +234,7 @@ register(CodableTest.benchmarks)
register(Combos.benchmarks)
register(ClassArrayGetter.benchmarks)
register(CreateObjects.benchmarks)
register(CxxVectorSum.benchmarks)
register(DataBenchmarks.benchmarks)
register(DeadArray.benchmarks)
register(DevirtualizeProtocolComposition.benchmarks)