GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
libstdc++ Performance Win

libstdc++: Improve string construction from streambuf.

String construction from streambuf iterators is up to 3x faster using `sgetn`.

This commit optimizes string construction from streambuf iterators in libstdc++. By utilizing the sgetn member function, which efficiently reads character sequences, the construction process is up to three times faster for iterator pairs and ranges. This change includes new test cases to cover various construction scenarios, including those with sentinels and C++11 support.

In Details

This patch modifies basic_string construction in libstdc++ to use the streambuf::sgetn method when initializing from istreambuf_iterator ranges. Previously, these constructions were less efficient. The optimization targets the _M_construct internal function and range constructors in basic_string.h and basic_string.tcc, providing a significant performance improvement (up to 3x) for this specific initialization path. New test cases in testsuite/21_strings validate this functionality, including C++11 compatibility and sentinel usage.

For Context
libstdc++
The C++ standard library implementation used by GCC.
streambuf
An abstract base class in C++ that provides an interface for reading and writing character sequences, often used as the underlying buffer for stream classes like istream and ostream.
istreambuf_iterator
An input iterator that reads characters from a streambuf object.
sgetn
A member function of streambuf that reads a specified number of characters from the buffer into a character array.
sentinel
In C++ iterator concepts, a special value or iterator used to mark the end of a range, distinct from the actual end iterator.
PR112925
A bug report identifier referencing a specific issue in the GCC bug tracking system related to libstdc++ string construction.
Filed Under: libstdc++performancestring