Brotli compression and decompression in safe Rust
mbrotli is a port of Google's Brotli encoder with a Rust-native API, plus a native Rust decoder. Reuse working memory between payloads, plug into Read and Write, drive either codec with your own buffers, and schedule parallel compression on your own threads.
A round trip
[dependencies]
mbrotli = "0.5.21"
use mbrotli::{Compressor, Decompressor, EncoderConfig, Quality};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = "hello, smaller world! ".repeat(1000);
let mut encoder = Compressor::new(EncoderConfig::default().with_quality(Quality::Q5))?;
let compressed = encoder.compress(input.as_bytes())?;
let mut decoder = Decompressor::new(Default::default())?;
assert_eq!(decoder.decompress(&compressed)?, input.as_bytes());
Ok(())
}
Requires Rust 1.89 or later. Getting started continues from here with memory reuse, streaming and feature selection.
Pick a quality
Quality is the first decision: the default is 11, the slowest and smallest. Each bar is the compressed size of the 211 MB Silesia corpus against its original size.
| Quality | Compressed size | Compress | Decompress |
|---|---|---|---|
| 0 | 37.0% | 549 MB/s | 585 MB/s |
| 2 | 32.1% | 186 MB/s | 703 MB/s |
| 5 | 28.1% | 78.2 MB/s | 788 MB/s |
| 8 | 27.0% | 21.8 MB/s | 862 MB/s |
| 11 | 23.8% | 1.23 MB/s | 713 MB/s |
mbrotli 0.5.20 in lzbench 2.4.1 on one thread of an AMD Ryzen 9 7950X VDS. Your data and machine will differ.
See the lzbench run for the command and raw output, and benchmark results for every quality against Google’s C implementation and other Rust crates.
Find your path
no_std.
Decode untrusted inputInput, output and workspace budgets.
StreamingReaders, writers, sans-I/O sessions and finish.
Parallel compressionOne input across scoped threads, Rayon or your executor.
Framing containers experimentalSeveral resources and metadata in one container.
How it worksOwnership, dispatch and state machines, with diagrams.
What you can rely on
- Safe implementation, not a C wrapper. Production code forbids
unsafe; SIMD goes throughfearless_simd. Google’s C library is only a test and benchmark oracle. - Reference-compatible output. Qualities 0–11 are compared byte for byte with Google Brotli under equivalent streaming settings. See what is claimed and how it is checked.
- No hidden I/O or threads. Sessions touch only the buffers you pass in, and parallel compression hands tasks to your scheduler. The library does not create a thread pool.
- Reusable state. A codec keeps its workspace between payloads, under a retention policy you choose.
Framing and other formats behind the experimental feature may change in a
patch release.