graphics: pool the shader SRT evaluator memo storage #578

Closed
github-bot wants to merge 1 commit from gh-pr/578/head into gh-pr/578/base
Owner

@⁠MehmetCambaz on GitHub · 2026-09-11 23:35 UTC · original
MehmetCambaz:perf/srt-evaluator-memo-pool → main · +812 −10 · 4 files · 1 commits · closed without merging

Problem

Each SRT evaluator builds a private std::unordered_map memo for one evaluation and destroys it afterwards. In resource-heavy workloads this creates a large amount of allocator traffic.

In a fixed Kena: Bridge of Spirits scene, evaluator memo storage was reaching the system allocator about 87,800 times per frame.

Change

Use a per-thread std::pmr::unsynchronized_pool_resource for evaluator memo storage, while keeping one private std::pmr::unordered_map per Evaluator.

  • Memo contents are not shared between evaluators.
  • Main, clean and ReadFirstLane child evaluators still have independent maps.
  • Evaluation order and semantics are unchanged.
  • Outputs such as snapshots, flattened SRT data and specialization state are not allocated from the pool.
  • No evaluation-result caching is added.
  • The pool retains its high-water mark until the evaluating thread exits. In the measured Kena workload this was about 0.3 MiB. If evaluation becomes multi-threaded in the future, retention would be per evaluating thread.

Evaluator is also made non-copyable so its polymorphic allocator cannot accidentally rebind to the default resource through a copy.

Performance

Windows, fixed Kena scenes, alternating legacy/pooled blocks in the same process:

Metric Legacy Pooled
GetGraphicsPrograms 10.33 ms/frame 8.89 ms/frame (-14.0%)
GetGraphicsPrograms, second scene 10.43 ms/frame 9.05 ms/frame (-13.3%)
Evaluator 8.76 ms/frame 7.32 ms/frame (-1.44 ms)
Mean frame time — roughly 2–3% better depending on scene
Evaluator memo allocations reaching the system allocator ~87.8k/frame 0 after warm-up

All pooled blocks improved mean frame time relative to their neighbouring legacy blocks.

These numbers are from one title on Windows; equivalent gains are not claimed for other games or platforms.

Validation

Added srt_evaluator_memo_tests, which runs the evaluator with pooled and unpooled memo storage and compares return status, outputs and reader-call order.

The tests cover, among other cases:

  • separate main and clean evaluator contexts
  • parent and ReadFirstLane child isolation
  • nested ReadFirstLane
  • evaluation re-entered from a reader callback
  • two threads evaluating the same plan with independent readers
  • thread exit and recreation
  • allocation failure at every allocation point
  • successful dependency memoization across enclosing failures
  • preservation of 64-bit memo values and previously returned output

The focused suite passes 1,131 checks.

Full CTest result:

  • current upstream: 36/37
  • this change: 37/38

The only failure in both is the existing kernel_file_system Windows link failure.

A Kena gameplay smoke test with the production patch also completed without visual corruption or a crash.

Portability

The implementation uses standard C++ PMR facilities:

  • std::pmr::unordered_map
  • std::pmr::unsynchronized_pool_resource
  • std::pmr::new_delete_resource

It was built and tested with clang-cl and the MSVC standard library on Windows.

The project's current Linux/libstdc++ and macOS/libc++ toolchains support these facilities, but I did not build or benchmark this change on those platforms. Performance validation is Windows-only.

Frame-pacing note

During the Kena A/B runs, pooled blocks also showed a small repeatable increase in frames landing near the ~100 ms vsync band: about +2.0 percentage points in one heavier scene and +0.4 points in a lighter scene.

This did not correlate with allocator activity or evaluator cost: warm pooled frames made no evaluator-memo system allocations, long frames did not show additional pool requests or an evaluator-time spike, while mean frame time and GetGraphicsPrograms consistently improved and P99 / >110 ms behaviour did not materially worsen.

I could not identify a causal mechanism in the pool for this observation.

AI assistance was used during investigation, implementation and testing. I reviewed the resulting change and validation before submitting it.

