Repository navigation
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
Draft
decompress: copy 32 bytes up front for matches in ZSTD_execSequence; skip match prefetch on Apple#5067abedegno wants to merge 1 commit into
abedegno wants to merge 1 commit into
Conversation
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.
3 of 4 tasks
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.
Draft. More benchmarks will follow before this is marked ready:
zstd -b -don Silesia with this patch ondevon 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.offset >= 16, the match copy now does two 16-byte copies up front and callsZSTD_wildcopy()only for matches longer than 32 bytes. Before, it did one 16-byte copy, then branched onmatchLength > 16and ran the 32-byte loop. For all targets.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. SomatchLength > 32is 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:
oMatchEnd <= oend_w, withoend_w = oend - WILDCOPY_OVERLENGTHandWILDCOPY_OVERLENGTH == 32. In the extDict-spanning caseopmoves forward butoMatchEnddoes not, soop + 32 <= oendstill holds. Writing 32 bytes fromopstays 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). Anassert(WILDCOPY_OVERLENGTH >= 32)documents this.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 readsmatch + 16 .. match + 31, which is at or beforeop + 15, already written. This is the same property the existingZSTD_wildcopy(..., ZSTD_no_overlap)loop relies on.ZSTD_execSequenceSplitLitBuffer()is not changed: there the literal buffer can sit insidedst, 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 withDEBUGLEVEL=2(asserts on):fuzzer -T180s(8,655 tests),zstreamtest -T180s(10,370),zstreamtest --newapi -t1 -T180s(20,818),decodecorpus -t -T180(34,995 frames),invalidDictionariesandlegacy: 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.devand patcheddevat 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 stockdev.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):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
-O2and with-falign-loops=64 -falign-functions=64, so it does not look like a code-layout accident. Ondev(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:So on x86-64 the copy helps data with long matches and is neutral on Silesia within about 1%.
Not measured
If anyone can run
zstd -b# -dorfullbenchon those, I would be glad to add the numbers, and to adjust the guard either way.Checklist
ZSTD_execSequence()'s arguments and Enable PREFETCH_L1 for RISC-V to improve performance #4643 extends the same prefetch#ifto RISC-V; both overlap textually only, see below.)Overlap with open PRs: #5054 renames
sequence.matchLengthtomatchLengthin 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.