GPU: draws find the views their tables hold without the view registry's lock #3275

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

@⁠KainanP on GitHub · 2026-11-26 15:26 UTC · original
KainanP:perf/view-lookups-unlocked → main · +133 −6 · 1 files · 1 commits · open

Part of #2586 (the cost of list methods on the title's threads). Local change, same behaviour. Second of two lock fixes for draws recorded on several threads at once. The first is #3274, which removes the most contended lock. This one is smaller and pays off mostly together with #2821.

The problem

Every slot a draw binds looks up its view in the view registry descriptor::VIEWS: HostHeap::mirror → view_at for the slots a table names, and descriptor::view on the table paths. Each lookup takes the registry's lock. On main that's a mutex. With #2821 it becomes an RwLock read, but readers still write the lock word's shared count, so the five recording threads bounce that cache line between cores. With main + #1978 + #2821 + #3258, a per-call-site lock counter put it second, after TEXTURES_AT (#3274): six takes per draw, 157 ns on one thread and 439 ns each on five (ranking table in #3274).

The change

A registry entry keeps its view for as long as its generation is current. An entry gets another view only after forget has freed it under a new generation. So:

  • each entry's live generation (0 while it is free) is kept in chunks of 4,096 AtomicU32. The chunks are made as the list grows and never freed. They are written only under the registry's exclusive lock: when an entry gets a view in record, and when forget frees it;
  • each thread keeps the views it looked up: 1,024 slots of (entry, generation, view), indexed by entry;
  • a lookup whose entry is still live at the asked generation and in the thread's slots takes the thread's copy without the lock. Every other lookup goes through the registry as before. A view found under the lock is kept only if its entry is live at that generation, under the same lock. A freed entry still holds its last view under the generation the next view will get, so it is never kept.

Same answers as the locked lookup. A view released right after the check is the same race as a view released right after the locked lookup returns. Entries past 4,096 × 1,024 are never kept, so they are always looked up under the lock.

Tests

New: descriptor::tests::threads_that_looked_views_up_see_them_rewritten_and_released. A second thread looks views up (and keeps them) while the main thread:

  • rewrites a slot: the second thread must see the new view;
  • releases a resource: its view must be gone;
  • gives the freed entry to another view: the old record still finds nothing, and the new one finds the new view.

I checked that the test catches the bug it's meant for: without the set_live(i, 0) in forget, it fails at "released".

