Skip to content

Commit

Permalink
feat(deflate): evade a bounds check in deflate for a small perf impro…
Browse files Browse the repository at this point in the history
…vement
  • Loading branch information
oyvindln committed May 29, 2024
1 parent 2ba520a commit b4baed3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ mod oxide {
1,
"benches/data/bin"
);
compress_bench!(
compress_bin_lvl_2,
tdefl_compress_mem_to_heap,
2,
"benches/data/bin"
);
compress_bench!(
compress_bin_lvl_6,
tdefl_compress_mem_to_heap,
Expand Down
6 changes: 6 additions & 0 deletions miniz_oxide/src/deflate/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,8 @@ impl DictOxide {
let pos = pos & LZ_DICT_SIZE_MASK;
let end = pos + 4;
// Somehow this assertion makes things faster.
// TODO: as of may 2024 this does not seem to make any difference
// so consider removing.
assert!(end < LZ_DICT_FULL_SIZE);

let bytes: [u8; 4] = self.b.dict[pos..end].try_into().unwrap();
Expand All @@ -1252,6 +1254,10 @@ impl DictOxide {
/// type T.
#[inline]
fn read_unaligned_u64(&self, pos: usize) -> u64 {
// Help evade bounds/panic code check by masking the position value
// This provides a small speedup at the cost of an instruction or two instead of
// having to use unsafe.
let pos = pos & LZ_DICT_SIZE_MASK;
let bytes: [u8; 8] = self.b.dict[pos..pos + 8].try_into().unwrap();
u64::from_le_bytes(bytes)
}
Expand Down

0 comments on commit b4baed3

Please sign in to comment.