GPU: use RwLock for read-mostly tables and add per-HostList VA/view caches #1356

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

@⁠wbkepler on GitHub · 2026-11-24 17:35 UTC · original
wbkepler:gpu/concurrent-read-locks-and-va-cache → main · +708 −75 · 8 files · 1 commits · open

Summary

Reduces multi-thread CPU recording contention and repeated lookup overhead in victory-gpu:

  • Converts read-mostly global Mutex tables queried on per-draw/dispatch/bind hot paths to std::sync::RwLock with shared .read() hit paths (shadow::{BUFFERS, LOOSE}, descriptor::{SHADER_HEAPS, VIEWS}, HostHeap::made, texture::{STAGING, HOST_WRITTEN}, resource::PLACED_TEXTURES, draw::host_signature::MADE, pipeline::SHADERS), using .iter().rev().find in descriptor::shader_slot.
  • Adds bounded, allocation-free 16-slot per-HostList inline ShadowCache (with nested-buffer-safe CachedRange::Subrange/Exact interval bounds) and ViewCache, cleared on HostList::reset() and invalidated via Acquire/Release AtomicU64 generation counters bumped under write locks in shadow::{register, unregister} and descriptor::forget.
  • Routes draw.rs per-draw VB/IB/root-view/indirect/table/readable lookups through HostList::{find_shadow, lookup_view} and HostHeap::mirror_cached.

Verification

  • cargo clippy --workspace --all-targets & -p victory --features diagnostics --all-targets — passed (0 new warnings)
  • cargo test --workspace — passed
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --lib --features diagnostics host::tests — passed (6/6)
  • cargo xtask policy — passed (151 files ok)
**@⁠wbkepler** on GitHub · 2026-11-24 17:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1356) `wbkepler:gpu/concurrent-read-locks-and-va-cache` → `main` · +708 −75 · 8 files · 1 commits · open ## Summary Reduces multi-thread CPU recording contention and repeated lookup overhead in `victory-gpu`: - Converts read-mostly global `Mutex` tables queried on per-draw/dispatch/bind hot paths to `std::sync::RwLock` with shared `.read()` hit paths (`shadow::{BUFFERS, LOOSE}`, `descriptor::{SHADER_HEAPS, VIEWS}`, `HostHeap::made`, `texture::{STAGING, HOST_WRITTEN}`, `resource::PLACED_TEXTURES`, `draw::host_signature::MADE`, `pipeline::SHADERS`), using `.iter().rev().find` in `descriptor::shader_slot`. - Adds bounded, allocation-free 16-slot per-`HostList` inline `ShadowCache` (with nested-buffer-safe `CachedRange::Subrange`/`Exact` interval bounds) and `ViewCache`, cleared on `HostList::reset()` and invalidated via `Acquire`/`Release` `AtomicU64` generation counters bumped under write locks in `shadow::{register, unregister}` and `descriptor::forget`. - Routes `draw.rs` per-draw VB/IB/root-view/indirect/table/readable lookups through `HostList::{find_shadow, lookup_view}` and `HostHeap::mirror_cached`. ## Verification - `cargo clippy --workspace --all-targets` & `-p victory --features diagnostics --all-targets` — passed (0 new warnings) - `cargo test --workspace` — passed - `VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --lib --features diagnostics host::tests` — passed (6/6) - `cargo xtask policy` — passed (151 files ok)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:28:37 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:23 UTC · original

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-gpu/src/descriptor.rs, crates/victory-gpu/src/descriptor/host.rs, crates/victory-gpu/src/draw.rs, crates/victory-gpu/src/host.rs, crates/victory-gpu/src/resource.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/1356#issuecomment-7149503804) <!-- victory-triage v1 head=0804561542508f6837c46c44bc7093e3d239d755 verdict=needs-rebase at=2026-11-25T01:23:54Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-gpu/src/descriptor.rs`, `crates/victory-gpu/src/descriptor/host.rs`, `crates/victory-gpu/src/draw.rs`, `crates/victory-gpu/src/host.rs`, `crates/victory-gpu/src/resource.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

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

A data point for the buffer-lookup cache in this PR: on main + #1978 + #2821 + #3258 + #3274 + #3275, a rough thread-local stand-in for it in front of shadow::find (validated by a generation counter, but with no nested-buffer check) took the list bench's draw sequence from 2,485 to 1,894 ns per draw on 5 threads (−24%) and from 1,351 to 1,242 ns on 1 thread (RTX 4060 Laptop, median of 7). Details are in #2586 (7197644545). Your CachedRange version is the correct form of it. Rebased on its own (BUFFERS is a Mutex on main, and #2821/#2096 change it), it would likely be the next clear gain for the recording threads.

