Skip to content

Commit

Permalink
Slightly improve the performance of fastcdc by ~30%
Browse files Browse the repository at this point in the history
Made some minor changes to give fastcdc a slight boost in speed
by getting some of the rust-checks out of the way.
  • Loading branch information
allada committed Nov 10, 2021
1 parent 2dba31c commit 3063d2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
41 changes: 21 additions & 20 deletions util/fastcdc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ impl Decoder for FastCDC {
if buf.len() <= self.min_size {
return Ok(None);
}
let mut found_split_point = Option::<usize>::None;
// Zero means not found.
let mut split_point = 0;

let start_point = std::cmp::max(self.state.position, self.min_size);

for i in start_point..buf.len() {
// Note: We use this kind of loop because it improved performance of this loop by 20%.
let mut i = start_point;
while i < buf.len() {
let byte = buf[i] as usize;
self.state.hash = (self.state.hash >> 1) + TABLE[byte];

Expand All @@ -102,29 +105,27 @@ impl Decoder for FastCDC {
} else {
self.mask_easy
};
if (self.state.hash & mask) == 0 {
found_split_point = Some(i);
break;
}
if i >= self.max_size {
found_split_point = Some(i);
if (self.state.hash & mask) == 0 || i >= self.max_size {
split_point = i;
break;
}
i += 1;
}

if let Some(split_point) = found_split_point {
if split_point >= self.min_size {
self.state.reset();
assert!(
split_point <= self.max_size,
"Expected {} < {}",
split_point,
self.max_size
);
return Ok(Some(buf.split_to(split_point)));
}
if split_point >= self.min_size {
self.state.reset();
debug_assert!(
split_point <= self.max_size,
"Expected {} < {}",
split_point,
self.max_size
);
return Ok(Some(buf.split_to(split_point)));
}
self.state.position = split_point;
if self.state.position == 0 {
self.state.position = buf.len();
}
self.state.position = found_split_point.unwrap_or(buf.len());

return Ok(None);
}
Expand Down
1 change: 0 additions & 1 deletion util/tests/fastcdc_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ mod fastcdc_tests {
rng.fill(&mut data[..]);
data
};
// file.read_to_end(&mut data).await?;
let mut cursor = Cursor::new(&data);
let mut frame_reader = FramedRead::new(&mut cursor, FastCDC::new(1024, 2048, 4096));

Expand Down

0 comments on commit 3063d2c

Please sign in to comment.