Skip to content

Commit 9924c98

Browse files
committed
[nfc] [benchmark] [cxx-interop] Add "GlobalVars" benchmark to test global static vars.
Tests performance of loading static C++ variables from Swift. Tests both constexpr and non-constexpr statics.
1 parent 62e3896 commit 9924c98

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ set(SWIFT_BENCH_MODULES
192192
single-source/WordCount
193193
single-source/XorLoop
194194
cxx-source/CreateObjects
195+
cxx-source/GlobalVars
195196
)
196197

197198
set(SWIFT_MULTISOURCE_SWIFT_BENCHES

benchmark/cxx-source/GlobalVars.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- GlobalVars.swift -------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// This is a simple test that creates thousands of C++ objects and does nothing
14+
// with them.
15+
16+
import TestsUtils
17+
import CxxGlobalVars
18+
19+
public let GlobalVars = BenchmarkInfo(
20+
name: "GlobalVars",
21+
runFunction: run_GlobalVars,
22+
tags: [.validation, .bridging])
23+
24+
@inline(never)
25+
public func run_GlobalVars(_ N: Int) {
26+
for i in 0...(N * 10_000) {
27+
let localVar1 = globalInt
28+
let localVar2 = globalConstexprInt
29+
let localVar3 = globalFloat
30+
let localVar4 = globalConstexprFloat
31+
let localVar5 = globalBigObject
32+
let localVar6 = globalConstexprBigObject
33+
blackHole(localVar1)
34+
blackHole(localVar2)
35+
blackHole(localVar3)
36+
blackHole(localVar4)
37+
blackHole(localVar5)
38+
blackHole(localVar6)
39+
}
40+
}

benchmark/utils/CxxTests/GlobalVars.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef BENCHMARK_GLOBAL_VARS_H
2+
#define BENCHMARK_GLOBAL_VARS_H
3+
4+
static int globalInt = 42;
5+
static constexpr int globalConstexprInt = 42;
6+
static float globalFloat = 42;
7+
static constexpr float globalConstexprFloat = 42;
8+
9+
struct BigObject {
10+
char buff[512];
11+
};
12+
static BigObject globalBigObject = BigObject();
13+
static constexpr BigObject globalConstexprBigObject = BigObject();
14+
15+
#endif // BENCHMARK_GLOBAL_VARS_H
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
module CxxCreateObjects {
22
header "CreateObjects.h"
33
}
4+
5+
module CxxGlobalVars {
6+
header "GlobalVars.h"
7+
requires cplusplus
8+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import FlattenList
8686
import FloatingPointConversion
8787
import FloatingPointParsing
8888
import FloatingPointPrinting
89+
import GlobalVars
8990
import Hanoi
9091
import Hash
9192
import Histogram
@@ -280,6 +281,7 @@ registerBenchmark(FlattenListFlatMap)
280281
registerBenchmark(FloatingPointConversion)
281282
registerBenchmark(FloatingPointParsing)
282283
registerBenchmark(FloatingPointPrinting)
284+
registerBenchmark(GlobalVars)
283285
registerBenchmark(Hanoi)
284286
registerBenchmark(HashTest)
285287
registerBenchmark(Histogram)

0 commit comments

Comments
 (0)