Re: [PATCH] libstdc++: optimize ranges::for_each for segmented iterators

Jonathan Wakely <[email protected]>
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.libstdc++.devel
Message-ID <CACb0b4nc+YrPx7K+BYM8XiBQoGXZ=A3AVh4r32QzeMauh=6WjQ@mail.gmail.com>
On Wed, 19 Aug 2026 at 17:23, Yuao Ma <[email protected]> wrote:
>
> Hi!
>
> Similar to std::for_each, this patch optimizes ranges::for_each for
> segmented iterators.

If I understand correctly, this will break cases that require
std::invoke to invoke the function object, e.g.

ranges::for_each(r, &T::f);

>
> Fully tested on x86_64-linux with no regressions.
>
> Using the newly added benchmark, it shows a 3x improvement when using
> ranges::for_each with std::deque.
>
> === Wed Aug 19 03:28:22 PM UTC 2026 ===
> for_each.cc               std::for_each vector<int>   2r    1u    0s
>       0mem    0pf
> for_each.cc               std::for_each deque<int>   2r    2u    0s
>      0mem    0pf
> for_each.cc               std::for_each list<int>    13r   14u    0s
>       0mem    0pf
> for_each.cc               std::ranges::for_each vector<int>   2r    2u
>    0s         0mem    0pf
> for_each.cc               std::ranges::for_each deque<int>   6r    5u
>   0s         0mem    0pf
> for_each.cc               std::ranges::for_each list<int>  13r   14u
>  0s         0mem    0pf
> === Wed Aug 19 04:09:51 PM UTC 2026 ===
> for_each.cc               std::for_each vector<int>   2r    1u    0s
>       0mem    0pf
> for_each.cc               std::for_each deque<int>   2r    2u    0s
>      0mem    0pf
> for_each.cc               std::for_each list<int>    13r   14u    0s
>       0mem    0pf
> for_each.cc               std::ranges::for_each vector<int>   2r    2u
>    0s         0mem    0pf
> for_each.cc               std::ranges::for_each deque<int>   2r    1u
>   0s         0mem    0pf
> for_each.cc               std::ranges::for_each list<int>  13r   14u
>  0s         0mem    0pf
>
> Please take a look when you are available, thanks!
>
> Note: after preparing this patch I found the -std=gnu++11 in the check
> performance script based on Jonathan's guidance. I can prepare a patch
> for this tomorrow and get rid of the STD in the benchmark.
>
> Yuao
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.