[libcxx-commits] [libcxx] [libc++] Add benchmarks for std::replace_copy and std::replace_copy_if (PR #207070)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 14 05:52:02 PDT 2026


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

>From 59b8ac362ede7e106203ef43c453dc877e276369 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 1 Jul 2026 14:18:54 -0400
Subject: [PATCH 1/2] [libc++] Add benchmarks for std::replace_copy and
 std::replace_copy_if

Assisted by Claude
---
 .../modifying/replace_copy.bench.cpp          | 129 ++++++++++++++++++
 1 file changed, 129 insertions(+)
 create mode 100644 libcxx/test/benchmarks/algorithms/modifying/replace_copy.bench.cpp

diff --git a/libcxx/test/benchmarks/algorithms/modifying/replace_copy.bench.cpp b/libcxx/test/benchmarks/algorithms/modifying/replace_copy.bench.cpp
new file mode 100644
index 0000000000000..97117fc7ffec2
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/modifying/replace_copy.bench.cpp
@@ -0,0 +1,129 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <cstddef>
+#include <deque>
+#include <iterator>
+#include <list>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "../../GenerateInput.h"
+
+int main(int argc, char** argv) {
+  auto std_replace_copy = [](auto first, auto last, auto out, auto const& old_value, auto const& new_value) {
+    return std::replace_copy(first, last, out, old_value, new_value);
+  };
+  auto std_replace_copy_if = [](auto first, auto last, auto out, auto const& old_value, auto const& new_value) {
+    return std::replace_copy_if(
+        first,
+        last,
+        out,
+        [&](auto element) {
+          benchmark::DoNotOptimize(element);
+          return element == old_value;
+        },
+        new_value);
+  };
+
+  // Benchmark {std,ranges}::{replace_copy,replace_copy_if} on a sequence of the form xxxxxxxxxxyyyyyyyyyy
+  // where we replace the prefix of x's with z's.
+  {
+    auto bm = []<class Container>(std::string name, auto replace_copy) {
+      benchmark::RegisterBenchmark(
+          name,
+          [replace_copy](auto& st) {
+            std::size_t const size = st.range(0);
+            using ValueType        = typename Container::value_type;
+            Container c;
+            ValueType x = Generate<ValueType>::random();
+            ValueType y = random_different_from({x});
+            ValueType z = random_different_from({x, y});
+            std::fill_n(std::back_inserter(c), size / 2, x);
+            std::fill_n(std::back_inserter(c), size / 2, y);
+
+            std::vector<ValueType> out(size);
+
+            for ([[maybe_unused]] auto _ : st) {
+              benchmark::DoNotOptimize(c);
+              benchmark::DoNotOptimize(out);
+              benchmark::DoNotOptimize(x);
+              benchmark::DoNotOptimize(z);
+              auto result = replace_copy(c.begin(), c.end(), out.begin(), x, z);
+              benchmark::DoNotOptimize(result);
+            }
+          })
+          ->Arg(32)
+          ->Arg(50) // non power-of-two
+          ->Arg(1024)
+          ->Arg(8192);
+    };
+    // {std,ranges}::replace_copy
+    bm.operator()<std::vector<int>>("std::replace_copy(vector<int>) (prefix)", std_replace_copy);
+    bm.operator()<std::deque<int>>("std::replace_copy(deque<int>) (prefix)", std_replace_copy);
+    bm.operator()<std::list<int>>("std::replace_copy(list<int>) (prefix)", std_replace_copy);
+
+    // {std,ranges}::replace_copy_if
+    bm.operator()<std::vector<int>>("std::replace_copy_if(vector<int>) (prefix)", std_replace_copy_if);
+    bm.operator()<std::deque<int>>("std::replace_copy_if(deque<int>) (prefix)", std_replace_copy_if);
+    bm.operator()<std::list<int>>("std::replace_copy_if(list<int>) (prefix)", std_replace_copy_if);
+  }
+
+  // Benchmark {std,ranges}::{replace_copy,replace_copy_if} on a sequence of the form xyxyxyxyxyxyxyxyxyxy
+  // where we replace the x's with z's.
+  {
+    auto bm = []<class Container>(std::string name, auto replace_copy) {
+      benchmark::RegisterBenchmark(
+          name,
+          [replace_copy](auto& st) {
+            std::size_t const size = st.range(0);
+            using ValueType        = typename Container::value_type;
+            Container c;
+            ValueType x = Generate<ValueType>::random();
+            ValueType y = random_different_from({x});
+            ValueType z = random_different_from({x, y});
+            for (std::size_t i = 0; i != size; ++i) {
+              c.push_back(i % 2 == 0 ? x : y);
+            }
+
+            std::vector<ValueType> out(size);
+
+            for ([[maybe_unused]] auto _ : st) {
+              benchmark::DoNotOptimize(c);
+              benchmark::DoNotOptimize(out);
+              benchmark::DoNotOptimize(x);
+              benchmark::DoNotOptimize(z);
+              auto result = replace_copy(c.begin(), c.end(), out.begin(), x, z);
+              benchmark::DoNotOptimize(result);
+            }
+          })
+          ->Arg(32)
+          ->Arg(50) // non power-of-two
+          ->Arg(1024)
+          ->Arg(8192);
+    };
+    // {std,ranges}::replace_copy
+    bm.operator()<std::vector<int>>("std::replace_copy(vector<int>) (sprinkled)", std_replace_copy);
+    bm.operator()<std::deque<int>>("std::replace_copy(deque<int>) (sprinkled)", std_replace_copy);
+    bm.operator()<std::list<int>>("std::replace_copy(list<int>) (sprinkled)", std_replace_copy);
+
+    // {std,ranges}::replace_copy_if
+    bm.operator()<std::vector<int>>("std::replace_copy_if(vector<int>) (sprinkled)", std_replace_copy_if);
+    bm.operator()<std::deque<int>>("std::replace_copy_if(deque<int>) (sprinkled)", std_replace_copy_if);
+    bm.operator()<std::list<int>>("std::replace_copy_if(list<int>) (sprinkled)", std_replace_copy_if);
+  }
+
+  benchmark::Initialize(&argc, argv);
+  benchmark::RunSpecifiedBenchmarks();
+  benchmark::Shutdown();
+  return 0;
+}

>From d8d4a1bf8ede10bf4da2feaf7f8e172972b7e3ae Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 14 Jul 2026 08:51:51 -0400
Subject: [PATCH 2/2] Comments from other review

---
 .../algorithms/modifying/replace_copy.bench.cpp    | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)

