GPU: grow the write snapshot in place, and cap idle pool memory #472

Open
SaymonMaster14 wants to merge 1 commit from upstream-pr/472 into main
SaymonMaster14 commented 2026-11-24 03:49:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by SaymonMaster14 on 2026-11-24T03:49:16Z.


Four leaks in the copy bridge, found while measuring #471. Each one is small on
its own; together they cost the peak exactly where commit is tight.

shadow.rs, Shadow::writing: the snapshot grown to span a submission's
declared ranges allocated a second buffer of the new size, copied the old
into it and only then returned the old to the pool. At the largest growth that
holds both, and the world's heap is 174 MB — the peak doubles there. It is now
grown in place: reserve_exact for exactly the growth (the amortized doubling
of a buffer this size would double the capacity, and a copying realloc its
peak), then extend on either side.

shadow.rs, give_back: the pool kept its eight largest buffers whatever
their size, so one world load left eight 174 MB snapshots — about 1.4 GB idle,
never shrunk. The largest is now always kept (its reuse is what saves the
millions of page faults the 64 KiB pages were chosen for) and further buffers
only while they add up to 256 MiB, still at most eight.

texture.rs: every Staging kept a Vec of the whole texture for good, so the
resident cost was the sum of every texture that had been refreshed. One shared
SCRATCH, kept and already touched, makes it the largest texture instead. The
hash path does not use the buffer (it hashes the mapping directly), and the
staging mapping is written only at submission, so the lock costs nothing there.

dstorage.rs, staging_bytes: a zlib-then-BCPack request inflates into an
intermediate buffer that counted against no limit. It now counts against the
32 MiB staging cap, so the cap bounds what is really held. Requests of that
shape serialize more when one is large; no throughput was measured either way.

What is in it: those four, and nothing else. No interface change.

Tested: cargo build --release -p victory, cargo test -p victory-gpu -p victory-hle — 130 passed, 1 ignored, 0 failed. Live, release, world load, 16 GB
machine: the process reached 18.0 GB private and died at memory allocation of 70426816 bytes failed, system commit 48.8 GB of 49.0 GB. It still dies — these
reduce the slope, they do not remove the ceiling.

Not tested: the headroom case. The earlier runs measured the page file, not
PrivateMemorySize64, and there are no per-component counters, so "the peak
went down" cannot be claimed from the two side by side; #471 has the samples
and the counters that would settle it. Also unmeasured: whether the 256 MiB cap
is the right number (it keeps ~1.5 world heap snapshots; 192 MiB or 384 MiB are
as arguable), and the DStorage serialization cost.

Not a #407 fix. This is bridge work, which #407 proposes to delete. It holds
until stage 2 there — xmem asking a GPU-visible arena first — replaces the
copies. Worth merging only as the stopgap it is.

Fixes #471.


AI use: Claude (Sonnet 5.5) in a Maestri terminal drove the code reading, the
edits, the build and the test run; the memory sampling and the reproduction
were set up outside it.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6574179432 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/472). Originally posted by [SaymonMaster14](https://github.com/SaymonMaster14) on 2026-11-24T03:49:16Z. --- Four leaks in the copy bridge, found while measuring #471. Each one is small on its own; together they cost the peak exactly where commit is tight. `shadow.rs`, `Shadow::writing`: the snapshot grown to span a submission's declared ranges allocated a **second** buffer of the new size, copied the old into it and only then returned the old to the pool. At the largest growth that holds both, and the world's heap is 174 MB — the peak doubles there. It is now grown in place: `reserve_exact` for exactly the growth (the amortized doubling of a buffer this size would double the capacity, and a copying realloc its peak), then extend on either side. `shadow.rs`, `give_back`: the pool kept its eight largest buffers whatever their size, so one world load left eight 174 MB snapshots — about 1.4 GB idle, never shrunk. The largest is now always kept (its reuse is what saves the millions of page faults the 64 KiB pages were chosen for) and further buffers only while they add up to 256 MiB, still at most eight. `texture.rs`: every `Staging` kept a `Vec` of the whole texture for good, so the resident cost was the sum of every texture that had been refreshed. One shared `SCRATCH`, kept and already touched, makes it the largest texture instead. The hash path does not use the buffer (it hashes the mapping directly), and the staging mapping is written only at submission, so the lock costs nothing there. `dstorage.rs`, `staging_bytes`: a zlib-then-BCPack request inflates into an intermediate buffer that counted against no limit. It now counts against the 32 MiB staging cap, so the cap bounds what is really held. Requests of that shape serialize more when one is large; no throughput was measured either way. **What is in it:** those four, and nothing else. No interface change. **Tested:** `cargo build --release -p victory`, `cargo test -p victory-gpu -p victory-hle` — 130 passed, 1 ignored, 0 failed. Live, release, world load, 16 GB machine: the process reached 18.0 GB private and died at `memory allocation of 70426816 bytes failed`, system commit 48.8 GB of 49.0 GB. It still dies — these reduce the slope, they do not remove the ceiling. **Not tested:** the headroom case. The earlier runs measured the page file, not `PrivateMemorySize64`, and there are no per-component counters, so "the peak went down" cannot be claimed from the two side by side; #471 has the samples and the counters that would settle it. Also unmeasured: whether the 256 MiB cap is the right number (it keeps ~1.5 world heap snapshots; 192 MiB or 384 MiB are as arguable), and the DStorage serialization cost. **Not a #407 fix.** This is bridge work, which #407 proposes to delete. It holds until stage 2 there — xmem asking a GPU-visible arena first — replaces the copies. Worth merging only as the stopgap it is. Fixes #471. --- AI use: Claude (Sonnet 5.5) in a Maestri terminal drove the code reading, the edits, the build and the test run; the memory sampling and the reproduction were set up outside it.
mercuryVM commented 2026-11-24 06:53:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T05:47:20Z.

Review: APPROVED


Tested f200a2a on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (264 passed, 0 failed). I could not measure the memory peaks without a game package.

  • Shadow::writing: the in-place growth is correct. reserve_exact reserves exactly the new span. splice(0..0, host(start, at).iter().copied()) uses an exact-size iterator, so the tail moves once. The upper extend starts at old_end. One note on the comment: reserve_exact can still be a copying realloc when the heap cannot grow the block in place, and for blocks this size that is likely. The peak can still be old + new for that one step. It no longer goes through the pool, though, which is a clear improvement over before.
  • give_back (nit): kept adds every buffer after the largest, including ones retain rejects. Once one oversized buffer is rejected, smaller ones that would fit in POOL_BYTES are rejected too. Adding only kept buffers (if n <= 8 && kept + cap <= POOL_BYTES { kept += cap; true } else { false }) would match the doc comment.
  • texture.rs: the shared SCRATCH serialises the detile path of watched textures. Refreshes run in the submission's prepare loop on one thread, so I see no contention today.
  • dstorage.rs: counting intermediate_size for zlib+BCPack matches the extra buffer execute allocates.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6034305614 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/472#pullrequestreview-6034305614). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T05:47:20Z. **Review:** APPROVED --- Tested f200a2a on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (264 passed, 0 failed). I could not measure the memory peaks without a game package. - `Shadow::writing`: the in-place growth is correct. `reserve_exact` reserves exactly the new span. `splice(0..0, host(start, at).iter().copied())` uses an exact-size iterator, so the tail moves once. The upper extend starts at `old_end`. One note on the comment: `reserve_exact` can still be a copying realloc when the heap cannot grow the block in place, and for blocks this size that is likely. The peak can still be old + new for that one step. It no longer goes through the pool, though, which is a clear improvement over before. - `give_back` (nit): `kept` adds every buffer after the largest, including ones `retain` rejects. Once one oversized buffer is rejected, smaller ones that would fit in `POOL_BYTES` are rejected too. Adding only kept buffers (`if n <= 8 && kept + cap <= POOL_BYTES { kept += cap; true } else { false }`) would match the doc comment. - `texture.rs`: the shared `SCRATCH` serialises the detile path of watched textures. Refreshes run in the submission's prepare loop on one thread, so I see no contention today. - `dstorage.rs`: counting `intermediate_size` for zlib+BCPack matches the extra buffer `execute` allocates. AI-assisted (Claude Code).
DaviTostes commented 2026-11-24 06:55:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T03:51:10Z.


Verified f200a2a on Arch Linux x86_64, Windows MSVC release build under Wine 11.22 (victory-gpu, --test-threads=1): 43 passed, 0 failed, 1 ignored (same count as main, no new test in this head).

Not covered: the snapshot growth and idle-pool cap under a real world frame (no game data here).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121481845 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/472#issuecomment-7121481845). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T03:51:10Z. --- Verified `f200a2a` on **Arch Linux x86_64**, Windows MSVC release build under **Wine 11.22** (`victory-gpu`, `--test-threads=1`): **43 passed, 0 failed, 1 ignored** (same count as `main`, no new test in this head). Not covered: the snapshot growth and idle-pool cap under a real world frame (no game data here).
erickbobd commented 2026-11-24 14:04:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by erickbobd on 2026-11-24T13:08:10Z.


Companion test-only PR against your memory/peak-during-world-load branch: SaymonMaster14/VIctory#1, based exactly on f200a2a.

Two synthetic tests cover snapshot byte preservation/allocation reuse with spare capacity and intermediate DStorage staging for file and memory inputs. On the pure parent plus these tests: 46 victory-gpu passed / 1 ignored; 87 victory-hle passed, serial. Replacing each function with its real pre-#472 version makes its test fail; restoring both passes the full suites again. Asset policy: 151 files OK.

Tested on RTX 5070 Ti / NVIDIA 617.14, Windows 11 26200.9974, Rust 1.98.1 / MSVC x64. This verifies the regression fixtures, not live-game memory savings. AI-assisted (Codex); synthetic inputs only.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7132398444 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/472#issuecomment-7132398444). Originally posted by [erickbobd](https://github.com/erickbobd) on 2026-11-24T13:08:10Z. --- Companion test-only PR against your `memory/peak-during-world-load` branch: [SaymonMaster14/VIctory#1](https://github.com/SaymonMaster14/VIctory/pull/1), based exactly on `f200a2a`. Two synthetic tests cover snapshot byte preservation/allocation reuse with spare capacity and intermediate DStorage staging for file and memory inputs. On the pure parent plus these tests: **46 victory-gpu passed / 1 ignored; 87 victory-hle passed**, serial. Replacing each function with its real pre-#472 version makes its test fail; restoring both passes the full suites again. Asset policy: 151 files OK. Tested on RTX 5070 Ti / NVIDIA 617.14, Windows 11 26200.9974, Rust 1.98.1 / MSVC x64. This verifies the regression fixtures, not live-game memory savings. AI-assisted (Codex); synthetic inputs only.
BaggyALPHA commented 2026-11-24 16:05:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BaggyALPHA on 2026-11-24T15:42:28Z.


I tried a Series S WARP comparison with f200a2a applied on top of the PresentX fix from #211 and the diagnostics from #484 and #602. For the patched build I adjusted the local #602 probe to read the new shared texture scratch buffer. Both runs used copies of the same profile, separate caches, and the same commit guard.

The fresh control stopped at its time limit before opening the story archive. The patched run opened it, then reached the commit guard before a confirmed world frame. An earlier run of the same control binary and profile did open the story archive. Against that earlier control, private commit differed by less than 1% at archive open and at the 10- and 20-second marks after it. The sampled idle snapshot capacity stayed zero, and the cached upload peak was the same in those two runs.

This is a comparison of early loading only. It does not establish whether the patch lowers peak commit during world rendering or whether the snapshot-pool cap was exercised. Neither run logged an out-of-memory error or device removal before stopping.

AI-assisted testing and write-up (OpenAI Codex).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7136476686 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/472#issuecomment-7136476686). Originally posted by [BaggyALPHA](https://github.com/BaggyALPHA) on 2026-11-24T15:42:28Z. --- I tried a Series S WARP comparison with `f200a2a` applied on top of the PresentX fix from #211 and the diagnostics from #484 and #602. For the patched build I adjusted the local #602 probe to read the new shared texture scratch buffer. Both runs used copies of the same profile, separate caches, and the same commit guard. The fresh control stopped at its time limit before opening the story archive. The patched run opened it, then reached the commit guard before a confirmed world frame. An earlier run of the same control binary and profile did open the story archive. Against that earlier control, private commit differed by less than 1% at archive open and at the 10- and 20-second marks after it. The sampled idle snapshot capacity stayed zero, and the cached upload peak was the same in those two runs. This is a comparison of early loading only. It does not establish whether the patch lowers peak commit during world rendering or whether the snapshot-pool cap was exercised. Neither run logged an out-of-memory error or device removal before stopping. AI-assisted testing and write-up (OpenAI Codex).
AgneyaK commented 2026-11-24 17:04:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AgneyaK on 2026-11-24T16:37:03Z.


Test report on an RTX 3050 6 GB Laptop (i7-13700H, 16 GB RAM, Windows 11 26300), diag build, title 1.0.2.1, Series X profile. Base tree: main ffe310e + #208 + #223 #233 #320 #439 + #452 + #579 (VICTORY_SKIP_WAVE64=mesh), from #1232. #472, #322 and #1024 merge cleanly onto it.

tree world frames frame preparing copies textures GPU copying back
base 35 4,641 ms 2,915 943 242 931 741
+ #472 + #322 + #1024 60 4,101 ms 2,615 607 180 836 604

So about 12% shorter frames, mostly copies (−36%) and copy-back (−18%). One run each, and the three were
not separated.

On this 16 GB machine much of the rest looks like paging: private bytes reach 22.9 GB and the working set is trimmed to 2-5 GB (numbers in #1232).

AI use: Claude Code (Claude Opus 5.5) merged and built the trees, ran the title on my machine and drafted this note; results are from the runs described.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137945086 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/472#issuecomment-7137945086). Originally posted by [AgneyaK](https://github.com/AgneyaK) on 2026-11-24T16:37:03Z. --- Test report on an RTX 3050 6 GB Laptop (i7-13700H, 16 GB RAM, Windows 11 26300), diag build, title 1.0.2.1, Series X profile. Base tree: `main` `ffe310e` + #208 + #223 #233 #320 #439 + #452 + #579 (`VICTORY_SKIP_WAVE64=mesh`), from #1232. #472, #322 and #1024 merge cleanly onto it. | tree | world frames | frame | preparing | copies | textures | GPU | copying back | |---|---|---|---|---|---|---|---| | base | 35 | 4,641 ms | 2,915 | 943 | 242 | 931 | 741 | | + #472 + #322 + #1024 | 60 | **4,101 ms** | 2,615 | **607** | 180 | 836 | **604** | So about 12% shorter frames, mostly copies (−36%) and copy-back (−18%). One run each, and the three were not separated. On this 16 GB machine much of the rest looks like paging: private bytes reach 22.9 GB and the working set is trimmed to 2-5 GB (numbers in #1232). AI use: Claude Code (Claude Opus 5.5) merged and built the trees, ran the title on my machine and drafted this note; results are from the runs described.
Marvisak commented 2026-11-25 01:07:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:20:03Z.


Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-gpu/src/shadow.rs, crates/victory-gpu/src/texture.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148399789 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/472#issuecomment-7148399789). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:20:03Z. --- <!-- victory-triage v1 head=f200a2a101e2a61999a8a9f46117231f7fcde71a verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-gpu/src/shadow.rs`, `crates/victory-gpu/src/texture.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
desplmfao commented 2026-11-25 07:03:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by desplmfao on 2026-11-25T06:21:28Z.


Your commit here now conflicts with main, so I opened #2153 with it brought up to date. Your commit is kept with you as author, and only the conflicts are resolved. The details are in #2153: main has since split the snapshot into segments, so the in-place growth part no longer applies, but the pool cap and the shared SCRATCH carry over unchanged.

Measured in Grand Theft Auto VI's world (RTX 5080, Linux): the snapshot pool held 2,040 MB on main and 111–214 MB with your change, with no change in frame time. If you rebase this PR yourself, I'll close #2153.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7154707688 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/472#issuecomment-7154707688). Originally posted by [desplmfao](https://github.com/desplmfao) on 2026-11-25T06:21:28Z. --- Your commit here now conflicts with `main`, so I opened #2153 with it brought up to date. Your commit is kept with you as author, and only the conflicts are resolved. The details are in #2153: `main` has since split the snapshot into segments, so the in-place growth part no longer applies, but the pool cap and the shared `SCRATCH` carry over unchanged. Measured in Grand Theft Auto VI's world (RTX 5080, Linux): the snapshot pool held 2,040 MB on `main` and 111–214 MB with your change, with no change in frame time. If you rebase this PR yourself, I'll close #2153.
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/472:upstream-pr/472'
git switch 'upstream-pr/472'

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