AI use: Claude Code (Claude Opus 5.5) ran the measurement and drafted this comment.

**@⁠KainanP** on GitHub · 2026-11-26 16:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1356#issuecomment-7197647405) A data point for the buffer-lookup cache in this PR: on main + #1978 + #2821 + #3258 + #3274 + #3275, a rough thread-local stand-in for it in front of `shadow::find` (validated by a generation counter, but with no nested-buffer check) took the list bench's draw sequence from 2,485 to 1,894 ns per draw on 5 threads (−24%) and from 1,351 to 1,242 ns on 1 thread (RTX 4060 Laptop, median of 7). Details are in #2586 (7197644545). Your `CachedRange` version is the correct form of it. Rebased on its own (`BUFFERS` is a `Mutex` on main, and #2821/#2096 change it), it would likely be the next clear gain for the recording threads. _AI use: Claude Code (Claude Opus 5.5) ran the measurement and drafted this comment._
Author
Owner

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

@⁠wbkepler I rebased this PR onto current main (63bcca4), since its per-list CachedRange cache looked like the next clear gain for the title's threads (my earlier note above). The result is on a branch for you to take if you want it. I'm not opening a competing PR.

Branch: KainanP/VIctory rebase/1356-cached-range

  • f1346618: your commit, rebased. It keeps its author and message, and only the conflict resolution changed (listed below).
  • 4e9715ca: optional, mine. The root views a shader reads as title memory (user data and its offsets in bind, and State::root_view) were added to main after this PR. They now go through HostList::find_shadow too.

To take it:

git fetch https://github.com/KainanP/VIctory rebase/1356-cached-range
git checkout gpu/concurrent-read-locks-and-va-cache
git reset --hard FETCH_HEAD        # or: git reset --hard f1346618 (without my extra commit)
git push --force-with-lease

Or from a fresh main: git cherry-pick f1346618 4e9715ca.

How the conflicts were resolved

  • shadow.rs: main now has its own GENERATION (bumped when a copy is made or a copied buffer is released). Bumping it makes memory::prepare rewrite the heaps' page tables. I kept it as it is and gave the cache a separate counter, LOOKUPS / lookup_generation(), bumped on every register/unregister like your GENERATION was. That way registering a buffer does not force page-table rewrites. unregister bumps both when the buffer had a copy. find_with_range, lookup_entry, CachedRange and the RwLocks are as you wrote them. all() reads under the read lock.
  • descriptor/host.rs: on main, HostHeap::mirror also makes a slot again when needs_remake holds (a placed texture whose host texture was upgraded since). Your read-lock fast path returned the view whenever made[index] == key, which would skip that. So I moved needs_remake into a method and the fast path checks it too. main's other made users take read() (or write() in the slow path).
  • descriptor.rs: main removed shader_slot, so SHADER_HEAPS stays an RwLock and all of main's users take read(). view_at, views_current (new on main) and view go through slot_key. I left view() unconditional because main calls it from more places now.
  • texture.rs: main moved STAGING/HOST_WRITTEN into each host copy's state, so those hunks are dropped. The branch doesn't change texture.rs.
  • resource.rs: the read fast path in texture_at keeps main's zeroing of the padding words and its current() check.
  • draw.rs: call sites follow main's signatures (root_view's uav, readable's marked, the not_run reasons). main no longer special-cases constant views in fresh tables, so that hunk only swaps in lookup_view. readable's constant-buffer lookups use find_shadow.
  • host.rs: the tests and fields are taken from both sides. The cache uses lookup_generation().

Tests (cargo test -p victory-gpu, same failures as main on each adapter):

adapter main 63bcca4 branch
RTX 4060 Laptop (skipping memory_tests::heap_direct_texture_reads_mark_their_heap_index, which access-violates on main too) 229 passed, 2 failed 233 passed, same 2 failed
Intel UHD a78b (local adapter-selection hook) 228 passed, 3 failed 232 passed, same 3 failed
WARP (VICTORY_D3D12_WARP=1, --features diagnostics, one thread) 246 passed 250 passed

