Skip to content

Commit e4db02b

Browse files
committed
[libc++] Add benchmark for std::exception_ptr
This commit adds benchmarks for `std::exception_ptr` to set a baseline in preparation for follow-up optimizations.
1 parent a80a6b3 commit e4db02b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

libcxx/test/benchmarks/exception_ptr.bench.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,37 @@ 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) {
22+
// Taken from https://github.com/llvm/llvm-project/issues/44892
23+
std::exception_ptr p2(p1); // Copy constructor
24+
std::exception_ptr p3(std::move(p2)); // Move constructor
25+
p2 = std::move(p1); // Move assignment
26+
p1 = p2; // Copy assignment
27+
swap(p1, p2); // Swap
28+
// Comparisons against nullptr. The overhead from creating temporary `exception_ptr`
29+
// instances should be optimized out.
30+
bool is_null = p1 == nullptr && nullptr == p2;
31+
bool is_equal = p1 == p2; // Comparison
32+
return is_null && is_equal;
33+
}
34+
35+
void bm_empty_exception_ptr(benchmark::State& state) {
36+
for (auto _ : state) {
37+
// All of the `exception_ptr_noops` are no-ops because
38+
// the exception_ptr is empty. Hence, the compiler should
39+
// be able to optimize them very aggressively.
40+
benchmark::DoNotOptimize(exception_ptr_moves_copies_swap(std::exception_ptr{nullptr}));
41+
}
42+
}
43+
BENCHMARK(bm_empty_exception_ptr);
44+
45+
void bm_exception_ptr(benchmark::State& state) {
46+
std::exception_ptr excptr = std::make_exception_ptr(42);
47+
for (auto _ : state) {
48+
benchmark::DoNotOptimize(excptr);
49+
benchmark::DoNotOptimize(exception_ptr_moves_copies_swap(excptr));
50+
}
51+
}
52+
BENCHMARK(bm_exception_ptr);
53+
2154
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)