[libcxx-commits] [libcxx] [libc++] Optimize ranges::{for_each, for_each_n} for segmented iterators (PR #132896)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 28 20:06:30 PDT 2025


================

----------------
winner245 wrote:

I found a better approach to completely avoid the additional traversal of the whole sequence for `join_view` in the `_n` algorithms. We can actually process segment by segment without needing to first determining the `last` iterator for the entire sequence. Previously, determining the `last` iterator was necessary because `std::__for_each_segment` only accepts a pair of iterator inputs. I've implemented a new `std::__for_each_n_segment` algorithm which takes the `first` iterator and a count value `n`, eliminating the need for the `last` iterator. This algorithm is better suited for the `for_each_n` algorithms, leading to a speedup for `join_view` similar to that of `deque`. I've updated the PR description with complete benchmarks. 

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


More information about the libcxx-commits mailing list