Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve decode code of read_non_compressed_block() #39

Merged
merged 1 commit into from Jul 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/deflate/decode.rs
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