Skip to content

decompress: keep prevOffset in local scalars in ZSTD_decodeSequence - #5053

Open
JoeAzar wants to merge 1 commit into
meta:devfrom
JoeAzar:pr1-scalar-prevoffset
Open

JoeAzar wants to merge 1 commit into
meta:devfrom
JoeAzar:pr1-scalar-prevoffset

Conversation

@JoeAzar

@JoeAzar JoeAzar commented Nov 26, 2026

Copy link
Copy Markdown

First split PR from #4751 (following the feedback in #4751 (review)).

Summary

On the !defined(__aarch64__) path of ZSTD_decodeSequence() in lib/decompress/zstd_decompress_block.c, small-offset decoding dynamically indexes seqState->prevOffset[ll0] and seqState->prevOffset[offset]. Because prevOffset[3] lives inside seqState_t alongside DStream, those variable-offset accesses inhibit LLVM SROA on the seqState_t stack allocation, forcing modern Clang to store seqState.DStream.bitsConsumed to the stack after every BIT_readBitsFast() and spill/reload prevOffset[0..2] on every sequence.

This PR mirrors the existing #if defined(__aarch64__) path (+23 / -10 lines): load seqState->prevOffset[0..2] into local scalars (prevOffset0, prevOffset1, prevOffset2) at the start of ZSTD_decodeSequence(), update them with constant indices/conditionals, and write them back at function exit.

  • No changes to bounds checks, external-dictionary handling, or ZSTD_execSequence*.
  • C90-compliant (-Wdeclaration-after-statement -Werror).

Assembly Evidence (ZSTD_decompressSequences_bmi2, Clang trunk)

Before (e50cdb0d4) — stack frame 0x188 bytes, 209 (%rsp) references, 870 instructions:

d36d: add    %r14d,%r13d
d370: mov    %r13d,0x90(%rsp)     ; spill bitsConsumed after offset bits
d37b: mov    %rax,0xf0(%rsp)      ; spill prevOffset[2]
...
d3c8: mov    %rdi,0xe8(%rsp)      ; spill prevOffset[1]
d3d5: mov    %rax,0xe0(%rsp)      ; spill prevOffset[0]
...
d3f1: add    %esi,%r13d
d3f4: mov    %r13d,0x90(%rsp)     ; spill bitsConsumed after mlBits
...
d42a: add    %edx,%r13d
d42d: mov    %r13d,0x90(%rsp)     ; spill bitsConsumed after llBits

After (pr1-scalar-prevoffset) — stack frame 0x118 bytes (-112 B), 134 (%rsp) references (-75), 776 instructions:

  • seqState.DStream.bitsConsumed stays purely in a register (add %edx,%r9d, add %esi,%r9d, ...) with zero stack stores per bit read.
  • Dynamic instructions:u (perf stat, -3 / -19): GCC 11 -1.85% / -1.85%, GCC 15 -1.64% / -1.72%, Clang 19 +0.39% / +0.43%, Clang trunk -11.04% / -11.47%.

Benchmarks (AMD EPYC 9B14 Genoa)

Protocol:

  • Base SHA: e50cdb0d407f5db512ed377fbee6c9ba473fea8e
  • Build flags: -O3 -Wall -Wextra -Wdeclaration-after-statement -Werror (identical flags and linkage per pair, HAVE_ZLIB=0 HAVE_LZMA=0 HAVE_LZ4=0 HAVE_THREAD=0, ASLR disabled, isolated pinned core)
  • Compilers: gcc 11 (Debian 11.5.0), gcc 15 (Debian 15.2.0), clang 19 (Debian 19.1.7), clang trunk (llvm/llvm-project@86801a03ccc8)
  • Corpus: silesia.tar (sha256: fc60dca8d229df75c4e8bc8ad4c60347ea7770823b97de31cea84352cf462b32) precompressed once by the baseline encoder at -1, -3, -9, -19, and -3 --long=27, plus an extDict workload (50 × 16 KiB slices of silesia.tar compressed at -3 with a 64 KiB trained dictionary).
  • Timing: Warmup (-i0) then 5 interleaved ABBA pairs alternating run order via zstd -b -d -i3 (-i2 for extDict). Base-vs-base paired noise floor on this host: within [-0.26%, +0.56%] across all four compilers.

