Skip to content

Commit

Permalink
perf: Optimise from_rng impl for WyRand (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluefinger authored Jul 22, 2024
1 parent 336b380 commit 89ee257
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wyrand"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["Gonçalo Rica Pais da Silva <[email protected]>"]
description = "A fast & portable non-cryptographic pseudorandom number generator and hashing algorithm"
Expand Down Expand Up @@ -35,6 +35,7 @@ serde = { version = "1.0", features = ["derive"], optional = true }
[dev-dependencies]
criterion = "0.5"
serde_test = "1.0"
rand = "0.8"

[[bench]]
name = "rand_bench"
Expand Down
7 changes: 6 additions & 1 deletion benches/rand_bench.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use criterion::{black_box, criterion_main, Criterion};

fn wyrand_benchmark(c: &mut Criterion) {
use rand_core::RngCore;
use rand::thread_rng;
use rand_core::{RngCore, SeedableRng};
use wyrand::WyRand;

c.bench_function("rand", |b| {
Expand Down Expand Up @@ -29,6 +30,10 @@ fn wyrand_benchmark(c: &mut Criterion) {
criterion::BatchSize::LargeInput,
)
});

c.bench_function("from_rng", |b| {
b.iter(|| black_box(WyRand::from_rng(thread_rng())))
});
}

#[cfg(feature = "wyhash")]
Expand Down
16 changes: 16 additions & 0 deletions src/final_v4_2/wyrand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,15 @@ impl RngCore for WyRand {
impl SeedableRng for WyRand {
type Seed = [u8; core::mem::size_of::<u64>()];

#[inline]
fn from_seed(seed: Self::Seed) -> Self {
Self::new(u64::from_ne_bytes(seed))
}

#[inline]
fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, rand_core::Error> {
Ok(Self::new(rng.next_u64()))
}
}

#[cfg(test)]
Expand Down Expand Up @@ -155,6 +161,16 @@ mod tests {
assert_eq!(rand_dyn(&mut rng), 412_509_173);
}

#[cfg(feature = "rand_core")]
#[test]
fn rand_core_from_rng() {
let mut source = WyRand::from_seed(Default::default());

let mut rng = WyRand::from_rng(&mut source).expect("from_rng should never fail here");

assert_eq!(rng.next_u32(), 844672934);
}

#[cfg(all(feature = "serde1", feature = "debug"))]
#[test]
fn serde_tokens() {
Expand Down
16 changes: 16 additions & 0 deletions src/legacy_final_v4/wyrand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,15 @@ impl RngCore for WyRandLegacy {
impl SeedableRng for WyRandLegacy {
type Seed = [u8; core::mem::size_of::<u64>()];

#[inline]
fn from_seed(seed: Self::Seed) -> Self {
Self::new(u64::from_ne_bytes(seed))
}

#[inline]
fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, rand_core::Error> {
Ok(Self::new(rng.next_u64()))
}
}

#[cfg(test)]
Expand Down Expand Up @@ -155,6 +161,16 @@ mod tests {
assert_eq!(rand_dyn(&mut rng), 4_283_336_045);
}

#[cfg(feature = "rand_core")]
#[test]
fn rand_core_from_rng() {
let mut source = WyRandLegacy::from_seed(Default::default());

let mut rng = WyRandLegacy::from_rng(&mut source).expect("from_rng should never fail here");

assert_eq!(rng.next_u32(), 4242651740);
}

#[cfg(all(feature = "serde1", feature = "debug"))]
#[test]
fn serde_tokens() {
Expand Down

0 comments on commit 89ee257

Please sign in to comment.