Your four tests pass on all three. host::tests passed 40 runs in a row on RTX, on the branch and on main. Two older descriptor::host timing tests (a_slot_rewritten_while_its_view_is_prepared_views_the_new_record, views_bound_while_another_slot_gets_its_texture_do_not_wait) are flaky when run in parallel with each other on main too: 88 failures in 150 paired runs on main, 101 on the branch. cargo clippy -p victory-gpu --all-targets -- -D warnings passes with and without --all-features.

Measured with the list bench (fork branch perf/list-call-bench, VICTORY_LIST_BENCH_SCALING=1): each thread records the draw sequence (object constants, root constants, index buffer, indexed draw) into its own list. Release build, ns per draw, median of 11 interleaved runs [min–max]:

1 thread 5 threads (per thread)
main 3,501 [3,226–4,321] 13,053 [11,934–16,877]
main + this branch 3,377 [3,196–3,471] 11,642 [10,702–13,799] (−11%)
stack¹ 1,561 [1,480–2,186] 3,234 [2,877–3,568]
stack¹ + this branch 1,570 [1,433–1,772] 2,496 [2,310–3,009] (−23%)

¹ main + #1978 + #2821 + #3258 + #3274 + #3275. On that stack, this PR overlaps #2821 (both turn BUFFERS, VIEWS and the heaps' made into reader/writer locks) and #3275 (a thread-local view cache). For this measurement only, I kept #2821's and #3275's versions of the overlapping lock changes and this PR's caches (find_with_range, ShadowCache, ViewCache). That merge is not pushed. The indirect-call rows (VICTORY_LIST_BENCH_INDIRECT=1) did not move beyond their spread.

The 1-thread cost doesn't change. The gain is in contention on 5 threads, as in the thread-local experiment (−24% there).

Hardware: RTX 4060 Laptop, i7-14650HX, Windows 11. Foreign CPU load: about six unrelated Python jobs (four CPU-bound) ran throughout, so absolute numbers are higher than in my earlier comments. Builds were interleaved run by run. Not measured: the title in game.

AI use: Claude Code (Claude Opus 5.5) did the rebase and conflict resolution, ran the tests and measurements and drafted this comment.

