[RFC] std::inplace_vector::assign avoid double loops
Tomasz Kaminski
tkaminsk@redhat.com
Mon Sep 21 18:10:21 GMT 2026
Double loop allows us to check capacity (and throw) early and avoid
insertions when
range is forward.
On Mon, Sep 21, 2026 at 7:44 PM François Dumont <frs.dumont@gmail.com>
wrote:
> Hi
>
> I wonder what you think about this patch.
>
> Maybe this has already been discuss, sorry if so.
>
> If fine I'll try to isolate _S_distance in a testable helper.
>
> libstdc++: Do not loop several times on range given to
> std::inplace_vector::assign
>
> std::inplace_vector::assign first compute the distance between the
> input iterators
> before running the assignment loop. The distance is computed as
> long as the input
> range is a sized-sentinel or forward iterators. It means that when
> computing distance
> on a forward_list like in the added test the range will be loop
> through twice.
>
> As inplace_vector do not need reallocation to increase its capacity
> it seems more
> optimal to only get the distance if it can be done in O(1).
> Otherwise we just loop
> through the range and add elements using emplace_back.
>
> libstdc++-v3/ChangeLog:
>
> * include/std/inplace_vector (inplace_vector::_S_distance):
> Return a distance
> only if sized_sentinel_for or iterator is derived from
> random_access_iterator_tag.
> *
> testsuite/23_containers/inplace_vector/modifiers/assign_forward_list.cc:
> New test
> case. Only checking that the right thing is done but not
> that it is done with only
> one loop on the input range.
>
> Quickly tested with the added test case for now.
>
> François
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20260921/bb094a99/attachment.htm>
More information about the Libstdc++
mailing list