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,12 +1,26 @@
#[inline]
pub fn bytes(bytes: &[u8]) -> u64 {
// inspired by https://curiouscoding.nl/posts/1brc/
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 shift = 64usize.saturating_sub(8 * bytes.len());
let khead = u64::from_ne_bytes(head) << shift;
let ktail = u64::from_ne_bytes(tail) >> shift;
khead + ktail
if cfg!(not(debug_assertions)) {
// inspired by https://curiouscoding.nl/posts/1brc/
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 shift = 64usize.saturating_sub(8 * bytes.len());
let khead = u64::from_ne_bytes(head) << shift;
let ktail = u64::from_ne_bytes(tail) >> shift;
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)]