Median Paired Throughput Delta vs. e50cdb0d4 (with [min, max] spread)

Frame gcc 11 gcc 15 clang 19 clang trunk
-1 +0.50% [+0.25, +0.76] +0.61% [+0.60, +0.68] +0.65% [+0.58, +0.87] +11.65% [+11.55, +12.16]
-3 +0.45% [+0.08, +0.50] +0.65% [+0.39, +0.93] +2.64% [+2.28, +2.81] +17.28% [+16.91, +17.52]
-9 +0.61% [+0.38, +0.99] +0.27% [-0.01, +0.63] +2.24% [+1.72, +2.75] +17.87% [+17.07, +18.31]
-19 +1.11% [+0.30, +1.88] -0.25% [-0.65, +0.80] +1.97% [+1.04, +2.38] +17.70% [+16.81, +18.12]
-3 --long=27 -0.22% [-0.51, +0.98] +0.84% [+0.80, +1.06] +3.00% [+2.56, +3.12] +16.50% [+16.40, +17.03]
extDict (50×16 KiB) +0.99% [+0.90, +1.23] +0.23% [+0.01, +0.36] +1.09% [+0.88, +1.29] +7.35% [+7.23, +7.61]

Absolute -3 medians (MB/s): gcc 11 1382.8 -> 1387.8, gcc 15 1436.6 -> 1445.8, clang 19 1322.6 -> 1358.1, clang trunk 1202.8 -> 1410.6.

Code Size Delta (zstd_decompress_block.o .text)

Compiler Base .text Candidate .text Delta
gcc 11 54,225 B 54,017 B -208 B
gcc 15 51,826 B 52,210 B +384 B
clang 19 55,328 B 56,174 B +846 B
clang trunk 60,310 B 57,765 B -2,545 B

Verification

  • lib/ builds cleanly with -std=gnu90 -Wall -Wextra -Wdeclaration-after-statement -Werror (gcc 11, gcc 15) and -std=c90 -Wall -Wextra -Wdeclaration-after-statement -Werror (clang 19, clang trunk).
  • Byte-exact SHA-256 verification against silesia.tar across all 5 frames (-1, -3, -9, -19, -3 --long=27) and all 50 extDict files across all four compilers.
  • ASan + UBSan + pointer-overflow (-fsanitize=address,undefined,pointer-overflow -fno-sanitize-recover=all): tests/fuzzer, tests/zstreamtest, and tests/decodecorpus passed.
  • 32-bit (-m32): tests/fuzzer and tests/zstreamtest passed.
  • make check passed.

On the !defined(__aarch64__) path of ZSTD_decodeSequence(), dynamic
array indexing into seqState->prevOffset[ll0] and
seqState->prevOffset[offset] inhibits LLVM SROA on the seqState_t
alloca. As a result, modern Clang spills seqState.DStream.bitsConsumed
to the stack after every BIT_readBitsFast() and spills/reloads
prevOffset[0..2] on every sequence.

Mirror the existing #if defined(__aarch64__) path by loading
seqState->prevOffset[0..2] into local scalars (prevOffset0,
prevOffset1, prevOffset2) and writing them back at function exit.
This eliminates all dynamic indexing into seqState_t, allowing Clang to
keep DStream.bitsConsumed and prevOffset[0..2] in registers (+11.7% to
+17.9% on Clang trunk, +0.7% to +3.0% on Clang 19, neutral to +1.1% on
GCC 11 and GCC 15 on AMD EPYC Genoa) while reducing Clang decoder .text
size by 2.5 KB.
@JoeAzar

JoeAzar commented Nov 27, 2026

Copy link
Copy Markdown
Author

cc @danlark1

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