**@⁠MehmetCambaz** on GitHub · 2026-09-11 23:35 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/578) `MehmetCambaz:perf/srt-evaluator-memo-pool` → `main` · +812 −10 · 4 files · 1 commits · closed without merging ### Problem Each SRT evaluator builds a private `std::unordered_map` memo for one evaluation and destroys it afterwards. In resource-heavy workloads this creates a large amount of allocator traffic. In a fixed Kena: Bridge of Spirits scene, evaluator memo storage was reaching the system allocator about 87,800 times per frame. ### Change Use a per-thread `std::pmr::unsynchronized_pool_resource` for evaluator memo storage, while keeping one private `std::pmr::unordered_map` per `Evaluator`. - Memo contents are not shared between evaluators. - Main, clean and `ReadFirstLane` child evaluators still have independent maps. - Evaluation order and semantics are unchanged. - Outputs such as snapshots, flattened SRT data and specialization state are not allocated from the pool. - No evaluation-result caching is added. - The pool retains its high-water mark until the evaluating thread exits. In the measured Kena workload this was about 0.3 MiB. If evaluation becomes multi-threaded in the future, retention would be per evaluating thread. `Evaluator` is also made non-copyable so its polymorphic allocator cannot accidentally rebind to the default resource through a copy. ### Performance Windows, fixed Kena scenes, alternating legacy/pooled blocks in the same process: | Metric | Legacy | Pooled | |---|---:|---:| | `GetGraphicsPrograms` | 10.33 ms/frame | 8.89 ms/frame (-14.0%) | | `GetGraphicsPrograms`, second scene | 10.43 ms/frame | 9.05 ms/frame (-13.3%) | | Evaluator | 8.76 ms/frame | 7.32 ms/frame (-1.44 ms) | | Mean frame time | — | roughly 2–3% better depending on scene | | Evaluator memo allocations reaching the system allocator | ~87.8k/frame | 0 after warm-up | All pooled blocks improved mean frame time relative to their neighbouring legacy blocks. These numbers are from one title on Windows; equivalent gains are not claimed for other games or platforms. ### Validation Added `srt_evaluator_memo_tests`, which runs the evaluator with pooled and unpooled memo storage and compares return status, outputs and reader-call order. The tests cover, among other cases: - separate main and clean evaluator contexts - parent and `ReadFirstLane` child isolation - nested `ReadFirstLane` - evaluation re-entered from a reader callback - two threads evaluating the same plan with independent readers - thread exit and recreation - allocation failure at every allocation point - successful dependency memoization across enclosing failures - preservation of 64-bit memo values and previously returned output The focused suite passes 1,131 checks. Full CTest result: - current upstream: 36/37 - this change: 37/38 The only failure in both is the existing `kernel_file_system` Windows link failure. A Kena gameplay smoke test with the production patch also completed without visual corruption or a crash. ### Portability The implementation uses standard C++ PMR facilities: - `std::pmr::unordered_map` - `std::pmr::unsynchronized_pool_resource` - `std::pmr::new_delete_resource` It was built and tested with clang-cl and the MSVC standard library on Windows. The project's current Linux/libstdc++ and macOS/libc++ toolchains support these facilities, but I did not build or benchmark this change on those platforms. Performance validation is Windows-only. ### Frame-pacing note During the Kena A/B runs, pooled blocks also showed a small repeatable increase in frames landing near the ~100 ms vsync band: about +2.0 percentage points in one heavier scene and +0.4 points in a lighter scene. This did not correlate with allocator activity or evaluator cost: warm pooled frames made no evaluator-memo system allocations, long frames did not show additional pool requests or an evaluator-time spike, while mean frame time and `GetGraphicsPrograms` consistently improved and P99 / >110 ms behaviour did not materially worsen. I could not identify a causal mechanism in the pool for this observation. AI assistance was used during investigation, implementation and testing. I reviewed the resulting change and validation before submitting it.
Every Evaluator builds a private memo map for a single evaluation and throws it away, so a
warm GPU thread spent a large part of GetGraphicsPrograms inside the system allocator: about
87,700 allocator calls per frame in a fixed Kena scene, one per memoized instruction plus the
map construction and bucket rehash.

Take that storage from one std::pmr::unsynchronized_pool_resource per thread instead. Each
Evaluator still owns a private map, and evaluation order, memo lookup order, cycle detection,
success-only insertion and failure behaviour are untouched; only where the nodes and bucket
arrays come from changes. The pool keeps its high-water mark until the thread exits (about
0.3 MiB in that scene); one GPU thread evaluates today, so that is a single high-water mark,
and retention would become per evaluating thread if evaluation is ever parallelized. Its
upstream is the static new/delete resource, so thread teardown only returns memory to
::operator delete. Evaluator becomes non-copyable because a copied polymorphic allocator
would silently fall back to the default resource.

Measured in one process in a fixed Kena scene, alternating blocks: GetGraphicsPrograms
10.33 -> 8.89 ms per frame, the evaluator 8.76 -> 7.32 ms per frame, mean frame time about
-3 percent, and evaluator memo allocations reaching the system allocator fall to zero after
warm-up. Numbers apply to that workload only.

Adds srt_evaluator_memo_tests, which runs every case with pooled and with unpooled storage
and compares return status, all outputs and the reader-call order, covering the separate
main/clean and ReadFirstLane contexts, nested and re-entrant evaluation, two threads, thread
exit, allocation failure and dependency survival after an enclosing failure.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:26:10 +00:00
github-bot closed this pull request 2026-11-26 09:26:10 +00:00
Author
Owner

@⁠MehmetCambaz on GitHub · 2026-09-16 00:14 UTC · original

Superseded by #628, which includes this optimization and builds the dense memo work on top of it. Closing this one to keep the review in one place.

**@⁠MehmetCambaz** on GitHub · 2026-09-16 00:14 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/578#issuecomment-5689976909) Superseded by #628, which includes this optimization and builds the dense memo work on top of it. Closing this one to keep the review in one place.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.