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

libstdc++: Specialize _Iter_sink for ostreambuf_iterator

Optimizes formatting to `std::ostreambuf_iterator` by specializing `_Iter_sink` for bulk writes.

This commit optimizes the formatting library in libstdc++ by adding a partial specialization of _Iter_sink for std::ostreambuf_iterator. This specialization leverages bulk sputn writes and zero-copy put-area operations, replacing per-character sputc calls. It ensures that all counting and truncation logic is handled within the specialization, allowing the base _Streambuf_sink to remain a pure I/O layer and enabling accurate calculation of total output length.

In Details

A partial specialization of __format::_Iter_sink is introduced for std::ostreambuf_iterator<...>. This specialization inherits from __format::_Streambuf_sink but overrides sputc with sputn for bulk writes and uses zero-copy access to the streambuf's put area. This optimization is limited to std::char_traits and ensures that _M_max (maximum count) and error handling are managed correctly within the sink, allowing format_to_n to determine the total output length.

For Context
std::ostreambuf_iterator
An output iterator that writes characters to a std::basic_streambuf. It's often used for efficient character output.
_Iter_sink
An internal helper class in libstdc++'s formatting library responsible for writing formatted output to an iterator.
_Streambuf_sink
An internal helper class in libstdc++'s formatting library designed for writing formatted output directly to a std::basic_streambuf.
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.
Filed Under: libstdc++formattingoptimization