descriptor: use per-slot reader-writer lock (BrLock) for VIEWS #203
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!203
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-203"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Lciarallo on GitHub · 2026-12-02 16:25 UTC · original
Lciarallo:pr-brlock-views→main· +148 −23 · 3 files · 2 commits · openFollow-up to #136, split into a focused standalone PR as suggested by @goldfishdev.
Problem
static VIEWSincrates/victory-gpu/src/descriptor.rsis looked up on every frame by multiple command-list recording threads in parallel, while writes are rare (only when a new view/shape is registered or a resource is released). Withstd::sync::RwLock<Views>, all concurrent readers increment and decrement the same single atomic counter, causing heavy cache-line bouncing across CPU cores during multi-threaded recording.Solution
Adds a small, self-contained distributed reader-writer lock (
BrLock<T>incrates/victory-gpu/src/brlock.rs) and switchesVIEWSto use it:#[repr(align(128))]), assigned per thread viathread_local!, so concurrent readers only touch their own cache line and do not contend with one another.Mutex<()>, raise anAtomicBool(writing), and wait for all 32 reader slots to drain before mutatingViews; readers that observewritingback off and wait until it clears.brlock.rs,descriptor.rs,lib.rs) with no new dependencies or runtime switches required.Testing
VIEWS.cargo xwin check --workspace --locked --target x86_64-pc-windows-msvcpasses cleanly (0 errors, 0 warnings); workspace unit tests pass.@Kal0s-design on GitHub · 2026-12-03 19:05 UTC · original
Tested this on Windows 11 native (MSVC toolchain), CPU-only, at
322192aand also merged onto current main, where it merges cleanly.I went through brlock.rs with the ordering in mind. The reader does fetch_add on its slot and then loads
writing, the writer storeswritingand then loads every slot, and all four are SeqCst, so the Dekker handshake holds. Either the reader sees the flag and backs off or the writer sees the count. The Release on read guard drop pairs with the writer's SeqCst slot load, and the Release store ofwriting = falsepairs with the reader's load, so the next reader sees what the writer did. Writers are serialized by the mutex, poisoning is ignored the same way the oldunwrap_or_else(into_inner)did, and the Sync bound (T: Send + Sync) matches RwLock. In descriptor.rs no read guard is held while write() is taken, and I didn't find anything running under a guard that takes VIEWS again.I added some local stress tests under cfg(test) in brlock.rs (not pushed) and ran them in debug and release.
All five pass in debug and release.
For the existing suite I ran
cargo test -p victory-gpu --lib -- --test-threads=4.descriptor::tests::views_die_with_their_resource, the test this PR touches, crashes with an access violation here, but it does that on the PR's base and on main as well, so I had to skip it. Without it the PR head gives 294 passed, 9 failed, 23 ignored, identical to its base with the same failing tests. Merged onto main it's 338 passed, 5 failed, 23 ignored, identical to main alone. The same set of tests fails here without the PR.Clippy (
-p victory-gpu --all-targets) shows no new warnings compared to base.One small note that isn't a bug. While a writer holds the lock, waiting readers spin and then yield_now instead of parking, so a long write section like forget() with many entries keeps them busy. Probably fine given how rare writes are.
Looks correct to me.
AI-assisted: yes
@Luxuride on GitHub · 2026-12-04 12:01 UTC · original
The motivation is real — recording threads do hit VIEWS constantly — but nobody, including you, can tell whether this is a win without a number, and it replaces a std primitive on the frame path with 140 lines of new
unsafeand no tests of its own.brlock.rs:77and the wait loop inwrite()), with no parking; a slow write (a resize of the 20k-entry table) burns a core per waiting recording thread. std'sRwLockparks on a futex, and a convoy is exactly the case this is meant to fix.brlock.rs:3says each reader counts on a cache line of its own, butNEXT.fetch_add(1, Relaxed) % SLOTSwithSLOTS = 32means thread 33 shares slot 0, and the instance is not per-lock, so twoBrLocks share one thread→slot map. Harmless for counting, misleading for a reader.ReadGuard::dropusesReleasewhile everything else isSeqCst(brlock.rs:111,137): correct, but the whole safety argument lives in those orderings, so comment or make them uniform.Rebase, add a stress test (N readers, 1 writer, no torn read, no lost update) and one bench at 1 and 8 threads; with the number this becomes an easy yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.