Shadow: support nested wide_each_scoped fan-out for Shadow::take_all #4488

Open
Bazovsky wants to merge 1 commit from upstream-pr/4488 into main
Bazovsky commented 2026-11-30 21:07:21 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Bazovsky on 2026-11-30T21:07:21Z.


Summary

Restores real multi-threaded fan-out for Shadow::take_all (>= 1 MiB shadow buffer copies) when invoked inside batch refresh passes (Batch::refresh_front_spans, Batch::refresh_ahead, and Batch::flush_all), and promotes the single largest dirty buffer span to the front of each batch so large copies begin fanning out immediately instead of tail-straggling.

Problem

  1. Commit 27d312d3 introduced wide_each_scoped with a single q.scoped: Option<ScopedTask> slot (silently falling back to single-threaded inline execution whenever q.scoped is occupied) and switched Batch::refresh_front_spans, Batch::refresh_ahead, and Batch::flush_all onto it.
  2. Commit f37bf099 later switched Shadow::take_all (>= 1 MiB copies split into 256 KiB parts) from wide_each to wide_each_scoped to eliminate per-call Box allocations.
  3. Because every batch refresh pass already occupies the single q.scoped slot, Shadow::take_all silently serialized back onto a single thread during batch refreshes — leaving idle victory-wide helpers and the waiting outer submitting thread blocked while one thread copied 4–14+ MiB sequentially.

Changes

  • Multi-slot scoped pool (crates/victory-gpu/src/shadow.rs):
    • Replace the single Option<ScopedTask> with a fixed MAX_SCOPED = 16 (const _: () = assert!(MAX_SCOPED <= 32)) array of slots, a scoped_ready: AtomicU32 hint bitmask, cache-line-aligned per-slot ScopedAtomics (#[repr(align(64))]), and cache-line-isolated busy_at: AlignedU64.
    • Track enclosing active scoped slots per thread in SCOPED_ANCESTORS: Cell<u32> (AncestorsGuard, reset to 0 around foreign jobs popped in Beside::drop) and maintain each ancestor's active descendants in ScopedAtomics::children: AtomicU32.
    • Idle victory-wide workers pick the deepest runnable slot first (ancestors.count_ones()). Waiting outer owners steal only strict descendants of their own slot ((task.ancestors & (1 << slot)) != 0), never unrelated concurrent callers — guaranteeing deadlock freedom (strict ancestor DAG) and avoiding cross-caller latency inversion or lock/TLS hazards.
    • Move slot join/cleanup (running == 0, clearing ancestor children bits, clearing q.scoped[slot]) into SlotGuard::drop and helper exit/panic cleanup into HelperGuard::drop, so the erased closure lifetime is structurally panic-safe and helper panics abort remaining chunks early.
    • Add spin_for(wait_spin(), ...) to wide_each_scoped (matching Beside::drop), masked strictly to running == 0 || (scoped_ready & children) != 0 so unrelated concurrent callers do not defeat VICTORY_WAIT_SPIN.
  • Promote largest dirty buffer span (crates/victory-gpu/src/host.rs):
    • Call largest_first (O(n) promote-one-to-front, preserving relative order of all other items) on dirty buffer spans in Batch::refresh_front_spans, Batch::refresh_ahead, and Batch::flush_all (!check_noop), and update largest_first's doc comment.
  • Deterministic tests (crates/victory-gpu/src/shadow.rs):
    • Rendezvous-based parallel nested fan-out test (nested_wide_each_scoped_fans_out_in_parallel).
    • Waiting-owner own-descendant stealing + foreign-caller isolation test (waiting_owner_steals_own_descendant_and_not_foreign_caller).
    • Overflow fallback observation (MAX_SCOPED + 4), concurrent top-level callers, deterministic helper panic propagation, deterministic owner panic with active-helper join verification, and non-vacuous slot-leak checks (nested_wide_each_scoped_handles_overflow_concurrency_and_panics).

Verification

Unit tests & lint (Windows MSVC target via cargo xwin, native Windows execution)

  • cargo xwin clippy --target x86_64-pc-windows-msvc --locked --no-deps -p victory-gpu --all-targets: 0 new warnings.
  • shadow::wide_tests --test-threads=8: 9/9 passed across repeated consecutive stress runs (0.05 s each).
  • host::promote_one_refresh_preserves_ties_and_all_other_jobs: passed.
  • watch::tests:: --test-threads=1: 43/43 passed.

Live A/B benchmark (GTA 6, 1440p DLSS Quality + FG 2x, RTX 5090 + Ryzen 9 9950X3D)

4 × 125 s ABBA runs (main_1, pr_1, pr_2, main_2) comparing pure origin/main (e1251c31) vs this PR with identical release flags (-C target-cpu=znver5) and VICTORY_SUBMISSION_TIMING=1:

Metric origin/main (A1 / A2 -> mean) This PR (B1 / B2 -> mean) Delta
Worst submit: refreshes ahead of writes spike 192.8 / 201.0 -> 196.9 ms 158.1 / 161.2 -> 159.7 ms -18.9%
Slow host submissions (full run count) 389 / 353 -> 371.0 340 / 351 -> 345.5 -6.9%
Slow host submissions (total time) 5,940 / 5,565 -> 5,752 ms 5,250 / 5,616 -> 5,433 ms -5.5%
submit: the lists' steps in slow subs 615 / 484 -> 549.5 ms 475 / 504 -> 489.3 ms -11.0%
refreshes per frame (gameplay t>=45s) 9.06 / 8.92 -> 8.99 ms 8.79 / 8.83 -> 8.81 ms -2.0%
Smooth gameplay seconds (<50 ms, t>=45s) 64/80, 63/80 -> 79.4% 67/81, 65/80 -> 82.0% +2.6 pp

Every key metric improved across both ABBA pairs with zero overlap between baseline and PR runs.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5573770718 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4488). Originally posted by [Bazovsky](https://github.com/Bazovsky) on 2026-11-30T21:07:21Z. --- ## Summary Restores real multi-threaded fan-out for `Shadow::take_all` (`>= 1 MiB` shadow buffer copies) when invoked inside batch refresh passes (`Batch::refresh_front_spans`, `Batch::refresh_ahead`, and `Batch::flush_all`), and promotes the single largest dirty buffer span to the front of each batch so large copies begin fanning out immediately instead of tail-straggling. ## Problem 1. Commit `27d312d3` introduced `wide_each_scoped` with a single `q.scoped: Option<ScopedTask>` slot (silently falling back to single-threaded inline execution whenever `q.scoped` is occupied) and switched `Batch::refresh_front_spans`, `Batch::refresh_ahead`, and `Batch::flush_all` onto it. 2. Commit `f37bf099` later switched `Shadow::take_all` (`>= 1 MiB` copies split into `256 KiB` parts) from `wide_each` to `wide_each_scoped` to eliminate per-call `Box` allocations. 3. Because every batch refresh pass already occupies the single `q.scoped` slot, `Shadow::take_all` silently serialized back onto a single thread during batch refreshes — leaving idle `victory-wide` helpers and the waiting outer submitting thread blocked while one thread copied 4–14+ MiB sequentially. ## Changes - **Multi-slot scoped pool (`crates/victory-gpu/src/shadow.rs`):** - Replace the single `Option<ScopedTask>` with a fixed `MAX_SCOPED = 16` (`const _: () = assert!(MAX_SCOPED <= 32)`) array of slots, a `scoped_ready: AtomicU32` hint bitmask, cache-line-aligned per-slot `ScopedAtomics` (`#[repr(align(64))]`), and cache-line-isolated `busy_at: AlignedU64`. - Track enclosing active scoped slots per thread in `SCOPED_ANCESTORS: Cell<u32>` (`AncestorsGuard`, reset to `0` around foreign jobs popped in `Beside::drop`) and maintain each ancestor's active descendants in `ScopedAtomics::children: AtomicU32`. - Idle `victory-wide` workers pick the deepest runnable slot first (`ancestors.count_ones()`). Waiting outer owners steal **only strict descendants of their own slot** (`(task.ancestors & (1 << slot)) != 0`), never unrelated concurrent callers — guaranteeing deadlock freedom (strict ancestor DAG) and avoiding cross-caller latency inversion or lock/TLS hazards. - Move slot join/cleanup (`running == 0`, clearing ancestor `children` bits, clearing `q.scoped[slot]`) into `SlotGuard::drop` and helper exit/panic cleanup into `HelperGuard::drop`, so the erased closure lifetime is structurally panic-safe and helper panics abort remaining chunks early. - Add `spin_for(wait_spin(), ...)` to `wide_each_scoped` (matching `Beside::drop`), masked strictly to `running == 0 || (scoped_ready & children) != 0` so unrelated concurrent callers do not defeat `VICTORY_WAIT_SPIN`. - **Promote largest dirty buffer span (`crates/victory-gpu/src/host.rs`):** - Call `largest_first` (O(n) promote-one-to-front, preserving relative order of all other items) on dirty buffer spans in `Batch::refresh_front_spans`, `Batch::refresh_ahead`, and `Batch::flush_all` (`!check_noop`), and update `largest_first`'s doc comment. - **Deterministic tests (`crates/victory-gpu/src/shadow.rs`):** - Rendezvous-based parallel nested fan-out test (`nested_wide_each_scoped_fans_out_in_parallel`). - Waiting-owner own-descendant stealing + foreign-caller isolation test (`waiting_owner_steals_own_descendant_and_not_foreign_caller`). - Overflow fallback observation (`MAX_SCOPED + 4`), concurrent top-level callers, deterministic helper panic propagation, deterministic owner panic with active-helper join verification, and non-vacuous slot-leak checks (`nested_wide_each_scoped_handles_overflow_concurrency_and_panics`). ## Verification ### Unit tests & lint (Windows MSVC target via `cargo xwin`, native Windows execution) - `cargo xwin clippy --target x86_64-pc-windows-msvc --locked --no-deps -p victory-gpu --all-targets`: 0 new warnings. - `shadow::wide_tests --test-threads=8`: 9/9 passed across repeated consecutive stress runs (0.05 s each). - `host::promote_one_refresh_preserves_ties_and_all_other_jobs`: passed. - `watch::tests:: --test-threads=1`: 43/43 passed. ### Live A/B benchmark (GTA 6, 1440p DLSS Quality + FG 2x, RTX 5090 + Ryzen 9 9950X3D) 4 × 125 s **ABBA** runs (`main_1`, `pr_1`, `pr_2`, `main_2`) comparing pure `origin/main` (`e1251c31`) vs this PR with identical release flags (`-C target-cpu=znver5`) and `VICTORY_SUBMISSION_TIMING=1`: | Metric | `origin/main` (A1 / A2 -> mean) | This PR (B1 / B2 -> mean) | Delta | | --- | --- | --- | --- | | Worst `submit: refreshes ahead of writes` spike | 192.8 / 201.0 -> **196.9 ms** | 158.1 / 161.2 -> **159.7 ms** | **-18.9%** | | Slow host submissions (full run count) | 389 / 353 -> **371.0** | 340 / 351 -> **345.5** | **-6.9%** | | Slow host submissions (total time) | 5,940 / 5,565 -> **5,752 ms** | 5,250 / 5,616 -> **5,433 ms** | **-5.5%** | | `submit: the lists' steps` in slow subs | 615 / 484 -> **549.5 ms** | 475 / 504 -> **489.3 ms** | **-11.0%** | | `refreshes` per frame (gameplay `t>=45s`) | 9.06 / 8.92 -> **8.99 ms** | 8.79 / 8.83 -> **8.81 ms** | **-2.0%** | | Smooth gameplay seconds (`<50 ms`, `t>=45s`) | 64/80, 63/80 -> **79.4%** | 67/81, 65/80 -> **82.0%** | **+2.6 pp** | Every key metric improved across both ABBA pairs with zero overlap between baseline and PR runs.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/4488:upstream-pr/4488'
git switch 'upstream-pr/4488'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/4488'
git switch 'upstream-pr/4488'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/4488'
git switch 'upstream-pr/4488'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/4488'
git switch 'main'
git merge --squash 'upstream-pr/4488'
git switch 'main'
git merge --ff-only 'upstream-pr/4488'
git switch 'main'
git merge 'upstream-pr/4488'
git push origin 'main'
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!4488
No description provided.