[libcxx-commits] [libcxx] [libc++] Use a custom once_flag implementation for locale::id::__get() (PR #212235)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jul 29 05:03:24 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Nikolas Klauser (philnik777)
<details>
<summary>Changes</summary>
This has multiple benefits:
- it removes the need to load multiple values and potentially acquire locks
- it allows for a more compact representation of `locale::id`
- `__locale` doesn't make use of anything in `<mutex>` anymore
This also improves the startup time of programs a tiny bit.
The old `__flag_` member is now used for the ID, since that has been zero-initialized in all versions of libc++, which isn't the case for the old `__id_` member.
---
Full diff: https://github.com/llvm/llvm-project/pull/212235.diff
5 Files Affected:
- (modified) libcxx/docs/ABIGuarantees.rst (+4)
- (modified) libcxx/include/__configuration/abi.h (+1)
- (modified) libcxx/include/__locale (+14-4)
- (modified) libcxx/src/include/atomic_support.h (+21)
- (modified) libcxx/src/locale.cpp (+23-3)
``````````diff
diff --git a/libcxx/docs/ABIGuarantees.rst b/libcxx/docs/ABIGuarantees.rst
index c5ebe9ad76be8..1a71d40a537f8 100644
--- a/libcxx/docs/ABIGuarantees.rst
+++ b/libcxx/docs/ABIGuarantees.rst
@@ -134,6 +134,10 @@ flag removes that artificial padding.
``char_traits<char_type>::eq_int_type()`` cannot distinguish between ``WEOF`` and ``WCHAR_MAX``. This flag changes
``basic_ios`` to instead track whether the fill value has been initialized using a separate boolean.
+``_LIBCPP_ABI_COMPACT_LOCALE_ID``
+---------------------------------
+This flag removes some ``locale::id`` members and makes the remaining ones smaller. In an old version ``locale::id``
+used ``once_flag``, which got replaced by a custom implementation that doesn't require as much memory.
Linking TUs which have been compiled against different releases of libc++
=========================================================================
diff --git a/libcxx/include/__configuration/abi.h b/libcxx/include/__configuration/abi.h
index 757ba69cb17b4..994b90a299406 100644
--- a/libcxx/include/__configuration/abi.h
+++ b/libcxx/include/__configuration/abi.h
@@ -79,6 +79,7 @@
# define _LIBCPP_ABI_TRIVIALLY_COPYABLE_BIT_ITERATOR
# define _LIBCPP_ABI_USE_SMALL_DEQUE_BLOCK_SIZE
# define _LIBCPP_ABI_VECTORIZED_MERSENNE_TWISTER_ENGINE
+# define _LIBCPP_ABI_COMPACT_LOCALE_ID
#elif _LIBCPP_ABI_VERSION == 1
// Feature macros for disabling pre ABI v1 features. All of these options
diff --git a/libcxx/include/__locale b/libcxx/include/__locale
index f878aa48071ca..55e6ff4cef41f 100644
--- a/libcxx/include/__locale
+++ b/libcxx/include/__locale
@@ -18,7 +18,6 @@
# include <__locale_dir/locale_base_api.h>
# include <__memory/addressof.h>
# include <__memory/shared_count.h>
-# include <__mutex/once_flag.h>
# include <__type_traits/make_unsigned.h>
# include <__utility/no_destroy.h>
# include <__utility/private_constructor_tag.h>
@@ -159,11 +158,22 @@ private:
};
class _LIBCPP_EXPORTED_FROM_ABI locale::id {
- once_flag __flag_;
- int32_t __id_;
+# ifdef _LIBCPP_ABI_COMPACT_LOCALE_ID
+ using __id_type _LIBCPP_NODEBUG = int32_t;
+# elif defined(_LIBCPP_ABI_MICROSOFT)
+ using __id_type _LIBCPP_NODEBUG = uintptr_t;
+# else
+ using __id_type _LIBCPP_NODEBUG = unsigned long;
+# endif
+
+ __id_type __id_ = 0;
+
+# ifndef _LIBCPP_ABI_COMPACT_LOCALE_ID
+ [[__maybe_unused__]] int32_t __dummy_ = 0;
+# endif
public:
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR id() : __id_(0) {}
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR id() = default;
void operator=(const id&) = delete;
id(const id&) = delete;
diff --git a/libcxx/src/include/atomic_support.h b/libcxx/src/include/atomic_support.h
index 410f64b267111..ebc2c210681af 100644
--- a/libcxx/src/include/atomic_support.h
+++ b/libcxx/src/include/atomic_support.h
@@ -80,6 +80,16 @@ inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_atomic_compare_exchange(
return __atomic_compare_exchange_n(__val, __expected, __after, true, __success_order, __fail_order);
}
+template <class _ValueType>
+inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_atomic_compare_exchange_strong(
+ _ValueType* __val,
+ _ValueType* __expected,
+ _ValueType __after,
+ int __success_order = _AO_Seq,
+ int __fail_order = _AO_Seq) {
+ return __atomic_compare_exchange_n(__val, __expected, __after, false, __success_order, __fail_order);
+}
+
#else // _LIBCPP_HAS_THREADS
enum __libcpp_atomic_order { _AO_Relaxed, _AO_Consume, _AO_Acquire, _AO_Release, _AO_Acq_Rel, _AO_Seq };
@@ -123,6 +133,17 @@ __libcpp_atomic_compare_exchange(_ValueType* __val, _ValueType* __expected, _Val
return false;
}
+template <class _ValueType>
+inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_atomic_compare_exchange_strong(
+ _ValueType* __val, _ValueType* __expected, _ValueType __after, int = 0, int = 0) {
+ if (*__val == *__expected) {
+ *__val = __after;
+ return true;
+ }
+ *__expected = *__val;
+ return false;
+}
+
#endif // _LIBCPP_HAS_THREADS
} // namespace
diff --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp
index 40613e72b1a9d..c7955e5f434a7 100644
--- a/libcxx/src/locale.cpp
+++ b/libcxx/src/locale.cpp
@@ -563,9 +563,29 @@ void locale::facet::__on_zero_shared() noexcept { delete this; }
// locale::id
long locale::id::__get() {
- constinit static int32_t next_id = 0;
- call_once(__flag_, [&] { __id_ = __libcpp_atomic_add(&next_id, 1); });
- return __id_ - 1;
+ constexpr __id_type uninitialized = 0;
+ constexpr __id_type pending = 1;
+ constexpr __id_type first_id = 2;
+
+ constinit static __id_type next_id = first_id;
+
+ auto val = __libcpp_atomic_load(&__id_, _AO_Acquire);
+ if (val >= first_id)
+ return val - first_id;
+
+ if (val == uninitialized && __libcpp_atomic_compare_exchange_strong(&__id_, &val, pending)) {
+ auto new_id = __libcpp_atomic_add(&next_id, 1) - 1;
+ __libcpp_atomic_store(&__id_, new_id);
+ return new_id - first_id;
+ }
+
+ // Another thread is already initializing __id_. Wait for that in a spin loop. The initialization should be incredibly
+ // fast, a spin loop should be efficient enough.
+ while (true) {
+ auto id = __libcpp_atomic_load(&__id_);
+ if (id >= first_id)
+ return id - first_id;
+ }
}
// template <> class collate_byname<char>
``````````
</details>
https://github.com/llvm/llvm-project/pull/212235
More information about the libcxx-commits
mailing list