Shorten amd64 shortcut loop and remove redundant checks

perfloop-oss/lz4 · Uncatalogued performance

https://perfloop.ai/t/oss/case_7yz78wmqmc

Verdict

VERIFIED · settled 2026-11-26 · merged as pierrec/lz4#323

What happened: The paired measurements met the required improvement.

Hypothesis

The x86-64 assembly decoder's hot shortcut loop executes 42 instructions per short sequence, including provably dead bounds checks, a serial three-step input-pointer increment chain, a 16-bit partial-register load stall, and a redundant double jump through loopcheck on every iteration. On this sandbox's CPU profiles, this loop accounts for 590ms of 670ms (88%) in BenchmarkUncompress, 260ms of 530ms (49%) in BenchmarkUncompressKernelCode, 160ms of 440ms (36%) in BenchmarkUncompressTwainLinked, and 240ms of 700ms (34%) in BenchmarkUncompressColumnarShort; a 9-round interleaved sandbox prototype cut BenchmarkUncompress by 16.2% (-17.4% vs an alignment-only control) and full-frame BenchmarkUncompressPg1661 and BenchmarkUncompressTwain by 9.6% and 8.8% with zero regressions across all tested workloads. As an engineering prior, reach is default: almost all ordinary LZ4 decompression workloads on x86-64 execute this shortcut on the majority of short literal-and-match sequences in the default configuration, so real-world savings scale with the share of decompression time spent in decodeBlock rather than checksums or I/O.

The owning generator definition is decodeAMD64 in internal/lz4block/_gen/amd64.go:198-809, targeting x86-64 with baseline SSE2 (no AVX2 or microarchitecture dependency). Inside loop at lines 253-331: (1) lines 256, 287, and 298 update SI in three serial steps (INCQ SI, ADDQ CX, SI, ADDQ $2, SI), keeping SI on the loop-carried critical path; (2) line 299 checks JC err_short_buf even though SI < R9 - 16 and CX <= 14 guarantee SI + 2 <= R9 - 1 < R9; (3) lines 304-305 execute CMPQ AX, DI; JA err_short_buf immediately after SUBQ DX, AX; JC err_corrupt, which is mathematically dead whenever CF=0 (20ms flat in the BenchmarkUncompress profile); (4) lines 296-297 test DX == 0 on the hot path even though DX == 0 always fails DX < 8 (or CX == 15) and can be checked only on bailout, matching decodeARM64 commit 3405ef7; (5) line 325 uses MOVW 16(AX), BX after MOVQ 8(AX), BX, a 16-bit partial-register write that stalls on the prior 64-bit load of BX; and (6) line 331 jumps to loopcheck (CMPQ SI, R9; JB loop) even though SI < R9 holds unconditionally at shortcut completion.

Sandbox profiling of `go test -run='^$' -bench='^BenchmarkUncompress$' -benchtime=500ms -cpu=1` showed 590ms of 670ms (88%) in loop, including 100ms across lines 104-108 (SUBQ/CMPQ AX, DI) and 250ms across lines 120-129 (the 8+8+2 match copy). On the attached provisional regression check `go test -count=1 -run '^TestShortcutLoopCostBound$' ./internal/lz4block`, unchanged code fails all three repeated runs at asm/go ratios of 0.724x-0.749x (3.0-3.3 ns/seq) against the 0.66x bound. A 9-round interleaved sandbox comparison against both unmodified base and a PCALIGN-$32-only alignment control showed: BenchmarkUncompress 207,305 ns -> 173,805 ns (-16.16% vs base, -17.44% vs alignment-only 210,530 ns), BenchmarkUncompressPg1661 334,146 ns -> 302,135 ns (-9.58% vs base, -10.90% vs align), BenchmarkUncompressTwain 216,105 ns -> 197,095 ns (-8.80% vs base, -11.48% vs align), plus a 10-round AB/BA check showing BenchmarkUncompressKernelCode -5.40% and BenchmarkUncompressColumnarShort -3.35%, while TestShortcutLoopCostBound passed at 0.619x-0.627x.

