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

Avoid useless memory copies in IPC reader. #2510

Merged
merged 1 commit into from
Aug 19, 2022
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
8 changes: 3 additions & 5 deletions arrow/src/ipc/compression/codec.rs
Original file line number Diff line number Diff line change
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

} else {
// decompress data using the codec
let mut uncompressed_buffer =
Expand Down
11 changes: 4 additions & 7 deletions arrow/src/ipc/reader.rs
Original file line number Diff line number Diff line change
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]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we cannot slice on Buffer with length, we cannot implement a_data as a Buffer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the calculation above, I think we don't need to truncate the length, as end_offset is the end of the buffer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end_offset is the end of buf, but not sure whether it is the end of a_data

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah yes, brain fart on my part. R.e. making a_data a Buffer, we could always pass the length separately...

// 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