Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The original implementation of
ThreadLocalEntropy
was based onrand
'sThreadRng
, making a few changes to not require reseeding or forking protection in order to assume cases more in-line with game application usage. However, as a result, it picked up the issues with the original implementation, such as leaking memory if the destructors forThreadLocalEntropy
didn't run.This new implementation does away with cloning an
Rc
reference, instead opting for keeping the references within theLocalKey::with
closure, avoiding any reference counting. TheRc
prevents premature freeing during thread-local destructors, while keeping the ref inside thewith
closure ensures that we never end up in a situation where we might have a ref counted instance that never gets cleaned up.This also apparently allows the compiler to make better optimisations with the thread local access. Normal release profile has the perf for the old implementation and new implementation around the same, with the new implementation slightly ahead for the worst case scenario and with better mean/median times. With
codegen-units = 1
and LTO, it optimises considerably better.NOTE: This is tuned for the specific usage of
ThreadLocalEntropy
withinbevy_rand
. For one time accesses withThreadLocalEntropy
that then use the RNG source before dropping, this implementation is much faster. For accesses that hold the reference for longer to be used repeatedly before dropping, the originalThreadRng
implementation is faster.bevy_rand
's case is more on the one-time accesses.