Skip to content

Commit

Permalink
Improve decode code of read_non_compressed_block()
Browse files Browse the repository at this point in the history
  • Loading branch information
Stargateur committed Jul 21, 2019
1 parent 2efa0ab commit 15811cb
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/deflate/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,23 @@ where
nlen
))
} else {
let old_len = self.buffer.len();
// We cannot use `self.buffer.set_len()` here because that would
// pass uninitialized memory to .read_exact(), which does
// NOT guarantee that it will never read from the buffer.
// See https://github.com/rust-lang/rust/pull/62102/
// Surprisingly, zero-initializing the buffer here
// makes decoding 5% **faster** than using reserve() + set_len()
self.buffer.resize(old_len + len as usize, 0);
self.bit_reader
.as_inner_mut()
.read_exact(&mut self.buffer[old_len..])?;
Ok(())
.take(len.into())
.read_to_end(&mut self.buffer)
.and_then(|used| {
if used != len.into() {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
format!(
"The reader has incorrect length: expected {}, read {}",
len, used
),
))
} else {
Ok(())
}
})
}
}
fn read_compressed_block<H>(&mut self, huffman: &H) -> io::Result<()>
Expand Down

0 comments on commit 15811cb

Please sign in to comment.