Skip to content

FSE: fuse symbol spreading with table building on the fast path - #4836

Open
deadcaf3 wants to merge 1 commit into
meta:devfrom
deadcaf3:fse-fused-table-build
Open

deadcaf3 wants to merge 1 commit into
meta:devfrom
deadcaf3:fse-fused-table-build

Conversation

@deadcaf3

Copy link
Copy Markdown

What and why

FSE_buildCTable_wksp() and ZSTD_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 valid tableLog, so the spread is an invertible permutation: position u holds the symbol at sorted index (u * inv) & (tableSize-1), with inv the inverse of the step modulo tableSize. Walking positions in order and reading symbols from the sorted layout removes the scattered writes and the read-back. FSE_invTableStep() (new, in fse.h) computes inv and 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 make flags. 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:

CPU FSE_buildCTable_wksp ZSTD_buildFSETable
Apple M1 449 → 365 (−19%) 509 → 389 (−24%)
Intel 8481C 677 → 512 (−24%) 600 → 524 (−13%)
AMD 9B14 547 → 460 (−16%) 628 → 511 (−19%)

Decompression speed, zstd -b1 -B<chunk> -i3 on 3.4 MB of C source, two interleaved runs:

chunk Intel AMD M1
4 KB +0.9% +2.1% +1.6%
16 KB +0.7% +2.1% +1.9%
32 KB +0.1% +1.1% +0.8%
64 KB −0.2% +0.3% 0%

Compression speed within ±1%, compressed sizes identical. Not measured: other data sets, levels above 1, MSVC, 32-bit.

Verification

  • New unit test in tests/fuzzer.c compares 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, zstreamtest and decodecorpus -t pass with asserts enabled. make staticAnalyze reports the same findings on dev and on this branch.
  • Applying the same fusion to FSE_buildDTable_internal() regressed 5 to 23% on Sapphire Rapids with gcc, so it is left out.

Checklist

  • I searched open pull requests and issues: this problem is not already being addressed.
  • Bug fix: it comes with a reproducer or a test that fails without the fix. (not a bug fix)
  • Performance change: numbers from repeated runs are included, with CPU, compiler, flags and data.
  • I stated below whether AI or other automated tools were used.

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.
@meta-cla meta-cla Bot added the CLA Signed label Oct 10, 2026
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant