compress: vectorize ZSTD_count() with SSE2#4612
Open
rmilkowski wants to merge 2 commits intofacebook:devfrom
Open
compress: vectorize ZSTD_count() with SSE2#4612rmilkowski wants to merge 2 commits intofacebook:devfrom
rmilkowski wants to merge 2 commits intofacebook:devfrom
Conversation
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.
Summary
Add an SSE2 fast path to
ZSTD_count()on x86/x86_64.After the initial machine-word compare, the new path compares 16 bytes at a time with
_mm_cmpeq_epi8()/_mm_movemask_epi8()and usesZSTD_countTrailingZeros32()to jump to the first mismatch. Non-x86 targets and builds without SSE2 keep the existing scalar path.Why
ZSTD_count()is a hot helper in match finding. When matches extend past the first word, counting them 16 bytes at a time reduces scalar work on match-heavy inputs.Benchmark
Host: Intel Xeon Gold 6254, Linux x86_64
Method:
1..223runs per file / levelOverall:
+16.1%+8.3%-1.8%Largest gains:
pattern_64kb_gap_0kb_128mb.bin:+98.3%at level22pattern_64kb_gap_0kb_128mb.bin:+86.0%at level10pattern_64kb_gap_0kb_128mb.bin:+75.3%at level9runlength_64kb_pad64_32mb.bin:+54.7%at level10Largest regressions observed:
pattern_1kb_gap_1023kb_128mb.bin:-6.7%at level8high_entropy_190sym_64mb.bin:-4.6%at level5pattern_1kb_gap_1023kb_128mb.bin:-2.7%at level1Overall the tradeoff looks favorable: large wins on repetitive / match-heavy inputs, with limited downside on sparse or high-entropy inputs.
Testing
playTests.shtest-fullbenchtest-fuzzertest-zstreamtest-invalidDictionariestest-legacytest-decodecorpustest-pooltest-longmatchCFLAGS='-Werror -O2' make -j1 lib zstdmake c89buildmake gnu90buildmake c99buildmake cxxtestNotes