[libcxx-commits] [libcxx] [libc++] Avoid a guard variable for the random_shuffle engine (PR #234815)
via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Oct 22 19:07:11 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Louis Dionne (ldionne)
<details>
<summary>Changes</summary>
The default engine used by std::random_shuffle currently uses a function local static. However, it turns out the thread safe guard variable is unnecessary since we're already under a lock in this case.
It would be even better to constant initialize mt19937 instead, however that's a more difficult change since the standard doesn't define it as constexpr.
---
Full diff: https://github.com/llvm/llvm-project/pull/234815.diff
1 Files Affected:
- (modified) libcxx/src/random_shuffle.cpp (+12-2)
``````````diff
diff --git a/libcxx/src/random_shuffle.cpp b/libcxx/src/random_shuffle.cpp
index aa0f78fafe06e..6f5627ecf758a 100644
--- a/libcxx/src/random_shuffle.cpp
+++ b/libcxx/src/random_shuffle.cpp
@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
+#include <__utility/no_destroy.h>
#include <algorithm>
#include <random>
@@ -42,9 +43,18 @@ __rs_default::~__rs_default() {
#endif
}
+// The engine is only accessed through a __rs_default, whose construction acquires the mutex (held until
+// the last copy is destroyed), so accesses are already serialized. This allows initializing the engine
+// lazily without a function-local static, which requires a guard variable.
+static constinit __no_destroy<mt19937> __global_mersenne_twister(__uninitialized_tag{});
+static constinit bool __initialized = false;
+
__rs_default::result_type __rs_default::operator()() {
- static mt19937 __rs_g;
- return __rs_g();
+ if (!__initialized) {
+ __global_mersenne_twister.__emplace();
+ __initialized = true;
+ }
+ return __global_mersenne_twister.__get()();
}
__rs_default __rs_get() { return __rs_default(); }
``````````
</details>
https://github.com/llvm/llvm-project/pull/234815
More information about the libcxx-commits
mailing list