pub struct CompressContext { /* private fields */ }Expand description
Compression context holding state during compression.
Implementations§
Source§impl CompressContext
impl CompressContext
Sourcepub fn new(level: CompressionLevel) -> Self
pub fn new(level: CompressionLevel) -> Self
Create a new compression context.
Sourcepub fn with_custom_tables(
level: CompressionLevel,
custom_tables: CustomFseTables,
) -> Self
pub fn with_custom_tables( level: CompressionLevel, custom_tables: CustomFseTables, ) -> Self
Create a compression context with custom FSE tables.
Custom tables allow overriding the predefined FSE tables used for sequence encoding. This can improve compression ratio when the data has symbol distributions that differ from the predefined tables.
Sourcepub fn with_options(
level: CompressionLevel,
custom_tables: Option<CustomFseTables>,
custom_huffman: Option<CustomHuffmanTable>,
) -> Self
pub fn with_options( level: CompressionLevel, custom_tables: Option<CustomFseTables>, custom_huffman: Option<CustomHuffmanTable>, ) -> Self
Create a compression context with all custom options.
This is the most flexible constructor, allowing both custom FSE tables and custom Huffman tables to be specified.
Sourcepub fn with_arena_size(level: CompressionLevel, arena_size: usize) -> Self
pub fn with_arena_size(level: CompressionLevel, arena_size: usize) -> Self
Create a new compression context with a custom arena size.
Larger arena sizes can improve performance for large inputs by reducing the number of heap allocations during compression.
Sourcepub fn custom_tables(&self) -> Option<&CustomFseTables>
pub fn custom_tables(&self) -> Option<&CustomFseTables>
Get the custom FSE tables, if any.
Sourcepub fn custom_huffman(&self) -> Option<&CustomHuffmanTable>
pub fn custom_huffman(&self) -> Option<&CustomHuffmanTable>
Get the custom Huffman table, if any.
Sourcepub fn arena_peak_usage(&self) -> usize
pub fn arena_peak_usage(&self) -> usize
Get the arena’s peak usage (useful for tuning arena size).
Sourcepub fn set_dictionary_id(&mut self, dict_id: u32)
pub fn set_dictionary_id(&mut self, dict_id: u32)
Set dictionary ID for dictionary compression.
Sourcepub fn compress(&mut self, input: &[u8]) -> Result<Vec<u8>>
pub fn compress(&mut self, input: &[u8]) -> Result<Vec<u8>>
Compress data into a Zstd frame.
By default, does NOT include a checksum (matching reference zstd level 1 behavior).
Use compress_with_checksum for checksum-protected frames.
Sourcepub fn compress_with_checksum(&mut self, input: &[u8]) -> Result<Vec<u8>>
pub fn compress_with_checksum(&mut self, input: &[u8]) -> Result<Vec<u8>>
Compress data into a Zstd frame with XXH64 checksum.
The checksum is the lower 32 bits of XXH64 of the original uncompressed content.
Sourcepub fn compress_speculative(&self, input: &[u8]) -> Result<Vec<u8>>
pub fn compress_speculative(&self, input: &[u8]) -> Result<Vec<u8>>
Compress using speculative multi-path compression.
This runs multiple compression strategies in parallel (when the parallel
feature is enabled) and returns the smallest result. This provides optimal
compression without needing to know the data characteristics in advance.
When to use:
- When compression ratio matters more than latency
- For mixed content where the optimal strategy varies
- When CPU cores are available for parallel execution
Performance characteristics:
- Uses all available CPU cores via work-stealing
- Minimal overhead for small inputs (< 4KB uses single-threaded)
- Near-linear scaling for large inputs
Sourcepub fn compress_speculative_fast(&self, input: &[u8]) -> Result<Vec<u8>>
pub fn compress_speculative_fast(&self, input: &[u8]) -> Result<Vec<u8>>
Compress using fast speculative compression.
Uses fewer strategies for lower latency while still trying multiple approaches.