[libcxx-commits] [libcxx] [libc++] Refactor benchmarking std::make_heap and std::sort_heap together (PR #180935)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 11 05:02:13 PST 2026


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/180935

We're trying to get the time it takes to run all the benchmarks down, so that we can run them on a regular basis. This patch saves us ~18  minutes per run.


>From 0d16506d7edf8d0261b85570fb4665ba86fd8248 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Wed, 11 Feb 2026 13:53:52 +0100
Subject: [PATCH] [libc++] Refactor benchmarking std::make_heap and
 std::sort_heap together

---
 .../make_heap_then_sort_heap.bench.cpp        | 39 --------
 .../ranges_make_heap_then_sort_heap.bench.cpp | 39 --------
 .../make_heap_then_sort_heap.bench.cpp        | 91 +++++++++++++++++++
 3 files changed, 91 insertions(+), 78 deletions(-)
 delete mode 100644 libcxx/test/benchmarks/algorithms/make_heap_then_sort_heap.bench.cpp
 delete mode 100644 libcxx/test/benchmarks/algorithms/ranges_make_heap_then_sort_heap.bench.cpp
 create mode 100644 libcxx/test/benchmarks/algorithms/sorting/make_heap_then_sort_heap.bench.cpp

diff --git a/libcxx/test/benchmarks/algorithms/make_heap_then_sort_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/make_heap_then_sort_heap.bench.cpp
deleted file mode 100644
index c6dc136be3ac4..0000000000000
--- a/libcxx/test/benchmarks/algorithms/make_heap_then_sort_heap.bench.cpp
+++ /dev/null
@@ -1,39 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17
-
-#include <algorithm>
-
-#include "common.h"
-
-namespace {
-template <class ValueType, class Order>
-struct MakeThenSortHeap {
-  size_t Quantity;
-
-  void run(benchmark::State& state) const {
-    runOpOnCopies<ValueType>(state, Quantity, Order(), BatchSize::CountElements, [](auto& Copy) {
-      std::make_heap(Copy.begin(), Copy.end());
-      std::sort_heap(Copy.begin(), Copy.end());
-    });
-  }
-
-  std::string name() const {
-    return "BM_MakeThenSortHeap" + ValueType::name() + Order::name() + "_" + std::to_string(Quantity);
-  };
-};
-} // namespace
-
-int main(int argc, char** argv) {
-  benchmark::Initialize(&argc, argv);
-  if (benchmark::ReportUnrecognizedArguments(argc, argv))
-    return 1;
-  makeCartesianProductBenchmark<MakeThenSortHeap, AllValueTypes, AllOrders>(Quantities);
-  benchmark::RunSpecifiedBenchmarks();
-}
diff --git a/libcxx/test/benchmarks/algorithms/ranges_make_heap_then_sort_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/ranges_make_heap_then_sort_heap.bench.cpp
deleted file mode 100644
index b84d3c2cea765..0000000000000
--- a/libcxx/test/benchmarks/algorithms/ranges_make_heap_then_sort_heap.bench.cpp
+++ /dev/null
@@ -1,39 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17
-
-#include <algorithm>
-
-#include "common.h"
-
-namespace {
-template <class ValueType, class Order>
-struct RangesMakeThenSortHeap {
-  size_t Quantity;
-
-  void run(benchmark::State& state) const {
-    runOpOnCopies<ValueType>(state, Quantity, Order(), BatchSize::CountElements, [](auto& Copy) {
-      std::ranges::make_heap(Copy);
-      std::ranges::sort_heap(Copy);
-    });
-  }
-
-  std::string name() const {
-    return "BM_RangesMakeThenSortHeap" + ValueType::name() + Order::name() + "_" + std::to_string(Quantity);
-  };
-};
-} // namespace
-
-int main(int argc, char** argv) {
-  benchmark::Initialize(&argc, argv);
-  if (benchmark::ReportUnrecognizedArguments(argc, argv))
-    return 1;
-  makeCartesianProductBenchmark<RangesMakeThenSortHeap, AllValueTypes, AllOrders>(Quantities);
-  benchmark::RunSpecifiedBenchmarks();
-}
diff --git a/libcxx/test/benchmarks/algorithms/sorting/make_heap_then_sort_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/sorting/make_heap_then_sort_heap.bench.cpp
new file mode 100644
index 0000000000000..9e0e612953407
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/sorting/make_heap_then_sort_heap.bench.cpp
@@ -0,0 +1,91 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <algorithm>
+#include <array>
+#include <cstddef>
+#include <deque>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "common.h"
+
+int main(int argc, char** argv) {
+  // Benchmark {std,ranges}::sort on various types of data
+  //
+  // We perform this benchmark in a batch because we need to restore the
+  // state of the container after the operation.
+  {
+    auto bm = []<class Container>(std::string name, auto pred, auto generate_data) {
+      benchmark::RegisterBenchmark(
+          name,
+          [pred, generate_data](auto& st) {
+            std::size_t const size          = st.range(0);
+            constexpr std::size_t BatchSize = 32;
+            using ValueType                 = typename Container::value_type;
+            std::vector<ValueType> data     = generate_data(size);
+            std::array<Container, BatchSize> c;
+            std::fill_n(c.begin(), BatchSize, Container(data.begin(), data.end()));
+
+            while (st.KeepRunningBatch(BatchSize)) {
+              for (std::size_t i = 0; i != BatchSize; ++i) {
+                benchmark::DoNotOptimize(c[i]);
+                sort(c[i].begin(), c[i].end());
+                std::make_heap(c[i].begin(), c[i].end(), pred);
+                std::sort_heap(c[i].begin(), c[i].end(), pred);
+                benchmark::DoNotOptimize(c[i]);
+              }
+
+              st.PauseTiming();
+              for (std::size_t i = 0; i != BatchSize; ++i) {
+                std::copy(data.begin(), data.end(), c[i].begin());
+              }
+              st.ResumeTiming();
+            }
+          })
+          ->Arg(8)
+          ->Arg(1024)
+          ->Arg(8192);
+    };
+
+    auto register_bm = [&](auto generate, std::string variant) {
+      auto gen2 = [generate](auto size) {
+        std::vector<int> data = generate(size);
+        std::vector<support::NonIntegral> real_data(data.begin(), data.end());
+        return real_data;
+      };
+      auto name = [variant](std::string op) { return op + " (" + variant + ")"; };
+      bm.operator()<std::vector<int>>(name("std::make_heap+std::sort_heap(vector<int>)"), std::less{}, generate);
+      bm.operator()<std::vector<support::NonIntegral>>(
+          name("std::make_heap+std::sort_heap(vector<NonIntegral>)"), std::less{}, gen2);
+      bm.operator()<std::deque<int>>(name("std::make_heap+std::sort_heap(deque<int>)"), std::less{}, generate);
+
+      auto pred = [](auto lhs, auto rhs) { return lhs < rhs; };
+      bm.operator()<std::vector<int>>(name("std::make_heap+std::sort_heap(vector<int>, pred)"), pred, generate);
+      bm.operator()<std::vector<support::NonIntegral>>(
+          name("std::make_heap+std::sort_heap(vector<NonIntegral>, pred)"), pred, gen2);
+      bm.operator()<std::deque<int>>(name("std::make_heap+std::sort_heap(deque<int>, pred)"), pred, generate);
+    };
+
+    register_bm(support::quicksort_adversarial_data<int>, "qsort adversarial");
+    register_bm(support::ascending_sorted_data<int>, "ascending");
+    register_bm(support::descending_sorted_data<int>, "descending");
+    register_bm(support::pipe_organ_data<int>, "pipe-organ");
+    register_bm(support::heap_data<int>, "heap");
+    register_bm(support::shuffled_data<int>, "shuffled");
+    register_bm(support::single_element_data<int>, "repeated");
+  }
+
+  benchmark::Initialize(&argc, argv);
+  benchmark::RunSpecifiedBenchmarks();
+  benchmark::Shutdown();
+  return 0;
+}



More information about the libcxx-commits mailing list