Jonathan Wakely 8bd872f1ea libstdc++: Implement P3107R5 optimizations for std::print [PR121790]
The names of the vprint functions follow the convention from P3235R3.

This takes advantage of the additional permission proposed by P3107R5 so
that std::print can write directly to a FILE stream, rather than
formatting to an intermediate std::string temporary and then writing
that to the stream. The change is to write to a new _File_sink type
instead of a _Str_sink that populates a std::string. There are three
implementations of _File_sink.

For non-Glibc targets that support POSIX flockfile and putc_unlocked,
the stream will be locked and then formatted characters will be buffered
on the stack (instead of allocating a std::string) and copied to the
stream when the buffer fills up.

For Glibc, _File_sink will lock the stream but then if the file is
line-buffered or fully buffered, characters will be written directly
into the file's output buffer. This avoids two levels of buffering and
copying the characters from one to the other. For an unbuffered stream
(like stderr) the _File_sink buffer will still be used, to avoid the
overhead of lots of small writes to the stream.  Because this version of
_File_sink accesses the stream's buffer directly it relies on
glibc-specific implementation details that are exposed in public
headers.

A fallback definition of _File_sink just wraps a _Str_sink so is
equivalent to the original code, and is used when flockfile isn't
available.

Both forms of std::println (taking a FILE* and a std::ostream) can be
implemented more efficiently by appending a newline to the format
string, to avoid formatting twice.

	PR libstdc++/121790

libstdc++-v3/ChangeLog:

	* acinclude.m4 (GLIBCXX_CHECK_STDIO_LOCKING): New macro to check
	for std::print dependencies.
	* config.h.in: Regenerate.
	* configure: Regenerate.
	* configure.ac: Use GLIBCXX_CHECK_STDIO_LOCKING.
	* include/bits/formatfwd.h (enable_nonlocking_formatter_optimization):
	Define new variable template.
	* include/bits/version.def (print): Bump value.
	* include/bits/version.h: Regenerate.
	* include/std/format (enable_nonlocking_formatter_optimization):
	Define specializations for variable template.
	* include/std/ostream (print) [!_WIN32]: Do not use
	vprint_unicode at all.
	(println): Append newline to format string instead of formatting
	twice.
	* include/std/print (_File_sink): New class.
	(vprint_nonunicode_locking): New function.
	(vprint_unicode_locking): New function reusing previous code
	from vprint_unicode.
	(vprintf_unicode): Defer to vprint_nonunicode for Windows or to
	vprint_unicode_locking otherwise.
	(print): [!_WIN32]: Do no use vprint_unicode at all.
	Check enable_nonlocking_formatter_optimization and defer to
	either vprint_nonunicode_locking or vprint_nonunicode.
	(println): Use vprint_unicode or format directly to a _File_sink
	instead of formatting twice.
	* testsuite/27_io/print/1.cc: Updated and added new tests.
	* testsuite/std/format/formatter/nonlocking.cc: New tests.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Co-authored-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-10-10 09:00:22 +02:00
2025-06-03 00:18:06 +00:00
2025-10-05 16:50:51 +00:00
2025-10-09 00:21:21 +00:00
2025-08-29 00:19:55 +00:00
2025-10-10 00:21:51 +00:00
2025-06-23 00:16:33 +00:00
2025-10-10 00:21:51 +00:00
2025-01-02 11:59:57 +01:00
2025-10-10 00:21:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-10 00:21:51 +00:00
2025-10-09 00:21:21 +00:00
2025-01-02 11:59:57 +01:00
2025-10-05 16:50:51 +00:00
2025-10-10 00:21:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-08 00:20:55 +00:00
2025-10-05 16:50:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-05 16:50:51 +00:00
2025-10-05 16:50:51 +00:00
2025-09-02 00:19:26 +00:00
2025-10-05 16:50:51 +00:00
2025-10-10 00:21:51 +00:00

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.

Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.
Description
No description provided
Readme 1.5 GiB
Languages
C++ 30.7%
C 30%
Ada 14.5%
D 6.1%
Go 5.7%
Other 12.5%