[libcxx-commits] [libcxx] [libc++] Add basic constant folding for std::format (PR #107197)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Feb 4 13:06:18 PST 2025


================
@@ -447,10 +449,46 @@ format_to(_OutIt __out_it, wformat_string<_Args...> __fmt, _Args&&... __args) {
 }
 #  endif
 
+// Try constant folding the format string instead of going through the whole formatting machinery. If there is no
+// constant folding no extra code should be emitted (with optimizations enabled) and the function returns nullopt. When
+// constant folding is successful, the formatting is performed and the resulting string is returned.
+template <class _CharT>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<basic_string<_CharT>> __try_constant_folding_format(
----------------
philnik777 wrote:

Did you make sure to run the benchmarks with optimizations enabled? I'm quite surprised that there'd be a significant difference, since I couldn't see any unnecessary copying in the assembly. I'd also like to optimize formatting integers and similar stuff later, which requires a string.

https://github.com/llvm/llvm-project/pull/107197


More information about the libcxx-commits mailing list