Skip to content

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

Open
KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:perf/view-lookups-unlocked
Open

KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:perf/view-lookups-unlocked

Conversation

@KainanP

@KainanP KainanP commented Nov 26, 2026

Copy link
Copy Markdown

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

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

…'s lock

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, Marvisak#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 <noreply@anthropic.com>
@oni-swr

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown

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)

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants