Improve SmallRng initialization performance#1482
Conversation
examples/smallrng-code-gen.rs
Outdated
| @@ -0,0 +1,45 @@ | |||
| extern crate rand; | |||
| use std::hint::black_box; | |||
There was a problem hiding this comment.
Seems like the min MSRV is problematic here, maybe this will have to be removed.
There was a problem hiding this comment.
Either that, or...
This example serves as a non-automated test to check code generation? Can we turn it into an automated test? Otherwise I suggest it just be removed.
dhardy
left a comment
There was a problem hiding this comment.
So this is just an optimisation which does not change any results?
examples/smallrng-code-gen.rs
Outdated
| @@ -0,0 +1,45 @@ | |||
| extern crate rand; | |||
| use std::hint::black_box; | |||
There was a problem hiding this comment.
Either that, or...
This example serves as a non-automated test to check code generation? Can we turn it into an automated test? Otherwise I suggest it just be removed.
|
Reminder: changes are needed to ensure the API remains consistent on 32-bit and 64-bit platforms. Also, these new benchmarks can't be merged since #1490 moved all benchmarks to use Criterion. |
6224cfb to
ae6ef7a
Compare
|
Rebased on |
ae6ef7a to
5410c5d
Compare
|
CI seems unhappy about a file I didn't change, not sure what's up. |
dhardy
left a comment
There was a problem hiding this comment.
One minor 'bug'; otherwise looks good.
src/rngs/xoshiro128plusplus.rs
Outdated
| i[0] = z.to_le() as u32; | ||
| i[1] = (z.to_le() >> 32) as u32; |
There was a problem hiding this comment.
We aren't round-tripping to LE bytes and back since your code now directly initializes state, hence we don't want .to_le() here.
This should trip a test, but it seems we never actually test seed_from_u64... because we don't guarantee value-stability for SmallRng anyway. (In other words, this isn't strictly a bug, but you should still remove .to_le(), and preferably also add some form of reference test for this method.)
There was a problem hiding this comment.
If I remove it, won't the result be different than before on BE? I was trying to preserve that here. But yeah, kinda pointless w/o a test to ensure that it's correct.
There was a problem hiding this comment.
Do you mean that this PR should add test for the expected state of the Rng after seed_from_u64?
There was a problem hiding this comment.
There's a reference test in the same file, but in this case testing a single sample should be enough. And yes, results should match from prior to your PR.
It doesn't really matter since we explicitly do not promise value stability of these RNGs, but feel like we should anyway.
CHANGELOG.mdentrySummary
Improve initialization speed for SmallRng
Motivation
SmallRngis plenty fast, but the initialization speed was suboptimal.Details
An example binary was added to check for code generation with
cargo asm.Benchmarks were also added. The results are shown below: