perf(agc): query unit shadows without copying the overlap list #856

Merged
meh7an merged 1 commit from upstream-pr/856 into main 2026-10-09 21:59:53 +00:00
meh7an commented 2026-10-06 09:59:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-06T09:59:25Z.


What

AnyShadowedOverlaps is asked on every guest access the flush hook sees and on several draw and copy paths. It collected the overlapping shadows into a std::vector<std::shared_ptr<UnitShadow>> (an allocation, and a reference count increment and decrement per shadow) only to read them under the registry lock it already holds. It now visits them in place through anyOverlapping, which overlapping() also uses for the callers that need the list.

Tested

Windows 11, MinGW GCC 15.2, RTX 2080 Ti. This branch builds, and the 165 agc driver, recompiler and execution test targets pass with it stacked on the other changes I'm sending.

Measured alone on Sonic Origins Plus's heaviest menu page (about 1,650 draws per frame), as asked. The base is main at this PR's base (5895ea72) plus the commits my branch needs to boot this title and to sample it (a launcher script, the title's boot fixes, the process sampler; none of them are in this PR), against the same base with this commit on top. Three interleaved rounds, each the median of six FPS readings:

Round 1 Round 2 Round 3 Mean
base 11.48 11.39 11.76 11.54 FPS (86.7 ms)
with this PR 11.76 11.87 11.81 11.81 FPS (84.7 ms)

Ahead in every round, though the gap is larger than the profile supports, so the profile is the better figure: over two 15 s profiles per side, AnyShadowedOverlaps took 39 of 5,119 AGC worker samples on the base and 24 of 4,920 with this PR. The 16 samples that went are the list building in overlapping and its heap allocation; what is left is the registry lock. That is about 0.3% of the worker, roughly 0.3 ms per frame.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4757122298 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/856). Originally posted by [meh7an](https://github.com/meh7an) on 2026-10-06T09:59:25Z. --- ### What `AnyShadowedOverlaps` is asked on every guest access the flush hook sees and on several draw and copy paths. It collected the overlapping shadows into a `std::vector<std::shared_ptr<UnitShadow>>` (an allocation, and a reference count increment and decrement per shadow) only to read them under the registry lock it already holds. It now visits them in place through `anyOverlapping`, which `overlapping()` also uses for the callers that need the list. ### Tested Windows 11, MinGW GCC 15.2, RTX 2080 Ti. This branch builds, and the 165 agc driver, recompiler and execution test targets pass with it stacked on the other changes I'm sending. Measured alone on Sonic Origins Plus's heaviest menu page (about 1,650 draws per frame), as asked. The base is `main` at this PR's base (5895ea72) plus the commits my branch needs to boot this title and to sample it (a launcher script, the title's boot fixes, the process sampler; none of them are in this PR), against the same base with this commit on top. Three interleaved rounds, each the median of six FPS readings: | | Round 1 | Round 2 | Round 3 | Mean | |---|---|---|---|---| | base | 11.48 | 11.39 | 11.76 | 11.54 FPS (86.7 ms) | | with this PR | 11.76 | 11.87 | 11.81 | 11.81 FPS (84.7 ms) | Ahead in every round, though the gap is larger than the profile supports, so the profile is the better figure: over two 15 s profiles per side, `AnyShadowedOverlaps` took 39 of 5,119 AGC worker samples on the base and 24 of 4,920 with this PR. The 16 samples that went are the list building in `overlapping` and its heap allocation; what is left is the registry lock. That is about 0.3% of the worker, roughly 0.3 ms per frame. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
oneandonlydean commented 2026-10-06 21:21:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-06T21:21:07Z.


Thanks. anyOverlapping visits the same shadows in the same order, and AnyShadowedOverlaps returns at the same live unit as before, so the result is unchanged.

One thing before it can go in. Performance PRs need before/after numbers for the change itself (the owner asked for this on #218). The 48.0 to 43.6 ms figure is for 15 changes together. Could you measure main against main plus this PR alone on the same page? The mean frame time over a few runs is enough, or the AnyShadowedOverlaps share of the profile.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6025678768 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/856#issuecomment-6025678768). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-06T21:21:07Z. --- Thanks. `anyOverlapping` visits the same shadows in the same order, and `AnyShadowedOverlaps` returns at the same live unit as before, so the result is unchanged. One thing before it can go in. Performance PRs need before/after numbers for the change itself (the owner asked for this on #218). The 48.0 to 43.6 ms figure is for 15 changes together. Could you measure `main` against `main` plus this PR alone on the same page? The mean frame time over a few runs is enough, or the `AnyShadowedOverlaps` share of the profile.
meh7an commented 2026-10-07 17:02:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-07T17:02:29Z.


Measured it alone and put the numbers in the description: the overlap list building and its allocation (16 of 5,119 AGC worker samples, about 0.3%) are gone, and what is left under AnyShadowedOverlaps is the registry lock. The FPS rounds were ahead each time, but the profile's 0.3 ms per frame is the figure to go by.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6042705707 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/856#issuecomment-6042705707). Originally posted by [meh7an](https://github.com/meh7an) on 2026-10-07T17:02:29Z. --- Measured it alone and put the numbers in the description: the overlap list building and its allocation (16 of 5,119 AGC worker samples, about 0.3%) are gone, and what is left under `AnyShadowedOverlaps` is the registry lock. The FPS rounds were ahead each time, but the profile's 0.3 ms per frame is the figure to go by.
Dextroid17 commented 2026-10-08 01:56:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T01:56:32Z.

Review: COMMENTED


Built and ran it — external PRs get no CI here.

Linux (CachyOS, GCC, Release), NVIDIA RTX 3080 Ti:

cmake --build build --parallel 16
ctest --test-dir build -j 16    →  359/359 passed

The rewrite is behaviour-preserving. AnyShadowedOverlaps returns true on the first live unit, which is exactly what the old loop did — the old body pushed into a vector and then scanned it, with no side effect after the push, so the early exit cannot change which unit is found. The guard (end <= address || byBase.empty()) and the upper_bound/--it walk are unchanged, so the search space is identical.

Two details that make the equivalence complete rather than approximate:

  • The overlapping() wrapper passes a visitor that always returns false, so it still walks the whole range in byBase order and builds the list in the same order for callers that need it. Nothing that consumes the vector sees a different ordering.
  • The span overload (AnyShadowedOverlaps(std::span<...>)) delegates to the address overload in a loop and short-circuits on the first true, so it inherits the change without modification — I checked it rather than assumed it, since that was the one place the treatment could have been left half-applied.

The win is the shared_ptr copies. Naming it explicitly because it is the more interesting half: the old path copied a shared_ptr per overlapping shadow, and each copy is an atomic increment plus, on destruction, a decrement. Removing the vector removes those atomics per query, not just a small allocation. That is a real cost in a path that runs per guest store.

What I could not do: measure it. There is no benchmark here that isolates AnyShadowedOverlaps, so what I have is equivalence and no regression, not a number — I would rather say that than quote a timing I did not take.

This is a comment, not an approval — I have no merge rights here.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5450514422 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/856#pullrequestreview-5450514422). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T01:56:32Z. **Review:** COMMENTED --- Built and ran it — external PRs get no CI here. **Linux (CachyOS, GCC, Release), NVIDIA RTX 3080 Ti:** ``` cmake --build build --parallel 16 ctest --test-dir build -j 16 → 359/359 passed ``` **The rewrite is behaviour-preserving.** `AnyShadowedOverlaps` returns true on the first live unit, which is exactly what the old loop did — the old body pushed into a vector and then scanned it, with no side effect after the push, so the early exit cannot change which unit is found. The guard (`end <= address || byBase.empty()`) and the `upper_bound`/`--it` walk are unchanged, so the search space is identical. Two details that make the equivalence complete rather than approximate: - The `overlapping()` wrapper passes a visitor that always returns `false`, so it still walks the whole range in `byBase` order and builds the list in the same order for callers that need it. Nothing that consumes the vector sees a different ordering. - The span overload (`AnyShadowedOverlaps(std::span<...>)`) delegates to the address overload in a loop and short-circuits on the first true, so it inherits the change without modification — I checked it rather than assumed it, since that was the one place the treatment could have been left half-applied. **The win is the `shared_ptr` copies.** Naming it explicitly because it is the more interesting half: the old path copied a `shared_ptr` per overlapping shadow, and each copy is an atomic increment plus, on destruction, a decrement. Removing the vector removes those atomics per query, not just a small allocation. That is a real cost in a path that runs per guest store. What I could not do: measure it. There is no benchmark here that isolates `AnyShadowedOverlaps`, so what I have is equivalence and no regression, not a number — I would rather say that than quote a timing I did not take. This is a comment, not an approval — I have no merge rights here. Written with AI assistance.
adriadam10 commented 2026-10-09 21:59:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T21:59:49Z.


Checked the diff (same guard and upper_bound walk, early exit at the first live unit as before, overlapping() keeps order and full walk), build + ctest on top of current main (535/535) and green CI on the head. The isolated before/after numbers requested above are in the description; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6089961529 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/856#issuecomment-6089961529). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T21:59:49Z. --- Checked the diff (same guard and `upper_bound` walk, early exit at the first live unit as before, `overlapping()` keeps order and full walk), build + ctest on top of current main (535/535) and green CI on the head. The isolated before/after numbers requested above are in the description; merging.
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!856
No description provided.