perf(gpu): lock-free warm fast path, off-lock sweep, and batched land in memory::ready #3943

Closed
github-bot wants to merge 1 commit from github-bot/gh-3943 into main AGit
Owner

@⁠steven-seagull27 on GitHub · 2026-11-30 12:33 UTC · original
steven-seagull27:perf/memory-ready-lock-convoy → main · +1352 −82 · 2 files · 1 commits · closed without merging

Summary

Eliminates the multi-threaded MEMORY mutex convoy in memory::ready() across concurrent command-list recording threads and submission-time memory::land callbacks, with zero behavioral changes:

  1. Off-lock per-list deduplication and steady-state fast path (memory.rs):

    • Added prefilter_dispatch to run per-list deduplication (named_key, land_key, written_key, small_key) and shadow::in_buffer resolution before acquiring MEMORY, preserving exact Named2::CheckIfNotPacked short-circuit semantics when multiple root views share a 64 KiB page.
    • Added ReadyFastState (FAST) mirroring fixed GPU virtual addresses (directory_gpu, faults_gpu), current_frame, copies_len, spare-page counts, and per-heap (base, made) UAV slot runs so warm dispatches whose prefiltered work is empty return Some(Ready) without locking MEMORY (while still checking READ, bring_up_key(copies_len), SPARE reserve, and diagnostics_active()).
    • Moved uses counting to atomic counters on ReadyFastState (note_use / take_uses) and measured WAIT / WAITED only on contended MEMORY.try_lock() misses (TryLockError::WouldBlock).
  2. Off-lock rolling frame sweep with generation / ABA validation (memory.rs):

    • Split look_again into plan_look_again (snapshots this frame's SWEEP slice under MEMORY), probe_look_again (runs watch::writes_since, dirty-page copy_mapped reads, and VirtualQuery packed-header checks off-lock), and apply_look_again (re-locks MEMORY and merges dirty dwords / retires vanished packed structures only if sweep_page_valid confirms looked, monotonic generation, (copy, at), born, and taken did not change while unlocked).
  3. Batched deferred memory::land per title-write epoch (host.rs, memory.rs):

    • Added Deferred::memory_lands and HostList::defer_memory_land to coalesce all Memory::land(at) addresses recorded within the same epoch into a single Step::Host closure in recorded order under one MEMORY lock acquisition at submission time, splitting automatically on Deferred::title_written and clearing on Deferred::clear.

Synthetic --release Multi-Threaded Contention Benchmark (Before 41adb551 vs After, 3-run median)

Measured with cargo test --locked -j 3 -p victory-gpu --lib --release -- memory::tests::bench_memory_ready_multithreaded_contention --ignored --nocapture --test-threads=1 on a synthetic workload of 4 recording threads × 150 dispatches/frame (600 calls/frame) over 120 frames (after 10 warmup frames) with 512 synthetic MEM_WRITE_WATCH mapped 64 KiB pages (4 swept pages/frame, 2 dirtied/frame):

Metric Legacy serialized (41adb551) Optimized Delta
Wall time per frame (4 threads, 600 calls/f) 0.309 ms 0.140 ms -54.7% (2.21× faster)
Summed ready() CPU time per frame 1.056 ms 0.332 ms -68.6% (3.18× lower)
Contended MEMORY mutex waits per frame 77.7 3.9 -95.0%
Summed MEMORY mutex wait time per frame 0.799 ms 0.105 ms -86.9% (7.61× lower)
Summed MEMORY mutex held time per frame 0.133 ms 0.057 ms -57.1% (2.33× lower)

Verification

  • Added synthetic unit tests and contention benchmark:
    • memory::tests::prefilter_dispatch_deduplicates_constants_and_addresses
    • memory::tests::prefilter_dispatch_defers_same_page_named2_until_packed_check
    • memory::tests::prefilter_dispatch_tracks_written_and_small_buffers
    • memory::tests::fast_state_hits_when_warm_and_invalidates_on_every_guard
    • memory::tests::sweep_page_valid_rejects_concurrent_updates_and_aba_remaps
    • memory::tests::defer_memory_land_batches_addresses_and_splits_across_title_written
    • memory::tests::bench_memory_ready_multithreaded_contention (#[ignore])
  • Ran:
    • cargo test --locked -j 3 -p victory-gpu --lib -- memory:: --test-threads=1 — 12 passed, 0 failed (1 ignored)
    • cargo test --locked -j 3 -p victory-gpu --lib -- released_view_heap_evicts_memory_heap_entry --test-threads=1 — 1 passed, 0 failed
    • cargo check --locked -j 3 -p victory-gpu --lib --features diagnostics — clean
    • cargo clippy --locked -j 3 -p victory-gpu --lib --tests — 0 new warnings in memory.rs or host.rs
    • git diff --check origin/main..HEAD — clean

AI-assisted. Synthetic verification only.

**@⁠steven-seagull27** on GitHub · 2026-11-30 12:33 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3943) `steven-seagull27:perf/memory-ready-lock-convoy` → `main` · +1352 −82 · 2 files · 1 commits · closed without merging ## Summary Eliminates the multi-threaded `MEMORY` mutex convoy in `memory::ready()` across concurrent command-list recording threads and submission-time `memory::land` callbacks, with zero behavioral changes: 1. **Off-lock per-list deduplication and steady-state fast path (`memory.rs`):** - Added `prefilter_dispatch` to run per-list deduplication (`named_key`, `land_key`, `written_key`, `small_key`) and `shadow::in_buffer` resolution before acquiring `MEMORY`, preserving exact `Named2::CheckIfNotPacked` short-circuit semantics when multiple root views share a 64 KiB page. - Added `ReadyFastState` (`FAST`) mirroring fixed GPU virtual addresses (`directory_gpu`, `faults_gpu`), `current_frame`, `copies_len`, spare-page counts, and per-heap `(base, made)` UAV slot runs so warm dispatches whose prefiltered work is empty return `Some(Ready)` without locking `MEMORY` (while still checking `READ`, `bring_up_key(copies_len)`, `SPARE` reserve, and `diagnostics_active()`). - Moved `uses` counting to atomic counters on `ReadyFastState` (`note_use` / `take_uses`) and measured `WAIT` / `WAITED` only on contended `MEMORY.try_lock()` misses (`TryLockError::WouldBlock`). 2. **Off-lock rolling frame sweep with generation / ABA validation (`memory.rs`):** - Split `look_again` into `plan_look_again` (snapshots this frame's `SWEEP` slice under `MEMORY`), `probe_look_again` (runs `watch::writes_since`, dirty-page `copy_mapped` reads, and `VirtualQuery` packed-header checks off-lock), and `apply_look_again` (re-locks `MEMORY` and merges dirty dwords / retires vanished packed structures only if `sweep_page_valid` confirms `looked`, monotonic `generation`, `(copy, at)`, `born`, and `taken` did not change while unlocked). 3. **Batched deferred `memory::land` per title-write epoch (`host.rs`, `memory.rs`):** - Added `Deferred::memory_lands` and `HostList::defer_memory_land` to coalesce all `Memory::land(at)` addresses recorded within the same epoch into a single `Step::Host` closure in recorded order under one `MEMORY` lock acquisition at submission time, splitting automatically on `Deferred::title_written` and clearing on `Deferred::clear`. ## Synthetic `--release` Multi-Threaded Contention Benchmark (Before `41adb551` vs After, 3-run median) Measured with `cargo test --locked -j 3 -p victory-gpu --lib --release -- memory::tests::bench_memory_ready_multithreaded_contention --ignored --nocapture --test-threads=1` on a synthetic workload of **4 recording threads × 150 dispatches/frame (`600` calls/frame)** over 120 frames (after 10 warmup frames) with 512 synthetic `MEM_WRITE_WATCH` mapped 64 KiB pages (4 swept pages/frame, 2 dirtied/frame): | Metric | Legacy serialized (`41adb551`) | Optimized | Delta | |---|---:|---:|---:| | Wall time per frame (4 threads, 600 calls/f) | 0.309 ms | 0.140 ms | **-54.7%** (2.21× faster) | | Summed `ready()` CPU time per frame | 1.056 ms | 0.332 ms | **-68.6%** (3.18× lower) | | Contended `MEMORY` mutex waits per frame | 77.7 | 3.9 | **-95.0%** | | Summed `MEMORY` mutex wait time per frame | 0.799 ms | 0.105 ms | **-86.9%** (7.61× lower) | | Summed `MEMORY` mutex held time per frame | 0.133 ms | 0.057 ms | **-57.1%** (2.33× lower) | ## Verification - Added synthetic unit tests and contention benchmark: - `memory::tests::prefilter_dispatch_deduplicates_constants_and_addresses` - `memory::tests::prefilter_dispatch_defers_same_page_named2_until_packed_check` - `memory::tests::prefilter_dispatch_tracks_written_and_small_buffers` - `memory::tests::fast_state_hits_when_warm_and_invalidates_on_every_guard` - `memory::tests::sweep_page_valid_rejects_concurrent_updates_and_aba_remaps` - `memory::tests::defer_memory_land_batches_addresses_and_splits_across_title_written` - `memory::tests::bench_memory_ready_multithreaded_contention` (`#[ignore]`) - Ran: - `cargo test --locked -j 3 -p victory-gpu --lib -- memory:: --test-threads=1` — **12 passed, 0 failed** (1 ignored) - `cargo test --locked -j 3 -p victory-gpu --lib -- released_view_heap_evicts_memory_heap_entry --test-threads=1` — **1 passed, 0 failed** - `cargo check --locked -j 3 -p victory-gpu --lib --features diagnostics` — clean - `cargo clippy --locked -j 3 -p victory-gpu --lib --tests` — **0 new warnings** in `memory.rs` or `host.rs` - `git diff --check origin/main..HEAD` — clean AI-assisted. Synthetic verification only.
Eliminate the multi-threaded MEMORY mutex convoy in memory::ready():
- Prefilter per-list deduplication keys and shadow::in_buffer lookups off-lock, and add ReadyFastState so warm steady-state dispatches return Ready without acquiring MEMORY.
- Split look_again into plan_look_again, off-lock probe_look_again, and generation-validated apply_look_again so write-watch and VirtualQuery probes do not stall concurrent recording threads.
- Coalesce per-epoch deferred Memory::land addresses on HostList into a single submission-time lock acquisition (split across title_written and reset).
- Add synthetic unit tests and an ignored 4-thread contention benchmark.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 12:34:41 +00:00
github-bot force-pushed github-bot/gh-3943 from 4ab1ace261 to 33377ca879 2026-11-30 18:34:19 +00:00 Compare
Author
Owner

@⁠steven-seagull27 on GitHub · 2026-11-30 18:29 UTC · original

Rebased cleanly onto latest main:

  • Preserved #3706 (Deferred::held) alongside Deferred::memory_lands in HostList, ReadyFastState lock-free warm fast path, and off-lock plan_look_again / probe_look_again / apply_look_again.
  • Verified memory:: unit tests, --features diagnostics check, clippy --lib --tests, and xtask policy.
**@⁠steven-seagull27** on GitHub · 2026-11-30 18:29 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3943#issuecomment-7287934112) Rebased cleanly onto latest `main`: - Preserved `#3706` (`Deferred::held`) alongside `Deferred::memory_lands` in `HostList`, `ReadyFastState` lock-free warm fast path, and off-lock `plan_look_again` / `probe_look_again` / `apply_look_again`. - Verified `memory::` unit tests, `--features diagnostics` check, `clippy --lib --tests`, and `xtask policy`.
Author
Owner

@⁠nicolasabosch on GitHub · 2026-11-30 21:52 UTC · original

Tested on an RTX 4070 Laptop 8 GB: no measurable fps difference in this scene (2 runs).

Tested on main e1251c31 with this PR (33377ca8) merged on top and nothing else changed. Release build, same profile as main.

Setup

  • PC: RTX 4070 Laptop 8 GB, i9-14900HX, 32 GB RAM, Windows 11.
  • Command: victory play --series-s --dlss performance --framegen 2, with no fps cap and the window minimized.
  • Route: scripted Enter presses into the world from a save, then the character idles there for 300 s.
  • fps is the mean of the last 40 host: reports, about 200 s. These are real frames, not generated ones.
  • The submit-thread figures come from the "preparing per frame, the submitting thread" line, also over the last 40 reports.
  • The second run starts from a fixed copy of the save. In the first run the save had moved on, so the scene was a little different.
run fps ms/frame submit thread ms refreshes ms
main, 7 runs 15.96 – 18.76 53.4 – 62.7 35.0 – 41.4 13.3 – 15.4
main, fixed save only (4 runs) 16.21 – 18.18 (median 16.37) 55.1 – 61.8 35.0 – 40.5 13.3 – 15.2
this PR, run 1 18.24 54.9 38.4 13.8
this PR, run 2 (fixed save) 16.07 62.3 39.4 14.6

On this laptop, identical main runs differ by up to about 2–2.5 fps, so only an effect of roughly 2–3 fps or more would be visible in this scene.

Not tested: driving, camera turns, interiors, an image comparison, other GPUs.

**@⁠nicolasabosch** on GitHub · 2026-11-30 21:52 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3943#issuecomment-7293023382) **Tested on an RTX 4070 Laptop 8 GB: no measurable fps difference in this scene (2 runs).** Tested on main `e1251c31` with this PR (`33377ca8`) merged on top and nothing else changed. Release build, same profile as main. **Setup** - PC: RTX 4070 Laptop 8 GB, i9-14900HX, 32 GB RAM, Windows 11. - Command: `victory play --series-s --dlss performance --framegen 2`, with no fps cap and the window minimized. - Route: scripted Enter presses into the world from a save, then the character idles there for 300 s. - fps is the mean of the last 40 `host:` reports, about 200 s. These are real frames, not generated ones. - The submit-thread figures come from the "preparing per frame, the submitting thread" line, also over the last 40 reports. - The second run starts from a fixed copy of the save. In the first run the save had moved on, so the scene was a little different. | run | fps | ms/frame | submit thread ms | refreshes ms | |---|---|---|---|---| | main, 7 runs | 15.96 – 18.76 | 53.4 – 62.7 | 35.0 – 41.4 | 13.3 – 15.4 | | main, fixed save only (4 runs) | 16.21 – 18.18 (median 16.37) | 55.1 – 61.8 | 35.0 – 40.5 | 13.3 – 15.2 | | this PR, run 1 | 18.24 | 54.9 | 38.4 | 13.8 | | this PR, run 2 (fixed save) | 16.07 | 62.3 | 39.4 | 14.6 | On this laptop, identical main runs differ by up to about 2–2.5 fps, so only an effect of roughly 2–3 fps or more would be visible in this scene. **Not tested:** driving, camera turns, interiors, an image comparison, other GPUs.
github-bot force-pushed github-bot/gh-3943 from 33377ca879 to 492f24084d 2026-12-01 00:58:02 +00:00 Compare
Author
Owner

@⁠upipibi on GitHub · 2026-12-01 04:13 UTC · original

Race review of #3943 (head 492f2408), on by default with no switch: it holds. I found no lost update or stale fast-path answer, and no crash in the tests below. One small difference from main, noted at the end. CPU and synthetic tests only, no game files.

The fast path (FAST.try_ready). Each mirror is written under MEMORY wherever its source changes:

  • copies_len where copies.push;
  • the heap runs with every heaps.insert/remove;
  • the page counts in hold, release and another. Every free.pop() goes through hold.
  • current_frame before the sweep.

Every check the slow path would act on is either repeated or makes the fast path decline:

  • the frame;
  • the spare-page refill, including another_failed_frame;
  • the READ keep (list.used(READ));
  • the heap run's made == copies_len;
  • bring_up_key(copies_len).

A torn read of two mirrors can only make it decline. So when it answers, it gives what the locked path would have given at the moment it loaded copies_len.

prefilter_dispatch off the lock. The keys are per list, and a list is recorded by one thread, so taking them before MEMORY changes no outcome. first_use is still taken in the same order as on main relative to in_buffer and possible, and named(2, at) waits for the packed check (CheckIfNotPacked).

The sweep off the lock (plan/probe/apply_look_again). The probe asks the write watch, then reads, in that order, as bring does on main. So a write between the two is seen at the next sweep. apply drops a page's result if another thread took it meanwhile: bring sets looked, and take/hold bump generation, and sweep_page_valid checks both, as well as taken, at, copy and born. A packed structure is retired only after there() is asked again under the lock. A plan that is still running when the next frame begins is checked the same way.

defer_memory_land. The batch is split at every step that writes title memory (Deferred::title_written) and cleared on reset. Within a batch, a later land now runs at the first one's place, earlier than on main relative to steps that only read title memory. A bring_up still follows each, so what it brings is still sent.

Tests. RTX 3070, Ryzen 9 5950X, Windows 11. This PR merged onto main 5fabb46e (clean):

what runs result
bench_memory_ready_multithreaded_contention (release, --ignored) 10 10 passed, no crash or hang
memory:: 5 12 passed, 0 failed each time
whole victory-gpu lib 5 1101 passed each time, but once 1 failed: card_steps::…::shared_scratch_eliminates_flight_settle_end_to_end
whole victory-gpu lib, main 5 1095 passed each time, but once 1 failed: uav_pool::…::host_list_uav_pool_lifecycle_retire_trim_and_info_queue

Both single failures are flakes I have also seen on main (#3902 covers the second).

One difference from main. On main, every ready() of a frame waits for that frame's sweep. Here, other threads go on while the sweep is unlocked, including through the fast path. So a page the sweep refreshes can reach a list recorded in that window a little later than on main. The sweep already refreshes only a slice of the pages each frame, so this looks harmless, but it isn't quite "zero behavioral changes". I didn't measure it in a game.

Tested with the help of Claude Code (Claude Opus 5.5).

**@⁠upipibi** on GitHub · 2026-12-01 04:13 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3943#issuecomment-7300514289) Race review of #3943 (head 492f2408), on by default with no switch: **it holds. I found no lost update or stale fast-path answer, and no crash in the tests below.** One small difference from `main`, noted at the end. CPU and synthetic tests only, no game files. **The fast path (`FAST.try_ready`).** Each mirror is written under `MEMORY` wherever its source changes: - `copies_len` where `copies.push`; - the heap runs with every `heaps.insert`/`remove`; - the page counts in `hold`, `release` and `another`. Every `free.pop()` goes through `hold`. - `current_frame` before the sweep. Every check the slow path would act on is either repeated or makes the fast path decline: - the frame; - the spare-page refill, including `another_failed_frame`; - the `READ` keep (`list.used(READ)`); - the heap run's `made == copies_len`; - `bring_up_key(copies_len)`. A torn read of two mirrors can only make it decline. So when it answers, it gives what the locked path would have given at the moment it loaded `copies_len`. **`prefilter_dispatch` off the lock.** The keys are per list, and a list is recorded by one thread, so taking them before `MEMORY` changes no outcome. `first_use` is still taken in the same order as on `main` relative to `in_buffer` and `possible`, and `named(2, at)` waits for the packed check (`CheckIfNotPacked`). **The sweep off the lock (`plan`/`probe`/`apply_look_again`).** The probe asks the write watch, then reads, in that order, as `bring` does on `main`. So a write between the two is seen at the next sweep. `apply` drops a page's result if another thread took it meanwhile: `bring` sets `looked`, and `take`/`hold` bump `generation`, and `sweep_page_valid` checks both, as well as `taken`, `at`, `copy` and `born`. A packed structure is retired only after `there()` is asked again under the lock. A plan that is still running when the next frame begins is checked the same way. **`defer_memory_land`.** The batch is split at every step that writes title memory (`Deferred::title_written`) and cleared on reset. Within a batch, a later `land` now runs at the first one's place, earlier than on `main` relative to steps that only read title memory. A `bring_up` still follows each, so what it brings is still sent. **Tests.** RTX 3070, Ryzen 9 5950X, Windows 11. This PR merged onto `main` 5fabb46e (clean): | what | runs | result | |---|---|---| | `bench_memory_ready_multithreaded_contention` (release, `--ignored`) | 10 | 10 passed, no crash or hang | | `memory::` | 5 | 12 passed, 0 failed each time | | whole `victory-gpu` lib | 5 | 1101 passed each time, but once 1 failed: `card_steps::…::shared_scratch_eliminates_flight_settle_end_to_end` | | whole `victory-gpu` lib, `main` | 5 | 1095 passed each time, but once 1 failed: `uav_pool::…::host_list_uav_pool_lifecycle_retire_trim_and_info_queue` | Both single failures are flakes I have also seen on `main` (#3902 covers the second). **One difference from `main`.** On `main`, every `ready()` of a frame waits for that frame's sweep. Here, other threads go on while the sweep is unlocked, including through the fast path. So a page the sweep refreshes can reach a list recorded in that window a little later than on `main`. The sweep already refreshes only a slice of the pages each frame, so this looks harmless, but it isn't quite "zero behavioral changes". I didn't measure it in a game. *Tested with the help of Claude Code (Claude Opus 5.5).*
Author
Owner

@⁠albertik322-sudo on GitHub · 2026-12-01 17:00 UTC · original

Small positive on a laptop, in a batch with #4394 and #4395. PC: RTX 3070 Laptop 8 GB, i7-11800H, 32 GB RAM, Windows 11. Series S, 1080p output, DLSS Performance, FG x4.

prep medians across sessions: ~46 ms before this batch, ~44-45 ms after; fps median 17.4 to 17.8 in light scenes. Within scene noise individually, but the direction held across three separate sessions. No instability, no crashes attributable to it (unlike #4944, which I reported separately).

**@⁠albertik322-sudo** on GitHub · 2026-12-01 17:00 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3943#issuecomment-7318595316) Small positive on a laptop, in a batch with #4394 and #4395. PC: RTX 3070 Laptop 8 GB, i7-11800H, 32 GB RAM, Windows 11. Series S, 1080p output, DLSS Performance, FG x4. prep medians across sessions: ~46 ms before this batch, ~44-45 ms after; fps median 17.4 to 17.8 in light scenes. Within scene noise individually, but the direction held across three separate sessions. No instability, no crashes attributable to it (unlike #4944, which I reported separately).
github-bot closed this pull request 2026-12-01 18:50:58 +00:00
Author
Owner

@⁠trustno1-coder on GitHub · 2026-12-01 18:47 UTC · original

Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

**@⁠trustno1-coder** on GitHub · 2026-12-01 18:47 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3943#issuecomment-7321560962) Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

Pull request closed

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/trustno1-coder--VIctory-NVIDIA!3943
No description provided.