perf(relinker): stream scans and reuse executable buffers #5116

Merged
sanxchep merged 1 commit from upstream-pr/5116 into main 2026-10-14 12:02:11 +00:00
sanxchep commented 2026-10-13 12:09:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by sanxchep on 2026-10-13T12:09:49Z.


What

Stream the relinker's instruction scan through a visitor instead of materializing every decoded match. The syscall scanner retains only its bounded 64-instruction lookback for inline-stub detection; call-site resolution decodes once. Move the input ELF buffer through the conversion pipeline and avoid copying the text section when unused-symbol filtering is disabled. The behavior is generic to relinker inputs and contains no Kena-specific paths.

Tested

Hardware: Intel Core i9-13900H (14 cores / 20 threads), 32 GiB RAM, Intel Iris Xe and NVIDIA GeForce RTX 4070 Laptop GPU. Ubuntu 24.04, Linux 7.0.0-38-generic, GCC 13.3.0. This is a CPU-only conversion benchmark; neither GPU was used.

Build and automated checks: Release relinker-only build with ANYPS5_RELINKER_ONLY=ON and BUILD_TESTING=ON; 149/149 CTest cases passed on this branch. python3 tools/check_conventions.py --base origin/main and git diff --check origin/main...HEAD passed.

Undeclared Game name: same 171,748,944-byte input ELF and 36 bundled modules, default Linux conversion, five paired sequential runs on the same machine. Measured with /usr/bin/time; elapsed seconds and maximum resident set size (KiB):

Pair main time PR time main peak RSS PR peak RSS
1 2.03 1.47 1,139,656 421,392
2 2.10 1.21 1,139,932 421,400
3 2.31 1.50 1,139,484 421,472
4 2.28 1.22 1,139,696 421,080
5 2.31 1.53 1,139,688 421,468
Median 2.28 1.47 1,139,688 421,400

Median elapsed time fell 35.5% and peak RSS 63.0%. Baseline was clean main at 84e2a8c0b; relinker sources were unchanged between that revision and this PR's base 14036cb0e. The complete default Linux output, including all 36 converted modules, was byte-identical between baseline and PR. The unused-filter=1 output was also byte-identical (10.88 s / 1,688,192 KiB baseline; 10.42 s / 1,509,596 KiB PR, one run). Windows and macOS cross-target conversions with bundled modules skipped produced byte-identical outputs; they were generated on Linux, not executed on those hosts.

APS5 frame traces do not apply to this pre-execution CLI conversion; the paired /usr/bin/time measurements above are the performance evidence. The converted Interactive Program was not run for this PR, so no InteractivePlay or frame-rate claim is made. No Interactive Program files are included in the change.