RTX 4060 Laptop Intel UHD (a78b, local adapter-selection hook) WARP (--features diagnostics, VICTORY_D3D12_WARP=1)
main 63bcca4 228–229 pass, 2 fail (+1 timing flake) 228 pass, 3 fail 246 pass, 0 fail
this PR 230 pass, same 2 fail 230 pass, same 3 fail (with #3274) 248 pass, 0 fail (with #3274)

The failures are main's (see #3274). Clippy passes with -D warnings for the workspace, all targets, with and without --all-features. repository_passes_policy passes, and rustfmt is clean on the changed lines.

Measured

Same method as #3274: pipeline::list_bench's draw sequence on 1 and 5 threads, 7 runs per build interleaved round-robin, medians (ranges). RTX 4060 Laptop, i7-14650HX, release. Seven single-threaded CPU-bound processes I don't control ran throughout, which interleaving spreads evenly over the builds. ns per draw, per thread:

tree 1 thread 5 threads
main 3144 (3015–3322) 12119 (10965–14624)
main + this PR 3102 (2995–3325) 12020 (11476–13522)
main + #1978 + #2821 + #3258 1559 (1419–1893) 5100 (4188–5889)
the same + this PR 1524 (1459–1561) 4650 (3264–5077)
the same + #3274 1430 (1356–1569) 2895 (2692–3066)
the same + #3274 + this PR 1426 (1388–1578) 2824 (2755–2997)

Two more interleaved rounds gave, on five threads: with #3274 2893 → 2609 (5 runs) and 2803 → 2691 (7 runs); without it 4818 → 3784 (7 runs).

On main alone it changes nothing measurable: the other shared locks (TEXTURES_AT, main's mutexes) dominate. Together with #2821 it saves about 0.5–1 µs per draw on five threads, and on top of #2821 + #3274 about 0.1–0.3 µs (−2 to −10%).

Conflicts

The text conflicts with #2821, #2099, #3234 and #2848 (lines next to view_at/view, or the registry's lock calls). With #2821: take this PR's bodies of view_at and view, and in view_of change VIEWS.lock() to VIEWS.read(). Without that change the merge doesn't compile, so nothing breaks silently. That is the measured tree.

What this does not show

  • No in-game numbers: I didn't run this in a game. Please A/B it in a title that records on several threads, together with #2821 and #3274.
  • Memory: up to about 140 KB per thread that looks views up (1,024 slots), plus 16 KB per 4,096 registry entries.
  • What's left on five threads after both fixes, by the same counter: each texture's own host mutex plus its Arc count (six per draw), and shadow::BUFFERS (four per draw). Removing the mutex alone saved about 80 ns, so most of the remaining cost is the shared Arc counts. That one belongs with the host-copy lookup in #3258.

AI use: Claude Code (Claude Opus 5.5) profiled the locks, wrote the change and the test, and ran the benchmarks and test suites listed above; I reviewed the results.

🤖 Generated with Claude Code

**@⁠KainanP** on GitHub · 2026-11-26 15:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/3275) `KainanP:perf/view-lookups-unlocked` → `main` · +133 −6 · 1 files · 1 commits · open Part of #2586 (the cost of list methods on the title's threads). Local change, same behaviour. Second of two lock fixes for draws recorded on several threads at once. The first is #3274, which removes the most contended lock. This one is smaller and pays off mostly together with #2821. ## The problem Every slot a draw binds looks up its view in the view registry `descriptor::VIEWS`: `HostHeap::mirror` → `view_at` for the slots a table names, and `descriptor::view` on the table paths. Each lookup takes the registry's lock. On main that's a mutex. With #2821 it becomes an `RwLock` read, but readers still write the lock word's shared count, so the five recording threads bounce that cache line between cores. With main + #1978 + #2821 + #3258, a per-call-site lock counter put it second, after `TEXTURES_AT` (#3274): six takes per draw, 157 ns on one thread and 439 ns each on five (ranking table in #3274). ## The change A registry entry keeps its view for as long as its generation is current. An entry gets another view only after `forget` has freed it under a new generation. So: - each entry's live generation (0 while it is free) is kept in chunks of 4,096 `AtomicU32`. The chunks are made as the list grows and never freed. They are written only under the registry's exclusive lock: when an entry gets a view in `record`, and when `forget` frees it; - each thread keeps the views it looked up: 1,024 slots of (entry, generation, view), indexed by entry; - a lookup whose entry is still live at the asked generation and in the thread's slots takes the thread's copy without the lock. Every other lookup goes through the registry as before. A view found under the lock is kept only if its entry is live at that generation, under the same lock. A freed entry still holds its last view under the generation the next view will get, so it is never kept. Same answers as the locked lookup. A view released right after the check is the same race as a view released right after the locked lookup returns. Entries past 4,096 × 1,024 are never kept, so they are always looked up under the lock. ## Tests New: `descriptor::tests::threads_that_looked_views_up_see_them_rewritten_and_released`. A second thread looks views up (and keeps them) while the main thread: - rewrites a slot: the second thread must see the new view; - releases a resource: its view must be gone; - gives the freed entry to another view: the old record still finds nothing, and the new one finds the new view. I checked that the test catches the bug it's meant for: without the `set_live(i, 0)` in `forget`, it fails at "released". | | RTX 4060 Laptop | Intel UHD (a78b, local adapter-selection hook) | WARP (`--features diagnostics`, `VICTORY_D3D12_WARP=1`) | |---|---|---|---| | main 63bcca4 | 228–229 pass, 2 fail (+1 timing flake) | 228 pass, 3 fail | 246 pass, 0 fail | | this PR | 230 pass, same 2 fail | 230 pass, same 3 fail (with #3274) | 248 pass, 0 fail (with #3274) | The failures are main's (see #3274). Clippy passes with `-D warnings` for the workspace, all targets, with and without `--all-features`. `repository_passes_policy` passes, and rustfmt is clean on the changed lines. ## Measured Same method as #3274: `pipeline::list_bench`'s draw sequence on 1 and 5 threads, 7 runs per build interleaved round-robin, medians (ranges). RTX 4060 Laptop, i7-14650HX, release. Seven single-threaded CPU-bound processes I don't control ran throughout, which interleaving spreads evenly over the builds. ns per draw, per thread: | tree | 1 thread | 5 threads | |---|---:|---:| | main | 3144 (3015–3322) | 12119 (10965–14624) | | main + this PR | 3102 (2995–3325) | 12020 (11476–13522) | | main + #1978 + #2821 + #3258 | 1559 (1419–1893) | 5100 (4188–5889) | | the same + this PR | 1524 (1459–1561) | **4650** (3264–5077) | | the same + #3274 | 1430 (1356–1569) | 2895 (2692–3066) | | the same + #3274 + this PR | 1426 (1388–1578) | **2824** (2755–2997) | Two more interleaved rounds gave, on five threads: with #3274 2893 → 2609 (5 runs) and 2803 → 2691 (7 runs); without it 4818 → 3784 (7 runs). On main alone it changes nothing measurable: the other shared locks (`TEXTURES_AT`, main's mutexes) dominate. Together with #2821 it saves about 0.5–1 µs per draw on five threads, and on top of #2821 + #3274 about 0.1–0.3 µs (−2 to −10%). ## Conflicts The text conflicts with #2821, #2099, #3234 and #2848 (lines next to `view_at`/`view`, or the registry's lock calls). With #2821: take this PR's bodies of `view_at` and `view`, and in `view_of` change `VIEWS.lock()` to `VIEWS.read()`. Without that change the merge doesn't compile, so nothing breaks silently. That is the measured tree. ## What this does not show - No in-game numbers: I didn't run this in a game. Please A/B it in a title that records on several threads, together with #2821 and #3274. - Memory: up to about 140 KB per thread that looks views up (1,024 slots), plus 16 KB per 4,096 registry entries. - What's left on five threads after both fixes, by the same counter: each texture's own `host` mutex plus its `Arc` count (six per draw), and `shadow::BUFFERS` (four per draw). Removing the mutex alone saved about 80 ns, so most of the remaining cost is the shared `Arc` counts. That one belongs with the host-copy lookup in #3258. _AI use: Claude Code (Claude Opus 5.5) profiled the locks, wrote the change and the test, and ran the benchmarks and test suites listed above; I reviewed the results._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Every slot a draw binds looks its view up in VIEWS (`view_at` from
`HostHeap::mirror`, `descriptor::view` from the table paths), each under
the registry's lock, which title threads recording lists at once all
wrote (as a mutex here; as a shared count once reads share it, #2821).

The registry entry a slot names keeps its view while its generation is
current: an entry gets another view only after `forget` freed it under a
new generation. So each entry's live generation (0 when free) is now kept
in chunks of atomics, written under the registry's exclusive lock, and
each thread keeps the views it looked up (entry, generation, view; 1024
slots by entry). A lookup whose entry is still live at that generation
takes the thread's copy without the lock; others look it up as before.

Test: a thread that looked views up finds a slot's new view once it is
rewritten, and none for a released resource's view, also when its entry
was given to another view since.

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

@⁠oni-swr on GitHub · 2026-11-26 15:44 UTC · original

WARP verification report (test report only, no code changes)

  • PR head: b90ab5f9
  • main: 63bcca43 (control); WARP software adapter forced with VICTORY_WARP=1 VICTORY_D3D12_WARP=1, --features diagnostics, d3d10warp.dll 10.0.26100.9818; dxil.dll 1.8.2502.11 (Windows Kit 10.0.26100.0, via VICTORY_DXIL).
  • Each test run in its own process (--exact), -j 4, synthetic tests only, no game data.
  • New test descriptor::tests::threads_that_looked_views_up_see_them_rewritten_and_released: passes on the PR (1 passed).
  • Control on main: the test only uses existing APIs, so I ran it on main with only the test added. It also passes on main. It guards the behaviour kept while the lookup moves off the views lock (rewritten and released views seen from another thread), not a bug on main, so it does not distinguish the two.
  • victory-gpu lib suite: PR 247 passed, 0 failed, 1 ignored; main 246 passed, 0 failed, 1 ignored. No failures on either.
  • Not tested: physical GPU. Not measured: the contention the change removes.

AI use: Claude Code (Claude Sonnet 5.5)

**@⁠oni-swr** on GitHub · 2026-11-26 15:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/3275#issuecomment-7196690945) WARP verification report (test report only, no code changes) - PR head: `b90ab5f9` - main: `63bcca43` (control); WARP software adapter forced with `VICTORY_WARP=1 VICTORY_D3D12_WARP=1`, `--features diagnostics`, d3d10warp.dll 10.0.26100.9818; dxil.dll 1.8.2502.11 (Windows Kit 10.0.26100.0, via `VICTORY_DXIL`). - Each test run in its own process (`--exact`), `-j 4`, synthetic tests only, no game data. - New test `descriptor::tests::threads_that_looked_views_up_see_them_rewritten_and_released`: **passes on the PR** (1 passed). - Control on main: the test only uses existing APIs, so I ran it on main with only the test added. It **also passes on main**. It guards the behaviour kept while the lookup moves off the views lock (rewritten and released views seen from another thread), not a bug on main, so it does not distinguish the two. - `victory-gpu` lib suite: PR 247 passed, 0 failed, 1 ignored; main 246 passed, 0 failed, 1 ignored. No failures on either. - Not tested: physical GPU. Not measured: the contention the change removes. AI use: Claude Code (Claude Sonnet 5.5)
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 15:50 UTC · original · approved these changes

Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):

  • Caching (entry, generation, View) in thread-local SEEN validated against the lock-free Acquire/Release generation table LIVE (updated under VIEWS in record and cleared to 0 in forget) cleanly avoids VIEWS mutex contention on hot draw paths while preserving invalidation on release/reuse.
  • cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released (RTX 5090): 22 passed, 0 failed (including threads_that_looked_views_up_see_them_rewritten_and_released).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 15:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/3275#pullrequestreview-6077043028) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090): - Caching `(entry, generation, View)` in thread-local `SEEN` validated against the lock-free `Acquire`/`Release` generation table `LIVE` (updated under `VIEWS` in `record` and cleared to `0` in `forget`) cleanly avoids `VIEWS` mutex contention on hot draw paths while preserving invalidation on release/reuse. - `cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released` (RTX 5090): 22 passed, 0 failed (including `threads_that_looked_views_up_see_them_rewritten_and_released`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3275'
git switch 'github-bot/gh-3275'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3275'
git switch 'github-bot/gh-3275'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3275'
git switch 'main'
git merge --squash 'github-bot/gh-3275'
git switch 'main'
git merge --ff-only 'github-bot/gh-3275'
git switch 'main'
git merge 'github-bot/gh-3275'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3275
No description provided.