descriptor: use a per-slot reader-writer lock (BrLock) for VIEWS #534

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

@⁠cerealfordinner on GitHub · 2026-12-04 23:29 UTC · original
cerealfordinner:upstream-pr-descriptor-brlock → main · +144 −23 · 3 files · 1 commits · open

Uses a per-slot reader-writer lock (BrLock, new brlock.rs) for the descriptor VIEWS table instead of a single lock, so readers of different slots don't serialize.

cargo check -p victory-gpu --lib --target x86_64-pc-windows-gnu: clean (one pre-existing dead-code warning).

Note: this touches the same descriptor VIEWS table as open PRs #506 and #510 — flagging in case they'd rather land first; happy to rebase or close if it overlaps.

Signed-off-by: cerealfordinner [email protected]

**@⁠cerealfordinner** on GitHub · 2026-12-04 23:29 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/534) `cerealfordinner:upstream-pr-descriptor-brlock` → `main` · +144 −23 · 3 files · 1 commits · open Uses a per-slot reader-writer lock (`BrLock`, new `brlock.rs`) for the descriptor VIEWS table instead of a single lock, so readers of different slots don't serialize. `cargo check -p victory-gpu --lib --target x86_64-pc-windows-gnu`: clean (one pre-existing dead-code warning). Note: this touches the same descriptor VIEWS table as open PRs #506 and #510 — flagging in case they'd rather land first; happy to rebase or close if it overlaps. Signed-off-by: cerealfordinner <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 23:33:27 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 00:52 UTC · original

