[libcxx-commits] [libcxx] [libc++] Speed up classic locale (PR #72112)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Nov 20 11:03:51 PST 2023


================
@@ -538,16 +543,31 @@ locale::__imp::use_facet(long id) const
 
 // locale
 
+std::atomic<locale::__imp*> locale::__imp::classic_;
+
 const locale&
 locale::__imp::make_classic()
 {
     // only one thread can get in here and it only gets in once
     alignas(locale) static std::byte buf[sizeof(locale)];
     locale* c = reinterpret_cast<locale*>(&buf);
     c->__locale_ = &make<__imp>(1u);
+    classic_.store(c->__locale_, std::memory_order_relaxed);
----------------
ldionne wrote:

Actually, I think we can do better here. At the end of the day, the classic locale is created in a static buffer:

```
template <class T, class ...Args>
T& make(Args ...args) {
    alignas(T) static std::byte buf[sizeof(T)];
    auto *obj = ::new (&buf) T(args...);
    return *obj;
}
```

If we refactored the code a bit, I think we could probably get rid of the need to have this atomic variable altogether. The address of the classic imp locale should be available even at compile-time, in theory.

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


More information about the libcxx-commits mailing list