[libcxx-commits] [libcxx] 8c93fb0 - [libc++] Refactor benchmarking std::make_heap and std::sort_heap together (#180935)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Feb 11 10:17:48 PST 2026
Author: Nikolas Klauser
Date: 2026-02-11T13:17:44-05:00
New Revision: 8c93fb0c05fc12b7f6bfa2b55b969d567aeb0537
URL: https://github.com/llvm/llvm-project/commit/8c93fb0c05fc12b7f6bfa2b55b969d567aeb0537
DIFF: https://github.com/llvm/llvm-project/commit/8c93fb0c05fc12b7f6bfa2b55b969d567aeb0537.diff
LOG: [libc++] Refactor benchmarking std::make_heap and std::sort_heap together (#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.
Added:
libcxx/test/benchmarks/algorithms/sorting/make_heap_then_sort_heap.bench.cpp
Modified:
Removed:
libcxx/test/benchmarks/algorithms/make_heap_then_sort_heap.bench.cpp
libcxx/test/benchmarks/algorithms/ranges_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