victory-gpu: keep heap buffer refresh spans disjoint and guard nested wide jobs #630

Open
github-bot wants to merge 1 commit from github-bot/gh-630 into a-publier AGit
Owner

@⁠zaixiakongyiji on GitHub · 2026-11-27 15:13 UTC · original
zaixiakongyiji:pr/heap-disjoint-refresh-spans → a-publier · +1164 −218 · 3 files · 1 commits · open

Problem

  1. Multi-hundred-MB unmapped-hole refresh scanning (draw.rs, host.rs):
    • HeapRanges / BufferSweep / heap_buffer_pass flattened all descriptor-heap buffer views targeting the same Shadow into one (min, max) envelope, and host.rs (front_spans, Port 6 ListFront, written_spans, Batch::add, watch::ask_once, stale_under_flights) re-flattened per-copy ranges to (min, max) as well.
    • When a large buffer (e.g. a 174 MiB heap buffer) has two small views near its start and end, every submission scanned ~174 MiB of untouched/unmapped middle pages (~1.67 GB/frame of wasted span coverage in city scenes).
  2. Same-Shadow worker contention & nested wide_each re-entrant self-deadlock (host.rs, shadow.rs):
    • Once a single Shadow produces multiple disjoint refresh spans, dispatching flat spans across wide_each / wide_run lets multiple workers contend on Shadow::copied, and if Shadow::copy_stale (while holding self.copied) enters take_all -> wide_each (>= 1 MiB dirty run), wide_each pops another outer job from WORKERS.jobs on the same thread and deadlocks trying to re-lock self.copied.

Changes

  1. crates/victory-gpu/src/draw.rs:
    • Track ref-counted disjoint sub-intervals per Shadow (MERGE_GAP_BYTES = 256 KiB, MAX_INTERVALS_PER_COPY = 16 merging smallest adjacent gaps first) in HeapRanges / BufferSweep.
    • Maintain per-copy slots: HashSet<u32> inside ShadowIntervals so dead-Shadow eviction (Arc::strong_count == 1) removes and rescans only its k slots in O(copies + k) instead of scanning the full heap.
  2. crates/victory-gpu/src/host.rs:
    • Preserve 256 KiB (4 * 64 KiB guard/write-watch pages) disjoint sub-intervals across stale_under_flights, watch::ask_once, front_spans, Renderer Port #6's ListFront (fold_touches, join_keys, join_fronts), written_spans, Batch::add / flush_one, and exact (gap = 0) Batch::ahead coverage.
    • Group refresh spans by Shadow (group_spans_by_shadow) before parallel dispatch in Batch::refresh_front (both default and VICTORY_PORT_GUARD=1 paths), Batch::refresh_ahead, and Batch::flush_all so each worker processes a distinct Shadow.
  3. crates/victory-gpu/src/shadow.rs:
    • Add thread-local IN_WIDE + RAII WideDepthGuard in workers(), wide(), and wide_each() so nested wide / wide_each calls execute inline without stealing outer jobs from WORKERS.jobs.

