[libcxx-commits] [libcxx] [libc++] Add benchmark for `std::exception_ptr` (PR #164278)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Oct 20 11:09:50 PDT 2025


================
@@ -18,4 +18,49 @@ void bm_make_exception_ptr(benchmark::State& state) {
 }
 BENCHMARK(bm_make_exception_ptr)->ThreadRange(1, 8);
 
+static bool exception_ptr_moves_copies_swap(std::exception_ptr p1) {
+  // Taken from https://github.com/llvm/llvm-project/issues/44892
+  std::exception_ptr p2(p1);            // Copy constructor
+  std::exception_ptr p3(std::move(p2)); // Move constructor
+  p2 = std::move(p1);                   // Move assignment
+  p1 = p2;                              // Copy assignment
+  swap(p1, p2);                         // Swap
+  // Comparisons against nullptr. The overhead from creating temporary `exception_ptr`
+  // instances should be optimized out.
+  bool is_null  = p1 == nullptr && nullptr == p2;
+  bool is_equal = p1 == p2; // Comparison
+  return is_null && is_equal;
+}
+
+void bm_nonnull_exception_ptr(benchmark::State& state) {
+  std::exception_ptr excptr = std::make_exception_ptr(42);
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(excptr);
+    benchmark::DoNotOptimize(exception_ptr_moves_copies_swap(excptr));
+  }
+}
+BENCHMARK(bm_nonnull_exception_ptr);
+
+void bm_null_exception_ptr(benchmark::State& state) {
+  std::exception_ptr excptr;
+  for (auto _ : state) {
+    // All of the `exception_ptr_noops` are no-ops but the optimizer
+    // cannot optimize them away, because the `DoNotOptimize` calls
+    // prevent the optimizer from doing so.
+    benchmark::DoNotOptimize(excptr);
+    benchmark::DoNotOptimize(exception_ptr_moves_copies_swap(excptr));
+  }
+}
+BENCHMARK(bm_null_exception_ptr);
+
+void bm_optimized_null_exception_ptr(benchmark::State& state) {
----------------
ldionne wrote:

Can you explain the purpose of these benchmarks? Perhaps a comment above each would be helpful.

Are these benchmarks going to be useful going forward (e.g. preventing regressions), or are they only useful in the context of comparing stuff as you're actively working on the subsequent patch? `bm_optimized_null_exception_ptr` seems like it might only be useful to establish a baseline, but might not provide value on its own (but I don't fully understand its purpose yet)?

https://github.com/llvm/llvm-project/pull/164278


More information about the libcxx-commits mailing list