Skip to content

zstd: keep default-encoder hash primes in registers on arm64 - #1241

Merged
klauspost merged 1 commit into
klauspost:masterfrom
honeycombio:lizf.zstd-dfast-primes
Sep 28, 2026
Merged

klauspost merged 1 commit into
klauspost:masterfrom
honeycombio:lizf.zstd-dfast-primes

Conversation

@lizthegrey

@lizthegrey lizthegrey commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Speeds up SpeedDefault encoding on arm64 (+2–4%) with byte-identical output, by giving the default-level encoder the same hash-prime treatment as the better encoder got in #1230. amd64 is unchanged.

Changes (362a8de)

doubleFastEncoder and doubleFastEncoderDict hash with the same two 64-bit primes as betterFastEncoder. On arm64 each prime takes 4 instructions to build, and the compiler rebuilds it at every use.

  • betterHashPrimes becomes hashPrimes8and5 in hash.go, shared by both encoders. It still loads the primes from a package var only when hashPrimesInRegs is set (arm64), and returns the constants elsewhere.
  • dFastHashL/dFastHashS replace hashLen in the default encoders' loops, taking the primes as arguments. Reset and ResetPrefix keep hashLen.
  • The better encoder's generated code is unchanged.

Why output is unchanged

  • dFastHashL/dFastHashS compute exactly what hashLen computes for these table sizes. TestBetterHashMatchesHashLen in encoder_test.go becomes TestHashHelpersMatchHashLen and gains the two default-encoder cases.
  • sha256 of every frame's output matches master for 8 corpora × {default, better}, on arm64 and amd64.

Benchmarks

Same setup as #1230: 32 MiB decompressed prefixes of your real corpora, each encoded as 4 MiB EncodeAll frames with a 4 MiB window, CRC on and concurrency 1. Each result is 10 master/PR rounds, alternating within each round, on one pinned core, compared with benchstat. Figures are SpeedDefault MB/s vs master; ~ means not significant.

corpus Graviton2 (c6g) Graviton3 (c7g) Graviton4 (c8g) Sapphire Rapids (c7i)
apache.log +2.1% ~ ~ ~
cockroach.node1.log +2.4% +0.6% +1.3% ~
github-june-2days-2019.json +5.4% +1.8% +2.4% ~
github-ranks-backup.bin +7.3% +3.2% +2.5% ~
gob-stream +4.8% +1.6% +1.9% ~
nyc-taxi-data-10M.csv +2.0% +1.1% +1.0% −0.2% (p=0.050)
rawstudio-mint14.tar +9.7% +5.9% +3.8% ~
sofia-air-quality-dataset.tar +1.9% +1.6% +1.5% ~
geomean +4.4% +2.1% +1.9% flat

The arm64 cells are all improvements, and all are significant except the ~ ones. On amd64 the helpers return the constants, so the hashing is the same as master's; the one borderline Sapphire Rapids cell sits at the edge of significance, and the other seven are flat.

These runs were taken together with #1230's; that PR does not touch the default encoder, so the SpeedDefault rows measure this change alone.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Performance
    • Improved internal hash calculations used by fast compression paths, including dictionary-based encoding. Compression results and behavior remain unchanged.

Apply the better encoder's treatment to doubleFastEncoder and
doubleFastEncoderDict, which hash with the same two primes. The primes
are loaded once per Encode through hashPrimes8and5, now shared in
hash.go, and dFastHashL/dFastHashS replace hashLen in the encode loops.
Reset and ResetPrefix keep hashLen.

Output is byte-identical to master (8 corpora x default/better, arm64
and amd64). The better encoder's code generation is unchanged.

Default level, 4 MiB frames, benchstat MB/s vs master:
  Graviton2 +4.4%, Graviton3 +2.1%, Graviton4 +1.9%, Sapphire Rapids ~

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 0124a38a-80aa-42d6-9701-cf0591c6b5e5

📥 Commits

Reviewing files that changed from the base of the PR and between 0533a67 and 362a8de.

📒 Files selected for processing (4)
  • zstd/enc_better.go
  • zstd/enc_dfast.go
  • zstd/encoder_test.go
  • zstd/hash.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.


📝 Walkthrough

Walkthrough

The zstd better and double-fast encoders now use shared hash primes and prime-based hash helpers. The hash-helper test checks the better and double-fast long- and short-hash results against hashLen.

Changes

Zstd hash helper updates

Layer / File(s) Summary
Shared primes and better encoders
zstd/hash.go, zstd/enc_better.go
hashPrimes8and5 provides the long and short primes. Both better encoders use it instead of betterHashPrimes, which was removed.
Double-fast hash indexing
zstd/enc_dfast.go, zstd/encoder_test.go
The double-fast encoders use dFastHashL and dFastHashS for match-table lookups and updates. The test compares the better and double-fast helper results with hashLen.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Refactor

Merge Risk: ⚪ Minimal · up to 362a8

The identified repeat-offset path does not require a fix, and no actionable merge-blocking issue remains after normal checks.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 71.43% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: keeping the default encoder hash primes in registers on arm64. It matches the changes to the better and double-fast encoders.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@klauspost
klauspost merged commit 8a5e589 into klauspost:master Sep 28, 2026
29 checks passed
@lizthegrey
lizthegrey deleted the lizf.zstd-dfast-primes branch September 28, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants