perf(shadow): lock-free pending check, merge ranged under copied lock, chunk wide_each #1243

Closed
qlfu wants to merge 1 commit from upstream-pr/1243 into main
qlfu commented 2026-11-28 16:16:01 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by qlfu on 2026-11-28T16:16:01Z.


Summary

Reduces lock acquisitions, cross-thread scheduling overhead, and atomic contention in crates/victory-gpu/src/shadow.rs:

  1. Merge ranged under Shadow::copied (CopiedState):
    • Combined copied: Mutex<Vec<u64>> and ranged: Mutex<[(usize, usize, u64); 2]> into a single Mutex<CopiedState>, eliminating the extra lock acquisitions per nothing_new() and copy_stale() call while preserving the same lock ordering.
  2. Lock-free has_pending: AtomicBool mirror:
    • Avoids locking Shadow::pending on read/refresh paths (writes_pending, writes_pending_in, stale_under_writes, merge_under_writes, update, nothing_new, send_copied) in the common case when no shader writes are pending (Release store inside pending mutex on writing/copy_back_done, Acquire load before locking).
  3. wide() & wide_each() overhead reduction:
    • Replaced Arc<(Mutex<usize>, Condvar)> completion decrement with an AtomicUsize counter (notifying the Condvar only when fetch_sub(1, AcqRel) == 1).
    • Added n <= 1 fast-path (no Arc/Box/pool wakeups for 0 or 1 item) and adaptive index chunking (1..=32 when n > max_threads * 4) in wide_each().
  4. Unit tests added:
    • wide_each_covers_every_index_once_across_chunk_boundaries
    • copied_state_ranged_selects_latest_containing_interval

