Skip to main content

CompressContext

Struct CompressContext 

Source
pub struct CompressContext { /* private fields */ }
Expand description

Compression context holding state during compression.

Implementations§

Source§

impl CompressContext

Source

pub fn new(level: CompressionLevel) -> Self

Create a new compression context.

Source

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.

Source

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.

Source

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.

Source

pub fn custom_tables(&self) -> Option<&CustomFseTables>

Get the custom FSE tables, if any.

Source

pub fn custom_huffman(&self) -> Option<&CustomHuffmanTable>

Get the custom Huffman table, if any.

Source

pub fn arena_peak_usage(&self) -> usize

Get the arena’s peak usage (useful for tuning arena size).

Source

pub fn set_dictionary_id(&mut self, dict_id: u32)

Set dictionary ID for dictionary compression.

Source

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.

Source

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.

Source

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
Source

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.

Source

pub fn compress_speculative_best(&self, input: &[u8]) -> Result<Vec<u8>>

Compress using best speculative compression.

Uses aggressive strategies for maximum compression ratio.

Source

pub fn arena(&self) -> &Arena

Get access to the arena for temporary allocations.

This can be used by callers who want to use arena-allocated buffers for input data preparation.

Trait Implementations§

Source§

impl Debug for CompressContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.