Checklist

  • Based on current main; no other open PR found implementing this scanner and buffer optimization
  • One topic per PR; follow-ups go in a new PR
  • No code comments added
  • No unimplemented paths or silent stubs added
  • No investigation files, images, logs, or game data added to the repository
  • No new third-party code
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes; implementation, benchmarking, and PR preparation were AI-assisted and checked against tests and binary output
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4842997529 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5116). Originally posted by [sanxchep](https://github.com/sanxchep) on 2026-10-13T12:09:49Z. --- ### What Stream the relinker's instruction scan through a visitor instead of materializing every decoded match. The syscall scanner retains only its bounded 64-instruction lookback for inline-stub detection; call-site resolution decodes once. Move the input ELF buffer through the conversion pipeline and avoid copying the text section when unused-symbol filtering is disabled. The behavior is generic to relinker inputs and contains no Kena-specific paths. ### Tested **Hardware:** Intel Core i9-13900H (14 cores / 20 threads), 32 GiB RAM, Intel Iris Xe and NVIDIA GeForce RTX 4070 Laptop GPU. Ubuntu 24.04, Linux 7.0.0-38-generic, GCC 13.3.0. This is a CPU-only conversion benchmark; neither GPU was used. **Build and automated checks:** Release relinker-only build with `ANYPS5_RELINKER_ONLY=ON` and `BUILD_TESTING=ON`; 149/149 CTest cases passed on this branch. `python3 tools/check_conventions.py --base origin/main` and `git diff --check origin/main...HEAD` passed. **Undeclared Game name:** same 171,748,944-byte input ELF and 36 bundled modules, default Linux conversion, five paired sequential runs on the same machine. Measured with `/usr/bin/time`; elapsed seconds and maximum resident set size (KiB): | Pair | main time | PR time | main peak RSS | PR peak RSS | | --- | ---: | ---: | ---: | ---: | | 1 | 2.03 | 1.47 | 1,139,656 | 421,392 | | 2 | 2.10 | 1.21 | 1,139,932 | 421,400 | | 3 | 2.31 | 1.50 | 1,139,484 | 421,472 | | 4 | 2.28 | 1.22 | 1,139,696 | 421,080 | | 5 | 2.31 | 1.53 | 1,139,688 | 421,468 | | **Median** | **2.28** | **1.47** | **1,139,688** | **421,400** | Median elapsed time fell **35.5%** and peak RSS **63.0%**. Baseline was clean `main` at `84e2a8c0b`; relinker sources were unchanged between that revision and this PR's base `14036cb0e`. The complete default Linux output, including all 36 converted modules, was byte-identical between baseline and PR. The `unused-filter=1` output was also byte-identical (10.88 s / 1,688,192 KiB baseline; 10.42 s / 1,509,596 KiB PR, one run). Windows and macOS cross-target conversions with bundled modules skipped produced byte-identical outputs; they were generated on Linux, not executed on those hosts. APS5 frame traces do not apply to this pre-execution CLI conversion; the paired `/usr/bin/time` measurements above are the performance evidence. The converted Interactive Program was not run for this PR, so no InteractivePlay or frame-rate claim is made. No Interactive Program files are included in the change. ### Checklist - [x] Based on current `main`; no other open PR found implementing this scanner and buffer optimization - [x] One topic per PR; follow-ups go in a new PR - [x] No code comments added - [x] No unimplemented paths or silent stubs added - [x] No investigation files, images, logs, or game data added to the repository - [x] No new third-party code - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes; implementation, benchmarking, and PR preparation were AI-assisted and checked against tests and binary output
AaravMehta-07 commented 2026-10-13 12:23:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AaravMehta-07 on 2026-10-13T12:23:01Z.


I have a separate, locally tested correctness fix that rejects ET_REL inputs before executable conversion. It adds a guard at the start of RelinkerPipeline::Relink plus a Linux-output CLI regression; it does not change buffer ownership or scanning.

Your current signature/initialization hunk touches the same insertion point. Could this PR go first, with my guard rebased onto it afterward? I will hold submission until the order is agreed or this refactor merges. The guard follows the ELF GABI distinction between relocatable objects and executable images, and the new test fails without it. The current standalone suite passes 144/144 locally on Windows.

This investigation, patch and comment are AI-assisted. No title runtime or performance result is claimed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6145449355 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5116#issuecomment-6145449355). Originally posted by [AaravMehta-07](https://github.com/AaravMehta-07) on 2026-10-13T12:23:01Z. --- I have a separate, locally tested correctness fix that rejects `ET_REL` inputs before executable conversion. It adds a guard at the start of `RelinkerPipeline::Relink` plus a Linux-output CLI regression; it does not change buffer ownership or scanning. Your current signature/initialization hunk touches the same insertion point. Could this PR go first, with my guard rebased onto it afterward? I will hold submission until the order is agreed or this refactor merges. The guard follows the ELF GABI distinction between relocatable objects and executable images, and the new test fails without it. The current standalone suite passes 144/144 locally on Windows. This investigation, patch and comment are AI-assisted. No title runtime or performance result is claimed.
sanxchep commented 2026-10-13 12:50:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by sanxchep on 2026-10-13T12:50:19Z.


I have a separate, locally tested correctness fix that rejects ET_REL inputs before executable conversion. It adds a guard at the start of RelinkerPipeline::Relink plus a Linux-output CLI regression; it does not change buffer ownership or scanning.

Your current signature/initialization hunk touches the same insertion point. Could this PR go first, with my guard rebased onto it afterward? I will hold submission until the order is agreed or this refactor merges. The guard follows the ELF GABI distinction between relocatable objects and executable images, and the new test fails without it. The current standalone suite passes 144/144 locally on Windows.

This investigation, patch and comment are AI-assisted. No title runtime or performance result is claimed.

Thanks for checking. Your ET_REL guard sounds independent of the scanning and buffer changes, and the shared Relink hunk should be straightforward to rebase. Please feel free to submit your fix as a separate PR and note the overlap. If this PR lands first, you can rebase onto it; if yours lands first, I’ll rebase this one.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6145937026 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5116#issuecomment-6145937026). Originally posted by [sanxchep](https://github.com/sanxchep) on 2026-10-13T12:50:19Z. --- > I have a separate, locally tested correctness fix that rejects `ET_REL` inputs before executable conversion. It adds a guard at the start of `RelinkerPipeline::Relink` plus a Linux-output CLI regression; it does not change buffer ownership or scanning. > > Your current signature/initialization hunk touches the same insertion point. Could this PR go first, with my guard rebased onto it afterward? I will hold submission until the order is agreed or this refactor merges. The guard follows the ELF GABI distinction between relocatable objects and executable images, and the new test fails without it. The current standalone suite passes 144/144 locally on Windows. > > This investigation, patch and comment are AI-assisted. No title runtime or performance result is claimed. Thanks for checking. Your ET_REL guard sounds independent of the scanning and buffer changes, and the shared Relink hunk should be straightforward to rebase. Please feel free to submit your fix as a separate PR and note the overlap. If this PR lands first, you can rebase onto it; if yours lands first, I’ll rebase this one.
AaravMehta-07 commented 2026-10-13 12:57:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AaravMehta-07 on 2026-10-13T12:57:55Z.


Thanks for coordinating. The separate ET_REL guard is submitted as #5148, with the shared hunk and our rebasing agreement explicitly documented. Neither change depends semantically on the other; I will rebase promptly if your refactor merges first.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6146073779 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5116#issuecomment-6146073779). Originally posted by [AaravMehta-07](https://github.com/AaravMehta-07) on 2026-10-13T12:57:55Z. --- Thanks for coordinating. The separate ET_REL guard is submitted as #5148, with the shared hunk and our rebasing agreement explicitly documented. Neither change depends semantically on the other; I will rebase promptly if your refactor merges first.
CnRJay commented 2026-10-14 01:51:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T01:51:03Z.

Review: COMMENTED


I checked this against main and it looks good to me. The RSS drop is what I'd expect from no longer keeping every decoded InstructionMatch for a 170 MB input.

The points I checked:

  • The 64-entry window is large enough. ConstantSyscallNumber looks back SYSCALL_NUMBER_LOOKBACK (8) instructions. The inline-stub matchers look back at most 42 bytes, which is at most 42 instructions. step < recent.count is the same bound as the old step <= syscallIndex.
  • textSection is still filled whenever something reads it. Only the two filter levels and CompactPlt (level 2) read it. Call-site resolution for the registry goes through executableSegments. executableSegments.empty() is equivalent to the old textSection.empty() test, because empty segments are never added.
  • The buffer is not read after TakeBytes. On main, Conversion::Convert only hands elfReader to the pipeline, and RelinkResult holds copies, so nothing reads the reader after TakeBytes().
  • ripRelativeDispOffset accepts the same encodings as the two predicates it replaces. FF /2–/6 takes any REX, 8B needs REX.W, and both need mod=00, rm=101. The new CallSiteResolverTests cases cover the REX variants.

One small difference: the Syscalls progress stage now reports only once, at the end. The per-4096-instruction updates are gone, so a progress display shows Decode moving and then Syscalls jumping to done. The total work is the same, so this is cosmetic. It might still be worth one start report so the stage appears.

/usr/bin/time pairs plus byte-identical outputs are the right evidence for a CLI conversion; frame traces don't apply here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5512139547 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5116#pullrequestreview-5512139547). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T01:51:03Z. **Review:** COMMENTED --- I checked this against `main` and it looks good to me. The RSS drop is what I'd expect from no longer keeping every decoded `InstructionMatch` for a 170 MB input. The points I checked: - **The 64-entry window is large enough.** `ConstantSyscallNumber` looks back `SYSCALL_NUMBER_LOOKBACK` (8) instructions. The inline-stub matchers look back at most 42 bytes, which is at most 42 instructions. `step < recent.count` is the same bound as the old `step <= syscallIndex`. - **`textSection` is still filled whenever something reads it.** Only the two filter levels and `CompactPlt` (level 2) read it. Call-site resolution for the registry goes through `executableSegments`. `executableSegments.empty()` is equivalent to the old `textSection.empty()` test, because empty segments are never added. - **The buffer is not read after `TakeBytes`.** On `main`, `Conversion::Convert` only hands `elfReader` to the pipeline, and `RelinkResult` holds copies, so nothing reads the reader after `TakeBytes()`. - **`ripRelativeDispOffset` accepts the same encodings as the two predicates it replaces.** `FF /2–/6` takes any REX, `8B` needs REX.W, and both need mod=00, rm=101. The new `CallSiteResolverTests` cases cover the REX variants. One small difference: the `Syscalls` progress stage now reports only once, at the end. The per-4096-instruction updates are gone, so a progress display shows `Decode` moving and then `Syscalls` jumping to done. The total work is the same, so this is cosmetic. It might still be worth one start report so the stage appears. `/usr/bin/time` pairs plus byte-identical outputs are the right evidence for a CLI conversion; frame traces don't apply here.
oneandonlydean commented 2026-10-14 03:05:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T03:05:25Z.


Conflicts with main in RelinkerPipeline.cpp since #5148 added the ET_REL check at the top of Relink; please rebase and keep that check.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159957212 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5116#issuecomment-6159957212). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T03:05:25Z. --- Conflicts with main in `RelinkerPipeline.cpp` since #5148 added the `ET_REL` check at the top of `Relink`; please rebase and keep that check.
sanxchep commented 2026-10-14 05:42:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by sanxchep on 2026-10-14T05:42:57Z.


Conflicts with main in RelinkerPipeline.cpp since #5148 added the ET_REL check at the top of Relink; please rebase and keep that check.

done

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161774256 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5116#issuecomment-6161774256). Originally posted by [sanxchep](https://github.com/sanxchep) on 2026-10-14T05:42:57Z. --- > Conflicts with main in `RelinkerPipeline.cpp` since #5148 added the `ET_REL` check at the top of `Relink`; please rebase and keep that check. done
adriadam10 commented 2026-10-14 11:23:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T11:23:57Z.


Checked the streamed scan against the old code: the 64-entry window covers SYSCALL_NUMBER_LOOKBACK (8) and the 42-byte SDK inline stubs, step < recent.count matches the old step <= syscallIndex, and ripRelativeDispOffset is the old FF/mov64 checks merged with the same conditions. textSection is only read by unused-filter levels 1/2 and CompactPlt. The ET_REL check from #5148 is kept. relinker build and ctest (128/128) are green, relinker CI green on linux/windows/macos; merging once the full Windows build passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6167223365 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5116#issuecomment-6167223365). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T11:23:57Z. --- Checked the streamed scan against the old code: the 64-entry window covers `SYSCALL_NUMBER_LOOKBACK` (8) and the 42-byte SDK inline stubs, `step < recent.count` matches the old `step <= syscallIndex`, and `ripRelativeDispOffset` is the old FF/mov64 checks merged with the same conditions. `textSection` is only read by unused-filter levels 1/2 and `CompactPlt`. The `ET_REL` check from #5148 is kept. relinker build and ctest (128/128) are green, relinker CI green on linux/windows/macos; merging once the full Windows build passes.
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!5116
No description provided.