-
Notifications
You must be signed in to change notification settings - Fork 1k
Reuse zstd compression context when writing IPC #8405
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
Merged
+206
−36
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c3df57d
add resuable compression context for ipc writer
albertlockett 1b26f99
fix compiler errors and clippies in flight
albertlockett f829fe4
remove commented code
albertlockett e4b33d1
fix fmt in flight
albertlockett 499fcbe
PR feedback
albertlockett 4a40a43
fix compiler errors in flight and integ tests
albertlockett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,41 @@ use arrow_schema::ArrowError; | |
const LENGTH_NO_COMPRESSED_DATA: i64 = -1; | ||
const LENGTH_OF_PREFIX_DATA: i64 = 8; | ||
|
||
/// Additional context that may be needed for compression. | ||
/// | ||
/// In the case of zstd, this will contain the zstd context, which can be reused between subsequent | ||
/// compression calls to avoid the performance overhead of initialising a new context for every | ||
/// compression. | ||
pub struct CompressionContext { | ||
#[cfg(feature = "zstd")] | ||
compressor: zstd::bulk::Compressor<'static>, | ||
} | ||
|
||
// the reason we allow derivable_impls here is because when zstd feature is not enabled, this | ||
// becomes derivable. however with zstd feature want to be explicit about the compression level. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for this context |
||
#[allow(clippy::derivable_impls)] | ||
impl Default for CompressionContext { | ||
fn default() -> Self { | ||
CompressionContext { | ||
// safety: `new` here will only return error here if using an invalid compression level | ||
#[cfg(feature = "zstd")] | ||
compressor: zstd::bulk::Compressor::new(zstd::DEFAULT_COMPRESSION_LEVEL) | ||
.expect("can use default compression level"), | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for CompressionContext { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let mut ds = f.debug_struct("CompressionContext"); | ||
|
||
#[cfg(feature = "zstd")] | ||
ds.field("compressor", &"zstd::bulk::Compressor"); | ||
|
||
ds.finish() | ||
} | ||
} | ||
|
||
/// Represents compressing a ipc stream using a particular compression algorithm | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub enum CompressionCodec { | ||
|
@@ -58,6 +93,7 @@ impl CompressionCodec { | |
&self, | ||
input: &[u8], | ||
output: &mut Vec<u8>, | ||
context: &mut CompressionContext, | ||
) -> Result<usize, ArrowError> { | ||
let uncompressed_data_len = input.len(); | ||
let original_output_len = output.len(); | ||
|
@@ -67,7 +103,7 @@ impl CompressionCodec { | |
} else { | ||
// write compressed data directly into the output buffer | ||
output.extend_from_slice(&uncompressed_data_len.to_le_bytes()); | ||
self.compress(input, output)?; | ||
self.compress(input, output, context)?; | ||
|
||
let compression_len = output.len() - original_output_len; | ||
if compression_len > uncompressed_data_len { | ||
|
@@ -115,10 +151,15 @@ impl CompressionCodec { | |
|
||
/// Compress the data in input buffer and write to output buffer | ||
/// using the specified compression | ||
fn compress(&self, input: &[u8], output: &mut Vec<u8>) -> Result<(), ArrowError> { | ||
fn compress( | ||
&self, | ||
input: &[u8], | ||
output: &mut Vec<u8>, | ||
context: &mut CompressionContext, | ||
) -> Result<(), ArrowError> { | ||
match self { | ||
CompressionCodec::Lz4Frame => compress_lz4(input, output), | ||
CompressionCodec::Zstd => compress_zstd(input, output), | ||
CompressionCodec::Zstd => compress_zstd(input, output, context), | ||
} | ||
} | ||
|
||
|
@@ -175,17 +216,23 @@ fn decompress_lz4(_input: &[u8], _decompressed_size: usize) -> Result<Vec<u8>, A | |
} | ||
|
||
#[cfg(feature = "zstd")] | ||
fn compress_zstd(input: &[u8], output: &mut Vec<u8>) -> Result<(), ArrowError> { | ||
use std::io::Write; | ||
let mut encoder = zstd::Encoder::new(output, 0)?; | ||
encoder.write_all(input)?; | ||
encoder.finish()?; | ||
fn compress_zstd( | ||
input: &[u8], | ||
output: &mut Vec<u8>, | ||
context: &mut CompressionContext, | ||
) -> Result<(), ArrowError> { | ||
let result = context.compressor.compress(input)?; | ||
output.extend_from_slice(&result); | ||
Ok(()) | ||
} | ||
|
||
#[cfg(not(feature = "zstd"))] | ||
#[allow(clippy::ptr_arg)] | ||
fn compress_zstd(_input: &[u8], _output: &mut Vec<u8>) -> Result<(), ArrowError> { | ||
fn compress_zstd( | ||
_input: &[u8], | ||
_output: &mut Vec<u8>, | ||
_context: &mut CompressionContext, | ||
) -> Result<(), ArrowError> { | ||
Err(ArrowError::InvalidArgumentError( | ||
"zstd IPC compression requires the zstd feature".to_string(), | ||
)) | ||
|
@@ -227,7 +274,9 @@ mod tests { | |
let input_bytes = b"hello lz4"; | ||
let codec = super::CompressionCodec::Lz4Frame; | ||
let mut output_bytes: Vec<u8> = Vec::new(); | ||
codec.compress(input_bytes, &mut output_bytes).unwrap(); | ||
codec | ||
.compress(input_bytes, &mut output_bytes, &mut Default::default()) | ||
.unwrap(); | ||
let result = codec | ||
.decompress(output_bytes.as_slice(), input_bytes.len()) | ||
.unwrap(); | ||
|
@@ -240,7 +289,9 @@ mod tests { | |
let input_bytes = b"hello zstd"; | ||
let codec = super::CompressionCodec::Zstd; | ||
let mut output_bytes: Vec<u8> = Vec::new(); | ||
codec.compress(input_bytes, &mut output_bytes).unwrap(); | ||
codec | ||
.compress(input_bytes, &mut output_bytes, &mut Default::default()) | ||
.unwrap(); | ||
let result = codec | ||
.decompress(output_bytes.as_slice(), input_bytes.len()) | ||
.unwrap(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This always contains
zstd::bulk::Compressor
even when using lz4 compression?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do the same for
lz4_flex::frame::FrameEncoder
, does it help?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If using lz4 compression, I imagine the the
zstd
feature wouldn't be enabled and this would just be an empty struct, right?I imagine that it probably would help, although I didn't investigate as my use case was only focussed on zstd. My motivation behind adding this
CompressionContext
was that eventually it would be a good place to put something like this forlz4
. Maybe we could do this in a followup issue/PR?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A follow on sounds like a good idea to me -- I filed
lz4_flex::frame::FrameEncoder
#8423