perf(relinker): collect code instructions into sorted vectors #12154
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12154"
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 JJenkx on 2026-10-27T23:53:52Z.
What
A Windows conversion spends most of its time in
CodeInstructionCollector::Collect, called byWindowsTlsBuilder::Build. Within that, a large share goes tostd::set<std::uint64_t>insertions. Every decoded instruction address and every static branch target was inserted into a red-black tree, millions of insertions for a 35 MBeboot.Stack samples of
relinker --windowson PPSA28311'seboot(gdb attached every 150 ms, 40 samples, main thread) showed this. 34 samples were insideCollect. In 14 of them the frame directly belowCollectwasstd::_Rb_tree::_M_insert_unique, and a few more were the matching node allocation andfree. The conversion runs on one thread: 9.5 s of CPU in 9.8 s of wall time.Two commits:
staticTargetsis sorted and deduplicated before the check against skipped ranges, so it is walked in the same order as before.instructionscollects into a vector during the linear decode and the graph passes. It is sorted and deduplicated once, and thestd::setthatCollectreturns is built from that sorted list, which is linear time.unordered_set<VirtualAddress>, one node allocation each. In a profile of commit 1, its insertions and the matching heap allocations were 6 of 40 samples, and removing it gave more than its sample share suggested. Each code section now has avector<bool>over its bytes, sized on first use. An address outside its section, for which the decode below would have read past the section, is now reported as an error.The overlap check, the return type and all callers are unchanged.
Tested
Current-main refresh (2026-11-01): linear head
0e8027852cf4e9c9473ff75f9857123fda3fc89bon main93dbd0f82aea. Main's incrementalExtendControlFlowGraphand byte-per-offset reached state are retained. The decoded-address bitmap and sorted/deduplicated vector collection are adapted to that API. Linux x86-64, GCC 16.2.1, Ninja Release,ANYPS5_RELINKER_ONLY=ON,BUILD_TESTING=ON: rebuilt relinker and all registered tests, 557/557 CTests pass; conventions and whitespace pass. Historical platform and performance reports below remain pinned to their stated commits; no new conversion timing or runtime gain is claimed.Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release,
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON. Two builds:main6e9b9134f6;ad732ce0ec, two commits on that base.Output is identical. Four title executables (unwrapped
eboot.bin) were converted with--windows --skip-sce-module --registry, alternating base and candidate, three times each. Every run produced a byte-identicalapp.exeandapp.registry.jsonon both builds, and every run exited 0.Wall time, min/median/max over 3 runs each, in ms:
With commit 1 alone, the median changes were −15% to −17% (same titles and method).
Tests: all 372 relinker CTests pass in each of 3 runs on the candidate (both commits), and on the base.
python tools/check_conventions.py --base origin/mainandgit diff --checkpass.Overlap: Felipebmaitan's open #10861 rewrites the same graph-pass loop, including the
decodedInstructionsandinstructions.insert(address)lines changed here. Whichever lands second needs a small rebase.Not tested: Linux and macOS conversion times. The code is host-independent, and macOS conversion calls the same
Collect.Current refresh source proofs and unedited test logs, SHA-256
b4491a56de7e9655af33b7a34afbce236ceb43ebd4e44c11b42454b282648563.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T00:07:56Z.
Review: APPROVED
Verified on macOS (relinker-only build): full audit of the set→vector conversion, build 266/266, ctest 362/362 at head
a3e2c71bec.Equivalence audit (the risk of deferring dedup): I checked every use of both containers in
Collect()on the base tree:instructions— only two writes exist before the deferred sort (phase-1emplace_hintsite →push_back, phase-2 insert site →push_back). Every read that needs set semantics comes after the newsort/unique/set-construction block that followsreport(8500): the scan loop,instructions.size()in the progress callback, thelower_bound(address)lookback, and the return value. Cross-phase duplicates that the old set collapsed at insert time are collapsed by the finaluniqueinstead — same resulting set, and it's still constructed from a sorted range.staticTargets—sort+uniqueland directly above its single consumer (the branch-into-skipped-range-tail check), which iterated ascending under the oldstd::settoo, so iteration order, the throw condition and the throw order are unchanged.instructionList = {}after the set construction is memory hygiene only; the oldemplace_hint(end, …)hint has no observable effect either way.Drift check:
git diff <base>..upstream/mainonCodeInstructionCollector.cppis empty, so the audit holds against current main (the branch's base6e9b9134f6predates today's merges but the file is untouched since).Gates: fresh
ANYPS5_RELINKER_ONLYbuild 266/266; full ctest 362/362 — the end-to-end conversion fixtures exercise both collection phases (function walk +BuildControlFlowGraphreachability), covering the dedup path being moved.Perf rationale checks out: both hot containers allocate one red-black-tree node per address on real modules today; replacing them with
push_back+ one sort each removes the per-address allocation and keeps the addresses contiguous for the scan loop.Merge-order note (non-blocking): the only open PR sharing this file is #10861, and its hunks do overlap this region — but #10861 already conflicts with the base branch, so it needs a rebase regardless of ordering; this change is small and orthogonal enough to rebase either way.
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T02:50:52Z.
Review: APPROVED
Re-review after
ad732ce0ec(per-section bitmaps). My earlier approval covered onlya3e2c71bec.What I checked in the new commit:
ControlFlowGraph.cpponly addsvatoreachablewhenva - textVaddr < text.size(). Heretext.size() == header.FileSize == decoded.size(), andsectionReachable[i]is only ever filled by that section's graph. So theoffset >= decoded.size()check guards against a bad read and doesn't change behaviour. Before this commit, the same case would have read pasttext.instructionListis sorted and deduped before thestd::setis built, andaddRoot/addPointerRootonly insert intoroots/dataTargets, which are sets. The final instruction set and roots fixpoint are unchanged.instructionsisn't read anywhere between its declaration and the final sort/unique, so building it later is still safe (same check as for commit 1).FileSizebits per code section, about 4.4 MB for a 35 MB text.Evidence (macOS arm64, relinker-only, Release):
a3ac94774e: build 266/266,ctest366/366check_conventions --base upstream/mainpassed;git merge-treeagainst main is cleanpr_overlap: only #10861 (CodeInstructionCollector.cpp, couldn't be compared). That's a merge-order question, not a conflict in this diff.Still missing: the bot's
perfform asks for the twoaps5-summary.jsonllines (main vs. change). The gdb sampling in the description explains the change but isn't a substitute for those lines.