Problem
Our Huffman decode path in zstd/src/huff0/huff0_decoder.rs and zstd/src/decoding/literals_section_decoder.rs is scalar/table-driven only. The donor uses specialized decode loops and arch dispatch in lib/decompress/huf_decompress.c and lib/decompress/huf_decompress_amd64.S to reduce branch and memory stalls.
This leaves a material decode-speed gap on literal-heavy corpora.
Goal
Add architecture-specialized Huffman decode kernels with runtime CPU dispatch, covering x86-64 and ARM targets, while preserving the current safe scalar fallback.
Implementation plan
Core abstraction
- Introduce internal decode-kernel abstraction (scalar, x86 BMI2, x86 SIMD, ARM NEON).
- Add runtime selection once per decoder initialization via `std::arch::is_x86_feature_detected!` / `std::arch::is_aarch64_feature_detected!`.
- Keep exact bitstream semantics and error behavior parity with the scalar path.
x86-64 kernels
- BMI2 kernel: `bzhi`/`pext` for bit extraction in Huffman symbol decode loop.
- AVX2 kernel: Vectorized 4-stream interleaved decode — process 4×8 symbols per iteration with gather/scatter table lookups.
- AVX-512 VBMI2 kernel (Ice Lake+): Use `vpcompressb`/`vpexpandb` for variable-length bit extraction on 64 symbols in parallel. This is the highest-impact x86 optimization — C zstd v1.5.6+ shows 4-8x Huffman throughput gain with VBMI2 on literal-heavy blocks.
ARM kernels
- NEON kernel: `vld1q_u8` + table lookup via `vqtbl1q_u8` for 16 symbols per iteration. NEON is baseline on all AArch64, so this becomes the default ARM fast path.
- SVE/SVE2 kernel (Graviton 3+, Apple M4+): Scalable vector width (128-2048 bit) enables processing variable numbers of symbols per iteration with predicated loads. Lower priority — NEON covers most ARM hardware.
Testing & benchmarks
- Add corpus benchmarks focused on literals-heavy blocks and mixed blocks, per-kernel.
- Gate unsafe/SIMD code behind feature/cfg checks with deterministic fallback.
- Cross-validate byte-exact output across all kernels.
Acceptance criteria
Performance expectations
- BMI2 kernel: ~1.3-1.5x over scalar
- AVX2 kernel: ~2-3x over scalar
- VBMI2 kernel: ~4-8x over scalar (on Ice Lake+)
- NEON kernel: ~1.5-2x over scalar
Dependencies
Estimate
4d (BMI2 + AVX2 + NEON: 2d 4h, VBMI2 + SVE: 1d 4h)
Problem
Our Huffman decode path in zstd/src/huff0/huff0_decoder.rs and zstd/src/decoding/literals_section_decoder.rs is scalar/table-driven only. The donor uses specialized decode loops and arch dispatch in lib/decompress/huf_decompress.c and lib/decompress/huf_decompress_amd64.S to reduce branch and memory stalls.
This leaves a material decode-speed gap on literal-heavy corpora.
Goal
Add architecture-specialized Huffman decode kernels with runtime CPU dispatch, covering x86-64 and ARM targets, while preserving the current safe scalar fallback.
Implementation plan
Core abstraction
x86-64 kernels
ARM kernels
Testing & benchmarks
Acceptance criteria
Performance expectations
Dependencies
Estimate
4d (BMI2 + AVX2 + NEON: 2d 4h, VBMI2 + SVE: 1d 4h)