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

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 21 09:47:19 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: William Tran-Viet (smallp-o-p)

<details>
<summary>Changes</summary>

This patch implements a short-cut to only include information related to the queried environment encoding. This allows a user to check if their environment has some encoding without needing to store the entire data table in their binary.

It was mentioned in the original `text_encoding` PR that `environment_is` was designed to be optimized this way.

The encodings to optimize were chosen using this Wikipedia article: https://en.wikipedia.org/wiki/Popularity_of_text_encodings as I didn't deem it necessary to optimize _every_ encoding out there.

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


2 Files Affected:

- (modified) libcxx/include/text_encoding (+45-3) 
- (modified) libcxx/src/text_encoding.cpp (+4) 


``````````diff
diff --git a/libcxx/include/text_encoding b/libcxx/include/text_encoding
index c0708e3af05f7..e21f83b9fadde 100644
--- a/libcxx/include/text_encoding
+++ b/libcxx/include/text_encoding
@@ -70,6 +70,7 @@ struct text_encoding
 
 #    include <__algorithm/find_if.h>
 #    include <__algorithm/lower_bound.h>
+#    include <__algorithm/ranges_any_of.h>
 #    include <__cstddef/ptrdiff_t.h>
 #    include <__functional/hash.h>
 #    include <__iterator/iterator_traits.h>
@@ -77,6 +78,7 @@ struct text_encoding
 #    include <__ranges/view_interface.h>
 #    include <climits>
 #    include <cstdint>
+#    include <initializer_list>
 #    include <string_view>
 #    include <version>
 
@@ -85,6 +87,8 @@ struct text_encoding;
 
 _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
 
+_LIBCPP_AVAILABILITY_TEXT_ENCODING_ENVIRONMENT _LIBCPP_HIDDEN const char* __get_environment_encoding_name() noexcept;
+
 _LIBCPP_AVAILABILITY_TEXT_ENCODING_ENVIRONMENT _LIBCPP_EXPORTED_FROM_ABI text_encoding
 __get_locale_encoding(const char* __name);
 
@@ -606,12 +610,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);
+    }
+    if constexpr (_Id == id::ASCII) {
+      return std::ranges::any_of(
+          initializer_list<const char*>{
+              "US-ASCII",
+              "ANSI_X3.4-1968",
+              "ANSI_X3.4-1986",
+              "IBM367",
+              "ISO646-US",
+              "ISO_646.irv:1991",
+              "cp367",
+              "csASCII",
+              "ASCII",
+              "iso-ir-6",
+              "us"},
+          __predicate);
+    }
+    if constexpr (_Id == id::windows1252) {
+      return std::ranges::any_of(initializer_list<const char*>{"windows-1252, cswindows1252"}, __predicate);
+    }
+    if constexpr (_Id == id::ISOLatin1) {
+      return std::ranges::any_of(
+          initializer_list<const char*>{
+              "ISO-8859-1",
+              "ISO_8859-1:1987",
+              "CP819",
+              "IBM819",
+              "ISO_8859-1",
+              "csISOLatin1",
+              "iso-ir-100",
+              "l1",
+              "latin1"},
+          __predicate);
+    }
+
     return environment() == _Id;
 #      endif
   }
diff --git a/libcxx/src/text_encoding.cpp b/libcxx/src/text_encoding.cpp
index 0804c3457f1e6..eb0d3b59202d4 100644
--- a/libcxx/src/text_encoding.cpp
+++ b/libcxx/src/text_encoding.cpp
@@ -14,6 +14,10 @@
 _LIBCPP_BEGIN_NAMESPACE_STD
 _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
 
+const char* __get_environment_encoding_name() noexcept {
+  return __locale::__get_locale_encoding(static_cast<__locale::__locale_t>(0));
+}
+
 #if defined(__ANDROID__)
 // UTF-8 is the always the environment encoding on Android.
 std::text_encoding __get_locale_encoding([[maybe_unused]] const char* __name) { return std::text_encoding::id::UTF8; }

``````````

</details>


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


More information about the libcxx-commits mailing list