From 60cc17cba271d5bf75857d61f776a0f681340e37 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Tue, 20 Mar 2018 15:29:15 +0000 Subject: [PATCH 1/5] Generator benchmarks: reduce overhead for u32/u64 generation --- benches/generators.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/benches/generators.rs b/benches/generators.rs index 2d96b8719cb..525831376e6 100644 --- a/benches/generators.rs +++ b/benches/generators.rs @@ -45,9 +45,11 @@ macro_rules! gen_uint { fn $fnn(b: &mut Bencher) { let mut rng = $gen; b.iter(|| { + let mut accum: $ty = 0; for _ in 0..RAND_BENCH_N { - black_box(rng.gen::<$ty>()); + accum = accum.wrapping_add(rng.gen::<$ty>()); } + black_box(accum); }); b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N; } @@ -126,9 +128,11 @@ macro_rules! chacha_rounds { let mut rng = ChaChaRng::new(); rng.set_rounds($rounds); b.iter(|| { + let mut accum: u32 = 0; for _ in 0..RAND_BENCH_N { - black_box(rng.gen::()); + accum = accum.wrapping_add(rng.gen::()); } + black_box(accum); }); b.bytes = size_of::() as u64 * RAND_BENCH_N; } @@ -138,9 +142,11 @@ macro_rules! chacha_rounds { let mut rng = ChaChaRng::new(); rng.set_rounds($rounds); b.iter(|| { + let mut accum: u64 = 0; for _ in 0..RAND_BENCH_N { - black_box(rng.gen::()); + accum = accum.wrapping_add(rng.gen::()); } + black_box(accum); }); b.bytes = size_of::() as u64 * RAND_BENCH_N; } @@ -178,9 +184,11 @@ macro_rules! reseeding_uint { RESEEDING_THRESHOLD, EntropyRng::new()); b.iter(|| { + let mut accum: $ty = 0; for _ in 0..RAND_BENCH_N { - black_box(rng.gen::<$ty>()); + accum = accum.wrapping_add(rng.gen::<$ty>()); } + black_box(accum); }); b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N; } @@ -197,9 +205,11 @@ macro_rules! threadrng_uint { fn $fnn(b: &mut Bencher) { let mut rng = thread_rng(); b.iter(|| { + let mut accum: $ty = 0; for _ in 0..RAND_BENCH_N { - black_box(rng.gen::<$ty>()); + accum = accum.wrapping_add(rng.gen::<$ty>()); } + black_box(accum); }); b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N; } From d65fa1b4c5aed7bc01ba7124c24b8a6b59737cea Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Wed, 21 Mar 2018 09:10:24 +0100 Subject: [PATCH 2/5] Distribution benchmarks: reduce overhead --- benches/distributions.rs | 93 +++++++++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 25 deletions(-) diff --git a/benches/distributions.rs b/benches/distributions.rs index 0d756126370..d6184d2aac2 100644 --- a/benches/distributions.rs +++ b/benches/distributions.rs @@ -12,6 +12,46 @@ use test::{black_box, Bencher}; use rand::{Rng, NewRng, XorShiftRng}; use rand::distributions::*; +macro_rules! distr_int { + ($fnn:ident, $ty:ty, $distr:expr) => { + #[bench] + fn $fnn(b: &mut Bencher) { + let mut rng = XorShiftRng::new(); + let distr = $distr; + + b.iter(|| { + let mut accum = 0 as $ty; + for _ in 0..::RAND_BENCH_N { + let x: $ty = distr.sample(&mut rng); + accum = accum.wrapping_add(x); + } + black_box(accum); + }); + b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N; + } + } +} + +macro_rules! distr_float { + ($fnn:ident, $ty:ty, $distr:expr) => { + #[bench] + fn $fnn(b: &mut Bencher) { + let mut rng = XorShiftRng::new(); + let distr = $distr; + + b.iter(|| { + let mut accum = 0.0; + for _ in 0..::RAND_BENCH_N { + let x: $ty = distr.sample(&mut rng); + accum = accum + x; + } + black_box(accum); + }); + b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N; + } + } +} + macro_rules! distr { ($fnn:ident, $ty:ty, $distr:expr) => { #[bench] @@ -31,39 +71,39 @@ macro_rules! distr { } // range -distr!(distr_range_i8, i8, Range::new(20i8, 100)); -distr!(distr_range_i16, i16, Range::new(-500i16, 2000)); -distr!(distr_range_i32, i32, Range::new(-200_000_000i32, 800_000_000)); -distr!(distr_range_i64, i64, Range::new(3i64, 12345678901234)); +distr_int!(distr_range_i8, i8, Range::new(20i8, 100)); +distr_int!(distr_range_i16, i16, Range::new(-500i16, 2000)); +distr_int!(distr_range_i32, i32, Range::new(-200_000_000i32, 800_000_000)); +distr_int!(distr_range_i64, i64, Range::new(3i64, 12345678901234)); #[cfg(feature = "i128_support")] -distr!(distr_range_i128, i128, Range::new(-12345678901234i128, 12345678901234567890)); +distr_int!(distr_range_i128, i128, Range::new(-12345678901234i128, 12345678901234567890)); -distr!(distr_range_f32, f32, Range::new(2.26f32, 2.319)); -distr!(distr_range_f64, f64, Range::new(2.26f64, 2.319)); +distr_float!(distr_range_f32, f32, Range::new(2.26f32, 2.319)); +distr_float!(distr_range_f64, f64, Range::new(2.26f64, 2.319)); // uniform -distr!(distr_uniform_i8, i8, Uniform); -distr!(distr_uniform_i16, i16, Uniform); -distr!(distr_uniform_i32, i32, Uniform); -distr!(distr_uniform_i64, i64, Uniform); +distr_int!(distr_uniform_i8, i8, Uniform); +distr_int!(distr_uniform_i16, i16, Uniform); +distr_int!(distr_uniform_i32, i32, Uniform); +distr_int!(distr_uniform_i64, i64, Uniform); #[cfg(feature = "i128_support")] -distr!(distr_uniform_i128, i128, Uniform); +distr_int!(distr_uniform_i128, i128, Uniform); distr!(distr_uniform_bool, bool, Uniform); distr!(distr_uniform_alphanumeric, char, Alphanumeric); distr!(distr_uniform_codepoint, char, Uniform); -distr!(distr_uniform_f32, f32, Uniform); -distr!(distr_uniform_f64, f64, Uniform); +distr_float!(distr_uniform_f32, f32, Uniform); +distr_float!(distr_uniform_f64, f64, Uniform); // distributions -distr!(distr_exp, f64, Exp::new(2.71828 * 3.14159)); -distr!(distr_normal, f64, Normal::new(-2.71828, 3.14159)); -distr!(distr_log_normal, f64, LogNormal::new(-2.71828, 3.14159)); -distr!(distr_gamma_large_shape, f64, Gamma::new(10., 1.0)); -distr!(distr_gamma_small_shape, f64, Gamma::new(0.1, 1.0)); -distr!(distr_binomial, u64, Binomial::new(20, 0.7)); -distr!(distr_poisson, u64, Poisson::new(4.0)); +distr_float!(distr_exp, f64, Exp::new(2.71828 * 3.14159)); +distr_float!(distr_normal, f64, Normal::new(-2.71828, 3.14159)); +distr_float!(distr_log_normal, f64, LogNormal::new(-2.71828, 3.14159)); +distr_float!(distr_gamma_large_shape, f64, Gamma::new(10., 1.0)); +distr_float!(distr_gamma_small_shape, f64, Gamma::new(0.1, 1.0)); +distr_int!(distr_binomial, u64, Binomial::new(20, 0.7)); +distr_int!(distr_poisson, u64, Poisson::new(4.0)); // construct and sample from a range @@ -72,14 +112,17 @@ macro_rules! gen_range_int { #[bench] fn $fnn(b: &mut Bencher) { let mut rng = XorShiftRng::new(); - let high = $high; + let mut high = $high; + let fake_increment = 0; b.iter(|| { + let mut accum: $ty = 0; for _ in 0..::RAND_BENCH_N { - let x: $ty = rng.gen_range($low, high); - black_box(x); - black_box(high); + accum = accum.wrapping_add(rng.gen_range($low, high)); + high += fake_increment; } + black_box(accum); + black_box(fake_increment); }); b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N; } From bed17031dfae8a66c301cbd2a3b6c750279db66e Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 21 Mar 2018 09:56:39 +0000 Subject: [PATCH 3/5] Distribution benchmarks: further tweaks; improve range bench perf --- benches/distributions.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/benches/distributions.rs b/benches/distributions.rs index d6184d2aac2..746820d859f 100644 --- a/benches/distributions.rs +++ b/benches/distributions.rs @@ -108,30 +108,28 @@ distr_int!(distr_poisson, u64, Poisson::new(4.0)); // construct and sample from a range macro_rules! gen_range_int { - ($fnn:ident, $ty:ty, $low:expr, $high:expr) => { + ($fnn:ident, $ty:ty, $low:expr, $high:expr, $incr:expr) => { #[bench] fn $fnn(b: &mut Bencher) { let mut rng = XorShiftRng::new(); - let mut high = $high; - let fake_increment = 0; b.iter(|| { + let mut high = $high; let mut accum: $ty = 0; for _ in 0..::RAND_BENCH_N { accum = accum.wrapping_add(rng.gen_range($low, high)); - high += fake_increment; + high += $incr; // force recalculation of range each time } black_box(accum); - black_box(fake_increment); }); b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N; } } } -gen_range_int!(gen_range_i8, i8, 20i8, 100); -gen_range_int!(gen_range_i16, i16, -500i16, 2000); -gen_range_int!(gen_range_i32, i32, -200_000_000i32, 800_000_000); -gen_range_int!(gen_range_i64, i64, 3i64, 12345678901234); +gen_range_int!(gen_range_i8, i8, 20i8, 100, 0); +gen_range_int!(gen_range_i16, i16, -500i16, 2000, 1); +gen_range_int!(gen_range_i32, i32, -200_000_000i32, 800_000_000, 3); +gen_range_int!(gen_range_i64, i64, 3i64, 12345678901234, 3); #[cfg(feature = "i128_support")] -gen_range_int!(gen_range_i128, i128, -12345678901234i128, 12345678901234567890); +gen_range_int!(gen_range_i128, i128, -12345678901234i128, 12345678901234567890, 3); From 139eca544db36bfcefdef87361804f2a75a40f99 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Tue, 20 Mar 2018 15:30:32 +0000 Subject: [PATCH 4/5] rand-core: update homepage location and fix relative link --- Cargo.toml | 2 +- rand-core/Cargo.toml | 4 ++-- rand-core/README.md | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e01be354047..e75b9e30f91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-lang-nursery/rand" documentation = "https://docs.rs/rand" -homepage = "https://github.com/rust-lang-nursery/rand" +homepage = "https://crates.io/crates/rand" description = """ Random number generators and other randomness functionality. """ diff --git a/rand-core/Cargo.toml b/rand-core/Cargo.toml index 4577b4ea7ea..ba748f0a11e 100644 --- a/rand-core/Cargo.toml +++ b/rand-core/Cargo.toml @@ -5,8 +5,8 @@ authors = ["The Rust Project Developers"] license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-lang-nursery/rand" -documentation = "https://docs.rs/rand" -homepage = "https://github.com/rust-lang-nursery/rand" +documentation = "https://docs.rs/rand-core" +homepage = "https://crates.io/crates/rand-core" description = """ Core random number generator traits and tools for implementation. """ diff --git a/rand-core/README.md b/rand-core/README.md index e7d67b7e14d..20823d1f37f 100644 --- a/rand-core/README.md +++ b/rand-core/README.md @@ -16,7 +16,7 @@ prefer to use the main [rand] crate. [Documentation](https://docs.rs/rand-core) -[rand]: .. +[rand]: ../README.md # License From b2ba486c97d5d836961a4e8fdd93b8302300aa87 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Wed, 21 Mar 2018 12:15:21 +0100 Subject: [PATCH 5/5] Use mask in gen_range_i benchmarks --- benches/distributions.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/benches/distributions.rs b/benches/distributions.rs index 746820d859f..7032b3561f0 100644 --- a/benches/distributions.rs +++ b/benches/distributions.rs @@ -108,7 +108,7 @@ distr_int!(distr_poisson, u64, Poisson::new(4.0)); // construct and sample from a range macro_rules! gen_range_int { - ($fnn:ident, $ty:ty, $low:expr, $high:expr, $incr:expr) => { + ($fnn:ident, $ty:ident, $low:expr, $high:expr) => { #[bench] fn $fnn(b: &mut Bencher) { let mut rng = XorShiftRng::new(); @@ -118,7 +118,8 @@ macro_rules! gen_range_int { let mut accum: $ty = 0; for _ in 0..::RAND_BENCH_N { accum = accum.wrapping_add(rng.gen_range($low, high)); - high += $incr; // force recalculation of range each time + // force recalculation of range each time + high = high.wrapping_add(1) & std::$ty::MAX; } black_box(accum); }); @@ -127,9 +128,9 @@ macro_rules! gen_range_int { } } -gen_range_int!(gen_range_i8, i8, 20i8, 100, 0); -gen_range_int!(gen_range_i16, i16, -500i16, 2000, 1); -gen_range_int!(gen_range_i32, i32, -200_000_000i32, 800_000_000, 3); -gen_range_int!(gen_range_i64, i64, 3i64, 12345678901234, 3); +gen_range_int!(gen_range_i8, i8, -20i8, 100); +gen_range_int!(gen_range_i16, i16, -500i16, 2000); +gen_range_int!(gen_range_i32, i32, -200_000_000i32, 800_000_000); +gen_range_int!(gen_range_i64, i64, 3i64, 12345678901234); #[cfg(feature = "i128_support")] -gen_range_int!(gen_range_i128, i128, -12345678901234i128, 12345678901234567890, 3); +gen_range_int!(gen_range_i128, i128, -12345678901234i128, 12345678901234567890);