[libcxx-commits] [libcxx] [libc++] Rewrite the std::make_heap benchmark (PR #178696)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Feb 3 06:47:43 PST 2026
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/178696
>From 6c4326f880d6654f0ac101e17895452dd2ec6c28 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 29 Jan 2026 17:43:02 +0100
Subject: [PATCH] [libc++] Rewrite the std::make_heap benchmark
---
.../benchmarks/algorithms/make_heap.bench.cpp | 38 ---------
.../algorithms/ranges_make_heap.bench.cpp | 38 ---------
.../algorithms/ranges_pop_heap.bench.cpp | 38 ---------
.../algorithms/ranges_push_heap.bench.cpp | 42 ----------
.../sorting/heap_operations.bench.cpp | 84 +++++++++++++++++++
5 files changed, 84 insertions(+), 156 deletions(-)
delete mode 100644 libcxx/test/benchmarks/algorithms/make_heap.bench.cpp
delete mode 100644 libcxx/test/benchmarks/algorithms/ranges_make_heap.bench.cpp
delete mode 100644 libcxx/test/benchmarks/algorithms/ranges_pop_heap.bench.cpp
delete mode 100644 libcxx/test/benchmarks/algorithms/ranges_push_heap.bench.cpp
create mode 100644 libcxx/test/benchmarks/algorithms/sorting/heap_operations.bench.cpp
diff --git a/libcxx/test/benchmarks/algorithms/make_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/make_heap.bench.cpp
deleted file mode 100644
index 64d559620c512..0000000000000
--- a/libcxx/test/benchmarks/algorithms/make_heap.bench.cpp
+++ /dev/null
@@ -1,38 +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 MakeHeap {
- 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::string name() const {
- return "BM_MakeHeap" + 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<MakeHeap, AllValueTypes, AllOrders>(Quantities);
- benchmark::RunSpecifiedBenchmarks();
-}
diff --git a/libcxx/test/benchmarks/algorithms/ranges_make_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/ranges_make_heap.bench.cpp
deleted file mode 100644
index c04ea369fea0c..0000000000000
--- a/libcxx/test/benchmarks/algorithms/ranges_make_heap.bench.cpp
+++ /dev/null
@@ -1,38 +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 RangesMakeHeap {
- size_t Quantity;
-
- void run(benchmark::State& state) const {
- runOpOnCopies<ValueType>(state, Quantity, Order(), BatchSize::CountElements, [](auto& Copy) {
- std::ranges::make_heap(Copy);
- });
- }
-
- std::string name() const {
- return "BM_RangesMakeHeap" + 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<RangesMakeHeap, AllValueTypes, AllOrders>(Quantities);
- benchmark::RunSpecifiedBenchmarks();
-}
diff --git a/libcxx/test/benchmarks/algorithms/ranges_pop_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/ranges_pop_heap.bench.cpp
deleted file mode 100644
index ab3ae6f7c30ae..0000000000000
--- a/libcxx/test/benchmarks/algorithms/ranges_pop_heap.bench.cpp
+++ /dev/null
@@ -1,38 +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 RangesPopHeap {
- size_t Quantity;
-
- void run(benchmark::State& state) const {
- runOpOnCopies<ValueType>(state, Quantity, Order(), BatchSize::CountElements, [](auto& Copy) {
- for (auto B = Copy.begin(), I = Copy.end(); I != B; --I) {
- std::ranges::pop_heap(B, I);
- }
- });
- }
-
- std::string name() const { return "BM_RangesPopHeap" + 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<RangesPopHeap, AllValueTypes>(Quantities);
- benchmark::RunSpecifiedBenchmarks();
-}
diff --git a/libcxx/test/benchmarks/algorithms/ranges_push_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/ranges_push_heap.bench.cpp
deleted file mode 100644
index 8139ba32cb974..0000000000000
--- a/libcxx/test/benchmarks/algorithms/ranges_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 RangesPushHeap {
- 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::ranges::push_heap(Copy.begin(), I + 1);
- }
- });
- }
-
- bool skip() const { return Order() == ::Order::Heap; }
-
- std::string name() const {
- return "BM_RangesPushHeap" + 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<RangesPushHeap, AllValueTypes, AllOrders>(Quantities);
- benchmark::RunSpecifiedBenchmarks();
-}
diff --git a/libcxx/test/benchmarks/algorithms/sorting/heap_operations.bench.cpp b/libcxx/test/benchmarks/algorithms/sorting/heap_operations.bench.cpp
new file mode 100644
index 0000000000000..90bc8ee749eef
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/sorting/heap_operations.bench.cpp
@@ -0,0 +1,84 @@
+//===----------------------------------------------------------------------===//
+//
+// 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::make_heap 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 generate_data, auto pred) {
+ benchmark::RegisterBenchmark(
+ name,
+ [generate_data, pred](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::make_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);
+ return std::vector<support::NonIntegral>(data.begin(), data.end());
+ };
+ auto name = [variant](std::string op) { return op + " (" + variant + ")"; };
+ bm.operator()<std::vector<int>>(name("std::make_heap(vector<int>)"), generate, std::less{});
+ bm.operator()<std::vector<support::NonIntegral>>(name("std::make_heap(vector<NonIntegral>)"), gen2, std::less{});
+ bm.operator()<std::deque<int>>(name("std::make_heap(deque<int>)"), generate, std::less{});
+ auto pred = [](auto lhs, auto rhs) { return lhs < rhs; };
+ bm.operator()<std::vector<int>>(name("std::make_heap(vector<int>, pred)"), generate, pred);
+ bm.operator()<std::vector<support::NonIntegral>>(name("std::make_heap(vector<NonIntegral>, pred)"), gen2, pred);
+ bm.operator()<std::deque<int>>(name("std::make_heap(deque<int>, pred)"), generate, pred);
+ };
+
+ 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