Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl BatchCompressor {
let (res, size, _) =
compressor.compress(input, buf_slice, crate::compress::FlushMode::Finish);
if res == CompressResult::Success {
assert!(size <= bound);
unsafe {
buffer.set_len(size);
}
Expand Down Expand Up @@ -63,16 +64,20 @@ impl BatchDecompressor {
.map_init(
|| (Decompressor::new(), Vec::new()),
|(decompressor, buffer), (&input, &max_size)| {
buffer.clear();
if buffer.capacity() < max_size {
buffer.reserve(max_size.saturating_sub(buffer.len()));
}
unsafe {
buffer.set_len(max_size);
buffer.reserve(max_size);
}
let buf_uninit = buffer.spare_capacity_mut();
let buf_slice = &mut buf_uninit[..max_size];

let (res, _, size) = decompressor.decompress(input, buffer);
let (res, _, size) = unsafe { decompressor.decompress_uninit(input, buf_slice) };
if res == DecompressResult::Success {
Some(buffer[..size].to_vec())
assert!(size <= max_size);
unsafe {
buffer.set_len(size);
}
Some(buffer.to_vec())
} else {
None
}
Expand Down
24 changes: 7 additions & 17 deletions src/compress/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,17 +683,17 @@ impl Compressor {
let mode = if is_last { flush_mode } else { FlushMode::Sync };

let bound = Self::deflate_compress_bound(chunk.len());
buf.clear();
if buf.capacity() < bound {
buf.reserve(bound - buf.len());
}
unsafe {
buf.set_len(bound);
buf.reserve(bound);
}

let buf_uninit = slice_as_uninit_mut(buf);
let buf_uninit = buf.spare_capacity_mut();
let buf_uninit = &mut buf_uninit[..bound];

let (res, size, _) = compressor.compress(chunk, buf_uninit, mode);
if res == CompressResult::Success {
assert!(size <= bound);
unsafe {
buf.set_len(size);
}
Expand Down Expand Up @@ -920,12 +920,7 @@ impl Compressor {
self.dp_costs[0] = 0;

self.dp_path.clear();
if self.dp_path.capacity() < processed + 1 {
self.dp_path.reserve(processed + 1 - self.dp_path.len());
}
unsafe {
self.dp_path.set_len(processed + 1);
}
self.dp_path.resize(processed + 1, 0);

mf.reset();
let mut pos = 0;
Expand Down Expand Up @@ -1717,12 +1712,7 @@ impl Compressor {
self.dp_costs[0] = 0;

self.dp_path.clear();
if self.dp_path.capacity() < processed + 1 {
self.dp_path.reserve(processed + 1 - self.dp_path.len());
}
unsafe {
self.dp_path.set_len(processed + 1);
}
self.dp_path.resize(processed + 1, 0);

mf.reset();
let mut pos = 0;
Expand Down
45 changes: 27 additions & 18 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,26 @@ impl<W: Write + Send> DeflateEncoder<W> {
if !final_block {
bound += 5;
}
if output.len() < bound {
output.clear();
if output.capacity() < bound {
output
.try_reserve(bound - output.len())
.try_reserve(bound)
.map_err(io::Error::other)?;
unsafe {
output.set_len(bound);
}
}

let mode = if final_block {
crate::compress::FlushMode::Finish
} else {
crate::compress::FlushMode::Sync
};
let out_uninit = crate::common::slice_as_uninit_mut(output);
let out_uninit = output.spare_capacity_mut();
let out_uninit = &mut out_uninit[..bound];
let (res, size, _) = compressor.compress(chunk, out_uninit, mode);
if res == CompressResult::Success {
assert!(size <= bound);
unsafe {
output.set_len(size);
}
if let Some(writer) = &mut self.writer {
writer.write_all(&output[..size])?;
}
Expand All @@ -108,23 +111,26 @@ impl<W: Write + Send> DeflateEncoder<W> {
if !(final_block && i == num_chunks - 1) {
bound += 5;
}
if output.len() < bound {
output.clear();
if output.capacity() < bound {
output
.try_reserve(bound - output.len())
.try_reserve(bound)
.map_err(io::Error::other)?;
unsafe {
output.set_len(bound);
}
}

let mode = if final_block && i == num_chunks - 1 {
crate::compress::FlushMode::Finish
} else {
crate::compress::FlushMode::Sync
};
let out_uninit = crate::common::slice_as_uninit_mut(output);
let out_uninit = output.spare_capacity_mut();
let out_uninit = &mut out_uninit[..bound];
let (res, size, _) = compressor.compress(chunk, out_uninit, mode);
if res == CompressResult::Success {
assert!(size <= bound);
unsafe {
output.set_len(size);
}
Ok(size)
} else {
Err(io::Error::other("Compression failed"))
Expand Down Expand Up @@ -154,23 +160,26 @@ impl<W: Write + Send> DeflateEncoder<W> {
if !final_block {
bound += 5;
}
if output.len() < bound {
output.clear();
if output.capacity() < bound {
output
.try_reserve(bound - output.len())
.try_reserve(bound)
.map_err(io::Error::other)?;
unsafe {
output.set_len(bound);
}
}

let mode = if final_block {
crate::compress::FlushMode::Finish
} else {
crate::compress::FlushMode::Sync
};
let out_uninit = crate::common::slice_as_uninit_mut(output);
let out_uninit = output.spare_capacity_mut();
let out_uninit = &mut out_uninit[..bound];
let (res, size, _) = compressor.compress(&self.buffer, out_uninit, mode);
if res == CompressResult::Success {
assert!(size <= bound);
unsafe {
output.set_len(size);
}
if let Some(writer) = &mut self.writer {
writer.write_all(&output[..size])?;
}
Expand Down