Skip to content

Commit

Permalink
avoid useless copies (#2510)
Browse files Browse the repository at this point in the history
Signed-off-by: remzi <13716567376yh@gmail.com>

Signed-off-by: remzi <13716567376yh@gmail.com>
  • Loading branch information
HaoYang670 committed Aug 19, 2022
1 parent 601968b commit ebe3d43
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
8 changes: 3 additions & 5 deletions arrow/src/ipc/compression/codec.rs
Expand Up @@ -92,18 +92,16 @@ impl CompressionCodec {
/// [8 bytes]: uncompressed length
/// [remaining bytes]: compressed data stream
/// ```
pub(crate) fn decompress_to_buffer(&self, input: &[u8]) -> Result<Buffer> {
pub(crate) fn decompress_to_buffer(&self, input: &Buffer) -> Result<Buffer> {
// read the first 8 bytes to determine if the data is
// compressed
let decompressed_length = read_uncompressed_size(input);
let buffer = if decompressed_length == 0 {
// emtpy
let empty = Vec::<u8>::new();
Buffer::from(empty)
Buffer::from([])
} else if decompressed_length == LENGTH_NO_COMPRESSED_DATA {
// no compression
let data = &input[(LENGTH_OF_PREFIX_DATA as usize)..];
Buffer::from(data)
input.slice(LENGTH_OF_PREFIX_DATA as usize)
} else {
// decompress data using the codec
let mut uncompressed_buffer =
Expand Down
11 changes: 4 additions & 7 deletions arrow/src/ipc/reader.rs
Expand Up @@ -53,14 +53,11 @@ fn read_buffer(
) -> Result<Buffer> {
let start_offset = buf.offset() as usize;
let end_offset = start_offset + buf.length() as usize;
let buf_data = &a_data[start_offset..end_offset];
let buf_data = Buffer::from(&a_data[start_offset..end_offset]);
// corner case: empty buffer
if buf_data.is_empty() {
return Ok(Buffer::from(buf_data));
}
match compression_codec {
Some(decompressor) => decompressor.decompress_to_buffer(buf_data),
None => Ok(Buffer::from(buf_data)),
match (buf_data.is_empty(), compression_codec) {
(true, _) | (_, None) => Ok(buf_data),
(false, Some(decompressor)) => decompressor.decompress_to_buffer(&buf_data),
}
}

Expand Down

0 comments on commit ebe3d43

Please sign in to comment.