English | 简体中文
Pure-Rust, hardware-adaptive Zstandard (RFC 8878) codec — zero runtime dependencies, #![forbid(unsafe_code)], single-core L2-cache discipline, and built-in zero-dependency multi-core parallelism.
Most compression libraries either wrap C libzstd through FFI/unsafe, ship single-threaded decoders that stall on small messages, or hardcode oversized lookup tables that blow out CPU L1/L2 caches under concurrency. yolowzstd is built from first principles in 100% safe Rust (std only):
- Zero dependencies &
#![forbid(unsafe_code)]: auditable supply chain, purestd, works without a C toolchain, MSRV1.89(edition = "2024"). - Zero-dependency multi-core (
with_threads(0)):std::thread::scopeparallel chunked encoding producing valid concatenated Zstd frames plus zero-copysplit_at_mutparallel multi-frame decompression — beatinglibzstd(ZSTD_c_nbWorkers) by 2.3×–11.8× on L3/L9/L19 encoding and 3.3×–4.1× on multi-frame decoding (4.07–4.61 GB/s). - Sub-microsecond small-payload throughput: generational hash tables (
base/next_baseepoch counters, zero per-callmemset) and pooled reusable contexts achieve 4,063 MB/s decode and 225 MB/s encode on 64 B payloads (1.7× and 3.7× faster than Clibzstd). - Principled algorithmic & cache-conscious design: probes local cache sizes and SMT topology (Linux sysfs/cgroup when available, safe input-scale fallback elsewhere) and combines pure-safe-Rust 8-byte SWAR tag-filtered RowHash (
L5..=11), exact BT4 suffix trees (L12..=15), Q3 entropy-guided L2-chunked optimal parsing (L16..=22), MDL/KL-optimal FSE normalization, and 12-bit dual-symbol Huffman decoding.
Add to your Cargo.toml:
[dependencies]
yolowzstd = "0.2"use yolowzstd::{compress, decompress};
fn main() -> Result<(), yolowzstd::Error> {
let data = b"hello yolowzstd! ".repeat(256);
let compressed = compress(&data, 3);
let restored = decompress(&compressed, data.len())?;
assert_eq!(restored, data);
Ok(())
}Reuse [Compressor] and [Decompressor] to eliminate allocation overhead, and enable multi-core compression / multi-frame parallel decompression with .with_threads(0) (auto-detect) or an explicit worker count:
use yolowzstd::{CompressOptions, Compressor, DecompressOptions, Decompressor};
fn main() -> Result<(), yolowzstd::Error> {
let payload = b"{\"ok\":true,\"codec\":\"zstd\",\"safe\":true}\n".repeat(100_000);
let mut comp = Compressor::new();
let copts = CompressOptions::new(3).with_threads(0); // auto multi-core
let z = comp.compress_with(&payload, &copts);
let mut dec = Decompressor::new();
let dopts = DecompressOptions::new().with_threads(0); // auto parallel multi-frame decode
let out = dec.decompress_with(&z, &dopts, payload.len())?;
assert_eq!(out, payload);
Ok(())
}- RFC 8878 Decoder (
src/dec/, no dictionary support):- Multi-frame & skippable-frame streams, Raw / RLE / Compressed blocks (returns
Error::Unsupportedon frames requiring a dictionary) - Predefined, RLE, Custom, and Repeat FSE sequence tables with compact single-base tables and bounds-elided batch sequence execution (
src/dec/seq.rs) - 1-stream & 4-stream Huffman literals (
src/huff.rs) with tree reuse across blocks and a lazily built 12-bit dual-symbol canonical table (Moffat & Turpin 1997, 4 lookups per 64-bit load) automatically gated by exactP(l0 + l1 <= 12)tree profitability - 3-slot repeat offset state machine, single-pass sequence execution with cold-path outlining
as_chunks::<32>()XXH64 frame content checksum verification, block-boundFrame_Content_Sizepre-validation (blocking forged-FCS allocation DoS), and strict logical output-size bounds (max_out, no panics; caps decompressed output bytes rather than hard process RSS)
- Multi-frame & skippable-frame streams, Raw / RLE / Compressed blocks (returns
- Adaptive Multi-Tier Encoder (
level <= 0through22,src/enc/):level <= 0: Raw / RLE fast path1..=4: Generation-stamped L2-sized Fast / DoubleFast hash tables (src/lz/) with repeat-offset probing, SPRT-style high-entropy step skipping (Wald 1945), and incompressible Huffman skip at L15..=11: SWAR tag-filtered RowHash (src/row.rs) — 100% safe-Rust 8-byte zero-carry SWAR parallel tag filtering (SwissTable / Zstd 1.5 RowHash / Warren Hacker's Delight §6-1, §7-1), paired with a 128 KiB zero-false-positive exact-tagged 4B near-offset cache, 3B repeat-offset probing, and rate-distortion Lazy/Lazy2 lookahead12..=15: Exact BT4 suffix binary search tree (src/chain.rs) + rate-distortion lazy matching (with wideusizefallback for> 4 GiBinputs)16..=22: Entropy-guided optimal parser (src/opt/) — exact BT4 suffix tree (src/opt/bt.rs) + 1/8-bit Q3 fixed-point online Bayesian/EMA entropy model (src/opt/stats.rs, Zopfli / Zstdbtultra), L2-resident chunked DP (OPT_CHUNK = 4096) with telescoping literal-length potential and Pareto-dominance single-pass relaxation- Information-theoretic entropy backend (
src/entropy.rs,src/henc.rs,src/enc/seq_enc.rs): empirical Shannon entropy raw-skip lower bound, KL-divergence Repeat-tree fast decision, Rissanen MDL adaptive FSE table accuracy, 8.8 fixed-point exact cross-entropy mode selection, convex minimum-KL KKT integer quota normalization (Gross 1956 / Duda 2013 ANS) with RFC 8878-1support, andO(n)Kraft-truncated length-limited Huffman coding (Moffat & Katajainen 1995) - Multi-core engine (
src/mt.rs): aligns chunks to 128 KiB blocks, emits standard concatenated frames (100% interoperable with every RFC 8878 decoder), and decodes multi-frame streams with>= 2frames having validatedFrame_Content_Sizein parallel via zero-copysplit_at_mutdirectly into the output slice (falling back to single-thread otherwise).
Measured on Intel Core i7-10700 @ 2.90 GHz (8 cores / 16 threads, 15 GiB RAM, Ubuntu Linux 7.0, rustc 1.99.0, lto = "fat", codegen-units = 1), compared against C libzstd 1.5.7 (zstd crate 0.14.4 with zstdmt), Rust wrappers/ports, pure-Rust Zstd crates, and Deflate/LZ4 codecs. Full per-file tables and reproduction commands are in benchmarks/README.md.
Higher is better for speed (MB/s ↑) and ratio (↑).
| Level | yolowzstd auto Encode |
yolowzstd 8t / 16t Encode |
yolowzstd Ratio |
c_libzstd 8t / 16t Encode |
c_libzstd Ratio |
yolowzstd Parallel Decode (auto / 16t) |
c_libzstd Decode (1t) |
|---|---|---|---|---|---|---|---|
| L1 | 1,692.1 MB/s | 1,532.0 / 1,722.9 MB/s | 2.77x | 1,758.1 / 1,499.5 MB/s | 2.89x | 4,614.3 / 4,415.0 MB/s | 1,288.5 MB/s |
| L3 | 893.4 MB/s | 890.8 / 643.5 MB/s | 3.08x | 385.9 / 390.7 MB/s | 3.20x | 4,073.0 / 3,879.0 MB/s | 1,237.0 MB/s |
| L9 | 183.2 MB/s | 194.0 / 185.4 MB/s | 3.39x | 82.6 / 82.6 MB/s | 3.58x | 4,321.1 / 4,461.2 MB/s | 1,228.4 MB/s |
| L19 | 37.6 MB/s | 26.9 / 37.5 MB/s | 3.56x | 3.2 / 3.2 MB/s | 4.01x | 4,363.9 / 4,539.2 MB/s | 1,117.9 MB/s |
c_libzstduses single-frame / streamingZSTD_decompress(1 thread) because RFC 8878 single frames have sequential LZ77 dependencies;yolowzstdparallel encoding emits standard concatenated frames, allowing zero-copy parallel decoding when an input has>= 2independent frames that all declare validatedFrame_Content_Size(otherwise falling back to single-thread).
| Payload Size | yolowzstd Dec |
c_libzstd Dec |
rust-zstd Dec |
structured Dec |
yolowzstd Enc (L3) |
c_libzstd Enc (L3) |
rust-zstd Enc (L3) |
structured Enc (L3) |
|---|---|---|---|---|---|---|---|---|
| 64 B | 4,062.6 | 2,336.3 | 1,856.8 | 13.7 | 225.1 | 61.4 | 51.2 | 29.4 |
| 1 KiB | 414.7 | 583.0 | 524.6 | 144.8 | 103.6 | 168.6 | 148.1 | 137.9 |
| 4 KiB | 529.2 | 740.5 | 656.5 | 337.2 | 120.0 | 223.6 | 207.7 | 172.9 |
| 64 KiB | 681.7 | 1,061.2 | 915.2 | 752.9 | 106.0 | 195.9 | 182.8 | 177.0 |
Workload (MB/s ↑ / Ratio ↑) |
yolowzstd |
c_libzstd |
rust-zstd |
zstd-pure-rs |
structured-zstd |
zstd-rs |
oxiarc-zstd |
ruzstd |
|---|---|---|---|---|---|---|---|---|
| Decode L3 streams | 906.8 | 1,328.4 | 1,171.3 | 1,013.5 | 972.0 | 654.9 | 355.8 | 286.0 |
| Decode L19 streams | 842.6 | 1,040.7 | 931.5 | 932.9 | 830.6 | 566.5 | 344.6 | 284.8 |
| Encode L1 | 284.0 (2.86x) |
480.0 (2.89x) |
438.9 (2.89x) |
398.1 (2.89x) |
458.6 (2.89x) |
104.0 (2.87x) |
55.1 (2.71x) |
— |
| Encode L3 | 209.4 (3.13x) |
251.2 (3.20x) |
234.2 (3.20x) |
193.6 (3.20x) |
238.3 (3.19x) |
86.6 (3.18x) |
45.5 (2.88x) |
— |
| Encode L9 | 41.8 (3.55x) |
60.2 (3.59x) |
60.3 (3.59x) |
47.2 (3.59x) |
62.9 (3.57x) |
17.3 (3.52x) |
29.1 (2.89x) |
— |
- Cross-Codec L3 Encode/Decode (
MB/s ↑@ Ratio↑):yolowzstd207.6 / 836.6 @ 3.13x vslibdeflater100.0 / 1,210.5 @ 3.14x,zlib-rs81.8 / 841.1 @ 3.08x,lz4_flex650.2 / 3,227.3 @ 2.10x (yolowzstdencodes 2.1× faster thanlibdeflaterand 2.5× faster thanzlib-rsat comparable ratio). - Single-Process Peak RSS Delta (
mozillaL3,VmHWMKiB↓):- Decode delta:
rust-zstd49,276 KiB,zstd-pure-rs49,312 KiB,yolowzstd49,340 KiB,c_libzstd49,372 KiB,structured-zstd49,756 KiB,ruzstd55,828 KiB. - Encode delta:
c_libzstd17,972 KiB,zstd-pure-rs18,364 KiB,rust-zstd18,380 KiB,structured-zstd19,428 KiB,yolowzstd20,676 KiB.
- Decode delta:
yolowzstdforbidsunsafe(#![forbid(unsafe_code)]), so on single-core long-stream decode Clibzstdandunsafe-heavy / C-translated Rust crates (rust-zstd,zstd-pure-rs) remain faster per core (1,014–1,328 MB/svs906.8 MB/sat L3);yolowzstdleads all#![forbid(unsafe_code)]stream decoders (zstd-rs,oxiarc-zstd,ruzstd), surpassesstructured-zstdon L19 decode (842.6vs830.6 MB/s, reaching 81.0% of Clibzstd) andzstd-pure-rson L3 encode (209.4vs193.6 MB/s), leads 64 B encode/decode across every implementation tested (4.06 GB/s decode), and beatslibzstdacross multi-core L3/L9/L19 encoding and multi-frame decoding (4.07–4.61 GB/s).- Dictionary-compressed frames (
Dictionary_ID != 0) are not supported yet and returnError::Unsupported.max_outenforces the logical decoded output byte ceiling (and combined multi-frame budgets), rather than capping process-wide physical RSS. - Multi-core L19 partitions inputs into independent concatenated frames for parallel decode and throughput (
3.56xvs4.01xfor single-frame Clibzstdat L19) in exchange for 11.8× higher multi-core L19 encoding throughput (37.6vs3.2 MB/s) and 4.1× faster parallel decoding. At L1, fast-skipping favors throughput (284.0 MB/s) with a slight ratio tradeoff (2.86xvs2.89x).
Licensed under the MIT License.