perf(shader): keep memoized results in a set-associative table #6733

Merged
meh7an merged 1 commit from upstream-pr/6733 into main 2026-10-19 10:20:46 +00:00
meh7an commented 2026-10-16 08:19:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-16T08:19:43Z.


What

materializePreparedMemoized keeps each source's materialized results by variant and snapshot hash, so a capture that reproduces a snapshot gets the shared result instead of a fresh Populate. The memo is a std::list in LRU order with an unordered_map index (256 entries). Every hit splices the list. Every insert allocates a list node and a hash node, and evicts the oldest, which frees two more.

The memo is now a fixed table of 64 sets of 4 entries per source, allocated with its first insert:

  • an entry's set is its index with the high half folded into the low;
  • a lookup reads the 4 entries of one set;
  • an insert takes the set's least recently used entry by a per-source clock, so no node is allocated or freed;
  • the table holds 256 entries as before, and the counters are the same (an insert over a live entry counts as an eviction).

The eviction policy changes from LRU over all 256 entries to LRU within a set of 4.

On the S2 page of Sonic Origins Plus's main menu, the queue worker spent, per frame (APS5 sampler, 15 s):

  • on the memo itself (materializePreparedMemoized minus the materializeResult it calls on a miss): 1.87 to 1.92 in three runs without this change, 1.33 to 1.60 in four runs with it;
  • on materializeResult, the misses' work: 2.3 to 2.9 in all seven runs, so the sets do not miss noticeably more.

That is about 1.2% of the worker's frame (31 to 33 samples), so the frame times below move within run-to-run noise.

The change is in the recompiler's sources, so the shader cache version changes with it.

Tested

Windows 11 (10.0.26300), WinLibs GCC 15.2.0 (UCRT), Release, NVIDIA GeForce RTX 2080 Ti, driver 617.14.

  • On main 5d9dcc42d with this change, the full ctest suite passes 1298 of 1300 tests. (agc_device_fault was linked again and rerun after a locked file stopped its first link.)
    • agc_driver_gpu_indirect_arguments_misaligned_writer_ahead is not run: main itself does not compile it with MinGW (alignas(65536)).
    • agc_driver_multisample_fill_host_only failed once ("the sentinel fill did not land"). That is main's own race, #6512, which #6522 fixes; with #6522's line added it failed 0 of 200 runs (8 at a time).
  • Sonic Origins Plus, main menu, S2 page (the 3D background), 1233x694 window.
    • Captured with APS5_TRACE_SECONDS=200:15 on the same machine in one session, caches warm. Traces attached.
    • Baseline 40a918442, this change 11882cc99 (the summaries name the commits before them, see the note):
Metric Baseline This PR Change
frame_ms.p50 105.812 103.241 -2.4%
frame_ms.p90 113.658 107.636 -5.3%
mean_frame_ms 107.688 103.748 -3.7%
gpu_busy_ms_per_present 89.495 91.014 +1.7%
Driver.Packet (ms per frame) 105.641 101.713 -3.7%
Driver.Draw (ms per frame) 93.013 89.222 -4.1%

The FPS shown in the title went from 10.36 to 10.48 (median of six readings), and a sampled pair taken about 40 minutes earlier went from 10.50 to 10.82. Last night's windows of this page varied by about 5% at the median, so this pair's gap is mostly noise.

Note: measuring base. These numbers come from my fork's dev, on main 0a56aaaf4. The memo this change replaces is the same on main 5d9dcc42d.

  • The baseline is measure/pipe-2026-10-16 (40a918442).
  • This change is measure/memo-2026-10-16 (11882cc99).
  • Each build was made before its commit, so the summaries name the commit before it, dirty: 390f62471 for the baseline and 40a918442 for this change.

