diff options
author | edef <edef@unfathomable.blue> | 2022-05-02 18:37:30 +0000 |
---|---|---|
committer | edef <edef@unfathomable.blue> | 2022-05-02 18:37:30 +0000 |
commit | 988bb3edce739650088cfc965204280b5ee889a7 (patch) | |
tree | 10317b597535695a93f7cede3396223ca672de4a | |
parent | 71c71783d4efa7f44af910daf015e6972f069472 (diff) | |
download | unf-legacy-988bb3edce739650088cfc965204280b5ee889a7.tar.zst |
ripple/fossil/chunker: remove hasher initialisation bounds check
We already check for `self.buffer.len() <= MIN_CHUNK_SIZE`, but LLVM doesn't seem to notice. This boosts throughput by 35%. Change-Id: I1a0e07d276dcc285f8dec3149a629cb6e865c286
-rw-r--r-- | ripple/fossil/src/chunker/buz.rs | 9 | ||||
-rw-r--r-- | ripple/fossil/src/chunker/mod.rs | 7 |
2 files changed, 10 insertions, 6 deletions
diff --git a/ripple/fossil/src/chunker/buz.rs b/ripple/fossil/src/chunker/buz.rs index cd12833..6ddb08b 100644 --- a/ripple/fossil/src/chunker/buz.rs +++ b/ripple/fossil/src/chunker/buz.rs @@ -24,11 +24,10 @@ pub struct Rolling<const N: usize> { } impl<const N: usize> Rolling<N> { - pub fn from_slice(input: &[u8]) -> Rolling<N> { - let last_chunk = input - .rchunks_exact(N) - .next() - .expect("need at least Rolling::WINDOW_SIZE bytes"); + /// SAFETY: `input.len() >= N` must hold + #[inline] + pub unsafe fn from_slice_unchecked(input: &[u8]) -> Rolling<N> { + let last_chunk = input.rchunks_exact(N).next().unwrap_unchecked(); let mut window = [0; N]; window.copy_from_slice(last_chunk); diff --git a/ripple/fossil/src/chunker/mod.rs b/ripple/fossil/src/chunker/mod.rs index 16e81a2..88414a2 100644 --- a/ripple/fossil/src/chunker/mod.rs +++ b/ripple/fossil/src/chunker/mod.rs @@ -49,7 +49,12 @@ impl<'a> Iterator for Chunker<'a> { } let bytes = self.buffer.iter().take(MAX_CHUNK_SIZE).skip(MIN_CHUNK_SIZE); - let mut hasher = buz::Rolling::<WINDOW_SIZE>::from_slice(&self.buffer[..MIN_CHUNK_SIZE]); + let mut hasher = unsafe { + // SAFETY: `self.buffer.len > MIN_CHUNK_SIZE`, so this is in bounds + buz::Rolling::<WINDOW_SIZE>::from_slice_unchecked( + self.buffer.get_unchecked(..MIN_CHUNK_SIZE), + ) + }; for byte in bytes { let buz::Hash(x) = hasher.sum(); if x % DISCRIMINATOR == DISCRIMINATOR.wrapping_sub(1) { |