Repository navigation
[fast] Keep only ip0 and ip2 in the noDict search loop - #4865
Open
jeffchen006 wants to merge 1 commit into
Open
jeffchen006 wants to merge 1 commit into
jeffchen006 wants to merge 1 commit into
Conversation
In ZSTD_compressBlock_fast_noDict_generic(), ip1 is always ip0 + 1 and ip3 is always ip2 + 1. Remove these two variables and derive them from ip0 and ip2 where they are used. Every pointer value, hash table write, prefetch address and loop condition stays the same, so the compressed output is unchanged. With two fewer live pointers in the hot loop, clang 14 generates 102 fewer stack accesses in ZSTD_compressBlock_fast() and executes 1.6% fewer instructions for level 1 on silesia.tar. Whole silesia.tar compression speed, AMD EPYC 9654, median of 10 alternating pairs: clang 14: level 1 +4.1%, level 2 +2.5%, --fast=1..10 +1.9% to +3.4% gcc 11: level 1 +0.7%, level 2 +0.8%, --fast=1..10 +0.6% to +1.7% Decompression and level 3 are neutral. Code size is smaller (text: gcc -224 bytes, clang -1088 bytes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jeffchen006
marked this pull request as ready for review
October 27, 2026 00:00
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.
In
ZSTD_compressBlock_fast_noDict_generic(),ip1is alwaysip0 + 1andip3is alwaysip2 + 1. This PR removes those two variables and computes them fromip0andip2where they are used.Every pointer value, hash table write, prefetch address and loop condition stays the same, so the compressed output is byte-identical. The only difference is that the hot loop has two fewer live pointers. With clang 14,
ZSTD_compressBlock_fast()has 102 fewer stack accesses (static count, 884 → 782) and runs 1.6% fewer instructions for-1onsilesia.tar. With gcc 11 the static count changes less (684 → 671) and the gain is smaller.The change applies to both
useCmovvariants (#4165), because they share the generic function.Results
silesia.tar, whole file, compression,zstd -b<level> -i2. Each cell is the median of 10 alternatingdev/PR pairs on one pinned core. "Change" is the median of the per-pair ratios.devMB/s--fast=1--fast=3--fast=5--fast=10--fast=1--fast=3--fast=5--fast=10The compressed sizes are identical to
devin every cell (for example, 73,191,101 bytes at-1and 69,303,779 bytes at-2). A baseline-vs-baseline (A/A) run on the same core gave −0.03% (gcc) and −0.11% (clang) at-1.Unchanged paths (the code is not used, or the effect is noise):
Small blocks (
-B, sosilesia.taris compressed as independent chunks, which selects thecmovvariant), level 1:devMB/sThe gcc 16 KB row is inside the A/A noise, but its sign is consistent, so I list it as neutral to slightly negative. With small chunks, the per-block setup takes most of the time and the loop runs only a few iterations.
Code size
sizetext,zstdCLI built withmake zstd:devzstd_fast.o: gcc −256 bytes, clang −1,072 bytes.Correctness
ip1 == ip0 + 1andip3 == ip2 + 1always hold. They hold at_start, and after each iteration the oldip3 = ip1 + step = ip0 + 1 + step = ip2 + 1. Every use of the removed pointers maps one-to-one toip0 + 1orip2 + 1._offset,_matchand_cleanupdo not readip1orip3.devand PR: about 70,000 cases had 0 differences. They cover--fast=1..10,-1,-2,minMatch4–7,wlog10/11/12/18,tlen,--long,-T2 --rsyncable, file and stdin input, and input sizes 0–80 B, 96–4097 B and 128 KB ± 10 B / 256 KB ± 10 B (block end). These builds were tested: gcc, clang,-m32,ZSTD_NO_INTRINSICSandDEBUGLEVEL=1.fuzzer -T180s,zstreamtest -T180s, an ASan+UBSanfuzzer -T120sandplayTests.shall passed.Method and more data
make zstdwith default flags (-O3) and no-march.silesia.tar, 211,957,760 bytes, SHA-2560b5ec70c8048321525f820bc1d76429840bd22ee2c7a116175253a0fec2c724d.zstd -b<level> -i2(plus-B4K/-B16Kfor the small-block rows). Each pair runsdevand the PR back to back, and the order alternates between pairs.-1+4.09% (12/12), clang-2+2.02% (12/12), gcc-1+0.62% (12/12), gcc-2+1.05% (11/12).silesia.tar, compression:-1 --zstd=minMatch=4-1 --zstd=minMatch=5-1 --zstd=minMatch=6-1 --zstd=minMatch=7-1 --zstd=wlog=18-1 --zstd=minMatch=4-1 --zstd=minMatch=5-1 --zstd=minMatch=6-1 --zstd=minMatch=7-1 --zstd=wlog=18-O2, wholesilesia.tar, compression: clang-1+2.25%,-2+1.85%; gcc-1+0.82%,-2+1.48%.-1, 8 pairs): clang +1.8% to +12.7% on 11 of 12 files,nci−0.09% (4/8). gcc +0.7% to +2.1% on all 12 files.perf stat,zstd -1 --single-threadon the wholesilesia.tar, 3 alternating runs:Limits
fastloop (levels 1–2, negative levels, andstrategy=fast). ThedictMatchStateandextDictvariants are unchanged.Checklist
A gain in one configuration is not a general gain: see the Performance section of CONTRIBUTING.md.
Tools used
Claude Code (Claude Opus) found this optimization, wrote the patch, ran the correctness checks (byte-identity harnesses, fuzzer, zstreamtest, ASan/UBSan, playTests) and the benchmarks, and drafted this description. A separate Claude Code agent reviewed the patch independently and repeated the measurements. I reviewed the diff and the benchmark data before submitting.