Skip to content

Commit

Permalink
fn emit_dist_static: implicitly use the static dist tree
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Dec 16, 2024
1 parent 152ce7c commit 5be3d04
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
6 changes: 3 additions & 3 deletions zlib-rs/src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,11 +1114,12 @@ impl<'a> BitWriter<'a> {
match_bits_len
}

pub(crate) fn emit_dist_static(&mut self, dtree: &[Value], lc: u8, dist: u16) -> usize {
pub(crate) fn emit_dist_static(&mut self, lc: u8, dist: u16) -> usize {
let precomputed_len = trees_tbl::STATIC_LTREE_ENCODINGS[lc as usize];
let mut match_bits = precomputed_len.code() as u64;
let mut match_bits_len = precomputed_len.len() as usize;

let dtree = self::trees_tbl::STATIC_DTREE.as_slice();
let (dist_match_bits, dist_match_bits_len) = encode_dist(dtree, dist);

match_bits |= dist_match_bits << match_bits_len;
Expand Down Expand Up @@ -1494,15 +1495,14 @@ impl<'a> State<'a> {

fn compress_block_static_trees(&mut self) {
let ltree = self::trees_tbl::STATIC_LTREE.as_slice();
let dtree = self::trees_tbl::STATIC_DTREE.as_slice();
for chunk in self.sym_buf.filled().chunks_exact(3) {
let [dist_low, dist_high, lc] = *chunk else {
unreachable!("out of bound access on the symbol buffer");
};

match u16::from_le_bytes([dist_low, dist_high]) {
0 => self.bit_writer.emit_lit(ltree, lc) as usize,
dist => self.bit_writer.emit_dist_static(dtree, lc, dist),
dist => self.bit_writer.emit_dist_static(lc, dist),
};
}

Expand Down
8 changes: 3 additions & 5 deletions zlib-rs/src/deflate/algorithm/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,9 @@ pub fn deflate_quick(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockSt
// in `emit_dist_static`, dist must be less than DIST_CODE_LEN, so u16 has plenty of space
let dist = u16::try_from(dist).unwrap();

state.bit_writer.emit_dist_static(
StaticTreeDesc::D.static_tree,
(match_len - STD_MIN_MATCH) as u8,
dist,
);
state
.bit_writer
.emit_dist_static((match_len - STD_MIN_MATCH) as u8, dist);
state.lookahead -= match_len;
state.strstart += match_len;
continue;
Expand Down

0 comments on commit 5be3d04

Please sign in to comment.