perf(relinker): replace O(n²) zero-padding scan in StrictReachability #1522
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1522"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by silentone12725 on 2026-10-08T11:02:54Z.
What
addGapandbuildEdgeseach calledisZeroPadding(address, end)at every instruction boundary.isZeroPaddingscans from the current address to the region end, so the total work for a region with N instructions followed by a trailing-zero suffix of length Z was O(N×Z) — quadratic when Z is large.Replace with
trailingZeroStart, which scans backwards from the region end once to find the start of the trailing-zero suffix. The decode loop then uses that position as its stopping point, removing the per-instruction zero check entirely. The decode call itself continues to receive the original region end as the byte limit so that multi-byte instructions whose body contains zero bytes are decoded correctly.buildEdgespreviously resetlastFlowtoSequentialwhen the walk met zero padding, which gave a region ending in a jump or return followed by padding its fall-through edge. The reset is kept: it applies when the walk stops beforeregion.End, not when the last instruction merely ends in zero bytes.fallthroughAfterZeroPaddinginStrictNidFilterTests.cppcovers it and fails without the reset.This PR no longer carries the
ElfReader.cppchange; that stays in #1521.Tested
Linux:
strict_nid_filter_testspassed, including the newfallthroughAfterZeroPaddingcase, which fails when the reset is removed.Conventions check: passed (
python3 tools/check_conventions.py --base origin/main)Timing (
AnalyzeStrictReachability, one run of zero bytes followed by twonops,g++ -O2, best of 3; first reported by @Dextroid17, re-measured here):mainmainquadruples with every doubling, this PR doubles. Results are identical at every size.Differential fuzz: 30 000 random inputs (jumps, returns, import thunks, UD2, zero runs of 1-40 bytes, optional trailing padding, optional function regions) give the same result or the same exception on
mainand this PR. The same fuzz flags 23 inputs when thelastFlowreset is removed and 9 405 with the earliereffectiveEnd != region.Endcondition.Build workflow run on the fork, dispatched on head
cfb2198c: passed for Linux, Windows, and the relinker tests on Ubuntu (g++), macOS (clang++) and Windows (g++) (run 37833572111). It is not in the PR checks because the upstream run for this head is waiting for maintainer approval (#1631).Changes since the first revision
breakalso dropped thelastFlow = Sequentialreset, so a region ending in a jump or return followed by zero padding lost its fall-through edge. Fixed by resettinglastFlowwhen the walk stops beforeregion.End.effectiveEnd != region.End) broke the existingud1DeadTailcase, because zero bytes inside the last instruction's own operand are not padding. The condition used here isaddress < region.End.fallthroughAfterZeroPaddingtoStrictNidFilterTests.cpp; it fails without the reset.ElfReader.cpphunk is removed from this PR; it belongs to #1521.main(see Tested).main.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T11:16:20Z.
Review: COMMENTED
Good performance optimization that eliminates the O(n²) zero-padding scan in StrictReachability. The approach of finding the trailing-zero start once and using it as a stopping point is elegant and efficient.
Strengths:
Minor notes:
This looks ready to merge once rebased and CI passes.
Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T11:22:46Z.
Review: APPROVED
Good performance optimization and correctness fix. This PR addresses two important issues in the relinker.
Strengths:
Technical notes:
effectiveEndonce per region and using it as the decode loop's stopping point is clean and efficientThese changes improve both performance and robustness without changing behavior. Ready to merge.
Imported from boykopovar/AnyPS5. Originally posted by silentone12725 on 2026-10-08T13:29:27Z.
Hi @klosixgaming-arch, the branch has been rebased onto main and conventions checks pass. Would you be able to merge this when you get a chance? Thanks!
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T15:53:24Z.
Verified this with a standalone harness against
main's analyser and yours, on two shapes: the one your title is about, and one where the change is not equivalent. Both numbers are reproducible from the commands at the end.1. The perf claim holds — and here is the timing your description does not have. Shape: a 99 998-byte run of zero bytes (49 999
add [rax],alinstructions) with twonops at the far end, so the tail is never all-zero andisZeroPaddingis called at every instruction boundary, each call scanning to the next non-zero byte:111× faster, identical results. Worth putting in the description — a
perf(...)PR currently has no number in it, and the maintainer's review of the sibling PRs asked for exactly that.2. But it is not a pure refactor: one edge is lost.
buildEdgesreadslastFlowafter the loop:and the loop you replaced set
lastFlow = Codegen::ControlFlowKind::Sequentialbefore breaking on zero padding. That reset is what made the region fall through toregion.End. Without it,lastFlowis the flow kind of the last decoded instruction, so a region whose code ends with an unconditional jump or a return followed by zero padding no longer gets that edge.Harness case:
A = [0x1000,0x1010)is onejmp rel32then zero padding;B = [0x1010,0x1018)is anFF 25import thunk for slot0x2000:The strict filter can therefore drop an import that
mainkeeps — the kind of difference that shows up as a title failing to resolve a NID after conversion, not as a test failure, which is why the suite passes either way.3. The fix is one line, and I checked that it restores it. After the loop:
(
effectiveEnd != region.Endis exactly "the walk met the padding", which is when the old loop resetlastFlow.)4. This PR also carries #1521's change.
core/relinker/relinker/src/parsing/ElfReader.cpp +6/-0is the same two zero-entry-size guards as #1521 — a second topic in aperfPR. Beyond the one-topic rule, both guards are unreachable onmain:ReadProgramHeadersalready throws whenProgramHeaderCount != 0 && ProgramHeaderEntrySize != 56(a superset of== 0, and a run reports that message), andReadSectionHeaders()has no caller in the tree. So merging this would land dead code as a side effect of a perf fix. I would drop that hunk here and let #1521 stand on its own.Reproducing any of this:
Happy to paste the harness inline if that is easier than re-deriving it — it prints the live imports, the live/total region counts and the timing above.
Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T01:08:54Z.
Checked the new
trailingZeroStartloop against the old per-instruction check: same stop point, andlastFlowis reset toSequentialexactly when the walk ends beforeregion.End. Relinker ctest (42 tests, incl.strict_nid_filter) green locally; removing theaddress < region.Endreset makesfallthroughAfterZeroPaddingfail. CI green on the head. Merging.