Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block RNGs: remove unaligned memory cast #783

Merged
merged 2 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions benches/distributions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use std::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128};
use test::Bencher;
use std::time::Duration;

use rand::{Rng, FromEntropy};
use rand::rngs::SmallRng;
use rand::prelude::*;
use rand_distr::{*, weighted::WeightedIndex};

macro_rules! distr_int {
Expand Down Expand Up @@ -291,3 +290,23 @@ fn dist_iter(b: &mut Bencher) {
});
b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;
}

macro_rules! sample_binomial {
($name:ident, $n:expr, $p:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
let (n, p) = ($n, $p);
b.iter(|| {
let d = Binomial::new(n, p).unwrap();
rng.sample(d)
})
}
}
}

sample_binomial!(misc_binomial_1, 1, 0.9);
sample_binomial!(misc_binomial_10, 10, 0.9);
sample_binomial!(misc_binomial_100, 100, 0.99);
sample_binomial!(misc_binomial_1000, 1000, 0.01);
sample_binomial!(misc_binomial_1e12, 1000_000_000_000, 0.2);
6 changes: 3 additions & 3 deletions benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ gen_bytes!(gen_bytes_isaac, IsaacRng::from_entropy());
gen_bytes!(gen_bytes_isaac64, Isaac64Rng::from_entropy());
gen_bytes!(gen_bytes_std, StdRng::from_entropy());
gen_bytes!(gen_bytes_small, SmallRng::from_entropy());
gen_bytes!(gen_bytes_os, OsRng::new().unwrap());
gen_bytes!(gen_bytes_os, OsRng);

macro_rules! gen_uint {
($fnn:ident, $ty:ty, $gen:expr) => {
Expand Down Expand Up @@ -107,7 +107,7 @@ gen_uint!(gen_u32_isaac, u32, IsaacRng::from_entropy());
gen_uint!(gen_u32_isaac64, u32, Isaac64Rng::from_entropy());
gen_uint!(gen_u32_std, u32, StdRng::from_entropy());
gen_uint!(gen_u32_small, u32, SmallRng::from_entropy());
gen_uint!(gen_u32_os, u32, OsRng::new().unwrap());
gen_uint!(gen_u32_os, u32, OsRng);

gen_uint!(gen_u64_xorshift, u64, XorShiftRng::from_entropy());
gen_uint!(gen_u64_xoshiro256starstar, u64, Xoshiro256StarStar::from_entropy());
Expand All @@ -127,7 +127,7 @@ gen_uint!(gen_u64_isaac, u64, IsaacRng::from_entropy());
gen_uint!(gen_u64_isaac64, u64, Isaac64Rng::from_entropy());
gen_uint!(gen_u64_std, u64, StdRng::from_entropy());
gen_uint!(gen_u64_small, u64, SmallRng::from_entropy());
gen_uint!(gen_u64_os, u64, OsRng::new().unwrap());
gen_uint!(gen_u64_os, u64, OsRng);

macro_rules! init_gen {
($fnn:ident, $gen:ident) => {
Expand Down
24 changes: 2 additions & 22 deletions benches/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const RAND_BENCH_N: u64 = 1000;
use test::Bencher;

use rand::prelude::*;
use rand::distributions::{Distribution, Standard, Bernoulli};

#[bench]
fn misc_gen_bool_const(b: &mut Bencher) {
Expand Down Expand Up @@ -87,34 +88,14 @@ fn misc_bernoulli_var(b: &mut Bencher) {
let mut accum = true;
let mut p = 0.18;
for _ in 0..::RAND_BENCH_N {
let d = rand::distributions::Bernoulli::new(p);
let d = Bernoulli::new(p);
accum ^= rng.sample(d);
p += 0.0001;
}
accum
})
}

macro_rules! sample_binomial {
($name:ident, $n:expr, $p:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
let (n, p) = ($n, $p);
b.iter(|| {
let d = rand::distributions::Binomial::new(n, p);
rng.sample(d)
})
}
}
}

sample_binomial!(misc_binomial_1, 1, 0.9);
sample_binomial!(misc_binomial_10, 10, 0.9);
sample_binomial!(misc_binomial_100, 100, 0.99);
sample_binomial!(misc_binomial_1000, 1000, 0.01);
sample_binomial!(misc_binomial_1e12, 1000_000_000_000, 0.2);

#[bench]
fn gen_1k_iter_repeat(b: &mut Bencher) {
use std::iter;
Expand All @@ -128,7 +109,6 @@ fn gen_1k_iter_repeat(b: &mut Bencher) {

#[bench]
fn gen_1k_sample_iter(b: &mut Bencher) {
use rand::distributions::{Distribution, Standard};
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let v: Vec<u64> = Standard.sample_iter(&mut rng).take(128).collect();
Expand Down
84 changes: 0 additions & 84 deletions rand_core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,48 +210,6 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
}
}

// As an optimization we try to write directly into the output buffer.
// This is only enabled for little-endian platforms where unaligned writes
// are known to be safe and fast.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut filled = 0;

// Continue filling from the current set of results
if self.index < self.results.as_ref().len() {
let (consumed_u32, filled_u8) =
fill_via_u32_chunks(&self.results.as_ref()[self.index..],
dest);

self.index += consumed_u32;
filled += filled_u8;
}

let len_remainder =
(dest.len() - filled) % (self.results.as_ref().len() * 4);
let end_direct = dest.len() - len_remainder;

while filled < end_direct {
let dest_u32: &mut R::Results = unsafe {
&mut *(dest[filled..].as_mut_ptr() as
*mut <R as BlockRngCore>::Results)
};
self.core.generate(dest_u32);
filled += self.results.as_ref().len() * 4;
self.index = self.results.as_ref().len();
}

if len_remainder > 0 {
self.core.generate(&mut self.results);
let (consumed_u32, _) =
fill_via_u32_chunks(self.results.as_ref(),
&mut dest[filled..]);

self.index = consumed_u32;
}
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut read_len = 0;
while read_len < dest.len() {
Expand Down Expand Up @@ -416,48 +374,6 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
value
}

// As an optimization we try to write directly into the output buffer.
// This is only enabled for little-endian platforms where unaligned writes
// are known to be safe and fast.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut filled = 0;
self.half_used = false;

// Continue filling from the current set of results
if self.index < self.results.as_ref().len() {
let (consumed_u64, filled_u8) =
fill_via_u64_chunks(&self.results.as_ref()[self.index..],
dest);

self.index += consumed_u64;
filled += filled_u8;
}

let len_remainder =
(dest.len() - filled) % (self.results.as_ref().len() * 8);
let end_direct = dest.len() - len_remainder;

while filled < end_direct {
let dest_u64: &mut R::Results = unsafe {
::core::mem::transmute(dest[filled..].as_mut_ptr())
};
self.core.generate(dest_u64);
filled += self.results.as_ref().len() * 8;
self.index = self.results.as_ref().len();
}

if len_remainder > 0 {
self.core.generate(&mut self.results);
let (consumed_u64, _) =
fill_via_u64_chunks(&mut self.results.as_ref(),
&mut dest[filled..]);

self.index = consumed_u64;
}
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut read_len = 0;
self.half_used = false;
Expand Down