GPU: coalesce nearby shadow-buffer refreshes without spanning huge heap gaps #1555

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

@⁠Texbirds on GitHub · 2026-11-24 19:07 UTC · original
Texbirds:perf/gpu-batch-coalesce-ranges → main · +171 −33 · 1 files · 1 commits · open

Problem

In crates/victory-gpu/src/host.rs, Batch::add collapses all pending refreshes for a Shadow buffer into one [min(from), max(to)) bounding range.

When draws in a submission read small buffers at distant offsets inside a large shared GPU heap (e.g. a 174 MiB heap), Batch::flush calls shadow.update over the entire span between them—scanning write-watch/guard pages and copying unrelated dirty or unwatched megabytes via ReadProcessMemory synchronously before ExecuteCommandLists.

Fix

  • Record per-shadow [from, to) intervals in Batch and sort/coalesce overlapping or nearby intervals within COALESCE_GAP (64 KiB, one guard block) before flushing, so adjacent buffers still coalesce into a single update while megabyte-wide gaps are never spanned.
  • Add unit test coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate.

Verification

  • Pending windows-latest CI (cargo clippy default + --all-features and cargo test --workspace --locked).
**@⁠Texbirds** on GitHub · 2026-11-24 19:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/1555) `Texbirds:perf/gpu-batch-coalesce-ranges` → `main` · +171 −33 · 1 files · 1 commits · open ### Problem In `crates/victory-gpu/src/host.rs`, `Batch::add` collapses all pending refreshes for a `Shadow` buffer into one `[min(from), max(to))` bounding range. When draws in a submission read small buffers at distant offsets inside a large shared GPU heap (e.g. a 174 MiB heap), `Batch::flush` calls `shadow.update` over the entire span between them—scanning write-watch/guard pages and copying unrelated dirty or unwatched megabytes via `ReadProcessMemory` synchronously before `ExecuteCommandLists`. ### Fix - Record per-shadow `[from, to)` intervals in `Batch` and sort/coalesce overlapping or nearby intervals within `COALESCE_GAP` (64 KiB, one guard block) before flushing, so adjacent buffers still coalesce into a single update while megabyte-wide gaps are never spanned. - Add unit test `coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate`. ### Verification - Pending `windows-latest` CI (`cargo clippy` default + `--all-features` and `cargo test --workspace --locked`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:36:18 +00:00
Author
Owner

@⁠wwwwwwww3333333 on GitHub · 2026-11-24 21:51 UTC · original

On Windows, cargo test -p victory-gpu --lib doesn't compile with this PR: head bfffb01 on main ffe310e (clean merge), Rust 1.98.1 MSVC.

error[E0425]: cannot find function `coalesce_ranges` in this scope
    --> crates\victory-gpu\src\host.rs:2055:9
error[E0425]: cannot find value `COALESCE_GAP` in this scope
    --> crates\victory-gpu\src\host.rs:2055:39
(the same two at 2068)

The new test coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate sits in mod batch, which imports only super::Batch. coalesce_ranges and COALESCE_GAP are private items of host.rs (lines 1236 and 1240), so the module needs them too: use super::{Batch, COALESCE_GAP, coalesce_ranges};. This probably didn't show up off Windows because host.rs is Windows-only.

Unit tests only, no game files. AI-assisted (Claude; I ran it on my machine).

**@⁠wwwwwwww3333333** on GitHub · 2026-11-24 21:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7145474110) On Windows, `cargo test -p victory-gpu --lib` doesn't compile with this PR: head bfffb01 on main ffe310e (clean merge), Rust 1.98.1 MSVC. ``` error[E0425]: cannot find function `coalesce_ranges` in this scope --> crates\victory-gpu\src\host.rs:2055:9 error[E0425]: cannot find value `COALESCE_GAP` in this scope --> crates\victory-gpu\src\host.rs:2055:39 (the same two at 2068) ``` The new test `coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate` sits in `mod batch`, which imports only `super::Batch`. `coalesce_ranges` and `COALESCE_GAP` are private items of `host.rs` (lines 1236 and 1240), so the module needs them too: `use super::{Batch, COALESCE_GAP, coalesce_ranges};`. This probably didn't show up off Windows because host.rs is Windows-only. Unit tests only, no game files. AI-assisted (Claude; I ran it on my machine).
Author
Owner

@⁠Texbirds on GitHub · 2026-11-24 21:54 UTC · original

On Windows, cargo test -p victory-gpu --lib doesn't compile with this PR: head bfffb01 on main ffe310e (clean merge), Rust 1.98.1 MSVC.

