summary refs log tree commit diff
path: root/ripple/fossil/src/chunker
diff options
context:
space:
mode:
authoredef <edef@unfathomable.blue>2022-05-02 01:06:27 +0000
committeredef <edef@unfathomable.blue>2022-05-02 01:06:27 +0000
commitb3366c335d978b1485c83784a350c17c0d02d9a3 (patch)
tree0c169f8f77f572d3a3345c6d650be2e03d937d4a /ripple/fossil/src/chunker
parentacc115945c7788dea022a0899580e4326cf760a4 (diff)
downloadunf-legacy-b3366c335d978b1485c83784a350c17c0d02d9a3.tar.zst
ripple/fossil/chunker: hardcode the discriminator
This improves performance by ~17%. I had *expected* that rustc would
have reduce it to a constant already, but alas.

Change-Id: I5c15fe90244da64498d2d6562262db58242ffb24
Diffstat (limited to 'ripple/fossil/src/chunker')
-rw-r--r--ripple/fossil/src/chunker/mod.rs18
1 files changed, 15 insertions, 3 deletions
diff --git a/ripple/fossil/src/chunker/mod.rs b/ripple/fossil/src/chunker/mod.rs
index e5221c3..8dcf796 100644
--- a/ripple/fossil/src/chunker/mod.rs
+++ b/ripple/fossil/src/chunker/mod.rs
@@ -9,7 +9,9 @@ const WINDOW_SIZE: usize = 48;
 pub const MIN_CHUNK_SIZE: usize = AVG_CHUNK_SIZE / 4;
 pub const AVG_CHUNK_SIZE: usize = 64 * 1024;
 pub const MAX_CHUNK_SIZE: usize = AVG_CHUNK_SIZE * 4;
+const DISCRIMINATOR: u32 = 0xc17f;
 
+#[cfg(test)]
 fn discriminator_from_average(avg: u64) -> u32 {
 	let avg = avg as f64;
 	(avg / (-1.42888852e-7 * avg + 1.33237515)) as u32
@@ -44,12 +46,11 @@ impl<'a> Iterator for Chunker<'a> {
 			.take(MAX_CHUNK_SIZE)
 			.skip(MIN_CHUNK_SIZE);
 
-		let d = discriminator_from_average(AVG_CHUNK_SIZE as u64);
 		let mut hasher = buz::Rolling::<WINDOW_SIZE>::from_slice(&self.buffer[..MIN_CHUNK_SIZE]);
 		let chunk;
 		for (idx, byte) in bytes {
 			let buz::Hash(x) = hasher.sum();
-			if x % d == d.wrapping_sub(1) {
+			if x % DISCRIMINATOR == DISCRIMINATOR.wrapping_sub(1) {
 				// split point
 				(chunk, self.buffer) = self.buffer.split_at(idx);
 				return Some(chunk);
@@ -71,7 +72,10 @@ impl<'a> Iterator for Chunker<'a> {
 #[cfg(test)]
 mod test {
 	use {
-		super::{Chunker, MAX_CHUNK_SIZE, MIN_CHUNK_SIZE, WINDOW_SIZE},
+		super::{
+			discriminator_from_average, Chunker, AVG_CHUNK_SIZE, DISCRIMINATOR, MAX_CHUNK_SIZE,
+			MIN_CHUNK_SIZE, WINDOW_SIZE,
+		},
 		std::io::Read,
 	};
 
@@ -219,4 +223,12 @@ mod test {
 			(2, Some(MAX_CHUNK_SIZE / MIN_CHUNK_SIZE + 1))
 		);
 	}
+
+	#[test]
+	fn discriminator() {
+		assert_eq!(
+			DISCRIMINATOR,
+			discriminator_from_average(AVG_CHUNK_SIZE as u64)
+		);
+	}
 }