diff --git a/libcxx/test/benchmarks/algorithms/modifying/replace_copy.bench.cpp b/libcxx/test/benchmarks/algorithms/modifying/replace_copy.bench.cpp
index 97117fc7ffec2..0cad92d3b5ef3 100644
--- a/libcxx/test/benchmarks/algorithms/modifying/replace_copy.bench.cpp
+++ b/libcxx/test/benchmarks/algorithms/modifying/replace_copy.bench.cpp
@@ -24,15 +24,7 @@ int main(int argc, char** argv) {
     return std::replace_copy(first, last, out, old_value, new_value);
   };
   auto std_replace_copy_if = [](auto first, auto last, auto out, auto const& old_value, auto const& new_value) {
-    return std::replace_copy_if(
-        first,
-        last,
-        out,
-        [&](auto element) {
-          benchmark::DoNotOptimize(element);
-          return element == old_value;
-        },
-        new_value);
+    return std::replace_copy_if(first, last, out, [&](auto element) { return element == old_value; }, new_value);
   };
 
   // Benchmark {std,ranges}::{replace_copy,replace_copy_if} on a sequence of the form xxxxxxxxxxyyyyyyyyyy
@@ -53,7 +45,7 @@ int main(int argc, char** argv) {
 
             std::vector<ValueType> out(size);
 
-            for ([[maybe_unused]] auto _ : st) {
+            for (auto _ : st) {
               benchmark::DoNotOptimize(c);
               benchmark::DoNotOptimize(out);
               benchmark::DoNotOptimize(x);
@@ -97,7 +89,7 @@ int main(int argc, char** argv) {
 
             std::vector<ValueType> out(size);
 
-            for ([[maybe_unused]] auto _ : st) {
+            for (auto _ : st) {
               benchmark::DoNotOptimize(c);
               benchmark::DoNotOptimize(out);
               benchmark::DoNotOptimize(x);



More information about the libcxx-commits mailing list