[libcxx-commits] [libcxx] [libc++] Optimize `text_encoding::environment_is()` for UTF-8, ASCII, Windows1252 and ISOLatin1 (PR #225136)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Sep 22 00:20:33 PDT 2026


================
@@ -606,12 +611,50 @@ public:
 
   template <id _Id>
   [[nodiscard]] _LIBCPP_AVAILABILITY_TEXT_ENCODING_ENVIRONMENT static bool environment_is() {
-    // TODO: It may be worthwhile to implement an optimization for popular encodings, e.g. UTF-8,
-    // to allow checking the environment text encoding without needing to use the data tables.
-
 #      if defined(__ANDROID__)
     return _Id == std::text_encoding::id::UTF8;
 #      else
+    const char* __env_name = __get_environment_encoding_name();
+
+    auto __predicate = [__env_name](auto __str) { return __comp_name(__str, __env_name); };
+
+    if constexpr (_Id == id::UTF8) {
+      return std::ranges::any_of(initializer_list<const char*>{"UTF-8, csUTF8"}, __predicate);
----------------
philnik777 wrote:

We shouldn't use `std::ranges::any_of`. It's incredibly expensive to compile compared to `std::any_of`.

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


More information about the libcxx-commits mailing list