[libcxx-commits] [libcxx] [libc++] Refactor std::sort_heap benchmark (PR #180941)

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


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

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 ~80 seconds per run.


>From 2fbca9296b601bd696c59aa15a46084b63a198cf Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Wed, 11 Feb 2026 14:21:17 +0100
Subject: [PATCH] [libc++] Refactor std::sort_heap benchmark

---
 .../algorithms/ranges_sort_heap.bench.cpp     | 36 ---------
 .../benchmarks/algorithms/sort_heap.bench.cpp | 36 ---------
 .../algorithms/sorting/sort_heap.bench.cpp    | 74 +++++++++++++++++++
 3 files changed, 74 insertions(+), 72 deletions(-)
 delete mode 100644 libcxx/test/benchmarks/algorithms/ranges_sort_heap.bench.cpp
 delete mode 100644 libcxx/test/benchmarks/algorithms/sort_heap.bench.cpp
 create mode 100644 libcxx/test/benchmarks/algorithms/sorting/sort_heap.bench.cpp

diff --git a/libcxx/test/benchmarks/algorithms/ranges_sort_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/ranges_sort_heap.bench.cpp
deleted file mode 100644
index 90e9b9a6933f5..0000000000000
--- a/libcxx/test/benchmarks/algorithms/ranges_sort_heap.bench.cpp
+++ /dev/null
@@ -1,36 +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>
-struct RangesSortHeap {
-  size_t Quantity;
-
-  void run(benchmark::State& state) const {
-    runOpOnCopies<ValueType>(state, Quantity, Order::Heap, BatchSize::CountElements, [](auto& Copy) {
-      std::ranges::sort_heap(Copy);
-    });
-  }
-
-  std::string name() const { return "BM_RangesSortHeap" + ValueType::name() + "_" + std::to_string(Quantity); };
-};
-} // namespace
-
-int main(int argc, char** argv) {
-  benchmark::Initialize(&argc, argv);
-  if (benchmark::ReportUnrecognizedArguments(argc, argv))
-    return 1;
-  makeCartesianProductBenchmark<RangesSortHeap, AllValueTypes>(Quantities);
-  benchmark::RunSpecifiedBenchmarks();
-}
diff --git a/libcxx/test/benchmarks/algorithms/sort_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/sort_heap.bench.cpp
deleted file mode 100644
index 1ce9f1a6df9af..0000000000000
--- a/libcxx/test/benchmarks/algorithms/sort_heap.bench.cpp
+++ /dev/null
@@ -1,36 +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>
-struct SortHeap {
-  size_t Quantity;
-
-  void run(benchmark::State& state) const {
-    runOpOnCopies<ValueType>(state, Quantity, Order::Heap, BatchSize::CountElements, [](auto& Copy) {
-      std::sort_heap(Copy.begin(), Copy.end());
-    });
-  }
-
-  std::string name() const { return "BM_SortHeap" + ValueType::name() + "_" + std::to_string(Quantity); };
-};
-} // namespace
-
-int main(int argc, char** argv) {
-  benchmark::Initialize(&argc, argv);
-  if (benchmark::ReportUnrecognizedArguments(argc, argv))
-    return 1;
-  makeCartesianProductBenchmark<SortHeap, AllValueTypes>(Quantities);
-  benchmark::RunSpecifiedBenchmarks();
-}
diff --git a/libcxx/test/benchmarks/algorithms/sorting/sort_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/sorting/sort_heap.bench.cpp
new file mode 100644
index 0000000000000..51b0d0a000beb
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/sorting/sort_heap.bench.cpp
@@ -0,0 +1,74 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <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]);
+                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 gen2 = [](auto size) {
+      std::vector<int> data = support::heap_data<int>(size);
+      return std::vector<support::NonIntegral>(data.begin(), data.end());
+    };
+    bm.operator()<std::vector<int>>("std::sort_heap(vector<int>)", std::less{}, support::heap_data<int>);
+    bm.operator()<std::vector<support::NonIntegral>>("std::sort_heap(vector<NonIntegral>)", std::less{}, gen2);
+    bm.operator()<std::deque<int>>("std::sort_heap(deque<int>)", std::less{}, support::heap_data<int>);
+
+    auto pred = [](auto lhs, auto rhs) { return lhs < rhs; };
+    bm.operator()<std::vector<int>>("std::sort_heap(vector<int>, pred)", pred, support::heap_data<int>);
+    bm.operator()<std::vector<support::NonIntegral>>("std::sort_heap(vector<NonIntegral>, pred)", pred, gen2);
+    bm.operator()<std::deque<int>>("std::sort_heap(deque<int>, pred)", pred, support::heap_data<int>);
+  }
+
+  benchmark::Initialize(&argc, argv);
+  benchmark::RunSpecifiedBenchmarks();
+  benchmark::Shutdown();
+  return 0;
+}



More information about the libcxx-commits mailing list