2 releases
0.1.1 | Jan 27, 2024 |
---|---|
0.1.0 | Jan 27, 2024 |
#2119 in Algorithms
13KB
178 lines
rng_buffer
This small crate provides RngBufferCore
, a struct that wraps any rand::Rng
and implements BlockRngCore
so that,
when used in a rand_core::block::BlockRng64
, it will fetch more bytes with each call. This is mainly intended to
reduce the number of system calls when using rand::rngs::OsRng
or a client of a remote RNG, for purposes where you
want to reseed regularly to prevent subtle patterns in your random numbers, but don't need fast key erasure (mainly for
Monte Carlo simulations and game servers; I don't recommend it for cryptography or gambling). Profiling with Vtune on an
EC2 c7i.metal-24xl
instance running Linux HVM kernel version 6.1.72-96.166.amzn2023.x86_64
showed that this reduced
the number of CPU cycles spent inside system calls by 80% with a 256-byte buffer per thread (currently the default).
The following are also provided:
RngBufferWrapper
, a wrapper aroundRngBufferCore
that lets you share the buffer with all of its clones. It also bypasses the buffer and directly invokes the wrapped RNG whenfill_bytes
ortry_fill_bytes
is called with a slice that's as large as the buffer.RngWrapper
, a struct that wraps anyRng
in anRc<RefCell<_>>
so that clones will use the same instance. Useful for implementing custom replacements forrand::rngs::ThreadRng
.thread_rng()
, a drop-in replacement forrand::thread_rng()
that uses anRngBufferCore
per thread but otherwise behaves identically.thread_seed_source()
, which provides anRngBufferWrapper
around a thread-local instance ofOsRng
.build_default_seeder()
andbuild_default_rng()
, intended forno_std
environments where you can't use thread-locals.
Dependencies
~1.4–2.3MB
~40K SLoC