Verification

  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test -p victory-gpu --locked & cargo test -p victory-gpu --features diagnostics --locked: 451 + 1 passed, 0 failed (6 unit tests covering 174 MiB hole exclusion, O(k) dead-copy slot eviction, Port #6 fold_touches/join_keys hole preservation, exact Batch::ahead middle-hole coverage, group_spans_by_shadow, and nested wide_each under a non-recursive Mutex).
**@⁠zaixiakongyiji** on GitHub · 2026-11-27 15:13 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/630) `zaixiakongyiji:pr/heap-disjoint-refresh-spans` → `a-publier` · +1164 −218 · 3 files · 1 commits · open ### Problem 1. **Multi-hundred-MB unmapped-hole refresh scanning (`draw.rs`, `host.rs`)**: - `HeapRanges` / `BufferSweep` / `heap_buffer_pass` flattened all descriptor-heap buffer views targeting the same `Shadow` into one `(min, max)` envelope, and `host.rs` (`front_spans`, Port 6 `ListFront`, `written_spans`, `Batch::add`, `watch::ask_once`, `stale_under_flights`) re-flattened per-copy ranges to `(min, max)` as well. - When a large buffer (e.g. a 174 MiB heap buffer) has two small views near its start and end, every submission scanned ~174 MiB of untouched/unmapped middle pages (~1.67 GB/frame of wasted span coverage in city scenes). 2. **Same-`Shadow` worker contention & nested `wide_each` re-entrant self-deadlock (`host.rs`, `shadow.rs`)**: - Once a single `Shadow` produces multiple disjoint refresh spans, dispatching flat spans across `wide_each` / `wide_run` lets multiple workers contend on `Shadow::copied`, and if `Shadow::copy_stale` (while holding `self.copied`) enters `take_all -> wide_each` (>= 1 MiB dirty run), `wide_each` pops another outer job from `WORKERS.jobs` on the same thread and deadlocks trying to re-lock `self.copied`. ### Changes 1. **`crates/victory-gpu/src/draw.rs`**: - Track ref-counted disjoint sub-intervals per `Shadow` (`MERGE_GAP_BYTES = 256 KiB`, `MAX_INTERVALS_PER_COPY = 16` merging smallest adjacent gaps first) in `HeapRanges` / `BufferSweep`. - Maintain per-copy `slots: HashSet<u32>` inside `ShadowIntervals` so dead-`Shadow` eviction (`Arc::strong_count == 1`) removes and rescans only its `k` slots in `O(copies + k)` instead of scanning the full heap. 2. **`crates/victory-gpu/src/host.rs`**: - Preserve `256 KiB` (`4 * 64 KiB` guard/write-watch pages) disjoint sub-intervals across `stale_under_flights`, `watch::ask_once`, `front_spans`, Renderer Port #6's `ListFront` (`fold_touches`, `join_keys`, `join_fronts`), `written_spans`, `Batch::add` / `flush_one`, and exact (`gap = 0`) `Batch::ahead` coverage. - Group refresh spans by `Shadow` (`group_spans_by_shadow`) before parallel dispatch in `Batch::refresh_front` (both default and `VICTORY_PORT_GUARD=1` paths), `Batch::refresh_ahead`, and `Batch::flush_all` so each worker processes a distinct `Shadow`. 3. **`crates/victory-gpu/src/shadow.rs`**: - Add thread-local `IN_WIDE` + RAII `WideDepthGuard` in `workers()`, `wide()`, and `wide_each()` so nested `wide` / `wide_each` calls execute inline without stealing outer jobs from `WORKERS.jobs`. ### Verification - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean - `cargo test -p victory-gpu --locked` & `cargo test -p victory-gpu --features diagnostics --locked`: 451 + 1 passed, 0 failed (6 unit tests covering 174 MiB hole exclusion, `O(k)` dead-copy slot eviction, Port #6 `fold_touches`/`join_keys` hole preservation, exact `Batch::ahead` middle-hole coverage, `group_spans_by_shadow`, and nested `wide_each` under a non-recursive `Mutex`).
- Track per-Shadow disjoint sub-intervals (MERGE_GAP_BYTES = 256 KiB,
  capped at 16 intervals per copy) in HeapRanges / BufferSweep instead of
  flattening all views of a copy to a single (min, max) envelope, avoiding
  unmapped-hole scanning on 174 MiB buffers with sparse endpoint views.
  Keep per-copy slot index sets in ShadowIntervals so dead-Shadow eviction
  removes only its k slots in O(copies + k) without scanning the full heap.
- Preserve 256 KiB disjoint refresh sub-intervals end-to-end across
  host.rs: stale_under_flights, watch::ask_once, front_spans, renderer
  port #6's ListFront (fold_touches / join_keys / join_fronts),
  written_spans, Batch::add / flush_one, and exact Batch::ahead tracking.
- Group pending refresh sub-intervals by Shadow (group_spans_by_shadow)
  before parallel dispatch in Batch::refresh_front (both default and
  VICTORY_PORT_GUARD=1 paths), Batch::refresh_ahead, and Batch::flush_all
  so every worker owns a distinct copy without contending on Shadow::copied.
- Add thread-local IN_WIDE + RAII WideDepthGuard in shadow.rs (workers,
  wide, wide_each) so nested wide / wide_each calls (e.g. copy_stale ->
  take_all while holding Shadow::copied) run inline instead of stealing
  outer jobs from WORKERS.jobs and self-deadlocking.
- Add unit tests for 174 MiB hole skipping, O(k) dead-copy slot eviction,
  port #6 disjoint fold/join equivalence, group_spans_by_shadow, and nested
  wide_each under a non-recursive Mutex.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 15:14:18 +00:00
Author
Owner

@⁠gw1337x on GitHub · 2026-11-27 17:13 UTC · original

In-game numbers for e1a3cf8 on a card-bound scene. Memory Integrity was off.

PC: Ryzen 7 9800X3D, RX 7800 XT, 32 GB. Test: 270 s hands-off boat scene, RT off, Belobug launcher settings plus the 5 port switches, warm shader caches. #630 on 4eb6cfd vs 4eb6cfd; the base runs were taken before and between the PR runs in the same session.

