[libcxx-commits] [libcxx] [libc++] Optimize ranges::copy for random_access_iterator inputs (PR #120134)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jun 24 09:35:03 PDT 2025


================
@@ -50,6 +50,25 @@ constexpr auto wrap_input(std::vector<T>& input) {
   return std::ranges::subrange(std::move(b), std::move(e));
 }
 
+template <class Iter, class Sent>
+class random_access_range_wrapper {
----------------
ldionne wrote:

I think what I would do here is actually remove this type, and then do:

```c++
auto bm = [&generators, &bench_vb, &tostr]<template <class> class Iterator>(std::string range) {
  for (auto gen : generators)
    bench_vb("append_range(" + range + ")" + tostr(gen), [gen](auto& st) {
      auto const size = st.range(0);
      std::vector<int> in;
      std::generate_n(std::back_inserter(in), size, gen);
      std::ranges::subrange rg(Iterator(std::ranges::begin(in)), Iterator(std::ranges::end(in)));
      DoNotOptimizeData(in);

      Container c;
      for ([[maybe_unused]] auto _ : st) {
        c.append_range(rg);
        c.erase(c.begin(), c.end()); // avoid growing indefinitely
        DoNotOptimizeData(c);
      }
    });
};
bm.template operator()< cpp20_random_access_iterator >("ra_range");
```

Note that there was also a bug where you did `c.append_range(in)` instead of c.append_range(rg)`, making `rg` unused.

https://github.com/llvm/llvm-project/pull/120134


More information about the libcxx-commits mailing list