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

libstdc++: Add _Streambuf_sink for direct streambuf formatting

Introduces `_Streambuf_sink` for efficient direct formatting to `std::basic_streambuf`.

This commit introduces a new class, _Streambuf_sink, to libstdc++‘s formatting library. This class enables direct writing to std::basic_streambuf using sputn, prioritizing zero-copy writes into the streambuf’s put area. It also incorporates internal tracking of I/O errors to ensure that format-phase exceptions propagate correctly without setting badbit prematurely. The sink is specialized for char_traits<_CharT> to guarantee the presence of the __safe_pbump member.

In Details

The __format::_Streambuf_sink class template is introduced to write directly to std::basic_streambuf. It uses sputn and favors zero-copy writes via pptr/epptr/pbump. Stream errors are tracked internally via _M_write_failed to allow format exceptions (like bad_alloc) to propagate before setting badbit, adhering to [ostream.formatted.print]/(4.2). This sink is specialized for char_traits<_CharT> to ensure __safe_pbump is available.

For Context
_Streambuf_sink
An internal helper class in libstdc++'s formatting library designed for writing formatted output directly to a std::basic_streambuf.
std::basic_streambuf
The base class for stream buffer classes in C++. It manages the flow of characters between character sequences and stream objects.
sputn
A protected member function of std::basic_streambuf that writes a block of characters to the stream buffer.
zero-copy
A technique that avoids redundant data copying by allowing direct access to memory, in this case, the streambuf's internal buffer.
badbit
A stream state flag indicating that an error has occurred during an input or output operation.
Filed Under: libstdc++formattingoptimization