perf(gpu): optimize merge_under_writes, take_all nesting, and refresh batching #368

Manually merged
github merged 1 commit from github-bot/gh-368 into gh-pr/368/base 2026-12-02 20:40:15 +00:00 AGit
Owner

@⁠Androtim on GitHub · 2026-12-02 20:11 UTC · original
Androtim:perf/merge-under-writes-and-refresh-parallelism → main · +436 −126 · 4 files · 1 commits · merged 2026-12-02 20:35 UTC

## Summary
Reduces CPU submission overhead (lists_steps, refreshes, and merge_under_writes) by eliminating per-page allocations and lock contention during stale write-watch merging, unlocking nested parallel range splits in take_all, and lowering parallel refresh thresholds:

  • Nested parallel take_all (crates/victory-gpu/src/land.rs): Added Pool::each_or_inline and each_wide_sub (victory-wide-sub pool) so Shadow::take_all (with lowered LONG = 256 KiB and PART = 128 KiB) no longer falls back to single-threaded execution when invoked inside victory-wide.
  • Zero-alloc Shadow::merge_under_writes (crates/victory-gpu/src/shadow.rs):
    • Replaced per-page Mutex<Option<Vec<(usize, usize)>>> buckets with at most 8 MergeBucket accumulators writing coalesced shadow-relative runs directly into flat worker Vecs + bulk card_changed_runs_slices slice appends.
    • Added TLS PAGE_RUNS / SUB_RUNS + reusable-buffer word_runs_into / word_runs_avx2_into and stale_pages_taken_in_atomic linear two-pointer scan.
    • Queried crate::watch::parts_written once per range on regular write-watched shadows so merge_page_under_writes only reads changed 4 KiB sub-pages (part_runs).
    • Added sorted/in-place fast-path compaction in shadow::joined.
  • Lowered parallel refresh threshold (crates/victory-gpu/src/host.rs): Lowered FEW from 256 to 32 in Batch::refresh_front and Batch::flush_all, and wide_refresh minimum copy count from 4 to 2 in Batch::flush_all.

Plain --release A/B Benchmarks (vs origin/main @ 0c8200c)

1440p 90s ABBA (-PlayParity -AutoNudgeMenu -Res 1440 -WorldSeconds 90)

Metric main A1 cand B1 cand B2 main A2 Delta (med/mean)
FPS (med / mean / min) 15.35 / 14.96 / 3.2 13.95 / 12.99 / 0.8 15.25 / 15.41 / 6.9 14.60 / 13.29 / 5.3 -0.38 / +0.08 fps
preparing (med) 24.85 ms 22.55 ms 21.50 ms 24.70 ms -2.75 ms (-11.1%)
sub_tot (med / mean) 32.10 / 32.19 ms 28.00 / 27.35 ms 27.90 / 27.49 ms 32.10 / 32.58 ms -4.15 / -4.97 ms (-12.9% / -15.3%)
lists_steps (med / mean) 15.60 / 16.73 ms 13.90 / 14.64 ms 13.80 / 14.58 ms 15.95 / 17.09 ms -1.93 / -2.30 ms (-11.4% / -13.6%)
refreshes (med / mean) 9.00 / 9.65 ms 7.30 / 7.74 ms 7.30 / 7.82 ms 9.05 / 9.70 ms -1.73 / -1.90 ms (-19.1% / -19.6%)
under (merge_under_writes) 1.50 / 1.57 ms 0.90 / 0.85 ms 0.90 / 0.94 ms 1.75 / 1.77 ms -0.73 / -0.78 ms (-44.6% / -46.4%)
card_mem (med / mean) 3.60 / 3.70 ms 3.30 / 3.22 ms 3.40 / 3.46 ms 3.50 / 3.51 ms -0.20 / -0.27 ms
copyback (med) 9.85 ms 9.80 ms 9.45 ms 10.15 ms -0.38 ms
VRAM max 6028 MB 5947 MB 6004 MB 6086 MB -81.5 MB avg

