Skip to content

[nfc] [benchmark] [cxx-interop] Add "GlobalVars" benchmark to test static variables. #35318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ set(SWIFT_BENCH_MODULES
single-source/WordCount
single-source/XorLoop
cxx-source/CreateObjects
cxx-source/GlobalVars
)

set(SWIFT_MULTISOURCE_SWIFT_BENCHES
Expand Down
40 changes: 40 additions & 0 deletions benchmark/cxx-source/GlobalVars.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===--- GlobalVars.swift -------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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 creates thousands of C++ objects and does nothing
// with them.

import TestsUtils
import CxxGlobalVars

public let GlobalVars = BenchmarkInfo(
name: "GlobalVars",
runFunction: run_GlobalVars,
tags: [.validation, .bridging])

@inline(never)
public func run_GlobalVars(_ N: Int) {
for i in 0...(N * 10_000) {
let localVar1 = globalInt
let localVar2 = globalConstexprInt
let localVar3 = globalFloat
let localVar4 = globalConstexprFloat
let localVar5 = globalBigObject
let localVar6 = globalConstexprBigObject
blackHole(localVar1)
blackHole(localVar2)
blackHole(localVar3)
blackHole(localVar4)
blackHole(localVar5)
blackHole(localVar6)
}
}
15 changes: 15 additions & 0 deletions benchmark/utils/CxxTests/GlobalVars.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef BENCHMARK_GLOBAL_VARS_H
#define BENCHMARK_GLOBAL_VARS_H

static int globalInt = 42;
static constexpr int globalConstexprInt = 42;
static float globalFloat = 42;
static constexpr float globalConstexprFloat = 42;

struct BigObject {
char buff[512];
};
static BigObject globalBigObject = BigObject();
static constexpr BigObject globalConstexprBigObject = BigObject();

#endif // BENCHMARK_GLOBAL_VARS_H
5 changes: 5 additions & 0 deletions benchmark/utils/CxxTests/module.modulemap
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module CxxCreateObjects {
header "CreateObjects.h"
}

module CxxGlobalVars {
header "GlobalVars.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 @@ -86,6 +86,7 @@ import FlattenList
import FloatingPointConversion
import FloatingPointParsing
import FloatingPointPrinting
import GlobalVars
import Hanoi
import Hash
import Histogram
Expand Down Expand Up @@ -280,6 +281,7 @@ registerBenchmark(FlattenListFlatMap)
registerBenchmark(FloatingPointConversion)
registerBenchmark(FloatingPointParsing)
registerBenchmark(FloatingPointPrinting)
registerBenchmark(GlobalVars)
registerBenchmark(Hanoi)
registerBenchmark(HashTest)
registerBenchmark(Histogram)
Expand Down