Skip to content

Commit 0eb00bd

Browse files
committed
Restore unsafe fill_via_chunks implementation for performance
1 parent 2f9a42d commit 0eb00bd

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

rand_core/src/impls.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ macro_rules! fill_via_chunks {
5959
let chunk_size_u8 = min($src.len() * SIZE, $dst.len());
6060
let chunk_size = (chunk_size_u8 + SIZE - 1) / SIZE;
6161

62+
if cfg!(target_endian = "little") {
63+
unsafe {
64+
core::ptr::copy_nonoverlapping(
65+
$src.as_ptr() as *const u8,
66+
$dst.as_mut_ptr(),
67+
chunk_size_u8);
68+
}
69+
} else {
70+
for (&n, chunk) in $src.iter().zip($dst.chunks_mut(SIZE)) {
71+
let tmp = n.to_le();
72+
let src_ptr = &tmp as *const $ty as *const u8;
73+
unsafe {
74+
core::ptr::copy_nonoverlapping(
75+
src_ptr,
76+
chunk.as_mut_ptr(),
77+
chunk.len());
78+
}
79+
}
80+
}
81+
82+
// The following code is a safe replacement, but unfortunately ca. 8%
83+
// slower.
84+
/*
6285
let mut iter_src = $src.iter();
6386
let mut chunks = $dst.chunks_exact_mut(SIZE);
6487
for (chunk, n) in (&mut chunks).zip(&mut iter_src) {
@@ -68,6 +91,7 @@ macro_rules! fill_via_chunks {
6891
if let Some(n) = iter_src.next() {
6992
rem.copy_from_slice(&n.to_le_bytes()[..rem.len()]);
7093
}
94+
*/
7195

7296
(chunk_size, chunk_size_u8)
7397
}};

0 commit comments

Comments
 (0)