Checked a9684da on native Windows with MSVC (one NVIDIA desktop card plus WARP, didn't run the game). The base is current main, so everything below compares against b581f7e.

Debug and release builds of victory-gpu are clean and the workspace builds in debug. Clippy gives the same warning counts as main with and without diagnostics, and none of them are in brlock.rs.

cargo test -p victory-gpu --lib fails the same 5 tests on both sides (the two indirect host_list ones, the two pipeline library loads, a flaky watch test). The 40 descriptor:: tests pass at 1, 2, 4, 8 and 16 test threads, three runs each. With --features diagnostics and VICTORY_D3D12_DEBUG=1 the NVIDIA results match main. On WARP both sides also fail views_are_made_beside_the_heap_and_copied_in, and main hit the flaky watch test there too.

views_die_with_their_resource still dies with an access violation, on main and on the PR alike, and it isn't the lock. The fake resource 0x7777_0000 goes into entry(), then records::entry_made (descriptor.rs:791) reaches records::kind, which calls resource::buffer_bytes on it (records.rs:707). That function only checks for null before reading the object (resource.rs:3178-3183). A bare words_for(View::Shader(fake, None)) crashes the same way, and a view whose only resource is a fake counter doesn't.

On the lock itself, it isn't per slot of the table, which is how I first read the title. It's still one lock over all of Views. What's split is the reader count, 32 counters on their own cache lines (brlock.rs:13), each thread taking one round robin (brlock.rs:34-43), so there's no hashing and nothing to collide. That's actually good, because list, index, by_resource and shapes still change together under one writer. Cross-map consistency is what it was and there's no lock order to get wrong. A title like "big-reader lock" would avoid the confusion.

The orderings look right to me. The reader's fetch_add then writing.load (68-69) and the writer's writing.store then slot loads (81, 84) are all SeqCst, so they can't both miss each other, and the Release unlocks (107, 133) pair with those loads. Readers step back when the flag is up, so writers win. Reading again on a thread that already holds a read deadlocks if a writer is waiting, same as std. None of the call sites nest (836, 851, 912, 1231, 1236).

The one thing I'd change is the waiting. pause (45-52) spins 64 times and then calls yield_now forever, it never parks. A reader stuck behind a writer keeps a core busy the whole time, and writers do real work in there. entry() writes the records row (descriptor.rs:791), shape() does the same (747), and forget walks every entry of a resource. If a writer gets preempted mid-write, every recording thread that wants a read sits spinning, where std's RwLock would park them. It didn't cost anything in the numbers below, so not a blocker, but a fallback after N yields (WaitOnAddress on writing, or park/unpark) would be cheap insurance.

I also ran two throwaway stress tests (not committed). One hit BrLock directly, readers checking that three fields always match while writers bump them, from 4 readers with 1 writer up to 256 with 1 and 128 with 8. No torn reads, no lost writes, writers kept going. The other went through the real VIEWS path, threads calling words_for and view_at on a hot set of views plus new views followed by forget at 0, 1 and 10 per 1000 ops. Every result was checked. No mismatches and no hangs (180 s watchdog). This only exercises the lock path, since constant and placed views never reach VIEWS, so a real frame won't see ratios like these. Release build, three runs each, alternating PR and main, in Mops/s

                      PR         main
read only,  1 thr   20-27      18-26
read only, 16 thr   136-150    8.5-8.8
read only, 64 thr   126-145    7.4
1/1000,    16 thr   97-102     6.1
1/1000,    64 thr   76-87      5.7
10/1000,   16 thr   29-31      4.0-4.3
10/1000,   64 thr   18.5-19.6  3.6-3.7

Single thread is the same within noise. Main gets slower as threads are added while the PR scales, and that still holds when the machine is oversubscribed, spinning and all.

#506 changes the maps inside Views, not the lock, so the two only meet in the initializer and should rebase easily either way. #510 is draw.rs only.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 00:52 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/534#issuecomment-7428717100) Checked a9684da on native Windows with MSVC (one NVIDIA desktop card plus WARP, didn't run the game). The base is current main, so everything below compares against b581f7e. Debug and release builds of victory-gpu are clean and the workspace builds in debug. Clippy gives the same warning counts as main with and without `diagnostics`, and none of them are in brlock.rs. `cargo test -p victory-gpu --lib` fails the same 5 tests on both sides (the two indirect host_list ones, the two pipeline library loads, a flaky watch test). The 40 `descriptor::` tests pass at 1, 2, 4, 8 and 16 test threads, three runs each. With `--features diagnostics` and VICTORY_D3D12_DEBUG=1 the NVIDIA results match main. On WARP both sides also fail `views_are_made_beside_the_heap_and_copied_in`, and main hit the flaky watch test there too. `views_die_with_their_resource` still dies with an access violation, on main and on the PR alike, and it isn't the lock. The fake resource 0x7777_0000 goes into `entry()`, then `records::entry_made` (descriptor.rs:791) reaches `records::kind`, which calls `resource::buffer_bytes` on it (records.rs:707). That function only checks for null before reading the object (resource.rs:3178-3183). A bare `words_for(View::Shader(fake, None))` crashes the same way, and a view whose only resource is a fake counter doesn't. On the lock itself, it isn't per slot of the table, which is how I first read the title. It's still one lock over all of `Views`. What's split is the reader count, 32 counters on their own cache lines (brlock.rs:13), each thread taking one round robin (brlock.rs:34-43), so there's no hashing and nothing to collide. That's actually good, because `list`, `index`, `by_resource` and `shapes` still change together under one writer. Cross-map consistency is what it was and there's no lock order to get wrong. A title like "big-reader lock" would avoid the confusion. The orderings look right to me. The reader's `fetch_add` then `writing.load` (68-69) and the writer's `writing.store` then slot loads (81, 84) are all SeqCst, so they can't both miss each other, and the Release unlocks (107, 133) pair with those loads. Readers step back when the flag is up, so writers win. Reading again on a thread that already holds a read deadlocks if a writer is waiting, same as std. None of the call sites nest (836, 851, 912, 1231, 1236). The one thing I'd change is the waiting. `pause` (45-52) spins 64 times and then calls `yield_now` forever, it never parks. A reader stuck behind a writer keeps a core busy the whole time, and writers do real work in there. `entry()` writes the records row (descriptor.rs:791), `shape()` does the same (747), and `forget` walks every entry of a resource. If a writer gets preempted mid-write, every recording thread that wants a read sits spinning, where std's RwLock would park them. It didn't cost anything in the numbers below, so not a blocker, but a fallback after N yields (WaitOnAddress on `writing`, or park/unpark) would be cheap insurance. I also ran two throwaway stress tests (not committed). One hit `BrLock` directly, readers checking that three fields always match while writers bump them, from 4 readers with 1 writer up to 256 with 1 and 128 with 8. No torn reads, no lost writes, writers kept going. The other went through the real VIEWS path, threads calling `words_for` and `view_at` on a hot set of views plus new views followed by `forget` at 0, 1 and 10 per 1000 ops. Every result was checked. No mismatches and no hangs (180 s watchdog). This only exercises the lock path, since constant and placed views never reach VIEWS, so a real frame won't see ratios like these. Release build, three runs each, alternating PR and main, in Mops/s ``` PR main read only, 1 thr 20-27 18-26 read only, 16 thr 136-150 8.5-8.8 read only, 64 thr 126-145 7.4 1/1000, 16 thr 97-102 6.1 1/1000, 64 thr 76-87 5.7 10/1000, 16 thr 29-31 4.0-4.3 10/1000, 64 thr 18.5-19.6 3.6-3.7 ``` Single thread is the same within noise. Main gets slower as threads are added while the PR scales, and that still holds when the machine is oversubscribed, spinning and all. #506 changes the maps inside `Views`, not the lock, so the two only meet in the initializer and should rebase easily either way. #510 is draw.rs only. AI-assisted: yes
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/534/head:github-bot/gh-534'
git switch 'github-bot/gh-534'

Merge

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