[libcxx-commits] [libcxx] [libc++] Replace ranges::find_first_of with std::find_first_of in __try_constant_folding (PR #197641)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Thu May 14 02:25:27 PDT 2026


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/197641

This reduces the time it takes to instantiate `std::format` from ~160ms to ~120ms in my testing.


>From 51d20676cb9893ebe882fc5a8638374436eb3241 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 14 May 2026 11:24:40 +0200
Subject: [PATCH] [libc++] Replace ranges::find_first_of with
 std::find_first_of in __try_constant_folding

---
 libcxx/include/__format/format_functions.h | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/libcxx/include/__format/format_functions.h b/libcxx/include/__format/format_functions.h
index 7cf259d0e1db7..b58e487d7511e 100644
--- a/libcxx/include/__format/format_functions.h
+++ b/libcxx/include/__format/format_functions.h
@@ -11,7 +11,7 @@
 #define _LIBCPP___FORMAT_FORMAT_FUNCTIONS
 
 #include <__algorithm/clamp.h>
-#include <__algorithm/ranges_find_first_of.h>
+#include <__algorithm/find_first_of.h>
 #include <__chrono/statically_widen.h>
 #include <__concepts/convertible_to.h>
 #include <__concepts/same_as.h>
@@ -459,8 +459,12 @@ template <class _CharT>
     basic_string_view<_CharT> __fmt,
     basic_format_args<basic_format_context<back_insert_iterator<__format::__output_buffer<_CharT>>, _CharT>> __args) {
   // Fold strings not containing '{' or '}' to just return the string
-  if (bool __is_identity = [&] [[__gnu__::__pure__]] // Make sure the compiler knows this call can be eliminated
-      { return std::ranges::find_first_of(__fmt, array{'{', '}'}) == __fmt.end(); }();
+  if (bool __is_identity =
+          [&] [[__gnu__::__pure__]] // Make sure the compiler knows this call can be eliminated
+      {
+        char __vals[] = {'{', '}'};
+        return std::find_first_of(__fmt.begin(), __fmt.end(), std::begin(__vals), std::end(__vals)) == __fmt.end();
+      }();
       __builtin_constant_p(__is_identity) && __is_identity)
     return basic_string<_CharT>{__fmt};
 



More information about the libcxx-commits mailing list