Skip to content

lib: add restrict qualifiers to row match finder - #4867

Open
Xinlong-Chen wants to merge 1 commit into
facebook:devfrom
Xinlong-Chen:dev
Open

Xinlong-Chen wants to merge 1 commit into
facebook:devfrom
Xinlong-Chen:dev

Conversation

@Xinlong-Chen

@Xinlong-Chen Xinlong-Chen commented Oct 27, 2026 •

Copy link
Copy Markdown

What and why

ZSTD_RowFindBestMatch() is a major level 8 compression hotspot. It updates the match state while reading input and writing the selected offset, but the compiler cannot currently assume these memory regions do not alias.

This change adds a portable ZSTD_RESTRICT macro and applies it to ms, ip, and offsetPtr. Other compilers safely fall back to an empty macro.

The benchmark follows pts/compress-zstd-1.5.0: zstd -T1 -b8 on its 1007 MiB FreeBSD 12.2 image. Builds use -O2, LTO, and x86-64-v2. Results are the average of eight paired samples after removing the largest and smallest changes from 10 counterbalanced rounds.

Compression throughput improved across both tested CPUs:

  • AMD EPYC 7K62 (Zen 2): +3.21% with GCC 12 and +2.10% with GCC 16.
  • Intel Xeon Platinum 8255C (Cascade Lake): +1.71% with GCC 12 and +3.87% with GCC 16.

For GCC 12/16, the hot function is 7.21%/7.94% smaller, with 8.56%/6.41% fewer static instructions and 33.33%/31.25% fewer explicit stack accesses.

All runs produced identical output; decompression was unchanged within noise. Performance gains vary by compiler and CPU.

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.
  • Performance change: numbers from repeated runs are included, with CPU, compiler, flags and data.
    A gain in one configuration is not a general gain: see the Performance section of CONTRIBUTING.md.
  • I stated below whether AI or other automated tools were used.

Tools used

Cursor was used to assist with benchmark automation and drafting this description.

ZSTD_RowFindBestMatch() dominates level 8 compression. It mutates the
hash and tag tables reachable through the match state while reading input
and writing the selected offset. Without no-alias information, the compiler
must conservatively account for overlap between these accesses.

Add a portable ZSTD_RESTRICT macro and apply it only to the three pointer
parameters that access data in the hot path. Leave iLimit and the read-only
match counters unchanged.

Benchmarking followed pts/compress-zstd-1.5.0, using zstd -T1 -b8 on
its 1007 MiB FreeBSD 12.2 memstick image. The build used optimization
flags widely adopted by major Linux distributions: -O2, LTO, and an
x86-64-v2 baseline. Each base/opt pair was run for 10 counterbalanced
rounds on a fixed CPU. The highest and lowest paired changes were
discarded before aggregation.

On AMD EPYC 7K62 (Zen 2), paired compression throughput improves by
3.21% with GCC 12 and 2.10% with GCC 16.

On Intel Xeon Platinum 8255C (Cascade Lake), paired compression
throughput improves by 1.71% with GCC 12 and 3.87% with GCC 16.

For GCC 12 and GCC 16 respectively, the specialized hot function is
7.21% and 7.94% smaller, contains 8.56% and 6.41% fewer static
instructions, and has 33.33% and 31.25% fewer explicit stack accesses.

All runs produced identical compressed output, and decompression was
unchanged within noise. The exact benefit depends on compiler code
generation and the target microarchitecture.

Co-authored-by: xionghuluo <xionghuluo@tencent.com>
Signed-off-by: xinlongchen <xinlongchen@tencent.com>
@meta-cla

meta-cla Bot commented Oct 27, 2026

Copy link
Copy Markdown

Hi @Xinlong-Chen!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Oct 27, 2026
@meta-cla

meta-cla Bot commented Oct 27, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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