[libcxx-commits] [libcxx] [libc++] Rewrite the std::pop_heap benchmark (PR #179911)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Feb 9 06:48:52 PST 2026
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/179911
>From e6bbeb258550b12474d588c463299f59d3ea9cd9 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 5 Feb 2026 12:17:30 +0100
Subject: [PATCH] [libc++] Rewrite the std::pop_heap benchmark
---
libcxx/test/benchmarks/GenerateInput.h | 15 +++++
.../benchmarks/algorithms/pop_heap.bench.cpp | 38 ------------
.../algorithms/sorting/pop_heap.bench.cpp | 59 +++++++++++++++++++
3 files changed, 74 insertions(+), 38 deletions(-)
delete mode 100644 libcxx/test/benchmarks/algorithms/pop_heap.bench.cpp
create mode 100644 libcxx/test/benchmarks/algorithms/sorting/pop_heap.bench.cpp
diff --git a/libcxx/test/benchmarks/GenerateInput.h b/libcxx/test/benchmarks/GenerateInput.h
index d07cb8857579d..516ea77b93e12 100644
--- a/libcxx/test/benchmarks/GenerateInput.h
+++ b/libcxx/test/benchmarks/GenerateInput.h
@@ -40,6 +40,12 @@ inline IntT getRandomInteger(IntT Min, IntT Max) {
return static_cast<IntT>(dist(getRandomEngine()));
}
+template <class FloatT>
+inline FloatT getRandomFloat(FloatT Min, FloatT Max) {
+ std::uniform_real_distribution<FloatT> dist(Min, Max);
+ return static_cast<FloatT>(dist(getRandomEngine()));
+}
+
inline std::string getRandomString(std::size_t Len) {
std::string str(Len, 0);
std::generate_n(str.begin(), Len, &getRandomChar);
@@ -194,6 +200,15 @@ struct Generate<T> {
static T random() { return getRandomInteger<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::max()); }
};
+template <class T>
+ requires std::floating_point<T>
+struct Generate<T> {
+ static T arbitrary() { return 42; }
+ static T cheap() { return 42; }
+ static T expensive() { return 42; }
+ static T random() { return getRandomFloat<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::max()); }
+};
+
template <>
struct Generate<std::string> {
static std::string arbitrary() { return "hello world"; }
diff --git a/libcxx/test/benchmarks/algorithms/pop_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/pop_heap.bench.cpp
deleted file mode 100644
index e4b96a0ae48c7..0000000000000
--- a/libcxx/test/benchmarks/algorithms/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 PopHeap {
- 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::pop_heap(B, I);
- }
- });
- }
-
- std::string name() const { return "BM_PopHeap" + 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<PopHeap, AllValueTypes>(Quantities);
- benchmark::RunSpecifiedBenchmarks();
-}
diff --git a/libcxx/test/benchmarks/algorithms/sorting/pop_heap.bench.cpp b/libcxx/test/benchmarks/algorithms/sorting/pop_heap.bench.cpp
new file mode 100644
index 0000000000000..d545daa1241d2
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/sorting/pop_heap.bench.cpp
@@ -0,0 +1,59 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <deque>
+#include <iterator>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "../../GenerateInput.h"
+
+int main(int argc, char** argv) {
+ auto bm = []<class Container>(std::type_identity<Container>, std::string name) {
+ benchmark::RegisterBenchmark(
+ name,
+ [](benchmark::State& state) {
+ std::size_t size = state.range(0);
+
+ Container c;
+ std::generate_n(std::back_inserter(c), size, [] {
+ return Generate<typename Container::value_type>::random();
+ });
+
+ while (state.KeepRunningBatch(size)) {
+ state.PauseTiming();
+ std::make_heap(c.begin(), c.end());
+ state.ResumeTiming();
+
+ for (auto first = c.begin(), last = c.end(); last != first; --last) {
+ std::pop_heap(first, last);
+ }
+ }
+ })
+ ->Arg(8)
+ ->Arg(1024)
+ ->Arg(8192);
+ };
+
+ bm(std::type_identity<std::vector<int>>{}, "std::pop_heap(vector<int>)");
+ bm(std::type_identity<std::vector<float>>{}, "std::pop_heap(vector<float>)");
+ bm(std::type_identity<std::vector<size_t>>{}, "std::pop_heap(vector<size_t>)");
+ bm(std::type_identity<std::vector<std::string>>{}, "std::pop_heap(vector<std::string>)");
+ bm(std::type_identity<std::deque<int>>{}, "std::pop_heap(deque<int>)");
+ bm(std::type_identity<std::deque<float>>{}, "std::pop_heap(deque<float>)");
+ bm(std::type_identity<std::deque<size_t>>{}, "std::pop_heap(deque<size_t>)");
+ bm(std::type_identity<std::deque<std::string>>{}, "std::pop_heap(deque<std::string>)");
+
+ benchmark::Initialize(&argc, argv);
+ benchmark::RunSpecifiedBenchmarks();
+ benchmark::Shutdown();
+ return 0;
+}
More information about the libcxx-commits
mailing list