Skip to content

Commit 5853288

Browse files
vogelsgesangLukacma
authored andcommitted
[libc++] Add benchmark for std::exception_ptr (llvm#164278)
This commit adds benchmarks for `std::exception_ptr` to set a baseline in preparation for follow-up optimizations.
1 parent 117de38 commit 5853288

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

libcxx/test/benchmarks/exception_ptr.bench.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,116 @@ void bm_make_exception_ptr(benchmark::State& state) {
1818
}
1919
BENCHMARK(bm_make_exception_ptr)->ThreadRange(1, 8);
2020

21+
void bm_exception_ptr_copy_ctor_nonnull(benchmark::State& state) {
22+
std::exception_ptr excptr = std::make_exception_ptr(42);
23+
for (auto _ : state) {
24+
benchmark::DoNotOptimize(std::exception_ptr(excptr));
25+
}
26+
}
27+
BENCHMARK(bm_exception_ptr_copy_ctor_nonnull);
28+
29+
void bm_exception_ptr_copy_ctor_null(benchmark::State& state) {
30+
std::exception_ptr excptr = nullptr;
31+
for (auto _ : state) {
32+
std::exception_ptr excptr_copy(excptr);
33+
// The compiler should be able to constant-fold the comparison
34+
benchmark::DoNotOptimize(excptr_copy == nullptr);
35+
benchmark::DoNotOptimize(excptr_copy);
36+
}
37+
}
38+
BENCHMARK(bm_exception_ptr_copy_ctor_null);
39+
40+
void bm_exception_ptr_move_ctor_nonnull(benchmark::State& state) {
41+
std::exception_ptr excptr = std::make_exception_ptr(42);
42+
for (auto _ : state) {
43+
// Need to copy, such that the `excptr` is not moved from and
44+
// empty after the first loop iteration.
45+
std::exception_ptr excptr_copy(excptr);
46+
benchmark::DoNotOptimize(std::exception_ptr(std::move(excptr_copy)));
47+
}
48+
}
49+
BENCHMARK(bm_exception_ptr_move_ctor_nonnull);
50+
51+
void bm_exception_ptr_move_ctor_null(benchmark::State& state) {
52+
std::exception_ptr excptr = nullptr;
53+
for (auto _ : state) {
54+
std::exception_ptr new_excptr(std::move(excptr));
55+
// The compiler should be able to constant-fold the comparison
56+
benchmark::DoNotOptimize(new_excptr == nullptr);
57+
benchmark::DoNotOptimize(new_excptr);
58+
}
59+
}
60+
BENCHMARK(bm_exception_ptr_move_ctor_null);
61+
62+
void bm_exception_ptr_copy_assign_nonnull(benchmark::State& state) {
63+
std::exception_ptr excptr = std::make_exception_ptr(42);
64+
for (auto _ : state) {
65+
std::exception_ptr new_excptr;
66+
new_excptr = excptr;
67+
benchmark::DoNotOptimize(new_excptr);
68+
}
69+
}
70+
BENCHMARK(bm_exception_ptr_copy_assign_nonnull);
71+
72+
void bm_exception_ptr_copy_assign_null(benchmark::State& state) {
73+
std::exception_ptr excptr = nullptr;
74+
for (auto _ : state) {
75+
std::exception_ptr new_excptr;
76+
new_excptr = excptr;
77+
// The compiler should be able to constant-fold the comparison
78+
benchmark::DoNotOptimize(new_excptr == nullptr);
79+
benchmark::DoNotOptimize(new_excptr);
80+
}
81+
}
82+
BENCHMARK(bm_exception_ptr_copy_assign_null);
83+
84+
void bm_exception_ptr_move_assign_nonnull(benchmark::State& state) {
85+
std::exception_ptr excptr = std::make_exception_ptr(42);
86+
for (auto _ : state) {
87+
// Need to copy, such that the `excptr` is not moved from and
88+
// empty after the first loop iteration.
89+
std::exception_ptr excptr_copy(excptr);
90+
std::exception_ptr new_excptr;
91+
new_excptr = std::move(excptr_copy);
92+
benchmark::DoNotOptimize(new_excptr);
93+
}
94+
}
95+
BENCHMARK(bm_exception_ptr_move_assign_nonnull);
96+
97+
void bm_exception_ptr_move_assign_null(benchmark::State& state) {
98+
std::exception_ptr excptr = nullptr;
99+
for (auto _ : state) {
100+
std::exception_ptr new_excptr;
101+
new_excptr = std::move(excptr);
102+
// The compiler should be able to constant-fold the comparison
103+
benchmark::DoNotOptimize(new_excptr == nullptr);
104+
benchmark::DoNotOptimize(new_excptr);
105+
}
106+
}
107+
BENCHMARK(bm_exception_ptr_move_assign_null);
108+
109+
void bm_exception_ptr_swap_nonnull(benchmark::State& state) {
110+
std::exception_ptr excptr1 = std::make_exception_ptr(41);
111+
std::exception_ptr excptr2 = std::make_exception_ptr(42);
112+
for (auto _ : state) {
113+
swap(excptr1, excptr2);
114+
benchmark::DoNotOptimize(excptr1);
115+
benchmark::DoNotOptimize(excptr2);
116+
}
117+
}
118+
BENCHMARK(bm_exception_ptr_swap_nonnull);
119+
120+
void bm_exception_ptr_swap_null(benchmark::State& state) {
121+
std::exception_ptr excptr1 = nullptr;
122+
std::exception_ptr excptr2 = nullptr;
123+
for (auto _ : state) {
124+
swap(excptr1, excptr2);
125+
// The compiler should be able to constant-fold those comparisons
126+
benchmark::DoNotOptimize(excptr1 == nullptr);
127+
benchmark::DoNotOptimize(excptr2 == nullptr);
128+
benchmark::DoNotOptimize(excptr1 == excptr2);
129+
}
130+
}
131+
BENCHMARK(bm_exception_ptr_swap_null);
132+
21133
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)