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 (how hard to search), 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 quality first

EncoderConfig::default() uses quality 11, the most expensive search. Set the quality explicitly for your workload:

use mbrotli::{Compressor, EncoderConfig, Quality};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = EncoderConfig::default().with_quality(Quality::Q5);
    let mut encoder = Compressor::new(config)?;
    let compressed = encoder.compress(b"configured payload")?;
    assert!(!compressed.is_empty());
    Ok(())
}
QualityEncoderSearch
0–1Fast encoderFragment-based fast paths with direct entropy coding
2–9Greedy encoderHash-table match search with match scoring
10–11High-quality encoderTree search with command pricing and clustering

Speed and size depend on your data. The benchmark pages show both for every quality.

Encoder settings

EncoderConfig holds settings shared by successive streams:

SettingTypeBehavior
QualityQuality0–11; default 11
WindowWindowWindow::standard(bits) for 10–24 bits; Window::large(bits) for 10–62 bits
Input block sizeBlockSize / BlockBitsAutomatic or explicit 16–24 bits
Data modeCompressionModeGeneric, text, or font
Distance codingDistanceParamsValidated postfix bits and direct distance groups
Literal contextsLiteralContextModeControls literal context modelling

Validated values reject out-of-range inputs. Compressor::new and reconfigure check combinations, including Large Window’s quality minimum. The capability table lists the minimum quality of each format.

StreamConfig carries the declared input size and logical stream offset. Its default is unknown size and offset zero. For a complete known input, use StreamConfig::from(InputSize::Exact(input.len() as u64)).

What each quality supports

Codec capabilityAvailability
Standard Brotli (RFC 7932)Qualities 0–11
Large Window BrotliQualities 3–11
Prepared LZ77 prefix dictionariesQualities 5–11
Serialized dictionaries and custom static dictionary encodingexperimental; qualities 5–11
Headerless stream continuationsexperimental; qualities 2–11
Shared Brotli framed compressor, structured input and native sessionscompression,experimental; std or alloc
Framed encoder writer/reader adapterscompression,experimental; not available with no_std
Framed decoder, structured output and native eventsdecompression,experimental; std or alloc
Framed decoder event readerdecompression,experimental; not available with no_std

Unsupported combinations return errors. External dictionary references require the same dictionaries at the decoder. Large Window and shared-dictionary streams require a decoder that supports the corresponding extension. See dictionaries and extended formats for each format and compatibility and validation for what is checked against C.

Cargo features

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

ConfigurationDependency
Default std encoder + decodermbrotli = "0.5.21"
Encoder onlymbrotli = { version = "0.5.21", default-features = false, features = ["std", "compression"] }
Decoder onlymbrotli = { version = "0.5.21", default-features = false, features = ["std", "decompression"] }
Framing and experimental formatsmbrotli = { version = "0.5.21", features = ["experimental"] }
Alloc-backed codecs without stdmbrotli = { version = "0.5.21", default-features = false, features = ["no_std", "compression", "decompression"] }
FeatureSurface
stdRead / Write adapters and parallel compression
compression, decompressionOne codec each; they compile independently
no_stdAlloc-backed build; takes precedence over std when both are enabled
experimentalSerialized shared dictionaries, custom static encoding, nonzero stream offsets, and framing. This API may change in a patch release
diagnosticsPreparedDictionary::longest_match prefix coverage diagnostic
hotpath, hotpath-cpu, hotpath-allocOptional profiling instrumentation

no_std requires a global allocator. It excludes I/O adapters, parallel compression, framed I/O adapters, and profiling, and uses compile-time SIMD selection. Cargo features are additive: another dependency can re-enable a codec or std. Leave std and hotpath* disabled throughout the dependency graph for a std-free build.

See codec features and no_std support for the gates and their checks.