perf(relinker): flat address-taken vector and region query caching in strict reachability #6974

Merged
HamidRezaLS merged 1 commit from upstream-pr/6974 into main 2026-10-19 15:53:17 +00:00
HamidRezaLS commented 2026-10-16 23:29:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by HamidRezaLS on 2026-10-16T23:29:38Z.


What

A follow-up performance pass on StrictReachability and relocation handling after #2304:

  • Flat Vector in StrictReachability: Replaced the node-based std::set<VirtualAddress> addressTaken with a contiguous std::vector<VirtualAddress>, sorted once with std::sort + std::unique. On large binaries, this cuts out 3.3 million individual heap node allocations and tree rebalancings.
  • Region Query Caching: Added a lastOwner pointer cache in owner(VirtualAddress address). Since addressTaken roots are processed in ascending order, the majority of consecutive queries hit the exact same function region, turning millions of tree lookups into direct O(1) checks.
  • Flat Vector for Branch Targets: Changed branchTargets in WindowsTlsBuilder from std::set to a flat sorted std::vector, queried using std::upper_bound.
  • Hinted Insertion: Used emplace_hint during sequential instruction collection in CodeInstructionCollector.

Tested

  • OS / Machine: Linux x86_64, Fedora 44, GCC 15.2, AMD Ryzen 7 6800HS
  • Title Tested: Dead Cells: Return to Castlevania Edition (Title ID: PPSA15552, 39.7 MB executable, 23.8 MB .text)
  • Verification: Both Linux and Windows output binaries match baseline bit-for-bit (sha256sum):
    • Windows: 528ada1f9c1ee3a804fc71af08844d485fc44881af0b6ee2f4094c48e1778133
    • Linux: 84993f509af2c330f69dc2b33f4eea5e6d59b3dff5bfd9cf9d93d30ea00b02df
  • Tests: ctest -R "strict_nid_filter|relinker_conversion|to_intel_linux_stubs" passed cleanly. check_conventions.py passed with 0 findings.

Dead Cells (PPSA15552) conversion timings (unused-filter=2):

