use std::sync::OnceLock; static GEAR: OnceLock<[u64; 256]> = OnceLock::new(); fn build_gear() -> [u64; 256] { let mut table = [0u64; 256]; for (index, slot) in table.iter_mut().enumerate() { let mut hasher = blake3::Hasher::new(); hasher.update(b"veins/fastcdc/gear/v1"); hasher.update(&[index as u8]); let digest = hasher.finalize(); let mut value = [0u8; 8]; value.copy_from_slice(&digest.as_bytes()[..8]); *slot = u64::from_le_bytes(value); } table } pub fn gear_table() -> &'static [u64; 256] { GEAR.get_or_init(build_gear) } pub fn next_chunk(data: &[u8]) -> usize { const MIN_SIZE: usize = 16 * 1024; const AVG_SIZE: usize = 64 * 1024; const MAX_SIZE: usize = 256 * 1024; const MASK_SMALL: u64 = 0x3ffff; const MASK_LARGE: u64 = 0x3fff; let length = data.len(); if length <= MIN_SIZE { return length; } let limit = length.min(MAX_SIZE); let normal = AVG_SIZE.min(limit); let gear = gear_table(); let mut fingerprint = 0u64; let mut index = MIN_SIZE; unsafe { while index < normal { let byte = *data.get_unchecked(index); fingerprint = (fingerprint >> 1).wrapping_add(gear[byte as usize]); if fingerprint & MASK_SMALL == 0 { return index + 1; } index += 1; } while index < limit { let byte = *data.get_unchecked(index); fingerprint = (fingerprint >> 1).wrapping_add(gear[byte as usize]); if fingerprint & MASK_LARGE == 0 { return index + 1; } index += 1; } } limit }