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

switch gen_biguint to fill_bytes #53

Merged
merged 3 commits into from
Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 46 additions & 0 deletions benches/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,52 @@ fn from_str_radix_36(b: &mut Bencher) {
from_str_radix_bench(b, 36);
}

fn rand_bench(b: &mut Bencher, bits: usize) {
let mut rng = get_rng();

b.iter(|| rng.gen_bigint(bits));
}

#[bench]
fn rand_64(b: &mut Bencher) {
rand_bench(b, 1 << 6);
}

#[bench]
fn rand_256(b: &mut Bencher) {
rand_bench(b, 1 << 8);
}

#[bench]
fn rand_1009(b: &mut Bencher) {
rand_bench(b, 1009);
}

#[bench]
fn rand_2048(b: &mut Bencher) {
rand_bench(b, 1 << 11);
}

#[bench]
fn rand_4096(b: &mut Bencher) {
rand_bench(b, 1 << 12);
}

#[bench]
fn rand_8192(b: &mut Bencher) {
rand_bench(b, 1 << 13);
}

#[bench]
fn rand_65536(b: &mut Bencher) {
rand_bench(b, 1 << 16);
}

#[bench]
fn rand_131072(b: &mut Bencher) {
rand_bench(b, 1 << 17);
}

#[bench]
fn shl(b: &mut Bencher) {
let n = BigUint::one() << 1000;
Expand Down
15 changes: 9 additions & 6 deletions src/bigrand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use rand::prelude::*;
use rand::distributions::uniform::{SampleUniform, UniformSampler};
use rand::AsByteSliceMut;

use BigInt;
use BigUint;
Expand Down Expand Up @@ -39,13 +40,15 @@ impl<R: Rng + ?Sized> RandBigInt for R {
fn gen_biguint(&mut self, bit_size: usize) -> BigUint {
use super::big_digit::BITS;
let (digits, rem) = bit_size.div_rem(&BITS);
let mut data = Vec::with_capacity(digits + 1);
for _ in 0..digits {
data.push(self.gen());
}
let mut data = vec![BigDigit::default(); digits + (rem > 0) as usize];
// `fill_bytes` is faster than many `gen::<u32>` calls
self.fill_bytes(data[..].as_byte_slice_mut());
// Swap bytes per the `Rng::fill` source. This might be
// unnecessary if reproducibility across architectures is not
// desired.
data.to_le();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting question -- what guarantees should we make about reproducibility? Is this PR a breaking change altogether, since seeded RNGs may now produce a different BigUint? Maybe that's going too far to consider this across crate versions, but if we want it across architectures, we'll also need to be careful in the future about conditional BigDigit sizes, for instance. (Right now this is using the public BigUint::new though, which should always be u32-based even if the internals change.)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a value-breaking change (depending on the PRNG used), but I think will usually not be. (Rand does not specify that fill_bytes must output the same bits in the same order as repeated calls to next_u32, though I think this is the case for all current generators.)

if rem > 0 {
let final_digit: BigDigit = self.gen();
data.push(final_digit >> (BITS - rem));
data[digits] >>= BITS - rem;
}
BigUint::new(data)
}
Expand Down