changed hash to add debug mode

This commit is contained in:
Fabian Schmidt 2024-08-28 11:32:35 +02:00
parent b1c064a92f
commit 0aa9d8be86

View File

@ -1,5 +1,6 @@
#[inline] #[inline]
pub fn bytes(bytes: &[u8]) -> u64 { pub fn bytes(bytes: &[u8]) -> u64 {
if cfg!(not(debug_assertions)) {
// inspired by https://curiouscoding.nl/posts/1brc/ // inspired by https://curiouscoding.nl/posts/1brc/
let head: [u8; 8] = unsafe { bytes.get_unchecked(..8).as_chunks::<8>().0[0] }; let head: [u8; 8] = unsafe { bytes.get_unchecked(..8).as_chunks::<8>().0[0] };
let tail: [u8; 8] = unsafe { bytes.get_unchecked(bytes.len() - 8..).as_chunks::<8>().0[0] }; let tail: [u8; 8] = unsafe { bytes.get_unchecked(bytes.len() - 8..).as_chunks::<8>().0[0] };
@ -7,6 +8,19 @@ pub fn bytes(bytes: &[u8]) -> u64 {
let khead = u64::from_ne_bytes(head) << shift; let khead = u64::from_ne_bytes(head) << shift;
let ktail = u64::from_ne_bytes(tail) >> shift; let ktail = u64::from_ne_bytes(tail) >> shift;
khead + ktail khead + ktail
} else {
// debug friendly but slow
let mut head = [0u8; 8];
let mut tail = [0u8; 8];
let end = bytes.len().min(8);
let start = bytes.len().saturating_sub(8);
head[..end].copy_from_slice(&bytes[..end]);
tail[..end].copy_from_slice(&bytes[start..]);
let shift = 64usize.saturating_sub(8 * bytes.len());
let khead = u64::from_ne_bytes(head) << shift;
let ktail = u64::from_ne_bytes(tail) >> shift;
khead.wrapping_add(ktail)
}
} }
#[cfg(test)] #[cfg(test)]