changed key hash

This commit is contained in:
Fabian Schmidt 2024-08-28 09:37:38 +02:00
parent ac5c45f8d5
commit ea06a600ce

View File

@ -1,25 +1,12 @@
#[inline] #[inline]
pub fn bytes(bytes: &[u8]) -> u64 { pub fn bytes(bytes: &[u8]) -> u64 {
// hash from https://curiouscoding.nl/posts/1brc/ still wrong for measurements3.txt (and slower?) // inspired by https://curiouscoding.nl/posts/1brc/
//let mut key = [0u8; 8]; let head: [u8; 8] = unsafe { bytes.get_unchecked(..8).as_chunks::<8>().0[0] };
//let l = bytes.len().min(8); let tail: [u8; 8] = unsafe { bytes.get_unchecked(bytes.len() - 8..).as_chunks::<8>().0[0] };
//key[..l].copy_from_slice(&bytes[..l]); let shift = 64usize.saturating_sub(8 * bytes.len());
//key[0] ^= bytes.len() as u8; let khead = u64::from_ne_bytes(head) << shift;
//u64::from_ne_bytes(key) let ktail = u64::from_ne_bytes(tail) >> shift;
let mut hash: u64 = 0; khead + ktail //let mut hash: u64 = 0;
let (chunks, remainder) = bytes.as_chunks::<8>();
for &chunk in chunks {
hash = hash.wrapping_add(u64::from_be_bytes(chunk));
}
let mut r = [0_u8; 8];
r[0] = remainder.len() as u8;
let mut idx = 1;
for &byte in remainder {
r[idx] = byte;
idx += 1;
}
hash += u64::from_be_bytes(r);
hash
} }
#[cfg(test)] #[cfg(test)]