error[E0425]: cannot find function `coalesce_ranges` in this scope
    --> crates\victory-gpu\src\host.rs:2055:9
error[E0425]: cannot find value `COALESCE_GAP` in this scope
    --> crates\victory-gpu\src\host.rs:2055:39
(the same two at 2068)

The new test coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate sits in mod batch, which imports only super::Batch. coalesce_ranges and COALESCE_GAP are private items of host.rs (lines 1236 and 1240), so the module needs them too: use super::{Batch, COALESCE_GAP, coalesce_ranges};. This probably didn't show up off Windows because host.rs is Windows-only.

Unit tests only, no game files. AI-assisted (Claude; I ran it on my machine).

Thanks for catching that — fixed in 51a046d by importing COALESCE_GAP and coalesce_ranges in mod batch.

**@⁠Texbirds** on GitHub · 2026-11-24 21:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7145548929) > On Windows, `cargo test -p victory-gpu --lib` doesn't compile with this PR: head [bfffb01](https://github.com/Marvisak/VIctory/commit/bfffb01dd6b87459f5e3239e002076c90935c61d) on main [ffe310e](https://github.com/Marvisak/VIctory/commit/ffe310eb2098e52169f4c44db5ffdabf43a72622) (clean merge), Rust 1.98.1 MSVC. > > ``` > error[E0425]: cannot find function `coalesce_ranges` in this scope > --> crates\victory-gpu\src\host.rs:2055:9 > error[E0425]: cannot find value `COALESCE_GAP` in this scope > --> crates\victory-gpu\src\host.rs:2055:39 > (the same two at 2068) > ``` > > The new test `coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate` sits in `mod batch`, which imports only `super::Batch`. `coalesce_ranges` and `COALESCE_GAP` are private items of `host.rs` (lines 1236 and 1240), so the module needs them too: `use super::{Batch, COALESCE_GAP, coalesce_ranges};`. This probably didn't show up off Windows because host.rs is Windows-only. > > Unit tests only, no game files. AI-assisted (Claude; I ran it on my machine). Thanks for catching that — fixed in 51a046d by importing COALESCE_GAP and coalesce_ranges in mod batch.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:30 UTC · original · edited 2026-11-25 18:42 UTC

Automated triage for the maintainers.

Needs work (measurement only). The PR changes Batch in crates/victory-gpu/src/host.rs. It keeps a list of spans per host copy instead of one bounding range. In flush, coalesce_ranges merges spans less than 64 KiB (watch::PAGE) apart, so distant buffers in a large heap no longer pull the whole gap into shadow.update. current keeps the disjoint spans that are up to date. add keeps main's shadow.clean() early return. The code reads correctly. coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate and disjoint_ranges_skip_dirty_gap_pages cover the merge and the dirty pages in a gap. CI: passing (check).

This is a performance change with a trade-off that needs data. add now scans every current span of a copy on each call, and pending spans grow until flush. On a world submission that asks for about 9,000 copies, that could cost more than the gap copies it saves. Please post --features diagnostics numbers from the same retail scene on main and on this branch: the preparing: copies … ms … MB frame summary and the copies per frame line. If they show a drop, this can go to the maintainer.

On your reply: the build and log instructions for testers are clear. The verdict stays until such numbers are posted.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7151973248) · edited 2026-11-25 18:42 UTC <!-- victory-triage v1 head=6d5f103192aa08d57051d54dc33a78d4456167bb verdict=needs-work at=2026-11-25T18:41:54Z --> Automated triage for the maintainers. **Needs work (measurement only).** The PR changes `Batch` in `crates/victory-gpu/src/host.rs`. It keeps a list of spans per host copy instead of one bounding range. In `flush`, `coalesce_ranges` merges spans less than 64 KiB (`watch::PAGE`) apart, so distant buffers in a large heap no longer pull the whole gap into `shadow.update`. `current` keeps the disjoint spans that are up to date. `add` keeps main's `shadow.clean()` early return. The code reads correctly. `coalesce_ranges_merges_nearby_but_keeps_distant_ranges_separate` and `disjoint_ranges_skip_dirty_gap_pages` cover the merge and the dirty pages in a gap. CI: passing (`check`). This is a performance change with a trade-off that needs data. `add` now scans every current span of a copy on each call, and pending spans grow until `flush`. On a world submission that asks for about 9,000 copies, that could cost more than the gap copies it saves. Please post `--features diagnostics` numbers from the same retail scene on `main` and on this branch: the `preparing: copies … ms … MB` frame summary and the `copies per frame` line. If they show a drop, this can go to the maintainer. On your reply: the build and log instructions for testers are clear. The verdict stays until such numbers are posted. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Texbirds on GitHub · 2026-11-25 12:16 UTC · original

