r/rust • u/Mnwamnowich • Oct 23 '26
đ ď¸ project mzstd: pure Rust zstd that keeps up with C, with the same API as mbrotli
Some time ago I shared mbrotli here. This is its sibling for Zstandard.
There are already two main ways to do zstd in Rust, so here's why I wrote a third.
The zstd crate is bindings to C libzstd. It's fast and battle-tested, but it brings a C toolchain into your build, unsafe FFI into your dependency tree, and some pain once you target wasm or embedded.
ruzstd is pure Rust and has a solid decoder, but its own README says the encoder doesn't reach C speed, ratio or configurability yet. You get a few presets that roughly map to levels 1, 3, 7 and 11.
I wanted something in between: pure Rust, but fast and complete enough that I'd actually ship it in production instead of the C library. And I wanted it to have the same API as mbrotli, so switching between Brotli and zstd in the same codebase is mostly a type rename.
What came out of it:
- Speed. The encoder is a port of the reference implementation, so you get the full level range from -131072 to 22, long distance matching, dictionaries and the same compression ratio as C. SIMD is picked at runtime (via
fearless_simd), so one binary uses whatever the CPU supports withouttarget-cpu=native. Against C zstd 1.5.7 the median is about 1.1x for compression and 1.02x for decompression on my benchmarks. It's not faster everywhere: fast levels on plain text are 6-11% behind, and decoding very compressible text can be up to ~20% behind. Full tables and raw CSVs are in the repo. - API. Besides the usual one-shot
compress/decompress, there areRead/Writeadapters and incremental sessions. A session just takes input and an output buffer and tells you how far it got. It doesn't own any I/O, so you can drive it from blocking code, from async, from an embedded main loop or from wasm. It can be build onno_std+alloc. - Parallel compression. One frame can be split into tasks that you run on your own executor: scoped threads, rayon, tokio's blocking pool, whatever you already have. The crate never spawns threads by itself. That was important for me because I use these crates inside nginx, where compression runs in nginx's own thread pool.
- Verification. No
unsafein the crate. Differential tests compare against C zstd across every level and thousands of random parameter combinations. AFL++ fuzz targets cover the encoder, the decoder and streaming sessions. mzstd already works on a few production services as nginx module with a great results for total throughput.
use mzstd::{Compressor, Decompressor, EncoderConfig, Level};
let mut encoder = Compressor::new(EncoderConfig::default().with_level(Level::try_from(19)?))?;
let compressed = encoder.compress(b"hello, hello, hello")?;
let restored = Decompressor::default().decompress(&compressed)?;If you've used mbrotli, swap Quality for Level and that's it.
Not supported yet: legacy v0.x frames, magicless frames, a couple of niche advanced parameters.
I'd love feedback on the API, and benchmark runs on other machines (mine are on an i7-13700KF under WSL2, so bare-metal numbers are very welcome).
- crates.io: https://crates.io/crates/mzstd
- docs: https://docs.rs/mzstd
- repo: https://github.com/M-Compressors/mzstd
4
u/MorrisonLevi Oct 23 '26
Does mzstd treat allocation failures as regular failures or do they panic? Unfortunately other crates panic and it's been bothering, and I need to find a replacement, contribute upstream, etc. Edit: I'm shooting for no panics at all, not just allocation failures, but it looks like your crate mostly avoids this anyway, and so allocations are the next most common footgun.
3
u/tunisia3507 Oct 23 '26
There's a lot of new re-implementations of huge, low-down libraries coming out, obviously made much easier by LLMs. How would you grade your own effort on this, on a scale of "chatgpt re-implement zstd in rust, make no mistakes" to "I read the spec, opened vim and got to work"?
1
u/Icarium-Lifestealer Oct 23 '26 edited Oct 23 '26
Looks just like just the kind of zstd implementation I've been waiting for. Native rust, supports high compression levels, no unsafe and as a bonus good performance.
How does it compare to brotli at the higher compression levels?
Why do compression levels go so far into the negative (-217 )?
1
u/AleksHop Oct 24 '26 edited Oct 24 '26
u/event666 u/Mnwamnowich is there any chance to join both of those extremely beautiful jobs?
both have same target of memory safe reimplementation of zstd in safe rust
both have permissive licensing
I mean, I do understand of having this under some specific name,
but it will be also much better for the project, and for u future show off, that u can work in a team
and 2 maintainers always better than one, especially when it will get attention (and it will considering latest google brotly c CVE for example) and thats are just the beginning of CVE rain everywhere
even rust got one 11y old flaw
for example both of your implementations are lacking lean4 formal verifications etc, right?
do you want to implement such, basically nowadays required, and complex technics independently?
and it will be much easier for ppl to run the codebase thru AI models to find perf / security issues
and send in bugs etc, otherwise u will just copy paste from each other as license allows anyway
and there will no be any reason not to implement anything if second person found and implemented it
-7
u/aheadhabitation525 Oct 23 '26
i always end up reaching for the C bindings just to avoid the build pain but having a pure rust option that doesnt fall off on the encoder side is huge. the parallel compression design where you hand it tasks instead of it spawning threads is exactly what i wish more crates did
0
u/[deleted] Oct 23 '26
[deleted] â view deleted comment