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

libstdc++: Reduce instantiations in `__formatter_str::_M_format_range`.

This commit reorders compile-time checks in `__formatter_str::_M_format_range` to reduce unnecessary template instantiations, potentially improving compile tim…

The __formatter_str::_M_format_range function in libstdc++ had a series of if constexpr checks that could lead to unnecessary template instantiations. By reordering these checks, the compiler can now directly call the appropriate format overload for prvalues of span or __simply_formattable_range types, reducing the number of instantiations and symbols generated. This can lead to faster compilation times and smaller binaries.

In Details

The __formatter_str::_M_format_range function is part of the implementation of the C++20 <format> library in libstdc++. It's responsible for formatting ranges of elements into a string. The original code had a series of if constexpr checks that were not ordered optimally, resulting in multiple template instantiations for certain range types. This commit reorders the checks to prioritize the most common cases, reducing the number of unnecessary instantiations. This change primarily affects compile time and code size, with minimal impact on runtime performance.

For Context

The C++20 <format> library provides a type-safe and extensible way to format data into strings. The libstdc++ implementation uses template metaprogramming to handle different data types and formatting options. This commit optimizes the internal implementation of the range formatting functionality to reduce the number of template instantiations during compilation. Reducing template instantiations can speed up compilation times, especially in code that makes heavy use of the <format> library.

Filed Under: libstdc++formatoptimizationtemplates