Repository navigation
Conversation
FSE_buildCTable_wksp() and ZSTD_buildFSETable_body() spread symbols
into the table in one pass, then walk the table in position order to
build the state transitions, reading each symbol back.
The spread step, tableSize/2 + tableSize/8 + 3, is odd for every valid
tableLog, so the spread is an invertible permutation: position u holds
the symbol at sorted index (u * inv) & (tableSize-1), where inv is the
inverse of step modulo tableSize. Walking positions in order and
reading symbols straight from the sorted layout fuses the two passes,
removing the scattered writes into the table and the read-back.
Only the fast path (no low-probability symbols) changes. The
low-probability spread skips positions and is left as is. Tables are
bit-identical, so compressed output is unchanged.
Fast-path table build time, ns/table, mixed tableLog 5-9:
FSE_buildCTable_wksp ZSTD_buildFSETable
Apple M1, clang 21 449 -> 365 509 -> 389
Xeon 8481C, gcc 13 677 -> 512 600 -> 524
EPYC 9B14, gcc 13 547 -> 460 628 -> 511
End-to-end decompression of independent 4 KB to 32 KB chunks improves
by 1-2% on all three machines; 64 KB and larger blocks are unchanged.
This was referenced Nov 4, 2026
Closed
zcsizmadia
added a commit
to zcsizmadia/zstd-native
that referenced
this pull request
Nov 5, 2026
#59) (#67) * Huffman asm: tzcnt instead of bsf in the bit reload (#57) The 4X1 and 4X2 asm loops count the consumed bits of each stream with `tzcntq` instead of `bsfq` (8 sites), as meta/zstd#4888 does. The register always holds the sentinel bit, so it is never zero and both give the same result; `bsf` has a false dependency on its destination. The asm already needs BMI2, and `cpu::Features::bmi2` checks BMI1 with it, so no new gating. profile_decode/profile_encode: `ZSTD_BENCH_ITERS=n` runs exactly n iterations instead of a time limit, for `perf stat` instruction counts. EPYC 7543 (Zen 3), CPU 16, Silesia (12 files), 3 separate copies of each binary at 3 path lengths, interleaved in random order, medians; base a7d7185. The asm loops are off by default on AMD, so they are measured forced on (`native-asm`); the default path (Rust loops) does not run this code. asm path, this vs main (geomean over 12 files; per-file min .. max): L1 +1.76% (+0.43% nci .. +4.83% x-ray) L3 +1.12% (+0.09% webster .. +3.61% sao) L9 +0.79% (-0.08% dickens .. +2.16% sao) L19 +0.72% (+0.00% dickens .. +2.02% osdb) Instructions per decode: unchanged (same count). Default path, separate run with only this change (base 01c87c0): L1 +0.01%, L3 +0.43%, L9 +0.08%, L19 +0.01% (noise); small frames (1-64 KB, source and log data) +0.1..+0.3% geomean per level. Not measured on Intel, where the asm is the default. * FSE: fused spread and table build, decoder and encoder (#59) Port of meta/zstd#4836. Without low-probability (-1) symbols the spread is a permutation (the step is odd): cell `u` holds the symbol at `(u * inv) & mask` of the sorted layout, with `inv` the inverse of the step modulo the table size (`fse::inv_table_step`, three Newton steps). The decoder's `build_seq_table_fast` and the encoder's `FseCTable::build` now walk the cells in order and read the sorted layout with stride `inv`, instead of scattering the symbols into a cell array and reading them back. Tables are bit-identical; the -1 path is unchanged. Tests: `inv_table_step` for every table log; the fused decoder build against the reference spread for every code kind and table log, flat and skewed distributions, with and without -1 symbols; the encoder state table against a plain spread + read-back reference for every table log 5-12 in the sequence, Huffman-weight and 256-symbol table types. A wrong inverse fails all of them. EPYC 7543 (Zen 3), CPU 16, 3 separate copies of each binary at 3 path lengths, interleaved in random order, medians; base a7d7185 (with #57; C is libzstd 1.5.7 from the zstd crate). Decode, Silesia (12 files), this vs main, geomean (per-file min..max), instructions per decode: L1 -0.01% (-0.50 .. +0.44) -0.08% L3 +0.24% (-0.97 .. +1.32) -0.14% L9 +0.26% (-0.58 .. +0.97) -0.12% L19 +0.30% (-0.44 .. +1.22) -0.20% (main vs C: +6.7%, +10.0%, +11.1%, +11.6%) Decode, small frames (small_frames, source and log data, 1/4/16/64 KB): L1 +1.33%, L3 +1.79%, L9 +2.06%, L19 +1.37% geomean; up to +3.8% at 4-16 KB, 64 KB -0.3 .. +1.1%. Encode (profile_encode), this vs main, instructions per run: Silesia concatenated L1 -0.18% L2 -0.55% L3 +0.32% L4 +0.18% L9 +0.00% (xml L19 +0.27%); instr -0.01..-0.05% source:16384 L1 +0.93% L2 +0.75% L3 +0.18% L4 +0.37% L9 +0.27% L19 +0.03%; instr -0.77..-0.01% The Silesia L1/L2 dips are within noise: in the same run a build whose encoder code is byte-identical to main measured L1 -0.33%, L2 -0.96%. * fse tests: 2 cases per table log under Miri
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
FSE_buildCTable_wksp()andZSTD_buildFSETable()build their tables in two passes: spread the symbols into the table, then walk it in position order and read each symbol back. This PR fuses the two passes on the fast path (no low-probability symbols).FSE_TABLESTEP()is odd for every validtableLog, so the spread is an invertible permutation: positionuholds the symbol at sorted index(u * inv) & (tableSize-1), withinvthe inverse of the step modulotableSize. Walking positions in order and reading symbols from the sorted layout removes the scattered writes and the read-back.FSE_invTableStep()(new, infse.h) computesinvand asserts it.Tables are bit-identical, so the format and the compressed output are unchanged. The low-probability path is left as is. No API change.
Performance
Default
makeflags. Apple M1 (clang 21), Intel Xeon 8481C and AMD EPYC 9B14 (gcc 13.3, Ubuntu 24.04).Table build, ns per table, 256 random distributions with tableLog 5 to 9, min of 7 rounds, base and new interleaved:
FSE_buildCTable_wkspZSTD_buildFSETableDecompression speed,
zstd -b1 -B<chunk> -i3on 3.4 MB of C source, two interleaved runs:Compression speed within ±1%, compressed sizes identical. Not measured: other data sets, levels above 1, MSVC, 32-bit.
Verification
tests/fuzzer.ccompares both builders against a plain spread-then-read-back reference for every tableLog, with and without low-probability symbols. It fails on three mutants, including a wrong but shared inverse that keeps encoder and decoder consistent.fuzzer,zstreamtestanddecodecorpus -tpass with asserts enabled.make staticAnalyzereports the same findings ondevand on this branch.FSE_buildDTable_internal()regressed 5 to 23% on Sapphire Rapids with gcc, so it is left out.Checklist