Target Baseline (main) This PR Delta
Linux (unused-filter=2) 7.65 s 4.56 s -3.09 s (-40.4% faster)
Windows (--windows unused-filter=2) 18.81 s 14.73 s -4.08 s (-21.7% faster)

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes, with manual review and local test runs
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4895433861 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6974). Originally posted by [HamidRezaLS](https://github.com/HamidRezaLS) on 2026-10-16T23:29:38Z. --- ### What A follow-up performance pass on `StrictReachability` and relocation handling after #2304: - **Flat Vector in StrictReachability:** Replaced the node-based `std::set<VirtualAddress> addressTaken` with a contiguous `std::vector<VirtualAddress>`, sorted once with `std::sort` + `std::unique`. On large binaries, this cuts out 3.3 million individual heap node allocations and tree rebalancings. - **Region Query Caching:** Added a `lastOwner` pointer cache in `owner(VirtualAddress address)`. Since `addressTaken` roots are processed in ascending order, the majority of consecutive queries hit the exact same function region, turning millions of tree lookups into direct $O(1)$ checks. - **Flat Vector for Branch Targets:** Changed `branchTargets` in `WindowsTlsBuilder` from `std::set` to a flat sorted `std::vector`, queried using `std::upper_bound`. - **Hinted Insertion:** Used `emplace_hint` during sequential instruction collection in `CodeInstructionCollector`. ### Tested - **OS / Machine:** Linux x86_64, Fedora 44, GCC 15.2, AMD Ryzen 7 6800HS - **Title Tested:** *Dead Cells: Return to Castlevania Edition* (Title ID: `PPSA15552`, 39.7 MB executable, 23.8 MB `.text`) - **Verification:** Both Linux and Windows output binaries match baseline bit-for-bit (`sha256sum`): - Windows: `528ada1f9c1ee3a804fc71af08844d485fc44881af0b6ee2f4094c48e1778133` - Linux: `84993f509af2c330f69dc2b33f4eea5e6d59b3dff5bfd9cf9d93d30ea00b02df` - **Tests:** `ctest -R "strict_nid_filter|relinker_conversion|to_intel_linux_stubs"` passed cleanly. `check_conventions.py` passed with 0 findings. #### Dead Cells (PPSA15552) conversion timings (`unused-filter=2`): | Target | Baseline (`main`) | This PR | Delta | | :--- | :---: | :---: | :---: | | **Linux (`unused-filter=2`)** | 7.65 s | **4.56 s** | **-3.09 s (-40.4% faster)** | | **Windows (`--windows unused-filter=2`)** | 18.81 s | **14.73 s** | **-4.08 s (-21.7% faster)** | ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes, with manual review and local test runs
github-actions[bot] commented 2026-10-16 23:33:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T23:33:41Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6618 core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp
PR 6977 core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6226664762 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6974#issuecomment-6226664762). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-16T23:33:41Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 6618](https://www.github.com/boykopovar/AnyPS5/pull/6618) | `core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp` |
simonbbby commented 2026-10-17 02:23:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-17T02:23:22Z.


Independent Linux correctness check on exact head 5d9fd5103f8e6a0f9b60f6411a1eab362013676e (Ubuntu 24.04 x86-64, GCC 13.3, Release relinker-only). This checks equivalence; it does not reproduce the Dead Cells timing measurement.

  • 40/40 focused CTests pass: strict NID filtering, relinker conversion, Intel Linux stubs and the TLS family, including protected entries, moved branches, displacement bounds and rejected forms.
  • 5,000 generated reachability cases per implementation: both the unchanged production parent and optimized AnalyzeStrictReachability agree with an independent region-graph traversal on all six result fields: exact instruction/import sets, indirect transfers, live/total regions and unique address-taken roots. Five fixed seeds cover 2–64 adjacent regions, shuffled entry order, interior and exact-boundary roots, duplicate pointer/data roots, cyclic extra edges, import slots and out-of-code pointer values. All code is generated NOP/RET functions; no title data is used.
  • Discriminating negative controls: making the cached region's end inclusive loses a live region at seed 1 / trial 4 (640 instead of 656 instruction addresses). Removing the vector's unique-erasure step fails seed 1 / trial 0 (113 instead of 63 address-taken roots). Thus the oracle detects both a boundary-cache error and duplicate-count drift.
  • 84 actual CLI comparisons: I rebuilt all three changed production objects from the parent and linked them with the identical remaining objects. Across generated constructor/strict-filter fixtures and both ELF/PE targets, 24 accepted conversions have identical output SHA-256 hashes; 60 refusals have identical exit status and diagnostic text. Every input remains unchanged.

Scope: the generated oracle covers the flat-root/region-cache changes, not every x86 instruction or relative-table encoding. The 84-case byte comparison is synthetic; it does not establish title performance, full large-title equivalence, Windows host execution or a current-main merge result.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6228455837 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6974#issuecomment-6228455837). Originally posted by [simonbbby](https://github.com/simonbbby) on 2026-10-17T02:23:22Z. --- Independent Linux correctness check on exact head `5d9fd5103f8e6a0f9b60f6411a1eab362013676e` (Ubuntu 24.04 x86-64, GCC 13.3, Release relinker-only). This checks equivalence; it does not reproduce the Dead Cells timing measurement. - **40/40 focused CTests pass:** strict NID filtering, relinker conversion, Intel Linux stubs and the TLS family, including protected entries, moved branches, displacement bounds and rejected forms. - **5,000 generated reachability cases per implementation:** both the unchanged production parent and optimized `AnalyzeStrictReachability` agree with an independent region-graph traversal on all six result fields: exact instruction/import sets, indirect transfers, live/total regions and unique address-taken roots. Five fixed seeds cover 2–64 adjacent regions, shuffled entry order, interior and exact-boundary roots, duplicate pointer/data roots, cyclic extra edges, import slots and out-of-code pointer values. All code is generated NOP/RET functions; no title data is used. - **Discriminating negative controls:** making the cached region's end inclusive loses a live region at seed 1 / trial 4 (640 instead of 656 instruction addresses). Removing the vector's unique-erasure step fails seed 1 / trial 0 (113 instead of 63 address-taken roots). Thus the oracle detects both a boundary-cache error and duplicate-count drift. - **84 actual CLI comparisons:** I rebuilt all three changed production objects from the parent and linked them with the identical remaining objects. Across generated constructor/strict-filter fixtures and both ELF/PE targets, 24 accepted conversions have identical output SHA-256 hashes; 60 refusals have identical exit status and diagnostic text. Every input remains unchanged. Scope: the generated oracle covers the flat-root/region-cache changes, not every x86 instruction or relative-table encoding. The 84-case byte comparison is synthetic; it does not establish title performance, full large-title equivalence, Windows host execution or a current-main merge result.
github-actions[bot] commented 2026-10-19 12:42:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T12:42:41Z.


This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6267474015 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6974#issuecomment-6267474015). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-19T12:42:41Z. --- <!-- pr-labels-perf --> This pull request is labeled `perf`, so it needs the summary line of a capture on `main` and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both `aps5-summary.jsonl` lines, attach the two traces and add the output of `tools/trace_digest.py`.
adriadam10 commented 2026-10-19 15:54:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T15:54:13Z.


Rebased onto current main (only conflict: the branchTargets declaration next to the new gsTraps in WindowsTlsBuilder.cpp). Checked that the sorted/unique vector + upper_bound is equivalent to the std::set lookups, that lastOwner points into a std::map (stable nodes), and that the cached code-header lookup returns the same header as the linear scan. Relinker build and ctest pass after the rebase; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6271381725 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6974#issuecomment-6271381725). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T15:54:13Z. --- Rebased onto current main (only conflict: the `branchTargets` declaration next to the new `gsTraps` in `WindowsTlsBuilder.cpp`). Checked that the sorted/unique vector + `upper_bound` is equivalent to the `std::set` lookups, that `lastOwner` points into a `std::map` (stable nodes), and that the cached code-header lookup returns the same header as the linear scan. Relinker build and ctest pass after the rebase; merged.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!6974
No description provided.