Verification

  • cargo clippy -p victory-gpu --all-targets --locked and --all-features --locked: 0 warnings/errors in modified lines (only pre-existing upstream shadow.rs:1720 turns % 16 == 0 present on main).
  • cargo test -p victory-gpu --lib --locked -- --test-threads=1: 456 passed, 0 failed, 36 ignored.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5542167482 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1243). Originally posted by [qlfu](https://github.com/qlfu) on 2026-11-28T16:16:01Z. --- ### Summary Reduces lock acquisitions, cross-thread scheduling overhead, and atomic contention in `crates/victory-gpu/src/shadow.rs`: 1. **Merge `ranged` under `Shadow::copied` (`CopiedState`)**: - Combined `copied: Mutex<Vec<u64>>` and `ranged: Mutex<[(usize, usize, u64); 2]>` into a single `Mutex<CopiedState>`, eliminating the extra lock acquisitions per `nothing_new()` and `copy_stale()` call while preserving the same lock ordering. 2. **Lock-free `has_pending: AtomicBool` mirror**: - Avoids locking `Shadow::pending` on read/refresh paths (`writes_pending`, `writes_pending_in`, `stale_under_writes`, `merge_under_writes`, `update`, `nothing_new`, `send_copied`) in the common case when no shader writes are pending (`Release` store inside `pending` mutex on `writing`/`copy_back_done`, `Acquire` load before locking). 3. **`wide()` & `wide_each()` overhead reduction**: - Replaced `Arc<(Mutex<usize>, Condvar)>` completion decrement with an `AtomicUsize` counter (notifying the `Condvar` only when `fetch_sub(1, AcqRel) == 1`). - Added `n <= 1` fast-path (no `Arc`/`Box`/pool wakeups for 0 or 1 item) and adaptive index chunking (`1..=32` when `n > max_threads * 4`) in `wide_each()`. 4. **Unit tests added**: - `wide_each_covers_every_index_once_across_chunk_boundaries` - `copied_state_ranged_selects_latest_containing_interval` ### Verification - `cargo clippy -p victory-gpu --all-targets --locked` and `--all-features --locked`: 0 warnings/errors in modified lines (only pre-existing upstream `shadow.rs:1720` `turns % 16 == 0` present on `main`). - `cargo test -p victory-gpu --lib --locked -- --test-threads=1`: **456 passed, 0 failed, 36 ignored**.
Bj0rnis commented 2026-11-28 17:11:15 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Bj0rnis on 2026-11-28T17:11:15Z.


Verified 9f53f3aa on RTX 5070 Ti 16 GB (Blackwell, driver 621.24) + Ryzen 7 9800X3D, 32 GB RAM, Windows 11 (Rust 1.98.1).

Automated checks & code audit

  • git merge-tree vs origin/main (9ce3cf9): clean.
  • Concurrency audit (shadow.rs): verified the AtomicUsize + Condvar completion gate in wide() acquires done.wait.0 before notify_all() (no lost-wakeup deadlock), and all pending mutation paths update has_pending with Release/Acquire inside the pending lock.
  • cargo test --locked -p victory-gpu --features diagnostics --lib (RTX 5070 Ti): 509 passed, 0 failed, 36 ignored (including both new wide_each_covers_every_index_once_across_chunk_boundaries and copied_state_ranged_selects_latest_containing_interval tests).
  • cargo clippy --locked -p victory-gpu --tests: 0 new warnings (only pre-existing shadow.rs:1720 manual_is_multiple_of).
  • cargo build --release --locked -p victory: succeeded cleanly.

Live gameplay (--release, Series X, 2560×1440, DLSS Quality, FG off, RT off, ~5 min)

  • Stability: 0 crashes, 0 OOM, 0 hangs/deadlocks in wide() / wide_each() across intro + heavy indoor room (~10.1k draws).
  • Telemetry (35 in-world ticks, draws >= 3000, warm cache):
    • In-world (~5.5k median draws): 26.9 fps median (34.2 peak); settled break room F3 (10,096 draws): 29.5 fps (33.9 ms, longest 36 ms, SUBMIT BUSY 28.3 ms, GPU IDLE BEHIND US 7.6 ms, COPY BACK 5.7 ms).
    • landing merge median: 1.69 ms (vs 1.78–1.82 ms in earlier non-RT runs on this rig); submit-thread preparing 34.1 ms / refreshes 8.8 ms; frame times 34.3 ms med / 50.2 ms p95 / 82.7 ms p99 (61 frames >100 ms). Overall FPS/CPU delta vs earlier main runs was within normal run-to-run variance on a 9800X3D, though pacing felt smooth with noticeably fewer big hitches.
  • Visuals: Clean overall; noticed a brief transient metallic/glittering artifact on an NPC guard's lower arms/hands that resolved itself (likely unrelated intermittent shader/sync behavior, noting for completeness).

Not tested

  • Strict back-to-back same-save A/B against main 9ce3cf9 (compared against earlier v2026.11.28.01 runs), RT-on (VICTORY_BVH_BUILDER=1), or WARP / lower-core-count CPUs where lock contention may show a larger win. (cargo xtask policy hit the pre-existing bindings.ini failure on main.)

AI-assisted test run and summary.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7242821492 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1243#issuecomment-7242821492). Originally posted by [Bj0rnis](https://github.com/Bj0rnis) on 2026-11-28T17:11:15Z. --- Verified **`9f53f3aa`** on **RTX 5070 Ti 16 GB** (Blackwell, driver `621.24`) + **Ryzen 7 9800X3D**, 32 GB RAM, Windows 11 (`Rust 1.98.1`). ### Automated checks & code audit - **`git merge-tree` vs `origin/main` (`9ce3cf9`):** clean. - **Concurrency audit (`shadow.rs`):** verified the `AtomicUsize` + `Condvar` completion gate in `wide()` acquires `done.wait.0` before `notify_all()` (no lost-wakeup deadlock), and all `pending` mutation paths update `has_pending` with `Release`/`Acquire` inside the `pending` lock. - **`cargo test --locked -p victory-gpu --features diagnostics --lib` (RTX 5070 Ti):** **509 passed, 0 failed, 36 ignored** (including both new `wide_each_covers_every_index_once_across_chunk_boundaries` and `copied_state_ranged_selects_latest_containing_interval` tests). - **`cargo clippy --locked -p victory-gpu --tests`:** **0 new warnings** (only pre-existing `shadow.rs:1720` `manual_is_multiple_of`). - **`cargo build --release --locked -p victory`:** succeeded cleanly. ### Live gameplay (`--release`, Series X, `2560×1440`, DLSS Quality, FG off, RT off, ~5 min) - **Stability:** **0 crashes, 0 OOM, 0 hangs/deadlocks** in `wide()` / `wide_each()` across intro + heavy indoor room (~10.1k draws). - **Telemetry (35 in-world ticks, `draws >= 3000`, warm cache):** - In-world (`~5.5k` median draws): **26.9 fps** median (`34.2` peak); settled break room F3 (`10,096` draws): **29.5 fps** (`33.9 ms`, `longest 36 ms`, `SUBMIT BUSY 28.3 ms`, `GPU IDLE BEHIND US 7.6 ms`, `COPY BACK 5.7 ms`). - `landing` merge median: **`1.69 ms`** (vs `1.78–1.82 ms` in earlier non-RT runs on this rig); submit-thread `preparing` `34.1 ms` / `refreshes` `8.8 ms`; frame times `34.3 ms` med / `50.2 ms` p95 / `82.7 ms` p99 (`61` frames `>100 ms`). Overall FPS/CPU delta vs earlier `main` runs was within normal run-to-run variance on a 9800X3D, though pacing felt smooth with noticeably fewer big hitches. - **Visuals:** Clean overall; noticed a brief transient metallic/glittering artifact on an NPC guard's lower arms/hands that resolved itself (likely unrelated intermittent shader/sync behavior, noting for completeness). ### Not tested - Strict back-to-back same-save A/B against `main` `9ce3cf9` (compared against earlier `v2026.11.28.01` runs), RT-on (`VICTORY_BVH_BUILDER=1`), or WARP / lower-core-count CPUs where lock contention may show a larger win. (`cargo xtask policy` hit the pre-existing `bindings.ini` failure on `main`.) *AI-assisted test run and summary.*
trustno1-coder commented 2026-11-28 19:23:02 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-11-28T19:23:02Z.


Thanks for this. I'm closing it without merging because it conflicts with the current main in several places (main has moved a lot since it was opened, with about 100 pull requests merged today), and I can't resolve that safely on your behalf. If you'd still like it in, please rebase it on the current main and open a new pull request.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7244502864 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1243#issuecomment-7244502864). Originally posted by [trustno1-coder](https://github.com/trustno1-coder) on 2026-11-28T19:23:02Z. --- Thanks for this. I'm closing it without merging because it conflicts with the current main in several places (main has moved a lot since it was opened, with about 100 pull requests merged today), and I can't resolve that safely on your behalf. If you'd still like it in, please rebase it on the current main and open a new pull request.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
trustno1-coder/VIctory-NVIDIA!1243
No description provided.