4K 90s A/B (-PlayParity -AutoNudgeMenu -Res 4k -WorldSeconds 90)

Metric main A1 cand B1 Delta
FPS (med / mean / min) 6.70 / 6.42 / 0.4 6.60 / 6.68 / 5.4 -0.10 med / +0.26 mean / +5.0 min
preparing (med) 20.80 ms 18.25 ms -2.55 ms (-12.3%)
refreshes (med / mean) 6.40 / 7.60 ms 5.00 / 6.82 ms -1.40 / -0.78 ms (-21.9%)
under (med / mean) 0.90 / 1.39 ms 0.80 / 1.12 ms -0.10 / -0.27 ms (-19.4% mean)
lists_steps (med / mean) 13.10 / 15.24 ms 12.40 / 14.34 ms -0.70 / -0.90 ms
Textures uploaded 54,093 68,771 +14,678 (final waiting 11,045 -> 7,416)
VRAM max 6386 MB 6389 MB +3 MB (over 0 MB)

Diagnostics Visual Check

  • --features diagnostics 1440p 90s (-PlayParity -AutoNudgeMenu): 6/6 PASS (world_windows=20, max_draws=13111, crashes=0, VRAM max=5940 MB / 6777 MB, mapped_rate=0.82 ms/s, pngs=24, lit=21/24, luma=97.46, near_black=0.09%, edge_energy=7.49, magenta=0.0%).
  • Inspected the 3 largest PNGs (frame-1713.png, frame-1721.png, frame-1703.png) cleanly: full world geometry, lighting, water, sky, vegetation, and textures match main cleanly.

Verification

  • cargo +1.98.1-x86_64-pc-windows-gnu xtask policy (329 files ok, host.rs 651,825 bytes <= 655,360 limit)
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --lib --locked land::tests (8 passed; 0 failed; 1 ignored)
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --lib --locked shadow:: -- --test-threads=1 (60 passed; 0 failed; 3 ignored)
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --lib --locked host::refresh_tests (5 passed; 0 failed; 2 ignored)
  • cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --locked -- -D warnings (clean)
  • cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --features diagnostics --locked -- -D warnings (clean)
