perf: look shader-visible heaps up under a shared lock #278

Open
github-bot wants to merge 1 commit from github-bot/gh-278 into main AGit
Owner

@⁠Skyfrye on GitHub · 2026-11-26 00:13 UTC · original
Skyfrye:nep/shader-heaps-read → main · +106 −13 · 1 files · 1 commits · draft

Draft: the in-game A/B is pending (the change, its tests and the profile it comes from are done; a measured run of the world with and without it is not).

Change

SHADER_HEAPS in crates/victory-gpu/src/descriptor.rs, the list of shader-visible descriptor heaps, was a Mutex. It is read far more often than written:

  • every view the title's recording threads write (create_cbv, create_placed_srv, copy_simple → record → mirror → shader_slot) looks its heap up in it;
  • the submitting thread holds it for the whole of sync_heaps, the pass over records the title copied into placed heaps as bytes, which runs at every submission (about 59 a frame in the world, about 517 a second);
  • only making a heap (create_heap) and dropping one (Drop for Heap) change it.

As a mutex, the recording threads stood behind that pass at every submission. It is now a RwLock (heaps_read for the lookups, the pass, release_dynamic, evictable_slots and evict_slots; heaps_write for making and dropping). Nothing that holds the list takes it again, as before (with the mutex that would have deadlocked).

