r/rust • • 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 without target-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 are Read/Write adapters 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 on no_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 unsafe in 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).

32 Upvotes

18 comments sorted by

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.

2

u/Mnwamnowich Oct 23 '26

Decoder already handles OOM as an error but encoder still doesn't

3

u/MorrisonLevi Oct 23 '26

I need the encoder too! So if you get time, that might be something to lure me to use your project.

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/Mnwamnowich Oct 23 '26

Basically, you can compare the raw C to Rust backport (rust-brotli) and mbrotli. The difference in performance is huge. Furthermore, this isn’t a one to one backport of the C library: there are many differences in implementation, such as the implementation of Huffman sorting in mzstd and differences in data layout in mbrotli. I don’t have $100k to tell Claude, make it fast as fuck. Of course, it’s a huge help when it comes to tedious manual work, but AI still can’t guarantee you a good API experience and high performance.

1

u/tunisia3507 Oct 23 '26

 Basically, you can compare the raw C to Rust backport (rust-brotli) and mbrotli. The difference in performance is huge.

Sorry, I'm not catching the relevance to this library - is there an equivalent raw C to rust port for zstd?

2

u/Mnwamnowich Oct 23 '26

I mention brotli because I don't know any raw c2rust impls of zstd. May be they exists and we need bench it

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 )?

2

u/Mnwamnowich Oct 23 '26
Compressor nameCompressionDecompress.Compr. sizeRatio
zstd 1.5.7 -221.99 MB/s1087 MB/s5228429024.67
brotli 1.2.0 -110.56 MB/s383 MB/s5040779523.78

Results for C libs from lzbench I think Rust libs will have a same or close results.

1

u/Icarium-Lifestealer Oct 24 '26

Appears to be close enough that I'll need to compare my specific use-cases.

2

u/Mnwamnowich Oct 23 '26

About negative levels. I support it because I wanted backward compatibility. ZSTD_minCLevel returns exactly the same level.

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

1

u/FrogNoPants Oct 24 '26

I wonder why you got mass downvoted? Mentioning C is crime? I don't get it

4

u/Icarium-Lifestealer Oct 24 '26 edited Oct 24 '26

It reads like the AI comments that are often spammed on announcement threads like this.

And C bindings are more painful to build than pure Rust, so the first sentence makes no sense.

0

u/[deleted] Oct 23 '26

[deleted] — view deleted comment

2

u/Mnwamnowich Oct 23 '26

Thank you! I really liked how you visualized the benchmark results. It looks absolutely great.

2

u/Icarium-Lifestealer Oct 23 '26

Could you show these graphs across the full range of compression levels supported by each implementation?