Closed Pr0methean closed 6 months ago
Is there an actual blocking/performance issue? If so, you haven't outlined it clearly, merely stated that there is a theoretical one.
Reseeding on Linux uses getrandom which uses the getrandom system call which is non-blocking except in the case that the system RNG has not yet been initialized since boot. So I don't think there is an issue.
But anyway, for MC models you might want to use a local RNG like ChaCha8Rng
with fixed seed just to get reproducible output?
A little micro-benchmarking of https://github.com/Pr0methean/rng_buffer/blob/master/src/lib.rs (a wrapper that combines several consecutive getrandom
calls per thread into one) shows that it improves performance just by amortizing the overhead of a system call. Sharing a buffer between threads, OTOH, makes the buffer access no faster than the syscall.
That's a lot of code to batch calls to the OsRng
...
... but what was the point of reseeding again?
1 << 68
words (2^70 bytes). But this number is huge, and this was never the goal of reseeding.So, if you're going to batch 16 new seeds, you might just as well set the reseeding interval to 16 times the original length instead, just so long as you don't exceed 2^70 bytes (impossible given that this is a 64-bit counter).
Background
What is your motivation?
To develop a drop-in replacement for
rand::thread_rng()
that works around https://github.com/rust-random/rand/issues/1357 by using a buffering wrapper around OsRng where the buffer is a channel shared across all threads.What type of application is this? (E.g. cryptography, game, numerical simulation)
A library intended for multithreaded Monte Carlo simulations.
Feature request
At https://github.com/Pr0methean/shared_buffer_rng/blob/85cf1caf6ffbb7018ff6d4464f4fc7b07d34b3d6/src/lib.rs#L110, to create an otherwise-identical drop-in replacement for
rand::thread_rng()
that was to differ only in how it obtained seeds, I had to copy many of ThreadRng's implementation details:This is not future-proof against any change to the default algorithm, the reseeding threshold, or the use of
Rc<UnsafeCell<_>>
. It would be better if there was a method that would construct aThreadRng
givenreseeder
as the only parameter, and otherwise promise to have the same implementation details asthread_rng()
.