**@⁠KainanP** on GitHub · 2026-11-26 17:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/1356#issuecomment-7198635510) @⁠wbkepler I rebased this PR onto current `main` (63bcca4), since its per-list `CachedRange` cache looked like the next clear gain for the title's threads (my earlier note above). The result is on a branch for you to take if you want it. I'm not opening a competing PR. **Branch:** [`KainanP/VIctory` `rebase/1356-cached-range`](https://github.com/KainanP/VIctory/tree/rebase/1356-cached-range) - `f1346618`: your commit, rebased. It keeps its author and message, and only the conflict resolution changed (listed below). - `4e9715ca`: optional, mine. The root views a shader reads as title memory (user data and its offsets in `bind`, and `State::root_view`) were added to `main` after this PR. They now go through `HostList::find_shadow` too. To take it: ``` git fetch https://github.com/KainanP/VIctory rebase/1356-cached-range git checkout gpu/concurrent-read-locks-and-va-cache git reset --hard FETCH_HEAD # or: git reset --hard f1346618 (without my extra commit) git push --force-with-lease ``` Or from a fresh `main`: `git cherry-pick f1346618 4e9715ca`. **How the conflicts were resolved** - `shadow.rs`: `main` now has its own `GENERATION` (bumped when a copy is made or a copied buffer is released). Bumping it makes `memory::prepare` rewrite the heaps' page tables. I kept it as it is and gave the cache a separate counter, `LOOKUPS` / `lookup_generation()`, bumped on every `register`/`unregister` like your `GENERATION` was. That way registering a buffer does not force page-table rewrites. `unregister` bumps both when the buffer had a copy. `find_with_range`, `lookup_entry`, `CachedRange` and the `RwLock`s are as you wrote them. `all()` reads under the read lock. - `descriptor/host.rs`: on `main`, `HostHeap::mirror` also makes a slot again when `needs_remake` holds (a placed texture whose host texture was upgraded since). Your read-lock fast path returned the view whenever `made[index] == key`, which would skip that. So I moved `needs_remake` into a method and the fast path checks it too. `main`'s other `made` users take `read()` (or `write()` in the slow path). - `descriptor.rs`: `main` removed `shader_slot`, so `SHADER_HEAPS` stays an `RwLock` and all of `main`'s users take `read()`. `view_at`, `views_current` (new on `main`) and `view` go through `slot_key`. I left `view()` unconditional because `main` calls it from more places now. - `texture.rs`: `main` moved `STAGING`/`HOST_WRITTEN` into each host copy's state, so those hunks are dropped. The branch doesn't change `texture.rs`. - `resource.rs`: the read fast path in `texture_at` keeps `main`'s zeroing of the padding words and its `current()` check. - `draw.rs`: call sites follow `main`'s signatures (`root_view`'s `uav`, `readable`'s `marked`, the `not_run` reasons). `main` no longer special-cases constant views in fresh tables, so that hunk only swaps in `lookup_view`. `readable`'s constant-buffer lookups use `find_shadow`. - `host.rs`: the tests and fields are taken from both sides. The cache uses `lookup_generation()`. **Tests** (`cargo test -p victory-gpu`, same failures as `main` on each adapter): | adapter | main 63bcca4 | branch | |---|---|---| | RTX 4060 Laptop (skipping `memory_tests::heap_direct_texture_reads_mark_their_heap_index`, which access-violates on main too) | 229 passed, 2 failed | 233 passed, same 2 failed | | Intel UHD a78b (local adapter-selection hook) | 228 passed, 3 failed | 232 passed, same 3 failed | | WARP (`VICTORY_D3D12_WARP=1`, `--features diagnostics`, one thread) | 246 passed | 250 passed | Your four tests pass on all three. `host::tests` passed 40 runs in a row on RTX, on the branch and on main. Two older `descriptor::host` timing tests (`a_slot_rewritten_while_its_view_is_prepared_views_the_new_record`, `views_bound_while_another_slot_gets_its_texture_do_not_wait`) are flaky when run in parallel with each other on main too: 88 failures in 150 paired runs on main, 101 on the branch. `cargo clippy -p victory-gpu --all-targets -- -D warnings` passes with and without `--all-features`. **Measured** with the list bench (fork branch `perf/list-call-bench`, `VICTORY_LIST_BENCH_SCALING=1`): each thread records the draw sequence (object constants, root constants, index buffer, indexed draw) into its own list. Release build, ns per draw, median of 11 interleaved runs [min–max]: | | 1 thread | 5 threads (per thread) | |---|---:|---:| | main | 3,501 [3,226–4,321] | 13,053 [11,934–16,877] | | main + this branch | 3,377 [3,196–3,471] | **11,642** [10,702–13,799] (−11%) | | stack¹ | 1,561 [1,480–2,186] | 3,234 [2,877–3,568] | | stack¹ + this branch | 1,570 [1,433–1,772] | **2,496** [2,310–3,009] (−23%) | ¹ main + #1978 + #2821 + #3258 + #3274 + #3275. On that stack, this PR overlaps #2821 (both turn `BUFFERS`, `VIEWS` and the heaps' `made` into reader/writer locks) and #3275 (a thread-local view cache). For this measurement only, I kept #2821's and #3275's versions of the overlapping lock changes and this PR's caches (`find_with_range`, `ShadowCache`, `ViewCache`). That merge is not pushed. The indirect-call rows (`VICTORY_LIST_BENCH_INDIRECT=1`) did not move beyond their spread. The 1-thread cost doesn't change. The gain is in contention on 5 threads, as in the thread-local experiment (−24% there). Hardware: RTX 4060 Laptop, i7-14650HX, Windows 11. Foreign CPU load: about six unrelated Python jobs (four CPU-bound) ran throughout, so absolute numbers are higher than in my earlier comments. Builds were interleaved run by run. Not measured: the title in game. _AI use: Claude Code (Claude Opus 5.5) did the rebase and conflict resolution, ran the tests and measurements and drafted this comment._
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/descriptor.rs
  • crates/victory-gpu/src/descriptor/host.rs
  • crates/victory-gpu/src/draw.rs
  • crates/victory-gpu/src/host.rs
  • crates/victory-gpu/src/pipeline.rs
  • crates/victory-gpu/src/resource.rs
  • crates/victory-gpu/src/shadow.rs
  • crates/victory-gpu/src/texture.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/1356/head:github-bot/gh-1356'
git switch 'github-bot/gh-1356'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1356'
git switch 'github-bot/gh-1356'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1356'
git switch 'github-bot/gh-1356'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1356'
git switch 'main'
git merge --squash 'github-bot/gh-1356'
git switch 'main'
git merge --ff-only 'github-bot/gh-1356'
git switch 'main'
git merge 'github-bot/gh-1356'
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!1356
No description provided.