[Bug libstdc++/112925] Optimize std::string construction from std::istreambuf_iterator

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue May 26 16:11:57 GMT 2026


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112925

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #0)
> +                 auto __r = std::__copy_n_a(__beg, capacity() - size(),
> +                                            data() + size(), false);

This uses an optimized overload of __copy_n_a that takes istreambuf_iterator,
which just uses streambuf::sgetn to extract directly to the string's char
buffer.

For the basic_string(from_range_t, R&&) constructor we don't have an optimized
ranges::copy_n for streambuf iterators (see Bug 111053).  But the string
from_range ctor can write its own loop using streambuf::sgetn.

We can also consider using streambuf::showmanyc() to find how many characters
the streambuf has available. For filebuf that can tell us the total number of
bytes so we can size the string correctly in one allocation.


More information about the Gcc-bugs mailing list