Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Configuration and features

Three things are configured separately: the encoder (level and parameters), the stream (what you know about one input), and the Cargo features (which codecs and adapters are compiled in). Decoder settings are covered in decompression and limits.

Choose a level first

EncoderConfig::default() uses level 3. Levels run from -131072 to 22; negative levels trade size for speed, and level 0 is an alias for 3.

use mzstd::{Compressor, EncoderConfig, Level};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = EncoderConfig::default().with_level(Level::try_from(19)?);
    let mut encoder = Compressor::new(config)?;
    let compressed = encoder.compress(b"configured payload")?;
    assert!(!compressed.is_empty());
    Ok(())
}

Speed and size depend on your data. The benchmark pages show both for every measured level. Serial output is byte-identical to zstd 1.5.7 at every level, so compressed sizes match the reference.

Encoder settings

EncoderConfig holds settings shared by successive frames:

SettingType / methodBehavior
Compression levelLevel / with_level-131072–22; default 3; 0 aliases 3
Advanced compression parametersCompressionParams / with_paramsWindow, hash/chain/search logs, match length, target length, strategy
Long distance matchingLongDistanceMatching / with_long_distance_matchingAutomatic, disabled, or explicit LdmParams
Content checksumwith_checksumAdd a checksum to the frame
Frame formatwith_formatStandard or magicless
Target compressed block sizewith_target_compressed_block_sizeOptional NonZeroU32 target, like ZSTD_c_targetCBlockSize

Explicit parameters override the level as ZSTD_CCtx_setParameter does. Compressor::new validates parameter combinations. CParameter provides a validated parameter-by-ID interface for integrations; see OpenZL integration.

StreamConfig carries an InputSize: unknown by default, or InputSize::Exact(input.len() as u64) when the full length is known. Declared sizes are checked. Session scheduling is part of the byte-identity contract.

Explicit parameters, a checksum and long distance matching on top of a level:

use mzstd::{CompressionParams, Compressor, EncoderConfig, LdmParams, Level, LongDistanceMatching, Strategy};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = EncoderConfig::default()
        .with_level(Level::try_from(9)?)
        .with_checksum(true)
        .with_params(CompressionParams::default().with_window_log(24).with_strategy(Strategy::Lazy2))
        .with_long_distance_matching(LongDistanceMatching::Enabled(LdmParams::default()));
    let mut encoder = Compressor::new(config)?;
    assert!(!encoder.compress(b"tuned payload")?.is_empty());
    Ok(())
}

See dictionaries and extended formats for dictionaries, large windows, long distance matching and magicless frames.

Cargo features

The default feature set is std, compression, and decompression.

ConfigurationDependency
Default std encoder + decodermzstd = "0.2.13"
Encoder onlymzstd = { version = "0.2.13", default-features = false, features = ["std", "compression"] }
Decoder onlymzstd = { version = "0.2.13", default-features = false, features = ["std", "decompression"] }
Framing containersmzstd = { version = "0.2.13", features = ["framed"] }
Alloc-backed codecs without stdmzstd = { version = "0.2.13", default-features = false, features = ["no_std", "compression", "decompression"] }
FeatureDefaultEnables
compressionyesmzstd::compressor
decompressionyesmzstd::decompressor
stdyesstd::error::Error, mzstd::io, parallel compression
no_stdnoAlloc-only build; overrides std and removes std-only I/O and parallel APIs
framednomzstd::framing: seekable containers of resources and metadata. Its API may change in a patch release
hotpath, hotpath-cpu, hotpath-alloc, hotpath-callgrind, hotpath-cloudnoProfiling anchors; Callgrind instruction counts and hotpath Cloud upload

See codec features and no_std support for the gates and the checked combinations.

Not supported

  • External sequence producers
  • Legacy (v0.x) frames
  • Byte identity with zstd’s multithreaded mode
  • Dictionaries in parallel compression and the C ABI