Skip to content

Commit

Permalink
Optimize fill_bytes()
Browse files Browse the repository at this point in the history
We only need to calculate one random u128,
and not one u128 for each byte.
  • Loading branch information
hasenbanck committed Aug 11, 2024
1 parent ef33f67 commit 968d135
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,21 @@ pub trait Random {

/// Fills a mutable `[u8]` slice with random bytes.
fn fill_bytes(&self, slice: &mut [u8]) {
const SIZE_BYTES: usize = (u128::BITS / 8) as usize;
const U128_BYTES: usize = (u128::BITS / 8) as usize;

let mut chunks = slice.chunks_exact_mut(SIZE_BYTES);
let mut chunks = slice.chunks_exact_mut(U128_BYTES);
for chunk in &mut chunks {
let random_bytes: [u8; SIZE_BYTES] = self.next().to_le_bytes();
let random_bytes: [u8; U128_BYTES] = self.u128().to_le_bytes();
chunk.copy_from_slice(&random_bytes)
}
chunks
.into_remainder()

let remainder = chunks.into_remainder();
let next_bytes = self.next().to_le_bytes();

remainder
.iter_mut()
.for_each(|x| *x = self.next() as u8);
.zip(next_bytes.as_slice())
.for_each(|(x, y)| *x = *y);
}

/// Generates an array filled with random bytes.
Expand Down

0 comments on commit 968d135

Please sign in to comment.