[PATCH v2] libstdc++: improve string construction from streambuf [PR112925]

Tomasz Kaminski tkaminsk@redhat.com
Tue Oct 27 17:46:52 GMT 2026


On Tue, Oct 27, 2026 at 6:02 PM Nathan Myers <ncm@cantrip.org> wrote:

> Changes in v2:
>  - When constructing from a streambuf iterator range, ensure its
>   sentinel is another streambuf iterator, or default_sentinel.
>  - Add #pragma diagnostic push for "if constexpr" in c++11.
>  - Add c++11 test for construction from raw iterators.
>  - Add test with a sentinel in place of default-ctor'd iterator.
>  - Add test with range pipeline.
>  - Remove spurious 'constexpr' in test.
>
> Constructing a string from a streambuf iterator pair or range has
> been found to be 3x faster using the streambuf member `sgetn`.
> This is the minimal implementation as outlined in [PR112925].
>
> Possible future improvements include doing the same optimization
> when appending, and preallocating when the final size can be
> known.
>
> libstdc++-v3/ChangeLog:
>         PR libstdc++/112925
>         * include/bits/basic_string.h: (basic_string range ctor)
>         Special-case ranges of streambuf iterators.
>         * include/bits/basic_string.tcc: (_M_construct) Special-case
>         streambuf iterators. These always consume the whole stream.
>         * testsuite/21_strings/basic_string/cons/char/from_sbufiter.cc:
>         New test, construction from two istreambuf_iterators.
>         * testsuite/21_strings/basic_string/cons/from_range2.cc:
>         New tests, construction from ranges.
> ---
>  libstdc++-v3/include/bits/basic_string.h      | 11 +++-
>  libstdc++-v3/include/bits/basic_string.tcc    | 20 ++++++++
>  .../basic_string/cons/char/from_sbufiter.cc   | 24 +++++++++
>  .../basic_string/cons/from_range2.cc          | 50 +++++++++++++++++++
>  4 files changed, 104 insertions(+), 1 deletion(-)
>  create mode 100644
> libstdc++-v3/testsuite/21_strings/basic_string/cons/char/from_sbufiter.cc
>  create mode 100644
> libstdc++-v3/testsuite/21_strings/basic_string/cons/from_range2.cc
>
> diff --git a/libstdc++-v3/include/bits/basic_string.h
> b/libstdc++-v3/include/bits/basic_string.h
> index fd9419220a4..6886c7b2632 100644
> --- a/libstdc++-v3/include/bits/basic_string.h
> +++ b/libstdc++-v3/include/bits/basic_string.h
> @@ -826,7 +826,16 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
>         basic_string(from_range_t, _Rg&& __rg, const _Alloc& __a =
> _Alloc())
>         : basic_string(__a)
>         {
> -         if constexpr (ranges::forward_range<_Rg> ||
> ranges::sized_range<_Rg>)
> +         using _SbufIter = istreambuf_iterator<_CharT,
> char_traits<_CharT>>;
> +         if constexpr (requires {
> +           requires is_same_v<ranges::iterator_t<_Rg>, _SbufIter>;
> +           requires is_same_v<ranges::sentinel_t<_Rg>, _SbufIter> ||
> +                    is_same_v<ranges::sentinel_t<_Rg>,
> default_sentinel_t>; })
> +           {
> +             _M_construct(__rg.begin(), _SbufIter(),
> input_iterator_tag{});
> +           }
> +         else if constexpr (
> +             ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
>             {
>               const auto __n =
> static_cast<size_type>(ranges::distance(__rg));
>               reserve(__n);
> diff --git a/libstdc++-v3/include/bits/basic_string.tcc
> b/libstdc++-v3/include/bits/basic_string.tcc
> index de82e997888..e8ed7b82bc4 100644
> --- a/libstdc++-v3/include/bits/basic_string.tcc
> +++ b/libstdc++-v3/include/bits/basic_string.tcc
> @@ -199,6 +199,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>
>         _M_init_local_buf();
>
> +       // NB: size() (i.e. _M_length) is initialized only at the end.
>         while (__beg != __end && __len < __capacity)
>           {
>             _M_local_buf[__len++] = *__beg;
> @@ -228,6 +229,25 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>                 _M_data(__another.__ptr);
>                 _M_capacity(__capacity);
>               }
> +
> +#if __cplusplus >= 201103L
> +# pragma GCC diagnostic push
> +# pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
> +
> +           using _SbufIter = istreambuf_iterator<_CharT,
> char_traits<_CharT>>;
> +           if constexpr (is_same<_InIterator, _SbufIter>::value)
> +             {
> +               if (!std::__is_constant_evaluated())
>
Do you need above condition? istreambuf_iterator at not usable at compile
time in the first place.
I also think that we need to check if traits_type is same as
allocator_traits<_CharT> as we are not
caling assign. Outside of that this LGTM

> +                {
> +                  auto __r = std::__copy_n_a(
> +                    __beg, __capacity - __len, _M_data() + __len, false);
> +                  __len = __r - data();
> +                  continue;
> +                }
> +             }
> +
> +# pragma GCC diagnostic pop
> +#endif
>             traits_type::assign(_M_data()[__len++],
>                                 static_cast<_CharT>(*__beg));
>             ++__beg;
> diff --git
> a/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/from_sbufiter.cc
> b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/from_sbufiter.cc
> new file mode 100644
> index 00000000000..338dcbcdd27
> --- /dev/null
> +++
> b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/from_sbufiter.cc
> @@ -0,0 +1,24 @@
> +// { dg-do run { target c++11 } }
> +
> +#include <string>
> +#include <sstream>
> +#include <iterator>
> +#include <streambuf>
> +#include <testsuite_hooks.h>
> +
> +std::string abcs{"abcdefghijklmnopqrstuvwxyz"};
> +std::string twice{abcs + " " + abcs};
> +
> +void
> +test_iters()
> +{
> +  std::istringstream stream{twice};
> +  std::istreambuf_iterator<char> iter{stream}, end;
> +  std::string str{iter, end};
> +  VERIFY(str == twice);
> +}
> +
> +int main()
> +{
> +  test_iters();
> +}
> diff --git
> a/libstdc++-v3/testsuite/21_strings/basic_string/cons/from_range2.cc
> b/libstdc++-v3/testsuite/21_strings/basic_string/cons/from_range2.cc
> new file mode 100644
> index 00000000000..7dd98b5c29b
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/from_range2.cc
> @@ -0,0 +1,50 @@
> +// { dg-do run { target c++23 } }
> +
> +#include <string>
> +#include <sstream>
> +#include <iterator>
> +#include <streambuf>
> +#include <ranges>
> +#include <testsuite_hooks.h>
> +
> +std::string abcs{"abcdefghijklmnopqrstuvwxyz"};
> +std::string twice{abcs + " " + abcs};
> +
> +void
> +test_range()
> +{
> +  std::istringstream stream{twice};
> +  std::istreambuf_iterator<char> it{stream}, end;
> +  std::ranges::subrange range{it, end};
> +  std::basic_string str{std::from_range, range};
> +  VERIFY(str == twice);
> +}
> +
> +void
> +test_sentinel()
> +{
> +  std::istringstream stream{twice};
> +  std::istreambuf_iterator<char> it{stream}, end;
> +  std::ranges::subrange range{it, std::default_sentinel};
> +  std::basic_string str{std::from_range, range};
> +  VERIFY(str == twice);
> +}
> +
> +void
> +test_take()
> +{
> +  std::istringstream stream{twice};
> +  std::istreambuf_iterator<char> it{stream}, end;
> +  std::ranges::subrange range{it, std::default_sentinel};
> +  std::basic_string str{std::from_range,
> +    range | std::views::take_while([](char c){ return c != ' '; }) };
> +  VERIFY(str == abcs);
> +}
> +
> +
> +int main()
> +{
> +  test_range();
> +  test_sentinel();
> +  test_take();
> +}
> --
> 2.56.0
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20261027/81fc76f8/attachment.htm>


More information about the Libstdc++ mailing list