Skip to content

Commit

Permalink
Use black_box to prevent constant propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
jongiddy committed Jun 9, 2020
1 parent ca0f9c6 commit 06581c6
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions benches/reject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate test;

use rand::prelude::*;
use rand::distributions::uniform::{UniformInt, UniformSampler};
use test::Bencher;
use test::{Bencher, black_box};

// In src/distributions/uniform.rs, we say:
// Implementation of [`sample_single`] is optional, and is only useful when
Expand All @@ -22,10 +22,10 @@ macro_rules! sample_dist {
#[bench]
fn $fnn(b: &mut Bencher) {
let mut rng = thread_rng();
let dist = UniformInt::<u32>::new($low, $high);
let dist = UniformInt::<u32>::new(black_box($low), black_box($high));
b.iter(|| {
test::black_box(dist.sample(&mut rng));
test::black_box(dist.sample(&mut rng));
black_box(dist.sample(&mut rng));
black_box(dist.sample(&mut rng));
});
}
};
Expand All @@ -38,8 +38,8 @@ macro_rules! sample_single {
fn $fnn(b: &mut Bencher) {
let mut rng = thread_rng();
b.iter(|| {
test::black_box(UniformInt::<u32>::sample_single($low, $high, &mut rng));
test::black_box(UniformInt::<u32>::sample_single($low, $high, &mut rng));
black_box(UniformInt::<u32>::sample_single(black_box($low), black_box($high), &mut rng));
black_box(UniformInt::<u32>::sample_single(black_box($low), black_box($high), &mut rng));
});
}
};
Expand All @@ -51,7 +51,7 @@ macro_rules! sample_single {
// n/2+1: almost half of the values are rejected, and we can do no better
// n/2: half the values are rejected, but could be doubled to have no rejection
// n/2-1: only a few values are rejected: expect this to be fast
// 4: almost half that values are rejected, but could have no rejection
// 4: half the values are rejected, but could have no rejection
// 3: 25% rejected, but we could reject one value

const HALF_32_BIT_UNSIGNED: u32 = 1 << 31;
Expand Down

0 comments on commit 06581c6

Please sign in to comment.