rust-random / rand

A Rust library for random number generation.
https://crates.io/crates/rand
Other
1.67k stars 432 forks source link

Improve SmallRng initialization performance #1482

Closed arthurprs closed 1 month ago

arthurprs commented 3 months ago

Summary

Improve initialization speed for SmallRng

Motivation

SmallRng is 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:

before
test init_from_seed_smallrng  ... bench:           0.84 ns/iter (+/- 0.06)
test init_from_u64_smallrng  ... bench:           6.19 ns/iter (+/- 0.58)
test init_smallrng           ... bench:           9.45 ns/iter (+/- 0.18)

after
test init_from_seed_smallrng  ... bench:           0.85 ns/iter (+/- 0.08)
test init_from_u64_smallrng  ... bench:           3.42 ns/iter (+/- 0.09)
test init_smallrng           ... bench:           6.65 ns/iter (+/- 0.15)
dhardy commented 2 months ago

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.

arthurprs commented 2 months ago

Rebased on main

arthurprs commented 2 months ago

CI seems unhappy about a file I didn't change, not sure what's up.