Skip to content

Commit be4a681

Browse files
committed
Address the hash regression with pointers!
1 parent bd439af commit be4a681

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

crates/hstr/src/dynamic.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,24 +257,18 @@ const fn multiply_mix(x: u64, y: u64) -> u64 {
257257

258258
// Const compatible helper function to read a u64 from a byte array at a given
259259
// offset
260+
#[inline(always)]
260261
const fn read_u64_le(bytes: &[u8], offset: usize) -> u64 {
261-
(bytes[offset] as u64)
262-
| ((bytes[offset + 1] as u64) << 8)
263-
| ((bytes[offset + 2] as u64) << 16)
264-
| ((bytes[offset + 3] as u64) << 24)
265-
| ((bytes[offset + 4] as u64) << 32)
266-
| ((bytes[offset + 5] as u64) << 40)
267-
| ((bytes[offset + 6] as u64) << 48)
268-
| ((bytes[offset + 7] as u64) << 56)
262+
let array = unsafe { bytes.as_ptr().add(offset) } as *const [u8; 8];
263+
u64::from_le_bytes(unsafe { *array })
269264
}
270265

271266
// Const compatible helper function to read a u32 from a byte array at a given
272267
// offset
268+
#[inline(always)]
273269
const fn read_u32_le(bytes: &[u8], offset: usize) -> u32 {
274-
(bytes[offset] as u32)
275-
| ((bytes[offset + 1] as u32) << 8)
276-
| ((bytes[offset + 2] as u32) << 16)
277-
| ((bytes[offset + 3] as u32) << 24)
270+
let array = unsafe { bytes.as_ptr().add(offset) } as *const [u8; 4];
271+
u32::from_le_bytes(unsafe { *array })
278272
}
279273

280274
/// Copied from `hash_bytes` of `rustc-hash`.

0 commit comments

Comments
 (0)