**@⁠Androtim** on GitHub · 2026-12-02 20:11 UTC · [original](https://github.com/Androtim/MiniVI/pull/368) `Androtim:perf/merge-under-writes-and-refresh-parallelism` → `main` · +436 −126 · 4 files · 1 commits · merged 2026-12-02 20:35 UTC ## Summary Reduces CPU submission overhead (`lists_steps`, `refreshes`, and `merge_under_writes`) by eliminating per-page allocations and lock contention during stale write-watch merging, unlocking nested parallel range splits in `take_all`, and lowering parallel refresh thresholds: - **Nested parallel `take_all` (`crates/victory-gpu/src/land.rs`):** Added `Pool::each_or_inline` and `each_wide_sub` (`victory-wide-sub` pool) so `Shadow::take_all` (with lowered `LONG = 256 KiB` and `PART = 128 KiB`) no longer falls back to single-threaded execution when invoked inside `victory-wide`. - **Zero-alloc `Shadow::merge_under_writes` (`crates/victory-gpu/src/shadow.rs`):** - Replaced per-page `Mutex<Option<Vec<(usize, usize)>>>` buckets with at most 8 `MergeBucket` accumulators writing coalesced shadow-relative runs directly into flat worker `Vec`s + bulk `card_changed_runs_slices` slice appends. - Added TLS `PAGE_RUNS` / `SUB_RUNS` + reusable-buffer `word_runs_into` / `word_runs_avx2_into` and `stale_pages_taken_in_atomic` linear two-pointer scan. - Queried `crate::watch::parts_written` once per range on regular write-watched shadows so `merge_page_under_writes` only reads changed 4 KiB sub-pages (`part_runs`). - Added sorted/in-place fast-path compaction in `shadow::joined`. - **Lowered parallel refresh threshold (`crates/victory-gpu/src/host.rs`):** Lowered `FEW` from `256` to `32` in `Batch::refresh_front` and `Batch::flush_all`, and `wide_refresh` minimum copy count from `4` to `2` in `Batch::flush_all`. ## Plain `--release` A/B Benchmarks (vs `origin/main` @ `0c8200c`) ### 1440p 90s ABBA (`-PlayParity -AutoNudgeMenu -Res 1440 -WorldSeconds 90`) | Metric | `main` A1 | `cand` B1 | `cand` B2 | `main` A2 | Delta (med/mean) | | --- | ---: | ---: | ---: | ---: | ---: | | FPS (med / mean / min) | 15.35 / 14.96 / 3.2 | 13.95 / 12.99 / 0.8 | 15.25 / 15.41 / 6.9 | 14.60 / 13.29 / 5.3 | -0.38 / +0.08 fps | | `preparing` (med) | 24.85 ms | 22.55 ms | 21.50 ms | 24.70 ms | **-2.75 ms (-11.1%)** | | `sub_tot` (med / mean) | 32.10 / 32.19 ms | 28.00 / 27.35 ms | 27.90 / 27.49 ms | 32.10 / 32.58 ms | **-4.15 / -4.97 ms (-12.9% / -15.3%)** | | `lists_steps` (med / mean) | 15.60 / 16.73 ms | 13.90 / 14.64 ms | 13.80 / 14.58 ms | 15.95 / 17.09 ms | **-1.93 / -2.30 ms (-11.4% / -13.6%)** | | `refreshes` (med / mean) | 9.00 / 9.65 ms | 7.30 / 7.74 ms | 7.30 / 7.82 ms | 9.05 / 9.70 ms | **-1.73 / -1.90 ms (-19.1% / -19.6%)** | | `under` (`merge_under_writes`) | 1.50 / 1.57 ms | 0.90 / 0.85 ms | 0.90 / 0.94 ms | 1.75 / 1.77 ms | **-0.73 / -0.78 ms (-44.6% / -46.4%)** | | `card_mem` (med / mean) | 3.60 / 3.70 ms | 3.30 / 3.22 ms | 3.40 / 3.46 ms | 3.50 / 3.51 ms | **-0.20 / -0.27 ms** | | `copyback` (med) | 9.85 ms | 9.80 ms | 9.45 ms | 10.15 ms | **-0.38 ms** | | VRAM max | 6028 MB | 5947 MB | 6004 MB | 6086 MB | **-81.5 MB avg** | ### 4K 90s A/B (`-PlayParity -AutoNudgeMenu -Res 4k -WorldSeconds 90`) | Metric | `main` A1 | `cand` B1 | Delta | | --- | ---: | ---: | ---: | | FPS (med / mean / min) | 6.70 / 6.42 / 0.4 | 6.60 / 6.68 / 5.4 | -0.10 med / **+0.26 mean** / **+5.0 min** | | `preparing` (med) | 20.80 ms | 18.25 ms | **-2.55 ms (-12.3%)** | | `refreshes` (med / mean) | 6.40 / 7.60 ms | 5.00 / 6.82 ms | **-1.40 / -0.78 ms (-21.9%)** | | `under` (med / mean) | 0.90 / 1.39 ms | 0.80 / 1.12 ms | **-0.10 / -0.27 ms (-19.4% mean)** | | `lists_steps` (med / mean) | 13.10 / 15.24 ms | 12.40 / 14.34 ms | **-0.70 / -0.90 ms** | | Textures uploaded | 54,093 | 68,771 | **+14,678** (final waiting 11,045 -> 7,416) | | VRAM max | 6386 MB | 6389 MB | +3 MB (over 0 MB) | ## Diagnostics Visual Check - `--features diagnostics` 1440p 90s (`-PlayParity -AutoNudgeMenu`): **6/6 PASS** (`world_windows=20, max_draws=13111, crashes=0, VRAM max=5940 MB / 6777 MB, mapped_rate=0.82 ms/s, pngs=24, lit=21/24, luma=97.46, near_black=0.09%, edge_energy=7.49, magenta=0.0%`). - Inspected the 3 largest PNGs (`frame-1713.png`, `frame-1721.png`, `frame-1703.png`) cleanly: full world geometry, lighting, water, sky, vegetation, and textures match `main` cleanly. ## Verification - `cargo +1.98.1-x86_64-pc-windows-gnu xtask policy` (`329 files ok`, `host.rs` `651,825` bytes <= `655,360` limit) - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --lib --locked land::tests` (`8 passed; 0 failed; 1 ignored`) - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --lib --locked shadow:: -- --test-threads=1` (`60 passed; 0 failed; 3 ignored`) - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --lib --locked host::refresh_tests` (`5 passed; 0 failed; 2 ignored`) - `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --locked -- -D warnings` (clean) - `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --features diagnostics --locked -- -D warnings` (clean)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 20:13:50 +00:00
github-bot changed target branch from main to gh-pr/368/base 2026-12-02 20:40:13 +00:00
github manually merged commit 99a85864bb into gh-pr/368/base 2026-12-02 20:40:15 +00:00
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 20:35 UTC · original

Architect Review (CI 46960529881 GREEN, allowlisted):

  • Verified shadow.rs (stale_pages_taken_in_atomic linear two-pointer scan over BTreeMap-ordered written, watch::parts_written gated strictly on self.watched == 1 with u16::MAX fallback for uncopied/t == 0 and non-1 watched states, flat MergeBucket + card_changed_runs_slices boundary merging, TLS PAGE_RUNS/SUB_RUNS word_runs_into, in-place joined), land.rs (each_wide_sub + each_or_inline preventing nested victory-wide turn-lock stalls, unit-tested), and host.rs (FEW = 32, wide_refresh >= 2).
  • Plain --release A/B shows major CPU submission wins: 1440p ABBA sub_tot -4.15 / -4.97 ms (-12.9% / -15.3%), preparing -2.75 ms (-11.1%), refreshes -1.73 / -1.90 ms (-19.1%), merge_under_writes -44.6% / -46.4%, VRAM -81.5 MB; 4K preparing -2.55 ms (-12.3%), mean FPS 6.42 -> 6.68, min FPS 0.4 -> 5.4, +14,678 textures uploaded (final_waiting 11,045 -> 7,416, over = 0 MB); --features diagnostics 6/6 PASS, 0 crashes.
  • Merging to main.
**@⁠Androtim** on GitHub · 2026-12-02 20:35 UTC · [original](https://github.com/Androtim/MiniVI/pull/368#issuecomment-7357980961) Architect Review (CI `46960529881` **GREEN**, allowlisted): - Verified `shadow.rs` (`stale_pages_taken_in_atomic` linear two-pointer scan over `BTreeMap`-ordered `written`, `watch::parts_written` gated strictly on `self.watched == 1` with `u16::MAX` fallback for uncopied/`t == 0` and non-1 watched states, flat `MergeBucket` + `card_changed_runs_slices` boundary merging, TLS `PAGE_RUNS`/`SUB_RUNS` `word_runs_into`, in-place `joined`), `land.rs` (`each_wide_sub` + `each_or_inline` preventing nested `victory-wide` turn-lock stalls, unit-tested), and `host.rs` (`FEW = 32`, `wide_refresh >= 2`). - Plain `--release` A/B shows major CPU submission wins: 1440p ABBA `sub_tot -4.15 / -4.97 ms (-12.9% / -15.3%)`, `preparing -2.75 ms (-11.1%)`, `refreshes -1.73 / -1.90 ms (-19.1%)`, `merge_under_writes -44.6% / -46.4%`, `VRAM -81.5 MB`; 4K `preparing -2.55 ms (-12.3%)`, `mean FPS 6.42 -> 6.68`, `min FPS 0.4 -> 5.4`, `+14,678` textures uploaded (`final_waiting 11,045 -> 7,416`, `over = 0 MB`); `--features diagnostics` `6/6 PASS`, `0` crashes. - Merging to `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/Androtim--MiniVI!368
No description provided.