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

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.

QualityCompressed sizeCompressDecompress
037.0%549 MB/s585 MB/s
232.1%186 MB/s703 MB/s
528.1%78.2 MB/s788 MB/s
827.0%21.8 MB/s862 MB/s
1123.8%1.23 MB/s713 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

What you can rely on

  • Safe implementation, not a C wrapper. Production code forbids unsafe; SIMD goes through fearless_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.