Re: [PATCH WIP 2/2] libstdc++: Optimize ranges::distance for segmented iterators [PR123211]

Patrick Palka <[email protected]>
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.libstdc++.devel
Message-ID <dea8c173-3855-61a1-45c0-2a8fa1d04aac@idea>
On Fri, 31 Jul 2026, Jonathan Wakely wrote:

> On Tue, 14 Jul 2026 at 05:30, Patrick Palka <[email protected]> wrote:
> >
> > For segmented iterators, ranges::distance is equivalent to the sum of
> > ranges::distance of each of its segments.
> 
> There's no [PATCH v2 2/2] that supersedes this, right?
> 
> OK for trunk if this is the right one.

Yes -- series pushed just now, thanks!

> 
> 
> >
> >         PR libstdc++/123211
> >
> > libstdc++-v3/ChangeLog:
> >
> >         * include/bits/ranges_base.h (__distance_fn::operator()): For
> >         the non-sized-sentinel overload,
> > ---
> >  libstdc++-v3/include/bits/ranges_base.h | 10 ++++++++++
> >  1 file changed, 10 insertions(+)
> >
> > diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h
> > index ce1c40fc17f6..7f169feaa6b1 100644
> > --- a/libstdc++-v3/include/bits/ranges_base.h
> > +++ b/libstdc++-v3/include/bits/ranges_base.h
> > @@ -994,6 +994,16 @@ namespace ranges
> >        constexpr iter_difference_t<_It>
> >        operator()[[nodiscard]](_It __first, _Sent __last) const
> >        {
> > +       if constexpr (__segmented_iterator<_It> && same_as<_It, _Sent>)
> > +         {
> > +           iter_difference_t<_It> __n = 0;
> > +           std::__for_each_segment(__first, __last, [this, &__n](auto __first, auto __last) {
> > +             __n += iter_difference_t<_It>(this->operator()(__first, __last));
> > +             return __last;
> > +           });
> > +           return __n;
> > +         }
> > +
> >         iter_difference_t<_It> __n = 0;
> >         while (__first != __last)
> >           {
> > --
> > 2.55.0.141.g55526a1826
> >
> 
>
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.