@⁠DenRakEiw Thanks for testing and catching the clippy::type_complexity warnings — those are fixed in bf7bd1e using PendingRefresh / CurrentRefresh, and CI check is now passing (along with the Batch doc comment placement and the disjoint_ranges_skip_dirty_gap_pages test).

Regarding the remaining triage point on real-submission numbers: Step::Refresh (via Shadow::refresh) and Step::Heap (bindless::refresh) both submit sub-ranges into Batch::add for command lists that don't invoke memory::prepare (Step::Memory). I don't have a retail capture setup locally to collect --features diagnostics (PREPARING / copystat) before/after numbers — if a tester with a retail build can compare bf7bd1e vs main (940d32f) with --features diagnostics and share whether PREPARING bytes/time drop (or if Step::Memory dominates in practice and makes this unnecessary), that would decide whether this is worth merging.

**@⁠Texbirds** on GitHub · 2026-11-25 12:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7162143200) @⁠DenRakEiw Thanks for testing and catching the `clippy::type_complexity` warnings — those are fixed in `bf7bd1e` using `PendingRefresh` / `CurrentRefresh`, and CI `check` is now passing (along with the `Batch` doc comment placement and the `disjoint_ranges_skip_dirty_gap_pages` test). Regarding the remaining triage point on real-submission numbers: `Step::Refresh` (via `Shadow::refresh`) and `Step::Heap` (`bindless::refresh`) both submit sub-ranges into `Batch::add` for command lists that don't invoke `memory::prepare` (`Step::Memory`). I don't have a retail capture setup locally to collect `--features diagnostics` (`PREPARING` / `copystat`) before/after numbers — if a tester with a retail build can compare `bf7bd1e` vs `main` (`940d32f`) with `--features diagnostics` and share whether `PREPARING` bytes/time drop (or if `Step::Memory` dominates in practice and makes this unnecessary), that would decide whether this is worth merging.
Author
Owner

@⁠Texbirds on GitHub · 2026-11-25 16:31 UTC · original

Rebased onto main (67e8ae1) in 6d5f103. I kept main's new shadow.clean() early return in Batch::add, now len == 0 || shadow.clean(), and dropped the texture parts that main removed. The per-span PendingRefresh / CurrentRefresh logic and both tests are unchanged. The open triage point is the same: this still needs before/after PREPARING / copystat numbers from a real submission.

**@⁠Texbirds** on GitHub · 2026-11-25 16:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7168130334) Rebased onto `main` (`67e8ae1`) in `6d5f103`. I kept `main`'s new `shadow.clean()` early return in `Batch::add`, now `len == 0 || shadow.clean()`, and dropped the texture parts that `main` removed. The per-span `PendingRefresh` / `CurrentRefresh` logic and both tests are unchanged. The open triage point is the same: this still needs before/after `PREPARING` / `copystat` numbers from a real submission.
Author
Owner

@⁠Texbirds on GitHub · 2026-11-25 17:13 UTC · original

For anyone able to run the diagnostics comparison, the current pair is 67e8ae1 (main) and 6d5f103 (this PR). Build both with:

cargo build --profile diag -p victory --features diagnostics

Please run the same world scene with the same settings after warm-up and share a few matching log intervals with:

  • The frame summary, including preparing: copies … ms … MB
  • The copies per frame line, including refresh batches and title memory

Those numbers will help show whether the time saved by skipping gap copies outweighs the cost of tracking spans. CI passed on 6d5f103. The retail comparison is still pending.

**@⁠Texbirds** on GitHub · 2026-11-25 17:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/1555#issuecomment-7169186081) For anyone able to run the diagnostics comparison, the current pair is **`67e8ae1` (main)** and **`6d5f103` (this PR)**. Build both with: ```powershell cargo build --profile diag -p victory --features diagnostics ``` Please run the same world scene with the same settings after warm-up and share a few matching log intervals with: - The frame summary, including `preparing: copies … ms … MB` - The `copies per frame` line, including `refresh batches` and `title memory` Those numbers will help show whether the time saved by skipping gap copies outweighs the cost of tracking spans. [CI passed on `6d5f103`](https://github.com/Marvisak/VIctory/actions/runs/45654471905/job/139347347659). The retail comparison is still pending.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/1555/head:github-bot/gh-1555'
git switch 'github-bot/gh-1555'

Merge

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