base 1 base 2 base 3 base 4 #630 1 #630 2
Steady fps (log) 28.5 28.7 27.9 28.6 28.3 28.4
Card running per frame at 200 s 31.4 ms 32.0 31.7 33.1 31.9 32.4
Frames over 50 ms (of 450, 100-240 s) 73 38 69 92 63 53
Submitting thread: the lists' steps 11.0 ms 10.5 10.3 11.0 13.7 14.0
of those, refreshes 6.1 ms 5.9 5.7 6.4 6.6 6.8
Submitting thread: asking the write watch 3.9 ms 3.7 3.8 3.8 4.4 4.3

The three submitting-thread rows are the median of every preparing per frame line once the world is on screen (about 220 lines per run).

What this shows here:

  • Frame rate and stability are the same. This scene is limited by the card (about 31-32 ms of card time a frame), so a CPU-side change can't show as fps.
  • The disjoint spans do skip memory: the last buffer views of the heap made ready line reads spans=5027 ... holes_mb=420 and spans=4888 ... holes_mb=508 (copies 4384 and 4203).
  • On the submitting thread the lists' steps take about 3 ms a frame more with the PR (13.7-14.0 vs 10.3-11.0 ms), with refreshes and the write-watch asking each a little higher. I have not measured which part of the change costs it.

Not tested: the city scenes the description is about. I don't have a save there, so I can't say whether the saving on wide envelopes outweighs this cost in them.

**@⁠gw1337x** on GitHub · 2026-11-27 17:13 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/630#issuecomment-7224165845) In-game numbers for `e1a3cf8` on a card-bound scene. Memory Integrity was off. PC: Ryzen 7 9800X3D, RX 7800 XT, 32 GB. Test: 270 s hands-off boat scene, RT off, Belobug launcher settings plus the 5 port switches, warm shader caches. #630 on `4eb6cfd` vs `4eb6cfd`; the base runs were taken before and between the PR runs in the same session. | | base 1 | base 2 | base 3 | base 4 | #630 1 | #630 2 | |---|---|---|---|---|---|---| | Steady fps (log) | 28.5 | 28.7 | 27.9 | 28.6 | 28.3 | 28.4 | | Card running per frame at 200 s | 31.4 ms | 32.0 | 31.7 | 33.1 | 31.9 | 32.4 | | Frames over 50 ms (of 450, 100-240 s) | 73 | 38 | 69 | 92 | 63 | 53 | | Submitting thread: the lists' steps | 11.0 ms | 10.5 | 10.3 | 11.0 | 13.7 | 14.0 | | of those, refreshes | 6.1 ms | 5.9 | 5.7 | 6.4 | 6.6 | 6.8 | | Submitting thread: asking the write watch | 3.9 ms | 3.7 | 3.8 | 3.8 | 4.4 | 4.3 | The three submitting-thread rows are the median of every `preparing per frame` line once the world is on screen (about 220 lines per run). What this shows here: - Frame rate and stability are the same. This scene is limited by the card (about 31-32 ms of card time a frame), so a CPU-side change can't show as fps. - The disjoint spans do skip memory: the last `buffer views of the heap made ready` line reads `spans=5027 ... holes_mb=420` and `spans=4888 ... holes_mb=508` (copies 4384 and 4203). - On the submitting thread the lists' steps take about 3 ms a frame more with the PR (13.7-14.0 vs 10.3-11.0 ms), with refreshes and the write-watch asking each a little higher. I have not measured which part of the change costs it. Not tested: the city scenes the description is about. I don't have a save there, so I can't say whether the saving on wide envelopes outweighs this cost in them.
This pull request can be merged automatically.
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/630/head:github-bot/gh-630'
git switch 'github-bot/gh-630'

Merge

Merge the changes and update on Forgejo.
git switch 'a-publier'
git merge --no-ff 'github-bot/gh-630'
git switch 'github-bot/gh-630'
git rebase 'a-publier'
git switch 'a-publier'
git merge --ff-only 'github-bot/gh-630'
git switch 'github-bot/gh-630'
git rebase 'a-publier'
git switch 'a-publier'
git merge --no-ff 'github-bot/gh-630'
git switch 'a-publier'
git merge --squash 'github-bot/gh-630'
git switch 'a-publier'
git merge --ff-only 'github-bot/gh-630'
git switch 'a-publier'
git merge 'github-bot/gh-630'
git push origin 'a-publier'
This discussion has been locked. Commenting is limited to contributors.
No description provided.