Skip to content

decompress: copy 32 bytes up front for matches in ZSTD_execSequence; skip match prefetch on Apple - #5067

Draft
abedegno wants to merge 1 commit into
meta:devfrom
abedegno:execsequence-copy32
Draft

abedegno wants to merge 1 commit into
meta:devfrom
abedegno:execsequence-copy32

Conversation

@abedegno

Copy link
Copy Markdown

Draft. More benchmarks will follow before this is marked ready: zstd -b -d on Silesia with this patch on dev on an idle Apple machine (the Apple numbers below were taken on a busy machine, though every patched run beat every stock run), and numbers from Arm servers if I can get them. Results from other machines are welcome.

What and why

This changes the fast path of ZSTD_execSequence() in two small ways. The output is unchanged.

  1. When offset >= 16, the match copy now does two 16-byte copies up front and calls ZSTD_wildcopy() only for matches longer than 32 bytes. Before, it did one 16-byte copy, then branched on matchLength > 16 and ran the 32-byte loop. For all targets.
  2. The AArch64 PREFETCH_L1(match) at the top of the function is skipped on Apple targets (#if defined(__aarch64__) && !defined(__APPLE__)). Other AArch64 targets keep it.

Why the copy helps: the branch it replaces, matchLength > 16, is close to a coin flip on some data. In log-like data from a Rust application (web access log text, 512 lines per frame, level 9), 50.9% of matches are longer than 16 bytes, 30.4% longer than 32, and the mean match length is 26.8. On Silesia, 16% of matches are longer than 16 bytes and 5 to 6% longer than 32. So matchLength > 32 is predicted well on both, and most matches finish in two straight-line copies.

Why the prefetch change is Apple-only: on an Apple M4 Pro the prefetch is a net cost. The match source is usually output written a few hundred bytes earlier, and dropping the prefetch alone gave 2.4% to 3.0% on Silesia frames. But the prefetch was added in #3164 with gains reported on Arm server cores, and I have no Arm server to measure on. So this PR leaves it in place everywhere except Apple targets. Numbers from Neoverse (N1, V1, V2) or other AArch64 cores would be very welcome, both for the copy change and for whether the prefetch still helps there. If the prefetch turns out to be neutral on Arm servers too, the guard could go; if the copy change hurts somewhere, I am happy to narrow it.

Safety:

  • The fast path is only taken when oMatchEnd <= oend_w, with oend_w = oend - WILDCOPY_OVERLENGTH and WILDCOPY_OVERLENGTH == 32. In the extDict-spanning case op moves forward but oMatchEnd does not, so op + 32 <= oend still holds. Writing 32 bytes from op stays inside the buffer, and the over-write past the match end is still under 32 bytes, as before (the old loop could also write up to 31 bytes past it). An assert(WILDCOPY_OVERLENGTH >= 32) documents this.
  • This branch requires offset >= WILDCOPY_VECLEN (16). Two 16-byte copies done in order are then correct even when the source overlaps bytes just written by the first copy: the second copy reads match + 16 .. match + 31, which is at or before op + 15, already written. This is the same property the existing ZSTD_wildcopy(..., ZSTD_no_overlap) loop relies on.
  • Every byte read lies in already-decoded output or the prefix, as before.
  • ZSTD_execSequenceSplitLitBuffer() is not changed: there the literal buffer can sit inside dst, and I did not want to widen the write window on that path.

Tests (Apple M4 Pro, macOS 26, Apple clang 21, on this branch):

  • make check (playTests.sh): passes ("All tests completed successfully").
  • tests/ built with DEBUGLEVEL=2 (asserts on): fuzzer -T180s (8,655 tests), zstreamtest -T180s (10,370), zstreamtest --newapi -t1 -T180s (20,818), decodecorpus -t -T180 (34,995 frames), invalidDictionaries and legacy: all pass.
  • tests/ built with -fsanitize=address,undefined -fno-sanitize-recover=all: fuzzer -T180s (5,245 tests), decodecorpus -t -T180 (21,848 frames), zstreamtest -T120s (3,099 tests): all pass, no sanitizer report.
  • Silesia round trips with stock dev and patched dev at levels 1, 3 and 19 (single-threaded), and Silesia cut into 128 KiB (level 3), 1 MiB (level 9) and 16 MiB (level 19) frames: decompressed output identical to the original and to stock dev.

Benchmarks

Decompression time; lower is better. Each A/B alternates the two binaries; "gain" is the change in the median. On Apple and x86-64 the measured binaries are exactly what this PR builds there: on Apple the prefetch is gone and the copy is new; on x86-64 the prefetch line was never compiled, so only the copy changes.

Apple M4 Pro, Apple clang 21, -O3, zstd 1.5.7 with the same change, in-process bench (one reused DCtx and output buffer, median of 21 passes per run over all frames, 7 passes for Silesia):

Data Rounds Stock (ms) Patched (ms) Gain
Log text frames, 512 lines each, level 9 (3,908 frames, 421 MB) 7 131.3 to 134.6 124.1 to 128.2 5.5%
same, second run 7 132.7 to 136.8 126.5 to 132.6 4.9%
Silesia, 128 KiB frames, level 3 7 114.9 to 116.8 107.8 to 109.6 5.8%
Silesia, 1 MiB frames, level 9 7 104.4 to 105.5 97.2 to 98.5 7.0%
Silesia, 16 MiB frames, level 19 7 114.8 to 126.9 106.2 to 119.1 about 7% in the quiet rounds (noisy)

Split by part on Silesia (same machine, 1.5.7): prefetch removal alone 2.4% to 3.0%, 32-byte copy alone 1.6% to 1.8%; together more than the sum. The gain held with -O2 and with -falign-loops=64 -falign-functions=64, so it does not look like a code-layout accident. On dev (e50cdb0) the same change gave 5.0% on the log frames (5 rounds, 141.8 to 144.4 ms against 136.0 to 140.0 ms).

x86-64, AMD EPYC Milan (8 vCPU VM, Ubuntu 24.04), zstd 1.5.7, -O3, Huffman assembly on, run pinned to one core, 11 alternating rounds, median of 21 passes per run. Only the 32-byte copy is live here:

Data Compiler Stock (ms) Patched (ms) Gain
Log text frames, level 9 gcc 13.3 226.4 to 231.4 220.3 to 224.6 2.9%
Log text frames, level 9 clang 18.1 232.7 to 235.1 223.1 to 225.9 4.2%
Silesia, 128 KiB frames, level 3 gcc 13.3 188.2 to 194.9 188.4 to 219.3 0.1% (noise)
Silesia, 128 KiB frames, level 3 clang 18.1 190.3 to 200.0 188.5 to 193.1 1.0% (noise)
Silesia, 1 MiB frames, level 9 gcc 13.3 177.1 to 184.5 178.2 to 185.7 -0.4% (noise)
Silesia, 1 MiB frames, level 9 clang 18.1 179.5 to 188.2 178.1 to 187.1 0.4% (noise)

So on x86-64 the copy helps data with long matches and is neutral on Silesia within about 1%.

Not measured

  • Arm server cores (Neoverse N1, V1, V2, Graviton, Ampere) and other non-Apple AArch64. On these only the copy change applies; the prefetch is kept.
  • Intel x86-64, and AMD cores other than Milan.
  • Apple chips other than the M4 Pro, and gcc on macOS.
  • 32-bit targets (not benchmarked; the same buffer bound guards the fast path there).

If anyone can run zstd -b# -d or fullbench on those, I would be glad to add the numbers, and to adjust the guard either way.

Checklist

Overlap with open PRs: #5054 renames sequence.matchLength to matchLength in this function, so whichever lands second needs a one-line rebase of the new copy block. #4643 changes #if defined(__aarch64__) to #if defined(__aarch64__) || defined(__riscv) on the prefetch; combined, the guard would be #if (defined(__aarch64__) && !defined(__APPLE__)) || defined(__riscv). #4813, #5053 and #3828 apply cleanly together with this change.

Tools used

The change was found, written and tested with Claude (Anthropic), which also ran the benchmarks and drafted this description. The measurements are in this PR.

When offset >= 16, do two 16-byte copies up front and call ZSTD_wildcopy()
only for matches longer than 32 bytes. oMatchEnd <= oend_w leaves room for
the 32 bytes, and offset >= 16 makes two sequential 16-byte copies correct.

Skip the AArch64 match prefetch on Apple targets, where it measured slower;
other AArch64 targets keep it.

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