Where this comes from: a 30 s Nsight Systems capture of the world (release build of main at 3e258c8, launcher fast set, 263 frames, 8.8 fps under the profiler). Time on the CPU taken from the 1 kHz timer samples; waits from the context switches (each switch-in's stack weighed by the time since the thread was switched out to wait). The five recording threads each spend 27-30 % of the time on the CPU, 12-13 % in contended locks and 45-50 % waiting in title code. For one of them (tid 18376), the contended-lock waits by caller, share of wall time:

Lock wait Share
descriptor::mirror ← record ← create_cbv 4.1 %
descriptor::mirror ← record ← create_placed_srv 2.1 %
descriptor::mirror ← copy_simple 0.5 %
HostHeap::mirror (its own made lock, not changed here) 2.3 %
a lock inside draw::State::bind (not changed here) 1.9 %

The three descriptor::mirror rows are the SHADER_HEAPS lookup (shader_slot is inlined there): 6.7 % of each recording thread's time.

Linked issue: none.

Verification

Windows 11 26300, Rust 1.98.1 (MSVC), on upstream main at 578098b, from third_party/victory:

cargo build --locked --release -p victory                 -> Finished, no new warning
cargo test --locked -p victory-gpu --lib -- descriptor::tests -> 10 passed
cargo test --locked -p victory-gpu --lib                  -> 185 passed, 3 failed, 3 ignored

The 3 failures are the same on main without this change (183 passed, 3 failed): descriptor::host::tests::spans_say_the_next_start_and_the_lowest_level_viewed, indirect::tests::warp_card_fetch_copies_flagged_pieces_to_their_slots, indirect::tests::warp_card_patch_writes_the_masked_bytes_and_no_others.

New tests:

  • a_heap_lookup_goes_on_during_the_pass_over_copied_records: holds the list as sync_heaps does and looks a heap up from another thread. Revert check: with the list back to a Mutex (both helpers returning its guard) it fails: "the lookup waited for the pass over the heap list (5 tries)". It tries again only when the lookup could have waited behind a heap another test made at that moment, which a mutex makes wait in every try.
  • a_heap_made_after_the_pass_is_found: making a heap waits for the pass, then the next lookup finds it.

Pending: an A/B in the world (frame time, and the recording threads' lock waits in a profile).

Oracle and test data

No game file. The tests make descriptor heaps in process memory. The profile numbers come from my own capture of my copy of the game; no game code, address or dump is in this PR.

Checklist

  • The PR covers a single reviewable behaviour (the heap list is shared by its readers).
  • A test fails without this change and passes with it.
  • The commands and results of each check are given above (the in-game A/B is pending, said above).
  • An unimplemented function returns an error (E_NOTIMPL), never a fake success. (Nothing unimplemented here.)
  • README: not touched.
  • No game, console or SDK file, no key, no dump, no download link.
  • Findings drawn from a title: thread and engine function names from a profile of the engine; no title code.
  • Code taken from another project: none.

AI assistance: written with Claude Code. Claude Opus 5.5 read the Nsight Systems capture (offline symbol resolution, on-CPU and wait accounting), wrote the change and its tests, and wrote this description.

🤖 Generated with Claude Code

**@⁠Skyfrye** on GitHub · 2026-11-26 00:13 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/278) `Skyfrye:nep/shader-heaps-read` → `main` · +106 −13 · 1 files · 1 commits · draft **Draft: the in-game A/B is pending** (the change, its tests and the profile it comes from are done; a measured run of the world with and without it is not). ## Change `SHADER_HEAPS` in `crates/victory-gpu/src/descriptor.rs`, the list of shader-visible descriptor heaps, was a `Mutex`. It is read far more often than written: - every view the title's recording threads write (`create_cbv`, `create_placed_srv`, `copy_simple` → `record` → `mirror` → `shader_slot`) looks its heap up in it; - the submitting thread holds it for the whole of `sync_heaps`, the pass over records the title copied into placed heaps as bytes, which runs at every submission (about 59 a frame in the world, about 517 a second); - only making a heap (`create_heap`) and dropping one (`Drop for Heap`) change it. As a mutex, the recording threads stood behind that pass at every submission. It is now a `RwLock` (`heaps_read` for the lookups, the pass, `release_dynamic`, `evictable_slots` and `evict_slots`; `heaps_write` for making and dropping). Nothing that holds the list takes it again, as before (with the mutex that would have deadlocked). Where this comes from: a 30 s Nsight Systems capture of the world (release build of `main` at `3e258c8`, launcher fast set, 263 frames, 8.8 fps under the profiler). Time on the CPU taken from the 1 kHz timer samples; waits from the context switches (each switch-in's stack weighed by the time since the thread was switched out to wait). The five recording threads each spend 27-30 % of the time on the CPU, 12-13 % in contended locks and 45-50 % waiting in title code. For one of them (tid 18376), the contended-lock waits by caller, share of wall time: | Lock wait | Share | |---|---| | `descriptor::mirror` ← `record` ← `create_cbv` | 4.1 % | | `descriptor::mirror` ← `record` ← `create_placed_srv` | 2.1 % | | `descriptor::mirror` ← `copy_simple` | 0.5 % | | `HostHeap::mirror` (its own `made` lock, not changed here) | 2.3 % | | a lock inside `draw::State::bind` (not changed here) | 1.9 % | The three `descriptor::mirror` rows are the `SHADER_HEAPS` lookup (`shader_slot` is inlined there): 6.7 % of each recording thread's time. Linked issue: none. ## Verification Windows 11 26300, Rust 1.98.1 (MSVC), on upstream `main` at `578098b`, from `third_party/victory`: ``` cargo build --locked --release -p victory -> Finished, no new warning cargo test --locked -p victory-gpu --lib -- descriptor::tests -> 10 passed cargo test --locked -p victory-gpu --lib -> 185 passed, 3 failed, 3 ignored ``` The 3 failures are the same on `main` without this change (183 passed, 3 failed): `descriptor::host::tests::spans_say_the_next_start_and_the_lowest_level_viewed`, `indirect::tests::warp_card_fetch_copies_flagged_pieces_to_their_slots`, `indirect::tests::warp_card_patch_writes_the_masked_bytes_and_no_others`. New tests: - `a_heap_lookup_goes_on_during_the_pass_over_copied_records`: holds the list as `sync_heaps` does and looks a heap up from another thread. Revert check: with the list back to a `Mutex` (both helpers returning its guard) it fails: "the lookup waited for the pass over the heap list (5 tries)". It tries again only when the lookup could have waited behind a heap another test made at that moment, which a mutex makes wait in every try. - `a_heap_made_after_the_pass_is_found`: making a heap waits for the pass, then the next lookup finds it. Pending: an A/B in the world (frame time, and the recording threads' lock waits in a profile). ## Oracle and test data No game file. The tests make descriptor heaps in process memory. The profile numbers come from my own capture of my copy of the game; no game code, address or dump is in this PR. ## Checklist - [x] The PR covers a single reviewable behaviour (the heap list is shared by its readers). - [x] A test fails without this change and passes with it. - [x] The commands and results of each check are given above (the in-game A/B is pending, said above). - [x] An unimplemented function returns an error (`E_NOTIMPL`), never a fake success. (Nothing unimplemented here.) - [x] README: not touched. - [x] No game, console or SDK file, no key, no dump, no download link. - [x] Findings drawn from a title: thread and engine function names from a profile of the engine; no title code. - [x] Code taken from another project: none. AI assistance: written with Claude Code. Claude Opus 5.5 read the Nsight Systems capture (offline symbol resolution, on-CPU and wait accounting), wrote the change and its tests, and wrote this description. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The list of shader-visible descriptor heaps was a mutex. Every view the
title's recording threads write looks its heap up in it, and the
submitting thread held it for the whole pass over records copied into
placed heaps (`sync_heaps`), once per submission (about 59 a frame in the
world). The recording threads stood behind that pass: in a 30 s Nsight
Systems profile of the world, each of the five stood 12-13 % of the time
in contended locks, 6.7 % of it in this lookup.

The list is now a read/write lock: lookups and the pass share it, only
making and dropping a heap takes it alone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:41:47 +00:00
Author
Owner

@⁠KainanP on GitHub · 2026-11-26 10:32 UTC · original

Test report for #278 on three adapters.

Merges cleanly onto current main. Setup: current origin/main (c4c93bd) merged with this PR, Windows 11, cargo test --locked in third_party/victory. Adapters: RTX 4060 Laptop (DXGI 0), Intel UHD 8086:a78b (16-lane waves, no FP64), WARP. The hardware adapter is picked by name through a local, uncommitted test hook and confirmed in its log; WARP for victory-gpu is --features diagnostics + VICTORY_D3D12_WARP=1. victory-gpu runs with --test-threads=1.

victory-gpu --lib main main + #278
RTX 4060 Laptop 183 passed, main's 3 failed 185 passed, main's 3 failed
Intel UHD 183 passed, main's 3 failed 185 passed, main's 3 failed
WARP 196 passed, main's 3 failed 198 passed, main's 3 failed

"main's 3" = descriptor::host::tests::spans_say_the_next_start_and_the_lowest_level_viewed, indirect::tests::warp_card_fetch_copies_flagged_pieces_to_their_slots, indirect::tests::warp_card_patch_writes_the_masked_bytes_and_no_others. They fail on all three adapters on main too. Both new descriptor::tests pass everywhere. No new failure. These are the same counts you give.

Note from reading the diff: Rust's RwLock on Windows lets a waiting writer hold back new readers. So a read() taken again on a thread that already holds one, while create_heap waits, would deadlock. Before, the Mutex would have deadlocked at once, so this is not new. But it now only shows when a heap is created at that moment. A doc line on heaps_read saying "never nest" would keep it that way.

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this.

**@⁠KainanP** on GitHub · 2026-11-26 10:32 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/278#issuecomment-7189499665) Test report for #278 on three adapters. Merges cleanly onto current main. Setup: current `origin/main` (c4c93bd) merged with this PR, Windows 11, `cargo test --locked` in `third_party/victory`. Adapters: RTX 4060 Laptop (DXGI 0), Intel UHD 8086:a78b (16-lane waves, no FP64), WARP. The hardware adapter is picked by name through a local, uncommitted test hook and confirmed in its log; WARP for victory-gpu is `--features diagnostics` + `VICTORY_D3D12_WARP=1`. victory-gpu runs with `--test-threads=1`. | `victory-gpu --lib` | main | main + #278 | |---|---|---| | RTX 4060 Laptop | 183 passed, main's 3 failed | 185 passed, main's 3 failed | | Intel UHD | 183 passed, main's 3 failed | 185 passed, main's 3 failed | | WARP | 196 passed, main's 3 failed | 198 passed, main's 3 failed | "main's 3" = `descriptor::host::tests::spans_say_the_next_start_and_the_lowest_level_viewed`, `indirect::tests::warp_card_fetch_copies_flagged_pieces_to_their_slots`, `indirect::tests::warp_card_patch_writes_the_masked_bytes_and_no_others`. They fail on all three adapters on main too. Both new `descriptor::tests` pass everywhere. No new failure. These are the same counts you give. Note from reading the diff: Rust's `RwLock` on Windows lets a waiting writer hold back new readers. So a `read()` taken again on a thread that already holds one, while `create_heap` waits, would deadlock. Before, the `Mutex` would have deadlocked at once, so this is not new. But it now only shows when a heap is created at that moment. A doc line on `heaps_read` saying "never nest" would keep it that way. _AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this._
This pull request has changes conflicting with the target branch.
  • third_party/victory/crates/victory-gpu/src/descriptor.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/278/head:github-bot/gh-278'
git switch 'github-bot/gh-278'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-278'
git switch 'github-bot/gh-278'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-278'
git switch 'github-bot/gh-278'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-278'
git switch 'main'
git merge --squash 'github-bot/gh-278'
git switch 'main'
git merge --ff-only 'github-bot/gh-278'
git switch 'main'
git merge 'github-bot/gh-278'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.