https://gcc.gnu.org/g:2b369385ea28b3aceb7cd28a4c9a96a8e2609031
commit r17-5909-g2b369385ea28b3aceb7cd28a4c9a96a8e2609031 Author: Nathan Myers <[email protected]> Date: Fri Oct 23 16:22:53 2026 -0400 libstdc++: improve string construction from streambuf [PR112925] 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]. Future improvements might 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. Diff: --- libstdc++-v3/include/bits/basic_string.h | 12 +++++- libstdc++-v3/include/bits/basic_string.tcc | 20 +++++++++ .../basic_string/cons/char/from_sbufiter.cc | 24 +++++++++++ .../21_strings/basic_string/cons/from_range2.cc | 50 ++++++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index fd9419220a49..3748f4164bd2 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -826,7 +826,17 @@ _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(ranges::begin(__rg), _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 de82e9978888..b10563df73ad 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 && + is_same<_Traits, char_traits<_CharT>>::value) + { + auto __r = std::__copy_n_a(__beg, __capacity - __len, + _M_data() + __len, false); + __len = __r - data(); + // We continue using __beg after passing it to copy_n_a. + // That is permitted for streambuf iterators. + 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 000000000000..338dcbcdd27e --- /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 000000000000..7dd98b5c29b5 --- /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(); +}
