From 988bb3edce739650088cfc965204280b5ee889a7 Mon Sep 17 00:00:00 2001 From: edef Date: Mon, 2 May 2022 18:37:30 +0000 Subject: 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 --- ripple/fossil/src/chunker/buz.rs | 9 ++++----- ripple/fossil/src/chunker/mod.rs | 7 ++++++- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'ripple/fossil/src/chunker') 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 { } impl Rolling { - pub fn from_slice(input: &[u8]) -> Rolling { - 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 { + 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::::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::::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) { -- cgit 1.4.1