You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dramatic slowdown with random.random on free-threading build. #118392
Acquiring critical sections for random methods causes this slowdown.
Removing @critical_section from the method, which uses genrand_uint32 and then updating genrand_uint32 to use atomic operation makes the performance acceptable.
Build
Elapsed
PI
Default (with specialization)
0.16528010368347168
3.144508
Free-threading (with no specialization)
0.548654317855835
3.1421
Free-threading with my patch (with no specialization)
It'd be great to have the default random number generator be safe and efficient when called from multiple threads, but I think that's a bigger change that requires more discussion and maybe a PEP.
In the meantime, I think the advice should be to use individual Random objects per-thread. In other words, the program should be written as:
I think that you also would need #118112 (or deferred reference counting) for the program to be efficient, or otherwise the reference count contention is going to be a bottleneck.
Reacted by Donghee Na, Joongi Kim, Erlend E. Aasland and Juhan Oskar Hennoste
Activity
corona10 commentedon Apr 29, 2024
cc @colesbury
pythongh-118392: Update genrand_uint32 to use atomic operation
colesbury commentedon Apr 29, 2024
It'd be great to have the default random number generator be safe and efficient when called from multiple threads, but I think that's a bigger change that requires more discussion and maybe a PEP.
In the meantime, I think the advice should be to use individual
Randomobjects per-thread. In other words, the program should be written as:I think that you also would need #118112 (or deferred reference counting) for the program to be efficient, or otherwise the reference count contention is going to be a bottleneck.
corona10 commentedon Apr 29, 2024
Yeah, it works. so it should be included somewhere in the migration guide.
corona10 commentedon Apr 29, 2024
#118393 (comment)
pythongh-118392: Add warning about random.random for multi thread app
colesbury commentedon Apr 29, 2024
We could also consider adopting a strategy like Go does, at least for the free-threaded build:
seed()has not been called, use a fast per-thread RNG for the global callsseed()is called, switch to the shared state RNG to preserve old behavior.gh-118392: Add note about random.random for multi thread app (gh-118396)
pythongh-118392: Add note about random.random for multi thread app (p…
pythongh-118392: Add note about random.random for multi thread app (p…