[libcxx-commits] [libcxx] [libc++] Add benchmarks for partitioning algorithms (PR #127324)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 19 13:57:19 PST 2025


https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/127324

>From 831002f04b96c4a7918e99f243fc1a1845fa22ba Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Sat, 15 Feb 2025 11:01:00 +0100
Subject: [PATCH 1/3] [libc++] Add benchmarks for partitioning algorithms

This patch adds benchmarks for std::partition, is_partitioned, etc
and their ranges:: variants.
---
 .../algorithms.partition_point.bench.cpp      | 131 ------------------
 .../partitions/is_partitioned.bench.cpp       |  94 +++++++++++++
 .../algorithms/partitions/partition.bench.cpp |  77 ++++++++++
 .../partitions/partition_copy.bench.cpp       |  74 ++++++++++
 .../partitions/partition_point.bench.cpp      |  73 ++++++++++
 .../partitions/stable_partition.bench.cpp     |  79 +++++++++++
 6 files changed, 397 insertions(+), 131 deletions(-)
 delete mode 100644 libcxx/test/benchmarks/algorithms/algorithms.partition_point.bench.cpp
 create mode 100644 libcxx/test/benchmarks/algorithms/partitions/is_partitioned.bench.cpp
 create mode 100644 libcxx/test/benchmarks/algorithms/partitions/partition.bench.cpp
 create mode 100644 libcxx/test/benchmarks/algorithms/partitions/partition_copy.bench.cpp
 create mode 100644 libcxx/test/benchmarks/algorithms/partitions/partition_point.bench.cpp
 create mode 100644 libcxx/test/benchmarks/algorithms/partitions/stable_partition.bench.cpp

diff --git a/libcxx/test/benchmarks/algorithms/algorithms.partition_point.bench.cpp b/libcxx/test/benchmarks/algorithms/algorithms.partition_point.bench.cpp
deleted file mode 100644
index e0bd7e36f78ad..0000000000000
--- a/libcxx/test/benchmarks/algorithms/algorithms.partition_point.bench.cpp
+++ /dev/null
@@ -1,131 +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 <array>
-#include <cassert>
-#include <cstdint>
-#include <tuple>
-#include <vector>
-
-#include "benchmark/benchmark.h"
-
-#include "../CartesianBenchmarks.h"
-#include "../GenerateInput.h"
-
-namespace {
-
-template <typename I, typename N>
-std::array<I, 10> every_10th_percentile_N(I first, N n) {
-  N step = n / 10;
-  std::array<I, 10> res;
-
-  for (size_t i = 0; i < 10; ++i) {
-    res[i] = first;
-    std::advance(first, step);
-  }
-
-  return res;
-}
-
-template <class IntT>
-struct TestIntBase {
-  static std::vector<IntT> generateInput(size_t size) {
-    std::vector<IntT> Res(size);
-    std::generate(Res.begin(), Res.end(), [] { return getRandomInteger<IntT>(0, std::numeric_limits<IntT>::max()); });
-    return Res;
-  }
-};
-
-struct TestInt32 : TestIntBase<std::int32_t> {
-  static constexpr const char* Name = "TestInt32";
-};
-
-struct TestInt64 : TestIntBase<std::int64_t> {
-  static constexpr const char* Name = "TestInt64";
-};
-
-struct TestUint32 : TestIntBase<std::uint32_t> {
-  static constexpr const char* Name = "TestUint32";
-};
-
-struct TestMediumString {
-  static constexpr const char* Name  = "TestMediumString";
-  static constexpr size_t StringSize = 32;
-
-  static std::vector<std::string> generateInput(size_t size) {
-    std::vector<std::string> Res(size);
-    std::generate(Res.begin(), Res.end(), [] { return getRandomString(StringSize); });
-    return Res;
-  }
-};
-
-using AllTestTypes = std::tuple<TestInt32, TestInt64, TestUint32, TestMediumString>;
-
-struct LowerBoundAlg {
-  template <class I, class V>
-  I operator()(I first, I last, const V& value) const {
-    return std::lower_bound(first, last, value);
-  }
-
-  static constexpr const char* Name = "LowerBoundAlg";
-};
-
-struct UpperBoundAlg {
-  template <class I, class V>
-  I operator()(I first, I last, const V& value) const {
-    return std::upper_bound(first, last, value);
-  }
-
-  static constexpr const char* Name = "UpperBoundAlg";
-};
-
-struct EqualRangeAlg {
-  template <class I, class V>
-  std::pair<I, I> operator()(I first, I last, const V& value) const {
-    return std::equal_range(first, last, value);
-  }
-
-  static constexpr const char* Name = "EqualRangeAlg";
-};
-
-using AllAlgs = std::tuple<LowerBoundAlg, UpperBoundAlg, EqualRangeAlg>;
-
-template <class Alg, class TestType>
-struct PartitionPointBench {
-  size_t Quantity;
-
-  std::string name() const {
-    return std::string("PartitionPointBench_") + Alg::Name + "_" + TestType::Name + '/' + std::to_string(Quantity);
-  }
-
-  void run(benchmark::State& state) const {
-    auto Data = TestType::generateInput(Quantity);
-    std::sort(Data.begin(), Data.end());
-    auto Every10Percentile = every_10th_percentile_N(Data.begin(), Data.size());
-
-    for (auto _ : state) {
-      for (auto Test : Every10Percentile)
-        benchmark::DoNotOptimize(Alg{}(Data.begin(), Data.end(), *Test));
-    }
-  }
-};
-
-} // namespace
-
-int main(int argc, char** argv) {
-  benchmark::Initialize(&argc, argv);
-  if (benchmark::ReportUnrecognizedArguments(argc, argv))
-    return 1;
-
-  const std::vector<size_t> Quantities = {1 << 8, 1 << 10, 1 << 20};
-  makeCartesianProductBenchmark<PartitionPointBench, AllAlgs, AllTestTypes>(Quantities);
-  benchmark::RunSpecifiedBenchmarks();
-}
diff --git a/libcxx/test/benchmarks/algorithms/partitions/is_partitioned.bench.cpp b/libcxx/test/benchmarks/algorithms/partitions/is_partitioned.bench.cpp
new file mode 100644
index 0000000000000..55f8f7dcb990a
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/partitions/is_partitioned.bench.cpp
@@ -0,0 +1,94 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <cassert>
+#include <deque>
+#include <iterator>
+#include <list>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "../../GenerateInput.h"
+
+auto compute_median(auto first, auto last) {
+  std::vector v(first, last);
+  auto middle = v.begin() + v.size() / 2;
+  std::nth_element(v.begin(), middle, v.end());
+  return *middle;
+}
+
+template <class Container, bool Partitioned, class Operation>
+void bm(std::string operation_name, Operation is_partitioned) {
+  auto bench = [is_partitioned](auto& st) {
+    auto const size = st.range(0);
+    using ValueType = typename Container::value_type;
+    Container c;
+    std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+
+    // Partition the container in two equally-sized halves, ensuring the median
+    // value appears in the left half. Note that the median value isn't located
+    // in the middle -- this isn't std::nth_element.
+    ValueType median = compute_median(c.begin(), c.end());
+    auto pred        = [median](auto const& element) { return element <= median; };
+    std::partition(c.begin(), c.end(), pred);
+    assert(std::is_partitioned(c.begin(), c.end(), pred));
+
+    if constexpr (!Partitioned) {
+      // De-partition the container by swapping the element containing the median
+      // value with the last one.
+      auto median_it = std::find(c.begin(), c.end(), median);
+      auto last_it   = std::next(c.begin(), c.size() - 1);
+      std::iter_swap(median_it, last_it);
+      assert(!std::is_partitioned(c.begin(), c.end(), pred));
+    }
+
+    for ([[maybe_unused]] auto _ : st) {
+      auto result = is_partitioned(c.begin(), c.end(), pred);
+      benchmark::DoNotOptimize(result);
+      benchmark::DoNotOptimize(c);
+      benchmark::ClobberMemory();
+    }
+  };
+  benchmark::RegisterBenchmark(operation_name, bench)->Arg(32)->Arg(1024)->Arg(8192);
+}
+
+int main(int argc, char** argv) {
+  auto std_is_partitioned    = [](auto first, auto last, auto pred) { return std::is_partitioned(first, last, pred); };
+  auto ranges_is_partitioned = [](auto first, auto last, auto pred) {
+    return std::ranges::is_partitioned(first, last, pred);
+  };
+
+  // std::is_partitioned
+  bm<std::vector<int>, true>("std::is_partitioned(vector<int>) (partitioned)", std_is_partitioned);
+  bm<std::vector<int>, false>("std::is_partitioned(vector<int>) (not partitioned)", std_is_partitioned);
+
+  bm<std::deque<int>, true>("std::is_partitioned(deque<int>) (partitioned)", std_is_partitioned);
+  bm<std::deque<int>, false>("std::is_partitioned(deque<int>) (not partitioned)", std_is_partitioned);
+
+  bm<std::list<int>, true>("std::is_partitioned(list<int>) (partitioned)", std_is_partitioned);
+  bm<std::list<int>, false>("std::is_partitioned(list<int>) (not partitioned)", std_is_partitioned);
+
+  // ranges::is_partitioned
+  bm<std::vector<int>, true>("ranges::is_partitioned(vector<int>) (partitioned)", ranges_is_partitioned);
+  bm<std::vector<int>, false>("ranges::is_partitioned(vector<int>) (not partitioned)", ranges_is_partitioned);
+
+  bm<std::deque<int>, true>("ranges::is_partitioned(deque<int>) (partitioned)", ranges_is_partitioned);
+  bm<std::deque<int>, false>("ranges::is_partitioned(deque<int>) (not partitioned)", ranges_is_partitioned);
+
+  bm<std::list<int>, true>("ranges::is_partitioned(list<int>) (partitioned)", ranges_is_partitioned);
+  bm<std::list<int>, false>("ranges::is_partitioned(list<int>) (not partitioned)", ranges_is_partitioned);
+
+  benchmark::Initialize(&argc, argv);
+  benchmark::RunSpecifiedBenchmarks();
+  benchmark::Shutdown();
+  return 0;
+}
diff --git a/libcxx/test/benchmarks/algorithms/partitions/partition.bench.cpp b/libcxx/test/benchmarks/algorithms/partitions/partition.bench.cpp
new file mode 100644
index 0000000000000..4e2cd3170fc6b
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/partitions/partition.bench.cpp
@@ -0,0 +1,77 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <list>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "../../GenerateInput.h"
+
+auto compute_median(auto first, auto last) {
+  std::vector v(first, last);
+  auto middle = v.begin() + v.size() / 2;
+  std::nth_element(v.begin(), middle, v.end());
+  return *middle;
+}
+
+template <class Container, class Operation>
+void bm(std::string operation_name, Operation partition) {
+  auto bench = [partition](auto& st) {
+    auto const size = st.range(0);
+    using ValueType = typename Container::value_type;
+    Container c;
+    std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+
+    std::vector<ValueType> yes(size), no(size);
+    ValueType median = compute_median(c.begin(), c.end());
+    auto pred1       = [median](auto const& element) { return element < median; };
+    auto pred2       = [median](auto const& element) { return element > median; };
+    bool toggle      = false;
+
+    for ([[maybe_unused]] auto _ : st) {
+      if (toggle) {
+        auto result = partition(c.begin(), c.end(), pred1);
+        benchmark::DoNotOptimize(result);
+      } else {
+        auto result = partition(c.begin(), c.end(), pred2);
+        benchmark::DoNotOptimize(result);
+      }
+      toggle = !toggle;
+
+      benchmark::DoNotOptimize(c);
+      benchmark::ClobberMemory();
+    }
+  };
+  benchmark::RegisterBenchmark(operation_name, bench)->Arg(32)->Arg(1024)->Arg(8192);
+}
+
+int main(int argc, char** argv) {
+  auto std_partition    = [](auto first, auto last, auto pred) { return std::partition(first, last, pred); };
+  auto ranges_partition = [](auto first, auto last, auto pred) { return std::ranges::partition(first, last, pred); };
+
+  // std::partition
+  bm<std::vector<int>>("std::partition(vector<int>)", std_partition);
+  bm<std::deque<int>>("std::partition(deque<int>)", std_partition);
+  bm<std::list<int>>("std::partition(list<int>)", std_partition);
+
+  // ranges::partition
+  bm<std::vector<int>>("ranges::partition(vector<int>)", ranges_partition);
+  bm<std::deque<int>>("ranges::partition(deque<int>)", ranges_partition);
+  bm<std::list<int>>("ranges::partition(list<int>)", ranges_partition);
+
+  benchmark::Initialize(&argc, argv);
+  benchmark::RunSpecifiedBenchmarks();
+  benchmark::Shutdown();
+  return 0;
+}
diff --git a/libcxx/test/benchmarks/algorithms/partitions/partition_copy.bench.cpp b/libcxx/test/benchmarks/algorithms/partitions/partition_copy.bench.cpp
new file mode 100644
index 0000000000000..68f3fa027169e
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/partitions/partition_copy.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 <deque>
+#include <iterator>
+#include <list>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "../../GenerateInput.h"
+
+auto compute_median(auto first, auto last) {
+  std::vector v(first, last);
+  auto middle = v.begin() + v.size() / 2;
+  std::nth_element(v.begin(), middle, v.end());
+  return *middle;
+}
+
+template <class Container, class Operation>
+void bm(std::string operation_name, Operation partition_copy) {
+  auto bench = [partition_copy](auto& st) {
+    auto const size = st.range(0);
+    using ValueType = typename Container::value_type;
+    Container c;
+    std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+
+    std::vector<ValueType> yes(size), no(size);
+    ValueType median = compute_median(c.begin(), c.end());
+    auto pred        = [median](auto const& element) { return element < median; };
+
+    for ([[maybe_unused]] auto _ : st) {
+      auto result = partition_copy(c.begin(), c.end(), yes.begin(), no.begin(), pred);
+      benchmark::DoNotOptimize(yes);
+      benchmark::DoNotOptimize(no);
+      benchmark::DoNotOptimize(result);
+      benchmark::DoNotOptimize(c);
+      benchmark::ClobberMemory();
+    }
+  };
+  benchmark::RegisterBenchmark(operation_name, bench)->Arg(32)->Arg(1024)->Arg(8192);
+}
+
+int main(int argc, char** argv) {
+  auto std_partition_copy = [](auto first, auto last, auto out_yes, auto out_no, auto pred) {
+    return std::partition_copy(first, last, out_yes, out_no, pred);
+  };
+  auto ranges_partition_copy = [](auto first, auto last, auto out_yes, auto out_no, auto pred) {
+    return std::ranges::partition_copy(first, last, out_yes, out_no, pred);
+  };
+
+  // std::partition_copy
+  bm<std::vector<int>>("std::partition_copy(vector<int>)", std_partition_copy);
+  bm<std::deque<int>>("std::partition_copy(deque<int>)", std_partition_copy);
+  bm<std::list<int>>("std::partition_copy(list<int>)", std_partition_copy);
+
+  // ranges::partition_copy
+  bm<std::vector<int>>("ranges::partition_copy(vector<int>)", ranges_partition_copy);
+  bm<std::deque<int>>("ranges::partition_copy(deque<int>)", ranges_partition_copy);
+  bm<std::list<int>>("ranges::partition_copy(list<int>)", ranges_partition_copy);
+
+  benchmark::Initialize(&argc, argv);
+  benchmark::RunSpecifiedBenchmarks();
+  benchmark::Shutdown();
+  return 0;
+}
diff --git a/libcxx/test/benchmarks/algorithms/partitions/partition_point.bench.cpp b/libcxx/test/benchmarks/algorithms/partitions/partition_point.bench.cpp
new file mode 100644
index 0000000000000..da810a59bff5b
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/partitions/partition_point.bench.cpp
@@ -0,0 +1,73 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <list>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "../../GenerateInput.h"
+
+auto compute_median(auto first, auto last) {
+  std::vector v(first, last);
+  auto middle = v.begin() + v.size() / 2;
+  std::nth_element(v.begin(), middle, v.end());
+  return *middle;
+}
+
+template <class Container, class Operation>
+void bm(std::string operation_name, Operation partition_point) {
+  auto bench = [partition_point](auto& st) {
+    auto const size = st.range(0);
+    using ValueType = typename Container::value_type;
+    Container c;
+    std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+
+    // Partition the container in two equally-sized halves. Based on experimentation, the running
+    // time of the algorithm doesn't change much depending on the size of the halves.
+    ValueType median = compute_median(c.begin(), c.end());
+    auto pred        = [median](auto const& element) { return element < median; };
+    std::partition(c.begin(), c.end(), pred);
+    assert(std::is_partitioned(c.begin(), c.end(), pred));
+
+    for ([[maybe_unused]] auto _ : st) {
+      auto result = partition_point(c.begin(), c.end(), pred);
+      benchmark::DoNotOptimize(result);
+      benchmark::DoNotOptimize(c);
+      benchmark::ClobberMemory();
+    }
+  };
+  benchmark::RegisterBenchmark(operation_name, bench)->Arg(32)->Arg(1024)->Arg(8192);
+}
+
+int main(int argc, char** argv) {
+  auto std_partition_point = [](auto first, auto last, auto pred) { return std::partition_point(first, last, pred); };
+  auto ranges_partition_point = [](auto first, auto last, auto pred) {
+    return std::ranges::partition_point(first, last, pred);
+  };
+
+  // std::partition_point
+  bm<std::vector<int>>("std::partition_point(vector<int>)", std_partition_point);
+  bm<std::deque<int>>("std::partition_point(deque<int>)", std_partition_point);
+  bm<std::list<int>>("std::partition_point(list<int>)", std_partition_point);
+
+  // ranges::partition_point
+  bm<std::vector<int>>("ranges::partition_point(vector<int>)", ranges_partition_point);
+  bm<std::deque<int>>("ranges::partition_point(deque<int>)", ranges_partition_point);
+  bm<std::list<int>>("ranges::partition_point(list<int>)", ranges_partition_point);
+
+  benchmark::Initialize(&argc, argv);
+  benchmark::RunSpecifiedBenchmarks();
+  benchmark::Shutdown();
+  return 0;
+}
diff --git a/libcxx/test/benchmarks/algorithms/partitions/stable_partition.bench.cpp b/libcxx/test/benchmarks/algorithms/partitions/stable_partition.bench.cpp
new file mode 100644
index 0000000000000..60e723a3a29d8
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/partitions/stable_partition.bench.cpp
@@ -0,0 +1,79 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <list>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "../../GenerateInput.h"
+
+auto compute_median(auto first, auto last) {
+  std::vector v(first, last);
+  auto middle = v.begin() + v.size() / 2;
+  std::nth_element(v.begin(), middle, v.end());
+  return *middle;
+}
+
+template <class Container, class Operation>
+void bm(std::string operation_name, Operation stable_partition) {
+  auto bench = [stable_partition](auto& st) {
+    auto const size = st.range(0);
+    using ValueType = typename Container::value_type;
+    Container c;
+    std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+
+    std::vector<ValueType> yes(size), no(size);
+    ValueType median = compute_median(c.begin(), c.end());
+    auto pred1       = [median](auto const& element) { return element < median; };
+    auto pred2       = [median](auto const& element) { return element > median; };
+    bool toggle      = false;
+
+    for ([[maybe_unused]] auto _ : st) {
+      if (toggle) {
+        auto result = stable_partition(c.begin(), c.end(), pred1);
+        benchmark::DoNotOptimize(result);
+      } else {
+        auto result = stable_partition(c.begin(), c.end(), pred2);
+        benchmark::DoNotOptimize(result);
+      }
+      toggle = !toggle;
+
+      benchmark::DoNotOptimize(c);
+      benchmark::ClobberMemory();
+    }
+  };
+  benchmark::RegisterBenchmark(operation_name, bench)->Arg(32)->Arg(1024)->Arg(8192);
+}
+
+int main(int argc, char** argv) {
+  auto std_stable_partition = [](auto first, auto last, auto pred) { return std::stable_partition(first, last, pred); };
+  auto ranges_stable_partition = [](auto first, auto last, auto pred) {
+    return std::ranges::stable_partition(first, last, pred);
+  };
+
+  // std::stable_partition
+  bm<std::vector<int>>("std::stable_partition(vector<int>)", std_stable_partition);
+  bm<std::deque<int>>("std::stable_partition(deque<int>)", std_stable_partition);
+  bm<std::list<int>>("std::stable_partition(list<int>)", std_stable_partition);
+
+  // ranges::stable_partition
+  bm<std::vector<int>>("ranges::stable_partition(vector<int>)", ranges_stable_partition);
+  bm<std::deque<int>>("ranges::stable_partition(deque<int>)", ranges_stable_partition);
+  bm<std::list<int>>("ranges::stable_partition(list<int>)", ranges_stable_partition);
+
+  benchmark::Initialize(&argc, argv);
+  benchmark::RunSpecifiedBenchmarks();
+  benchmark::Shutdown();
+  return 0;
+}

>From 4ee13a4ead5356c458730a47e60633ed87d6ea56 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Sun, 16 Feb 2025 00:46:50 +0100
Subject: [PATCH 2/3] Fix modulemap

---
 libcxx/include/module.modulemap | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index b0720703bd0de..1a173c3c0a7f4 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -673,6 +673,7 @@ module std [system] {
     }
     module ranges_partition {
       header "__algorithm/ranges_partition.h"
+      export std.ranges.subrange // return type
     }
     module ranges_pop_heap {
       header "__algorithm/ranges_pop_heap.h"
@@ -765,6 +766,7 @@ module std [system] {
     }
     module ranges_stable_partition {
       header "__algorithm/ranges_stable_partition.h"
+      export std.ranges.subrange // return type
     }
     module ranges_stable_sort {
       header "__algorithm/ranges_stable_sort.h"

>From d3c1f5bece972ea85fab0eaf8fcee44d99ce1eab Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 19 Feb 2025 16:57:08 -0500
Subject: [PATCH 3/3] Refactor benchmarks per other PRs

---
 .../partitions/is_partitioned.bench.cpp       | 101 +++++++++---------
 .../algorithms/partitions/partition.bench.cpp |  73 ++++++-------
 .../partitions/partition_copy.bench.cpp       |  64 +++++------
 .../partitions/partition_point.bench.cpp      |  64 +++++------
 .../partitions/stable_partition.bench.cpp     |  75 +++++++------
 5 files changed, 189 insertions(+), 188 deletions(-)

diff --git a/libcxx/test/benchmarks/algorithms/partitions/is_partitioned.bench.cpp b/libcxx/test/benchmarks/algorithms/partitions/is_partitioned.bench.cpp
index 55f8f7dcb990a..e78a5fb55d614 100644
--- a/libcxx/test/benchmarks/algorithms/partitions/is_partitioned.bench.cpp
+++ b/libcxx/test/benchmarks/algorithms/partitions/is_partitioned.bench.cpp
@@ -10,6 +10,7 @@
 
 #include <algorithm>
 #include <cassert>
+#include <cstddef>
 #include <deque>
 #include <iterator>
 #include <list>
@@ -26,66 +27,66 @@ auto compute_median(auto first, auto last) {
   return *middle;
 }
 
-template <class Container, bool Partitioned, class Operation>
-void bm(std::string operation_name, Operation is_partitioned) {
-  auto bench = [is_partitioned](auto& st) {
-    auto const size = st.range(0);
-    using ValueType = typename Container::value_type;
-    Container c;
-    std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
-
-    // Partition the container in two equally-sized halves, ensuring the median
-    // value appears in the left half. Note that the median value isn't located
-    // in the middle -- this isn't std::nth_element.
-    ValueType median = compute_median(c.begin(), c.end());
-    auto pred        = [median](auto const& element) { return element <= median; };
-    std::partition(c.begin(), c.end(), pred);
-    assert(std::is_partitioned(c.begin(), c.end(), pred));
-
-    if constexpr (!Partitioned) {
-      // De-partition the container by swapping the element containing the median
-      // value with the last one.
-      auto median_it = std::find(c.begin(), c.end(), median);
-      auto last_it   = std::next(c.begin(), c.size() - 1);
-      std::iter_swap(median_it, last_it);
-      assert(!std::is_partitioned(c.begin(), c.end(), pred));
-    }
-
-    for ([[maybe_unused]] auto _ : st) {
-      auto result = is_partitioned(c.begin(), c.end(), pred);
-      benchmark::DoNotOptimize(result);
-      benchmark::DoNotOptimize(c);
-      benchmark::ClobberMemory();
-    }
-  };
-  benchmark::RegisterBenchmark(operation_name, bench)->Arg(32)->Arg(1024)->Arg(8192);
-}
-
 int main(int argc, char** argv) {
-  auto std_is_partitioned    = [](auto first, auto last, auto pred) { return std::is_partitioned(first, last, pred); };
-  auto ranges_is_partitioned = [](auto first, auto last, auto pred) {
-    return std::ranges::is_partitioned(first, last, pred);
+  auto std_is_partitioned = [](auto first, auto last, auto pred) { return std::is_partitioned(first, last, pred); };
+
+  auto bm = []<class Container, bool Partitioned>(std::string name, auto is_partitioned) {
+    benchmark::RegisterBenchmark(
+        name,
+        [is_partitioned](auto& st) {
+          std::size_t const size = st.range(0);
+          using ValueType        = typename Container::value_type;
+          Container c;
+          std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+
+          // Partition the container in two equally-sized halves, ensuring the median
+          // value appears in the left half. Note that the median value isn't located
+          // in the middle -- this isn't std::nth_element.
+          ValueType median = compute_median(c.begin(), c.end());
+          auto pred        = [median](auto const& element) { return element <= median; };
+          std::partition(c.begin(), c.end(), pred);
+          assert(std::is_partitioned(c.begin(), c.end(), pred));
+
+          if constexpr (!Partitioned) {
+            // De-partition the container by swapping the element containing the median
+            // value with the last one.
+            auto median_it = std::find(c.begin(), c.end(), median);
+            auto last_it   = std::next(c.begin(), c.size() - 1);
+            std::iter_swap(median_it, last_it);
+            assert(!std::is_partitioned(c.begin(), c.end(), pred));
+          }
+
+          for ([[maybe_unused]] auto _ : st) {
+            benchmark::DoNotOptimize(c);
+            auto result = is_partitioned(c.begin(), c.end(), pred);
+            benchmark::DoNotOptimize(result);
+          }
+        })
+        ->Arg(32)
+        ->Arg(1024)
+        ->Arg(8192);
   };
 
   // std::is_partitioned
-  bm<std::vector<int>, true>("std::is_partitioned(vector<int>) (partitioned)", std_is_partitioned);
-  bm<std::vector<int>, false>("std::is_partitioned(vector<int>) (not partitioned)", std_is_partitioned);
+  bm.operator()<std::vector<int>, true>("std::is_partitioned(vector<int>) (partitioned)", std_is_partitioned);
+  bm.operator()<std::vector<int>, false>("std::is_partitioned(vector<int>) (unpartitioned)", std_is_partitioned);
 
-  bm<std::deque<int>, true>("std::is_partitioned(deque<int>) (partitioned)", std_is_partitioned);
-  bm<std::deque<int>, false>("std::is_partitioned(deque<int>) (not partitioned)", std_is_partitioned);
+  bm.operator()<std::deque<int>, true>("std::is_partitioned(deque<int>) (partitioned)", std_is_partitioned);
+  bm.operator()<std::deque<int>, false>("std::is_partitioned(deque<int>) (unpartitioned)", std_is_partitioned);
 
-  bm<std::list<int>, true>("std::is_partitioned(list<int>) (partitioned)", std_is_partitioned);
-  bm<std::list<int>, false>("std::is_partitioned(list<int>) (not partitioned)", std_is_partitioned);
+  bm.operator()<std::list<int>, true>("std::is_partitioned(list<int>) (partitioned)", std_is_partitioned);
+  bm.operator()<std::list<int>, false>("std::is_partitioned(list<int>) (unpartitioned)", std_is_partitioned);
 
   // ranges::is_partitioned
-  bm<std::vector<int>, true>("ranges::is_partitioned(vector<int>) (partitioned)", ranges_is_partitioned);
-  bm<std::vector<int>, false>("ranges::is_partitioned(vector<int>) (not partitioned)", ranges_is_partitioned);
+  bm.operator()<std::vector<int>, true>("rng::is_partitioned(vector<int>) (partitioned)", std::ranges::is_partitioned);
+  bm.operator()<std::vector<int>, false>(
+      "rng::is_partitioned(vector<int>) (unpartitioned)", std::ranges::is_partitioned);
 
-  bm<std::deque<int>, true>("ranges::is_partitioned(deque<int>) (partitioned)", ranges_is_partitioned);
-  bm<std::deque<int>, false>("ranges::is_partitioned(deque<int>) (not partitioned)", ranges_is_partitioned);
+  bm.operator()<std::deque<int>, true>("rng::is_partitioned(deque<int>) (partitioned)", std::ranges::is_partitioned);
+  bm.operator()<std::deque<int>, false>("rng::is_partitioned(deque<int>) (unpartitioned)", std::ranges::is_partitioned);
 
-  bm<std::list<int>, true>("ranges::is_partitioned(list<int>) (partitioned)", ranges_is_partitioned);
-  bm<std::list<int>, false>("ranges::is_partitioned(list<int>) (not partitioned)", ranges_is_partitioned);
+  bm.operator()<std::list<int>, true>("rng::is_partitioned(list<int>) (partitioned)", std::ranges::is_partitioned);
+  bm.operator()<std::list<int>, false>("rng::is_partitioned(list<int>) (unpartitioned)", std::ranges::is_partitioned);
 
   benchmark::Initialize(&argc, argv);
   benchmark::RunSpecifiedBenchmarks();
diff --git a/libcxx/test/benchmarks/algorithms/partitions/partition.bench.cpp b/libcxx/test/benchmarks/algorithms/partitions/partition.bench.cpp
index 4e2cd3170fc6b..8ac865e11e81f 100644
--- a/libcxx/test/benchmarks/algorithms/partitions/partition.bench.cpp
+++ b/libcxx/test/benchmarks/algorithms/partitions/partition.bench.cpp
@@ -9,6 +9,7 @@
 // UNSUPPORTED: c++03, c++11, c++14, c++17
 
 #include <algorithm>
+#include <cstddef>
 #include <deque>
 #include <iterator>
 #include <list>
@@ -25,50 +26,50 @@ auto compute_median(auto first, auto last) {
   return *middle;
 }
 
-template <class Container, class Operation>
-void bm(std::string operation_name, Operation partition) {
-  auto bench = [partition](auto& st) {
-    auto const size = st.range(0);
-    using ValueType = typename Container::value_type;
-    Container c;
-    std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+int main(int argc, char** argv) {
+  auto std_partition = [](auto first, auto last, auto pred) { return std::partition(first, last, pred); };
 
-    std::vector<ValueType> yes(size), no(size);
-    ValueType median = compute_median(c.begin(), c.end());
-    auto pred1       = [median](auto const& element) { return element < median; };
-    auto pred2       = [median](auto const& element) { return element > median; };
-    bool toggle      = false;
+  auto bm = []<class Container>(std::string name, auto partition) {
+    benchmark::RegisterBenchmark(
+        name,
+        [partition](auto& st) {
+          std::size_t const size = st.range(0);
+          using ValueType        = typename Container::value_type;
+          Container c;
+          std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
 
-    for ([[maybe_unused]] auto _ : st) {
-      if (toggle) {
-        auto result = partition(c.begin(), c.end(), pred1);
-        benchmark::DoNotOptimize(result);
-      } else {
-        auto result = partition(c.begin(), c.end(), pred2);
-        benchmark::DoNotOptimize(result);
-      }
-      toggle = !toggle;
+          std::vector<ValueType> yes(size), no(size);
+          ValueType median = compute_median(c.begin(), c.end());
+          auto pred1       = [median](auto const& element) { return element < median; };
+          auto pred2       = [median](auto const& element) { return element > median; };
+          bool toggle      = false;
 
-      benchmark::DoNotOptimize(c);
-      benchmark::ClobberMemory();
-    }
+          for ([[maybe_unused]] auto _ : st) {
+            benchmark::DoNotOptimize(c);
+            if (toggle) {
+              auto result = partition(c.begin(), c.end(), pred1);
+              benchmark::DoNotOptimize(result);
+            } else {
+              auto result = partition(c.begin(), c.end(), pred2);
+              benchmark::DoNotOptimize(result);
+            }
+            toggle = !toggle;
+          }
+        })
+        ->Arg(32)
+        ->Arg(1024)
+        ->Arg(8192);
   };
-  benchmark::RegisterBenchmark(operation_name, bench)->Arg(32)->Arg(1024)->Arg(8192);
-}
-
-int main(int argc, char** argv) {
-  auto std_partition    = [](auto first, auto last, auto pred) { return std::partition(first, last, pred); };
-  auto ranges_partition = [](auto first, auto last, auto pred) { return std::ranges::partition(first, last, pred); };
 
   // std::partition
-  bm<std::vector<int>>("std::partition(vector<int>)", std_partition);
-  bm<std::deque<int>>("std::partition(deque<int>)", std_partition);
-  bm<std::list<int>>("std::partition(list<int>)", std_partition);
+  bm.operator()<std::vector<int>>("std::partition(vector<int>)", std_partition);
+  bm.operator()<std::deque<int>>("std::partition(deque<int>)", std_partition);
+  bm.operator()<std::list<int>>("std::partition(list<int>)", std_partition);
 
   // ranges::partition
-  bm<std::vector<int>>("ranges::partition(vector<int>)", ranges_partition);
-  bm<std::deque<int>>("ranges::partition(deque<int>)", ranges_partition);
-  bm<std::list<int>>("ranges::partition(list<int>)", ranges_partition);
+  bm.operator()<std::vector<int>>("rng::partition(vector<int>)", std::ranges::partition);
+  bm.operator()<std::deque<int>>("rng::partition(deque<int>)", std::ranges::partition);
+  bm.operator()<std::list<int>>("rng::partition(list<int>)", std::ranges::partition);
 
   benchmark::Initialize(&argc, argv);
   benchmark::RunSpecifiedBenchmarks();
diff --git a/libcxx/test/benchmarks/algorithms/partitions/partition_copy.bench.cpp b/libcxx/test/benchmarks/algorithms/partitions/partition_copy.bench.cpp
index 68f3fa027169e..fd02c60db9dce 100644
--- a/libcxx/test/benchmarks/algorithms/partitions/partition_copy.bench.cpp
+++ b/libcxx/test/benchmarks/algorithms/partitions/partition_copy.bench.cpp
@@ -9,6 +9,7 @@
 // UNSUPPORTED: c++03, c++11, c++14, c++17
 
 #include <algorithm>
+#include <cstddef>
 #include <deque>
 #include <iterator>
 #include <list>
@@ -25,47 +26,46 @@ auto compute_median(auto first, auto last) {
   return *middle;
 }
 
-template <class Container, class Operation>
-void bm(std::string operation_name, Operation partition_copy) {
-  auto bench = [partition_copy](auto& st) {
-    auto const size = st.range(0);
-    using ValueType = typename Container::value_type;
-    Container c;
-    std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
-
-    std::vector<ValueType> yes(size), no(size);
-    ValueType median = compute_median(c.begin(), c.end());
-    auto pred        = [median](auto const& element) { return element < median; };
-
-    for ([[maybe_unused]] auto _ : st) {
-      auto result = partition_copy(c.begin(), c.end(), yes.begin(), no.begin(), pred);
-      benchmark::DoNotOptimize(yes);
-      benchmark::DoNotOptimize(no);
-      benchmark::DoNotOptimize(result);
-      benchmark::DoNotOptimize(c);
-      benchmark::ClobberMemory();
-    }
-  };
-  benchmark::RegisterBenchmark(operation_name, bench)->Arg(32)->Arg(1024)->Arg(8192);
-}
-
 int main(int argc, char** argv) {
   auto std_partition_copy = [](auto first, auto last, auto out_yes, auto out_no, auto pred) {
     return std::partition_copy(first, last, out_yes, out_no, pred);
   };
-  auto ranges_partition_copy = [](auto first, auto last, auto out_yes, auto out_no, auto pred) {
-    return std::ranges::partition_copy(first, last, out_yes, out_no, pred);
+
+  auto bm = []<class Container>(std::string name, auto partition_copy) {
+    benchmark::RegisterBenchmark(
+        name,
+        [partition_copy](auto& st) {
+          std::size_t const size = st.range(0);
+          using ValueType        = typename Container::value_type;
+          Container c;
+          std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+
+          std::vector<ValueType> yes(size), no(size);
+          ValueType median = compute_median(c.begin(), c.end());
+          auto pred        = [median](auto const& element) { return element < median; };
+
+          for ([[maybe_unused]] auto _ : st) {
+            benchmark::DoNotOptimize(c);
+            benchmark::DoNotOptimize(yes);
+            benchmark::DoNotOptimize(no);
+            auto result = partition_copy(c.begin(), c.end(), yes.begin(), no.begin(), pred);
+            benchmark::DoNotOptimize(result);
+          }
+        })
+        ->Arg(32)
+        ->Arg(1024)
+        ->Arg(8192);
   };
 
   // std::partition_copy
-  bm<std::vector<int>>("std::partition_copy(vector<int>)", std_partition_copy);
-  bm<std::deque<int>>("std::partition_copy(deque<int>)", std_partition_copy);
-  bm<std::list<int>>("std::partition_copy(list<int>)", std_partition_copy);
+  bm.operator()<std::vector<int>>("std::partition_copy(vector<int>)", std_partition_copy);
+  bm.operator()<std::deque<int>>("std::partition_copy(deque<int>)", std_partition_copy);
+  bm.operator()<std::list<int>>("std::partition_copy(list<int>)", std_partition_copy);
 
   // ranges::partition_copy
-  bm<std::vector<int>>("ranges::partition_copy(vector<int>)", ranges_partition_copy);
-  bm<std::deque<int>>("ranges::partition_copy(deque<int>)", ranges_partition_copy);
-  bm<std::list<int>>("ranges::partition_copy(list<int>)", ranges_partition_copy);
+  bm.operator()<std::vector<int>>("rng::partition_copy(vector<int>)", std::ranges::partition_copy);
+  bm.operator()<std::deque<int>>("rng::partition_copy(deque<int>)", std::ranges::partition_copy);
+  bm.operator()<std::list<int>>("rng::partition_copy(list<int>)", std::ranges::partition_copy);
 
   benchmark::Initialize(&argc, argv);
   benchmark::RunSpecifiedBenchmarks();
diff --git a/libcxx/test/benchmarks/algorithms/partitions/partition_point.bench.cpp b/libcxx/test/benchmarks/algorithms/partitions/partition_point.bench.cpp
index da810a59bff5b..b24e5261b1f0b 100644
--- a/libcxx/test/benchmarks/algorithms/partitions/partition_point.bench.cpp
+++ b/libcxx/test/benchmarks/algorithms/partitions/partition_point.bench.cpp
@@ -9,6 +9,7 @@
 // UNSUPPORTED: c++03, c++11, c++14, c++17
 
 #include <algorithm>
+#include <cstddef>
 #include <deque>
 #include <iterator>
 #include <list>
@@ -25,46 +26,45 @@ auto compute_median(auto first, auto last) {
   return *middle;
 }
 
-template <class Container, class Operation>
-void bm(std::string operation_name, Operation partition_point) {
-  auto bench = [partition_point](auto& st) {
-    auto const size = st.range(0);
-    using ValueType = typename Container::value_type;
-    Container c;
-    std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+int main(int argc, char** argv) {
+  auto std_partition_point = [](auto first, auto last, auto pred) { return std::partition_point(first, last, pred); };
 
-    // Partition the container in two equally-sized halves. Based on experimentation, the running
-    // time of the algorithm doesn't change much depending on the size of the halves.
-    ValueType median = compute_median(c.begin(), c.end());
-    auto pred        = [median](auto const& element) { return element < median; };
-    std::partition(c.begin(), c.end(), pred);
-    assert(std::is_partitioned(c.begin(), c.end(), pred));
+  auto bm = []<class Container>(std::string name, auto partition_point) {
+    benchmark::RegisterBenchmark(
+        name,
+        [partition_point](auto& st) {
+          std::size_t const size = st.range(0);
+          using ValueType        = typename Container::value_type;
+          Container c;
+          std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
 
-    for ([[maybe_unused]] auto _ : st) {
-      auto result = partition_point(c.begin(), c.end(), pred);
-      benchmark::DoNotOptimize(result);
-      benchmark::DoNotOptimize(c);
-      benchmark::ClobberMemory();
-    }
-  };
-  benchmark::RegisterBenchmark(operation_name, bench)->Arg(32)->Arg(1024)->Arg(8192);
-}
+          // Partition the container in two equally-sized halves. Based on experimentation, the running
+          // time of the algorithm doesn't change much depending on the size of the halves.
+          ValueType median = compute_median(c.begin(), c.end());
+          auto pred        = [median](auto const& element) { return element < median; };
+          std::partition(c.begin(), c.end(), pred);
+          assert(std::is_partitioned(c.begin(), c.end(), pred));
 
-int main(int argc, char** argv) {
-  auto std_partition_point = [](auto first, auto last, auto pred) { return std::partition_point(first, last, pred); };
-  auto ranges_partition_point = [](auto first, auto last, auto pred) {
-    return std::ranges::partition_point(first, last, pred);
+          for ([[maybe_unused]] auto _ : st) {
+            benchmark::DoNotOptimize(c);
+            auto result = partition_point(c.begin(), c.end(), pred);
+            benchmark::DoNotOptimize(result);
+          }
+        })
+        ->Arg(32)
+        ->Arg(1024)
+        ->Arg(8192);
   };
 
   // std::partition_point
-  bm<std::vector<int>>("std::partition_point(vector<int>)", std_partition_point);
-  bm<std::deque<int>>("std::partition_point(deque<int>)", std_partition_point);
-  bm<std::list<int>>("std::partition_point(list<int>)", std_partition_point);
+  bm.operator()<std::vector<int>>("std::partition_point(vector<int>)", std_partition_point);
+  bm.operator()<std::deque<int>>("std::partition_point(deque<int>)", std_partition_point);
+  bm.operator()<std::list<int>>("std::partition_point(list<int>)", std_partition_point);
 
   // ranges::partition_point
-  bm<std::vector<int>>("ranges::partition_point(vector<int>)", ranges_partition_point);
-  bm<std::deque<int>>("ranges::partition_point(deque<int>)", ranges_partition_point);
-  bm<std::list<int>>("ranges::partition_point(list<int>)", ranges_partition_point);
+  bm.operator()<std::vector<int>>("rng::partition_point(vector<int>)", std::ranges::partition_point);
+  bm.operator()<std::deque<int>>("rng::partition_point(deque<int>)", std::ranges::partition_point);
+  bm.operator()<std::list<int>>("rng::partition_point(list<int>)", std::ranges::partition_point);
 
   benchmark::Initialize(&argc, argv);
   benchmark::RunSpecifiedBenchmarks();
diff --git a/libcxx/test/benchmarks/algorithms/partitions/stable_partition.bench.cpp b/libcxx/test/benchmarks/algorithms/partitions/stable_partition.bench.cpp
index 60e723a3a29d8..b2e9fbc6c1325 100644
--- a/libcxx/test/benchmarks/algorithms/partitions/stable_partition.bench.cpp
+++ b/libcxx/test/benchmarks/algorithms/partitions/stable_partition.bench.cpp
@@ -9,6 +9,7 @@
 // UNSUPPORTED: c++03, c++11, c++14, c++17
 
 #include <algorithm>
+#include <cstddef>
 #include <deque>
 #include <iterator>
 #include <list>
@@ -25,52 +26,50 @@ auto compute_median(auto first, auto last) {
   return *middle;
 }
 
-template <class Container, class Operation>
-void bm(std::string operation_name, Operation stable_partition) {
-  auto bench = [stable_partition](auto& st) {
-    auto const size = st.range(0);
-    using ValueType = typename Container::value_type;
-    Container c;
-    std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+int main(int argc, char** argv) {
+  auto std_stable_partition = [](auto first, auto last, auto pred) { return std::stable_partition(first, last, pred); };
 
-    std::vector<ValueType> yes(size), no(size);
-    ValueType median = compute_median(c.begin(), c.end());
-    auto pred1       = [median](auto const& element) { return element < median; };
-    auto pred2       = [median](auto const& element) { return element > median; };
-    bool toggle      = false;
+  auto bm = []<class Container>(std::string name, auto stable_partition) {
+    benchmark::RegisterBenchmark(
+        name,
+        [stable_partition](auto& st) {
+          std::size_t const size = st.range(0);
+          using ValueType        = typename Container::value_type;
+          Container c;
+          std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
 
-    for ([[maybe_unused]] auto _ : st) {
-      if (toggle) {
-        auto result = stable_partition(c.begin(), c.end(), pred1);
-        benchmark::DoNotOptimize(result);
-      } else {
-        auto result = stable_partition(c.begin(), c.end(), pred2);
-        benchmark::DoNotOptimize(result);
-      }
-      toggle = !toggle;
+          std::vector<ValueType> yes(size), no(size);
+          ValueType median = compute_median(c.begin(), c.end());
+          auto pred1       = [median](auto const& element) { return element < median; };
+          auto pred2       = [median](auto const& element) { return element > median; };
+          bool toggle      = false;
 
-      benchmark::DoNotOptimize(c);
-      benchmark::ClobberMemory();
-    }
-  };
-  benchmark::RegisterBenchmark(operation_name, bench)->Arg(32)->Arg(1024)->Arg(8192);
-}
-
-int main(int argc, char** argv) {
-  auto std_stable_partition = [](auto first, auto last, auto pred) { return std::stable_partition(first, last, pred); };
-  auto ranges_stable_partition = [](auto first, auto last, auto pred) {
-    return std::ranges::stable_partition(first, last, pred);
+          for ([[maybe_unused]] auto _ : st) {
+            benchmark::DoNotOptimize(c);
+            if (toggle) {
+              auto result = stable_partition(c.begin(), c.end(), pred1);
+              benchmark::DoNotOptimize(result);
+            } else {
+              auto result = stable_partition(c.begin(), c.end(), pred2);
+              benchmark::DoNotOptimize(result);
+            }
+            toggle = !toggle;
+          }
+        })
+        ->Arg(32)
+        ->Arg(1024)
+        ->Arg(8192);
   };
 
   // std::stable_partition
-  bm<std::vector<int>>("std::stable_partition(vector<int>)", std_stable_partition);
-  bm<std::deque<int>>("std::stable_partition(deque<int>)", std_stable_partition);
-  bm<std::list<int>>("std::stable_partition(list<int>)", std_stable_partition);
+  bm.operator()<std::vector<int>>("std::stable_partition(vector<int>)", std_stable_partition);
+  bm.operator()<std::deque<int>>("std::stable_partition(deque<int>)", std_stable_partition);
+  bm.operator()<std::list<int>>("std::stable_partition(list<int>)", std_stable_partition);
 
   // ranges::stable_partition
-  bm<std::vector<int>>("ranges::stable_partition(vector<int>)", ranges_stable_partition);
-  bm<std::deque<int>>("ranges::stable_partition(deque<int>)", ranges_stable_partition);
-  bm<std::list<int>>("ranges::stable_partition(list<int>)", ranges_stable_partition);
+  bm.operator()<std::vector<int>>("rng::stable_partition(vector<int>)", std::ranges::stable_partition);
+  bm.operator()<std::deque<int>>("rng::stable_partition(deque<int>)", std::ranges::stable_partition);
+  bm.operator()<std::list<int>>("rng::stable_partition(list<int>)", std::ranges::stable_partition);
 
   benchmark::Initialize(&argc, argv);
   benchmark::RunSpecifiedBenchmarks();



More information about the libcxx-commits mailing list