Add thread safety to default random#16174
Add thread safety to default random#16174straight-shoota merged 13 commits intocrystal-lang:masterfrom
Conversation
|
We should probably extract the refactor of |
|
Or move to |
There was a problem hiding this comment.
This is quite a big patch. We should probably split it down a bit more into smaller, self-contained change sets.
The change for internalizing Random.default could each already be an individual PR:
- Change method signatures to
random = niland update docs - Add
Random.next_intandRandom.next_bool - Prefer
Random::Secure.random_bytes
a4bd030 to
e1a783a
Compare
|
Rebased from master with extracted commits squashed back in. |
e1a783a to
cf62e67
Compare
|
Extracted def from macro and undocumented We might want to formalize an API to "split a random" with a couple |
…6342) Adds a couple methods to formalize the API for splitting a `Random` instance: - `Random#split`: splits the instance by allocating a new instance. - `#split_internal`: splits the instance into an allocated instance (for example `ReferenceStorage(T)`). I initially wanted a `#split!` method with no argument, but we need access to both instances to avoid splits (and re-splits of splits) to end up sharing the same sequences. Extracted from #16174 and adapted with usages in the [random shard](ysbaddaden/random.cr#1, XoShiRo).
16e24a7 to
bc2c995
Compare
|
Final rebase on I renamed the TLS as |
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
See #16157 and its comments for more details.
Random.defaultthread local to replace all usages ofRandom::DEFAULT.Enumerable#sample(n)by splitting the thread local to a new random sequence.Random::DEFAULT.To be defined: we might want to keep
Random.defaultundocumented 🤔Depends on:
thread_localmacro #16173Crystal::System::Process#forkon UNIX #16191Random.next_booland.next_int#16297Random::Secure.random_bytes#16298randomarg tonilinstead ofRandom::DEFAULT#16299Closes #16157.