Skip to content

Conversation

@dhardy
Copy link
Member

@dhardy dhardy commented Nov 24, 2025

Draft over #1686 to use the Generator trait.

Mostly only chacha.rs and reseeding.rs differ from #1686.

Performance is now in-line with the old block code:

$ taskset -c 4 cargo bench --bench generators thread -- --baseline master
    Finished `bench` profile [optimized] target(s) in 0.03s
     Running benches/generators.rs (/home/dhardy-extra/.cache/cargo-build/release/deps/generators-4ea267bc6b4453ec)
random_bytes/thread     time:   [310.57 ns 310.73 ns 310.90 ns]
                        thrpt:  [3.0675 GiB/s 3.0691 GiB/s 3.0708 GiB/s]
                 change:
                        time:   [-3.5877% -3.4589% -3.3321%] (p = 0.00 < 0.05)
                        thrpt:  [+3.4470% +3.5828% +3.7212%]
                        Performance has improved.

random_u32/thread       time:   [1.2527 ns 1.2531 ns 1.2535 ns]
                        thrpt:  [2.9719 GiB/s 2.9729 GiB/s 2.9739 GiB/s]
                 change:
                        time:   [-1.6952% -1.6523% -1.6072%] (p = 0.00 < 0.05)
                        thrpt:  [+1.6334% +1.6800% +1.7245%]
                        Performance has improved.
Found 339 outliers among 1000 measurements (33.90%)
  150 (15.00%) low severe
  11 (1.10%) low mild
  33 (3.30%) high mild
  145 (14.50%) high severe

random_u64/thread       time:   [2.0978 ns 2.0982 ns 2.0986 ns]
                        thrpt:  [3.5502 GiB/s 3.5509 GiB/s 3.5516 GiB/s]
                 change:
                        time:   [+2.7585% +2.7905% +2.8206%] (p = 0.00 < 0.05)
                        thrpt:  [-2.7432% -2.7148% -2.6844%]
                        Performance has regressed.
Found 196 outliers among 1000 measurements (19.60%)
  68 (6.80%) low severe
  15 (1.50%) low mild
  79 (7.90%) high mild
  34 (3.40%) high severe

Comment on lines -75 to +81
impl<R, Rsdr> ReseedingRng<R, Rsdr>
impl<R: fmt::Debug, Rsdr: fmt::Debug> fmt::Debug for ReseedingRng<R, Rsdr>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needed an explicit impl because it needs an additional where clause: R::Result: Debug

Comment on lines 128 to 139
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
let Self { core, buffer } = self;
// NOTE: this fn always returns one word, thus cannot return u32 for any W: Word
le::next_word_via_gen_block(buffer, |block| core.generate(block))
}

#[inline(always)]
#[inline]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
let Self { core, buffer } = self;
// NOTE: this fn is specific to u32 buffers
le::next_u64_via_gen_block(buffer, |block| core.generate(block))
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the available le fns require us to fix W = u32. It would be nice if we could impl ReseedingRng for any W: Word but I don't think there are enough block-RNGs over u64 for this to be a blocking issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants