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

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

@⁠Bazovsky on GitHub · 2026-11-30 21:07 UTC · original
Bazovsky:pr/nested-wide-scoped → main · +676 −95 · 2 files · 1 commits · open

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.

**@⁠Bazovsky** on GitHub · 2026-11-30 21:07 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4488) `Bazovsky:pr/nested-wide-scoped` → `main` · +676 −95 · 2 files · 1 commits · open ## 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.
Commit 27d312d3 introduced `wide_each_scoped` with a single `q.scoped`
slot (falling back to single-threaded inline execution whenever occupied)
and switched `Batch::refresh_front_spans`, `Batch::refresh_ahead`, and
`Batch::flush_all` to it. Commit f37bf099 later switched `Shadow::take_all`
(>= 1 MiB shadow copies) from `wide_each` to `wide_each_scoped` to drop
per-call `Box` allocations, which inadvertently serialized `Shadow::take_all`
whenever invoked from those batch passes because the outer pass already held
the sole slot.

Changes:
- Replace the single `Option<ScopedTask>` slot in `Workers` with a fixed
  `MAX_SCOPED = 16` (`<= 32`, checked at compile time) array of scoped slots,
  a `scoped_ready: AtomicU32` hint bitmask, cache-line-aligned per-slot
  `ScopedAtomics`, and cache-line-isolated `busy_at`.
- Track enclosing active slots per thread in `SCOPED_ANCESTORS` (`u32` mask,
  reset to 0 around foreign jobs popped in `Beside::drop`) and record active
  descendant slots in each ancestor's `children: AtomicU32`. Idle workers
  pick the deepest runnable slot first (`ancestors.count_ones()`), and
  waiting outer owners steal only strict descendants of their own slot
  (never unrelated concurrent callers). Because every bit in `ancestors`
  stays live until all helpers of that slot exit, the wait/steal graph is a
  strict DAG with no cycles or cross-caller lock/TLS hazards.
- Move slot join/cleanup (`running == 0`, clearing ancestor `children` bits,
  and clearing `q.scoped[slot]`) into `SlotGuard::drop` and helper exit
  handling into `HelperGuard::drop`, making the erased closure lifetime
  structurally panic-safe and aborting remaining chunks on helper panic.
- Add `spin_for(wait_spin(), ...)` to `wide_each_scoped` (matching
  `Beside::drop`), masked strictly to the owner's own `running` / `children`
  state so unrelated concurrent callers do not defeat `VICTORY_WAIT_SPIN`.
- Promote the single largest dirty buffer span to index 0 via `largest_first`
  in `Batch::refresh_front_spans`, `Batch::refresh_ahead`, and
  `Batch::flush_all` (`!check_noop`) so >= 1 MiB copies begin and fan out
  immediately instead of trailing the batch.
- Add deterministic rendezvous/concurrency/overflow/panic/foreign-isolation
  unit tests in `shadow::wide_tests`.

Measured in GTA 6 (1440p DLSS Quality + FG 2x, RTX 5090 + Ryzen 9 9950X3D,
4x 125 s ABBA runs against `origin/main` e1251c31 with
`VICTORY_SUBMISSION_TIMING=1`, identical release flags):
- Worst `submit: refreshes ahead of writes` spike:
  196.9 ms (192.8 / 201.0) -> 159.7 ms (158.1 / 161.2) (-18.9%)
- Slow host submissions (full run):
  371.0 (5,752 ms) -> 345.5 (5,433 ms) (-6.9% count, -5.5% time)
- `refreshes` per frame (steady gameplay t>=45s):
  8.99 ms (9.06 / 8.92) -> 8.81 ms (8.79 / 8.83) (-2.0%)
- Smooth gameplay seconds (<50 ms, t>=45s):
  79.4% (64/80, 63/80) -> 82.0% (67/81, 65/80)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 21:10:42 +00:00
github-bot force-pushed github-bot/gh-4488 from c9e662d5c1 to 40b35c69bc 2026-12-01 10:36:14 +00:00 Compare
github-bot changed title from perf(gpu): support nested wide_each_scoped fan-out for Shadow::take_all to Shadow: support nested wide_each_scoped fan-out for Shadow::take_all 2026-12-01 18:18:58 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
The changes on this branch are already on the target branch. This will be an empty commit.
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/4489/head:github-bot/gh-4488'
git switch 'github-bot/gh-4488'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-4488'
git switch 'github-bot/gh-4488'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-4488'
git switch 'github-bot/gh-4488'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-4488'
git switch 'main'
git merge --squash 'github-bot/gh-4488'
git switch 'main'
git merge --ff-only 'github-bot/gh-4488'
git switch 'main'
git merge 'github-bot/gh-4488'
git push origin 'main'
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!4489
No description provided.