Both carry these commits from my fork:

  • the fixes a71073ce3, c0d875634, e3c68960e, 604f0f373 and 10ff693aa (#5778);
  • make-exe and the stage dumps (522230d62, deee71fde);
  • the crash-report sampler (8d9c3103e..f641e1ec9), which took the profiles;
  • #6037, #6066, #6077, #6067, #6082 (its first version, before #5906), #6095, #6266, #6292, #6312, #6609, #6611 and #6615;
  • smaller steps kept in my fork: e83260247, 97b1ff6c9, 84f5bf0ff, 3b47f7743 (covered on main by #5062), ce567bc3d, a79e58a7c, a0a891697, 63f7a8ed0, b04806167, 719fb71dd, a21a3ce81, e3b8d1c52 (which #6566 covers), 390f62471 (a draw decode cache; main reuses decodes through its draw data hits) and 40a918442 (the pipeline key outside the store's lock, which #5875 covers, and the thread's last pipeline).

The comparison tool marks the window's first present as a configuration difference: the window opens 200 s into each run. Both summaries set trace_dropped, because the worker filled its event buffer at about 2,700 draws per frame. The phase totals above stay complete.

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: NVIDIA GeForce RTX 2080 Ti, driver 617.14, Windows 11
  • AI-assisted: yes

Traces and summary lines (baseline 40a918442, whose summary says 390f624712bf; this change 11882cc99, whose summary says 40a91844263b):
memo-traces.zip

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4885527412 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6733). Originally posted by [meh7an](https://github.com/meh7an) on 2026-10-16T08:19:43Z. --- ### What `materializePreparedMemoized` keeps each source's materialized results by variant and snapshot hash, so a capture that reproduces a snapshot gets the shared result instead of a fresh `Populate`. The memo is a `std::list` in LRU order with an `unordered_map` index (256 entries). Every hit splices the list. Every insert allocates a list node and a hash node, and evicts the oldest, which frees two more. The memo is now a fixed table of 64 sets of 4 entries per source, allocated with its first insert: - an entry's set is its index with the high half folded into the low; - a lookup reads the 4 entries of one set; - an insert takes the set's least recently used entry by a per-source clock, so no node is allocated or freed; - the table holds 256 entries as before, and the counters are the same (an insert over a live entry counts as an eviction). The eviction policy changes from LRU over all 256 entries to LRU within a set of 4. On the S2 page of Sonic Origins Plus's main menu, the queue worker spent, per frame (APS5 sampler, 15 s): - on the memo itself (`materializePreparedMemoized` minus the `materializeResult` it calls on a miss): 1.87 to 1.92 in three runs without this change, 1.33 to 1.60 in four runs with it; - on `materializeResult`, the misses' work: 2.3 to 2.9 in all seven runs, so the sets do not miss noticeably more. That is about 1.2% of the worker's frame (31 to 33 samples), so the frame times below move within run-to-run noise. The change is in the recompiler's sources, so the shader cache version changes with it. ### Tested Windows 11 (10.0.26300), WinLibs GCC 15.2.0 (UCRT), Release, NVIDIA GeForce RTX 2080 Ti, driver 617.14. - On main 5d9dcc42d with this change, the full ctest suite passes 1298 of 1300 tests. (`agc_device_fault` was linked again and rerun after a locked file stopped its first link.) - `agc_driver_gpu_indirect_arguments_misaligned_writer_ahead` is not run: main itself does not compile it with MinGW (`alignas(65536)`). - `agc_driver_multisample_fill_host_only` failed once ("the sentinel fill did not land"). That is main's own race, #6512, which #6522 fixes; with #6522's line added it failed 0 of 200 runs (8 at a time). - Sonic Origins Plus, main menu, S2 page (the 3D background), 1233x694 window. - Captured with `APS5_TRACE_SECONDS=200:15` on the same machine in one session, caches warm. Traces attached. - Baseline 40a918442, this change 11882cc99 (the summaries name the commits before them, see the note): | Metric | Baseline | This PR | Change | | --- | ---: | ---: | ---: | | frame_ms.p50 | 105.812 | 103.241 | -2.4% | | frame_ms.p90 | 113.658 | 107.636 | -5.3% | | mean_frame_ms | 107.688 | 103.748 | -3.7% | | gpu_busy_ms_per_present | 89.495 | 91.014 | +1.7% | | Driver.Packet (ms per frame) | 105.641 | 101.713 | -3.7% | | Driver.Draw (ms per frame) | 93.013 | 89.222 | -4.1% | The FPS shown in the title went from 10.36 to 10.48 (median of six readings), and a sampled pair taken about 40 minutes earlier went from 10.50 to 10.82. Last night's windows of this page varied by about 5% at the median, so this pair's gap is mostly noise. > **Note: measuring base.** These numbers come from my fork's dev, on main 0a56aaaf4. The memo this change replaces is the same on main 5d9dcc42d. > - The baseline is [measure/pipe-2026-10-16](https://github.com/meh7an/AnyPS5/commits/measure/pipe-2026-10-16) (40a918442). > - This change is [measure/memo-2026-10-16](https://github.com/meh7an/AnyPS5/commits/measure/memo-2026-10-16) (11882cc99). > - Each build was made before its commit, so the summaries name the commit before it, `dirty`: 390f62471 for the baseline and 40a918442 for this change. > > Both carry these commits from my fork: > - the fixes a71073ce3, c0d875634, e3c68960e, 604f0f373 and 10ff693aa (#5778); > - make-exe and the stage dumps (522230d62, deee71fde); > - the crash-report sampler (8d9c3103e..f641e1ec9), which took the profiles; > - #6037, #6066, #6077, #6067, #6082 (its first version, before #5906), #6095, #6266, #6292, #6312, #6609, #6611 and #6615; > - smaller steps kept in my fork: e83260247, 97b1ff6c9, 84f5bf0ff, 3b47f7743 (covered on main by #5062), ce567bc3d, a79e58a7c, a0a891697, 63f7a8ed0, b04806167, 719fb71dd, a21a3ce81, e3b8d1c52 (which #6566 covers), 390f62471 (a draw decode cache; main reuses decodes through its draw data hits) and 40a918442 (the pipeline key outside the store's lock, which #5875 covers, and the thread's last pipeline). > > The comparison tool marks the window's first present as a configuration difference: the window opens 200 s into each run. Both summaries set `trace_dropped`, because the worker filled its event buffer at about 2,700 draws per frame. The phase totals above stay complete. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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: NVIDIA GeForce RTX 2080 Ti, driver 617.14, Windows 11 - [x] AI-assisted: yes Traces and summary lines (baseline 40a918442, whose summary says 390f624712bf; this change 11882cc99, whose summary says 40a91844263b): [memo-traces.zip](https://github.com/user-attachments/files/33534276/memo-traces.zip)
github-actions[bot] commented 2026-10-16 08:39:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T08:39:18Z.


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 3247 core/shader/recompiler/Recompiler.cpp
PR 4072 core/shader/recompiler/Recompiler.cpp
PR 5021 core/shader/recompiler/Recompiler.cpp
PR 5500 core/shader/recompiler/Recompiler.cpp
PR 5609 core/shader/recompiler/Recompiler.cpp
PR 5656 core/shader/recompiler/Recompiler.cpp
PR 6177 core/shader/recompiler/Recompiler.cpp
PR 6539 core/shader/recompiler/Recompiler.cpp
PR 6612 core/shader/recompiler/Recompiler.cpp
PR 6762 core/shader/recompiler/Recompiler.cpp
PR 6879 core/shader/recompiler/Recompiler.cpp
PR 6924 core/shader/recompiler/Recompiler.cpp
PR 6941 core/shader/recompiler/Recompiler.cpp
PR 6946 core/shader/recompiler/Recompiler.cpp
PR 6947 core/shader/recompiler/Recompiler.cpp
PR 6977 core/shader/recompiler/Recompiler.cpp
PR 7201 core/shader/recompiler/Recompiler.cpp
PR 7223 core/shader/recompiler/Recompiler.cpp
PR 7332 core/shader/recompiler/Recompiler.cpp
PR 7390 core/shader/recompiler/Recompiler.cpp
PR 7395 core/shader/recompiler/Recompiler.cpp
PR 7484 core/shader/recompiler/Recompiler.cpp
PR 7642 core/shader/recompiler/Recompiler.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6211574476 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6733#issuecomment-6211574476). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-16T08:39:18Z. --- <!-- 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 3247](https://www.github.com/boykopovar/AnyPS5/pull/3247) | `core/shader/recompiler/Recompiler.cpp` | | [PR 4072](https://www.github.com/boykopovar/AnyPS5/pull/4072) | `core/shader/recompiler/Recompiler.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/shader/recompiler/Recompiler.cpp` | | [PR 5500](https://www.github.com/boykopovar/AnyPS5/pull/5500) | `core/shader/recompiler/Recompiler.cpp` | | [PR 5609](https://www.github.com/boykopovar/AnyPS5/pull/5609) | `core/shader/recompiler/Recompiler.cpp` | | [PR 5656](https://www.github.com/boykopovar/AnyPS5/pull/5656) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6177](https://www.github.com/boykopovar/AnyPS5/pull/6177) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6539](https://www.github.com/boykopovar/AnyPS5/pull/6539) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6612](https://www.github.com/boykopovar/AnyPS5/pull/6612) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6762](https://www.github.com/boykopovar/AnyPS5/pull/6762) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6924](https://www.github.com/boykopovar/AnyPS5/pull/6924) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6941](https://www.github.com/boykopovar/AnyPS5/pull/6941) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6946](https://www.github.com/boykopovar/AnyPS5/pull/6946) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6947](https://www.github.com/boykopovar/AnyPS5/pull/6947) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/shader/recompiler/Recompiler.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/shader/recompiler/Recompiler.cpp` | | [PR 7223](https://www.github.com/boykopovar/AnyPS5/pull/7223) | `core/shader/recompiler/Recompiler.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/shader/recompiler/Recompiler.cpp` | | [PR 7390](https://www.github.com/boykopovar/AnyPS5/pull/7390) | `core/shader/recompiler/Recompiler.cpp` | | [PR 7395](https://www.github.com/boykopovar/AnyPS5/pull/7395) | `core/shader/recompiler/Recompiler.cpp` | | [PR 7484](https://www.github.com/boykopovar/AnyPS5/pull/7484) | `core/shader/recompiler/Recompiler.cpp` | | [PR 7642](https://www.github.com/boykopovar/AnyPS5/pull/7642) | `core/shader/recompiler/Recompiler.cpp` |
parl0v commented 2026-10-17 00:05:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-17T00:05:18Z.


Data point from a CPU-bound scene on Windows: RTX 3060 12 GB (NVIDIA 617.42), i5-12400F, Windows 10 19045, MinGW GCC Release. Minecraft Dungeons II (PPSA16064 01.100.100), title screen, no input, 200 s rows, APS5_PROFILE_DRAW=1 APS5_PROFILE_GPU=1; main af0fff762 vs main + this PR, interleaved with five other PRs in one batch, two rounds. FPS is the mean of the last 10 [present] windows, GPU busy and idle gaps per frame over the last 8. This scene is CPU-bound on this machine (44-47 ms of idle gaps per frame).

libs FPS GPU busy idle gaps
main 6.61 / 6.76 105 / 104 ms 46 / 44 ms
+ this PR (92e5b971c) 6.70 / 6.63 (-0.3 %) 104 / 104 ms 45 / 46 ms

Within the run-to-run spread (main itself moved 6.61 -> 6.76 between rounds), no regression, 0 fatal.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6227057391 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6733#issuecomment-6227057391). Originally posted by [parl0v](https://github.com/parl0v) on 2026-10-17T00:05:18Z. --- Data point from a CPU-bound scene on Windows: RTX 3060 12 GB (NVIDIA 617.42), i5-12400F, Windows 10 19045, MinGW GCC Release. Minecraft Dungeons II (PPSA16064 01.100.100), title screen, no input, 200 s rows, `APS5_PROFILE_DRAW=1 APS5_PROFILE_GPU=1`; `main` af0fff762 vs `main` + this PR, interleaved with five other PRs in one batch, two rounds. FPS is the mean of the last 10 `[present]` windows, GPU busy and idle gaps per frame over the last 8. This scene is CPU-bound on this machine (44-47 ms of idle gaps per frame). | libs | FPS | GPU busy | idle gaps | |---|---|---|---| | `main` | 6.61 / 6.76 | 105 / 104 ms | 46 / 44 ms | | + this PR (92e5b971c) | 6.70 / 6.63 (-0.3 %) | 104 / 104 ms | 45 / 46 ms | Within the run-to-run spread (`main` itself moved 6.61 -> 6.76 between rounds), no regression, 0 fatal. AI-assisted: yes (Claude Code)
adriadam10 commented 2026-10-19 10:22:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T10:22:02Z.


Checked the hit/insert paths under source.mutex, victim selection (empty ways first, then least recently used), the unchanged counters and capacity; the shader cache version bump is automatic. Merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6265002417 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6733#issuecomment-6265002417). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T10:22:02Z. --- Checked the hit/insert paths under `source.mutex`, victim selection (empty ways first, then least recently used), the unchanged counters and capacity; the shader cache version bump is automatic. 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!6733
No description provided.