The Case proof target is an x86-64 implementation in internal/lz4block/_gen/amd64.go (regenerating decode_amd64.s) verified by: (1) exact invariant reasoning (and optionally a machine-checked proof) that R13 = R9 - 17 and lit_len <= 14 keep 1(SI)..16(SI) and 1(SI)(CX*1)..2(SI)(CX*1) in bounds, keep SI < R9 at JMP loop, reject DX == 0 on every bailout path including CX == 15, and route any DI < DX borrow to dictionary handling rather than corrupting valid short-address inputs; (2) full unit tests including TestDecodeBlockInvalid, guardedTail PROT_NONE over-read tests, FuzzDecodeBlockDifferential, and -dynlink builds; and (3) interleaved AB/BA benchmarks against both base and an alignment-only control across text, binary, linked-block, columnar, and RLE workloads confirming broad gains with no regressions. The provisional check file and 0.66x asm/go ratio are check methods, not frozen repository requirements.

Current upstream open PRs on github.com/pierrec/lz4 are #310 and #311 (dictionary docs/tests and concurrency); neither touches internal/lz4block/_gen or decode_amd64.s. Local git history confirms commit 3405ef7 applied an analogous shortcut tightening to arm64 (-2% to -7.8% across five cores), leaving amd64's shortcut unchanged. README.md directs changes to GitHub pull requests; no other review venue was identified.

Change to test: Tighten the x86-64 baseline SSE2 shortcut loop in decodeAMD64: test token >= 0xF0 directly, collapse the three serial SI updates into one indexed offset load and LEAQ 3(SI)(CX*1), SI (with R13 = R9 - 17), drop the unreachable JC err_short_buf and dead CMPQ AX, DI / JA err_short_buf checks, move zero-offset rejection to a match_len_check bailout label, replace MOVW 16(AX), BX with zero-extending MOVWLZX to remove the partial-register dependency, and jump directly back to loop instead of bouncing through loopcheck. Regenerate internal/lz4block/decode_amd64.s.

Where it lives

perfloop-oss/lz4 · internal/lz4block/_gen/amd64.go

Evidence

Block and frame decompression · 26 sample pairs

metric baseline candidate paired median change confidence range required result
BenchmarkUncompress ns/op 211576 174696 −17.5% (−37076) −42430 to −33263 gain > 0 PASSED
BenchmarkUncompressPg1661 ns/op 342616 302624 −11.8% (−40284) −50197 to −34909 gain > 0 PASSED
BenchmarkUncompressTwain ns/op 222778 196258 −12% (−26647) −28701 to −22621 gain > 0 PASSED
BenchmarkUncompressKernelCode ns/op 353550 329863 −6.7% (−23777) −29863 to −16085 gain > 0 PASSED
BenchmarkUncompressTwainLinked ns/op 4842502 4675936 −3.5% (−170324) −257309 to −37357 gain > 0 PASSED
BenchmarkUncompressColumnarShort ns/op 86638 84623 −2% (−1774) −4905 to −196 gain > 0 PASSED
BenchmarkUncompressColumnarMed ns/op 36863 37010 +221.5 −338 to +855 no regression (noise ≤ 0.05 × |baseline median|) PASSED
BenchmarkUncompressRLE1 ns/op 35858 36036 +65.5 −504 to +504 no regression (noise ≤ 0.05 × |baseline median|) PASSED
BenchmarkUncompressRLE8 ns/op 35999 36016 +84.5 −846 to +583 no regression (noise ≤ 0.05 × |baseline median|) PASSED
BenchmarkUncompressRLE24 ns/op 33179 33321 +308 −1040 to +1563 no regression (noise ≤ 0.05 × |baseline median|) PASSED
BenchmarkSilesia/go4M ns/op 119391839 112776965 −5.4% (−6465034) −6998129 to −5154986 no regression (noise ≤ 0.05 × |baseline median|) PASSED
BenchmarkSilesia/go64K ns/op 118411444 111927026 −5.5% (−6519602) −8855260 to −5202605 no regression (noise ≤ 0.05 × |baseline median|) PASSED
BenchmarkSilesia/cLinked64K ns/op 145671026 138146773 −5.1% (−7451847) −10488131 to −4146551 no regression (noise ≤ 0.05 × |baseline median|) PASSED
BenchmarkSilesia/cLinked4M ns/op 126048284 117682229 −6.8% (−8548375) −10686488 to −5566130 no regression (noise ≤ 0.05 × |baseline median|) PASSED

The check passes with this change: `For the amd64 short-sequence path on live non-wrapping slices, with source end at least 17 and destination end at least 32, the room guards cover every shortcut load and store and leave the next token in bounds; parsed zero offsets are rejected before match-length extension, and unsigned borrow takes the slow match path, where valid matches that fit the destination reach dictionary handling.`

Checks: 6 of 6 passed. Verification: no defect found.

Timeline