[libcxx-commits] [libcxx] [libc++] Refactor std::push_heap benchmark (PR #181343)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Feb 13 02:07:25 PST 2026
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/181343
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 ~8 minutes per run.
Fixes #177028
>From f411d106f656bcb58436506acdc2d21b5f6ee8de Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Fri, 13 Feb 2026 11:02:55 +0100
Subject: [PATCH] [libc++] Refactor std::push_heap benchmark
---
.../benchmarks/algorithms/push_heap.bench.cpp | 42 ---------
.../algorithms/sorting/push_heap.bench.cpp | 90 +++++++++++++++++++
2 files changed, 90 insertions(+), 42 deletions(-)
delete mode 100644 libcxx/test/benchmarks/algorithms/push_heap.bench.cpp
create mode 100644 libcxx/test/benchmarks/algorithms/sorting/push_heap.bench.cpp
diff --git a/libcxx/test/benchmarks/algorithms/push_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/push_heap.bench.cpp
deleted file mode 100644
index 7dfa0285348bd..0000000000000
--- a/libcxx/test/benchmarks/algorithms/push_heap.bench.cpp
+++ /dev/null
@@ -1,42 +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 PushHeap {
- size_t Quantity;
-
- void run(benchmark::State& state) const {
- runOpOnCopies<ValueType>(state, Quantity, Order(), BatchSize::CountElements, [](auto& Copy) {
- for (auto I = Copy.begin(), E = Copy.end(); I != E; ++I) {
- std::push_heap(Copy.begin(), I + 1);
- }
- });
- }
-
- bool skip() const { return Order() == ::Order::Heap; }
-
- std::string name() const {
- return "BM_PushHeap" + 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<PushHeap, AllValueTypes, AllOrders>(Quantities);
- benchmark::RunSpecifiedBenchmarks();
-}
diff --git a/libcxx/test/benchmarks/algorithms/sorting/push_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/sorting/push_heap.bench.cpp
new file mode 100644
index 0000000000000..6d74a4a0795b5
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/sorting/push_heap.bench.cpp
@@ -0,0 +1,90 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 * size)) {
+ for (std::size_t i = 0; i != BatchSize; ++i) {
+ benchmark::DoNotOptimize(c[i]);
+ for (size_t k = 0; k != c[i].size(); ++k)
+ std::push_heap(c[i].begin(), c[i].begin() + k);
+ 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::push_heap(vector<int>)"), std::less{}, generate);
+ bm.operator()<std::vector<support::NonIntegral>>(name("std::push_heap(vector<NonIntegral>)"), std::less{}, gen2);
+ bm.operator()<std::deque<int>>(name("std::push_heap(deque<int>)"), std::less{}, generate);
+
+ auto pred = [](auto lhs, auto rhs) { return lhs < rhs; };
+ bm.operator()<std::vector<int>>(name("std::push_heap(vector<int>, pred)"), pred, generate);
+ bm.operator()<std::vector<support::NonIntegral>>(name("std::push_heap(vector<NonIntegral>, pred)"), pred, gen2);
+ bm.operator()<std::deque<int>>(name("std::push_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