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 93e5e21
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/deflate/decode.rs
Expand Up @@ -82,18 +82,20 @@ 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 93e5e21

Please sign in to comment.