Skip to content

Commit b81d32b

Browse files
committed
Add benchmarks for individual operations
1 parent a799f0e commit b81d32b

File tree

1 file changed

+125
-10
lines changed

1 file changed

+125
-10
lines changed

libcxx/test/benchmarks/exception_ptr.bench.cpp

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

21-
static bool exception_ptr_moves_copies_swap(std::exception_ptr p1) {
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(excptr);
25+
benchmark::DoNotOptimize(std::exception_ptr(excptr));
26+
}
27+
}
28+
BENCHMARK(bm_exception_ptr_copy_ctor_nonnull);
29+
30+
void bm_exception_ptr_copy_ctor_null(benchmark::State& state) {
31+
std::exception_ptr excptr = nullptr;
32+
for (auto _ : state) {
33+
benchmark::DoNotOptimize(excptr);
34+
benchmark::DoNotOptimize(std::exception_ptr(excptr));
35+
}
36+
}
37+
BENCHMARK(bm_exception_ptr_copy_ctor_null);
38+
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(excptr_copy);
47+
benchmark::DoNotOptimize(std::exception_ptr(std::move(excptr_copy)));
48+
}
49+
}
50+
BENCHMARK(bm_exception_ptr_move_ctor_nonnull);
51+
52+
void bm_exception_ptr_move_ctor_null(benchmark::State& state) {
53+
std::exception_ptr excptr = nullptr;
54+
for (auto _ : state) {
55+
benchmark::DoNotOptimize(excptr);
56+
benchmark::DoNotOptimize(std::exception_ptr(std::move(excptr)));
57+
}
58+
}
59+
BENCHMARK(bm_exception_ptr_move_ctor_null);
60+
61+
void bm_exception_ptr_copy_assign_nonnull(benchmark::State& state) {
62+
std::exception_ptr excptr = std::make_exception_ptr(42);
63+
for (auto _ : state) {
64+
benchmark::DoNotOptimize(excptr);
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+
benchmark::DoNotOptimize(excptr);
76+
std::exception_ptr new_excptr;
77+
new_excptr = excptr;
78+
benchmark::DoNotOptimize(new_excptr);
79+
}
80+
}
81+
BENCHMARK(bm_exception_ptr_copy_assign_null);
82+
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+
benchmark::DoNotOptimize(excptr_copy);
91+
std::exception_ptr new_excptr;
92+
new_excptr = std::move(excptr_copy);
93+
benchmark::DoNotOptimize(new_excptr);
94+
}
95+
}
96+
BENCHMARK(bm_exception_ptr_move_assign_nonnull);
97+
98+
void bm_exception_ptr_move_assign_null(benchmark::State& state) {
99+
std::exception_ptr excptr = nullptr;
100+
for (auto _ : state) {
101+
benchmark::DoNotOptimize(excptr);
102+
std::exception_ptr new_excptr;
103+
new_excptr = std::move(excptr);
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 excptr = std::make_exception_ptr(41);
111+
std::exception_ptr excptr2 = std::make_exception_ptr(42);
112+
for (auto _ : state) {
113+
swap(excptr, excptr2);
114+
benchmark::DoNotOptimize(excptr);
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 excptr = nullptr;
122+
std::exception_ptr excptr2 = nullptr;
123+
for (auto _ : state) {
124+
benchmark::DoNotOptimize(excptr);
125+
swap(excptr, excptr2);
126+
benchmark::DoNotOptimize(excptr2);
127+
}
128+
}
129+
BENCHMARK(bm_exception_ptr_swap_null);
130+
131+
// A chain of moves, copies and swaps.
132+
// In contrast to the previous benchmarks of individual operations,
133+
// this benchmark performs a chain of operations. It thereby
134+
// specifically stresses the information available to the compiler
135+
// for optimizations from the header itself.
136+
static bool exception_ptr_move_copy_swap(std::exception_ptr p1) {
22137
// Taken from https://llvm.org/PR45547
23138
std::exception_ptr p2(p1);
24139
std::exception_ptr p3(std::move(p2));
@@ -33,34 +148,34 @@ static bool exception_ptr_moves_copies_swap(std::exception_ptr p1) {
33148
}
34149

35150
// Benchmark copies, moves and comparisons of a non-null exception_ptr.
36-
void bm_nonnull_exception_ptr(benchmark::State& state) {
151+
void bm_exception_ptr_move_copy_swap_nonnull(benchmark::State& state) {
37152
std::exception_ptr excptr = std::make_exception_ptr(42);
38153
for (auto _ : state) {
39154
benchmark::DoNotOptimize(excptr);
40-
benchmark::DoNotOptimize(exception_ptr_moves_copies_swap(excptr));
155+
benchmark::DoNotOptimize(exception_ptr_move_copy_swap(excptr));
41156
}
42157
}
43-
BENCHMARK(bm_nonnull_exception_ptr);
158+
BENCHMARK(bm_exception_ptr_move_copy_swap_nonnull);
44159

45160
// Benchmark copies, moves and comparisons of a nullptr exception_ptr
46161
// where the compiler cannot prove that the exception_ptr is always
47162
// a nullptr and needs to emit runtime checks.
48-
void bm_null_exception_ptr(benchmark::State& state) {
163+
void bm_exception_ptr_move_copy_swap_null(benchmark::State& state) {
49164
std::exception_ptr excptr;
50165
for (auto _ : state) {
51166
benchmark::DoNotOptimize(excptr);
52-
benchmark::DoNotOptimize(exception_ptr_moves_copies_swap(excptr));
167+
benchmark::DoNotOptimize(exception_ptr_move_copy_swap(excptr));
53168
}
54169
}
55-
BENCHMARK(bm_null_exception_ptr);
170+
BENCHMARK(bm_exception_ptr_move_copy_swap_null);
56171

57172
// Benchmark copies, moves and comparisons of a nullptr exception_ptr
58173
// where the compiler can proof that the exception_ptr is always a nullptr.
59-
void bm_optimized_null_exception_ptr(benchmark::State& state) {
174+
void bm_exception_ptr_move_copy_swap_null_optimized(benchmark::State& state) {
60175
for (auto _ : state) {
61-
benchmark::DoNotOptimize(exception_ptr_moves_copies_swap(std::exception_ptr{nullptr}));
176+
benchmark::DoNotOptimize(exception_ptr_move_copy_swap(std::exception_ptr{nullptr}));
62177
}
63178
}
64-
BENCHMARK(bm_optimized_null_exception_ptr);
179+
BENCHMARK(bm_exception_ptr_move_copy_swap_null_optimized);
65180

66181
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)