Skip to content

GPU: a snapshot that fails to allocate frees the idle pool and tries once more - #2153

Open
desplmfao wants to merge 6 commits into
Marvisak:mainfrom
desplmfao:fix/snapshot-pool-472
Open

desplmfao wants to merge 6 commits into
Marvisak:mainfrom
desplmfao:fix/snapshot-pool-472

Conversation

@desplmfao

@desplmfao desplmfao commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

When a write snapshot needs a buffer and none in the idle pool fits, pooled calls Vec::with_capacity. If that allocation fails, the process aborts, even though the idle pool may hold hundreds of MB that nothing is using.

Change

crates/victory-gpu/src/shadow.rs, pooled (15 lines):

  • It tries try_reserve_exact first.
  • If that fails, it frees the idle pool, logs snapshot allocation failed; freeing the idle pool, and reserves once more with reserve_exact. That still aborts if memory is really gone.
  • give_back and POOL_BYTES are main's, unchanged.

History

This PR started as #472 (by @SaymonMaster14) brought up to date with main: an idle pool cap, a shared texture read buffer, and a dstorage buffer change. main has since done these its own way: a 512 MiB pool cap, and 8f2a6f14 for per-thread read buffers. As triage suggested, the PR is reduced to the one part main doesn't cover, the allocation retry. #472's commit is still in the branch history, but none of its changes are in the diff.

How it was found

Reading pooled while bringing #472 up to date with main. I haven't seen this abort in a title run; it matters on hosts where commit is tight, such as 16 GiB machines under Wine.

Tested

  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • shadow:: tests on an RTX 5080 under Proton: 10 passed.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

The snapshot `Shadow::writing` keeps, to tell later what a submission's shaders
wrote, is grown to span every range the submission declares. Growing it
allocated a second buffer of the new size, copied the old one in, and only then
returned it to the pool: at the largest growth that holds twice the buffer, and
the world's heap is 174 MB, so the peak doubles right where commit is already
tight.

Grow the old buffer in place instead (`reserve_exact` for exactly the growth,
then extend on either side). The pool that feeds the snapshot kept its eight
largest buffers whatever their size, so after the world had grown once it could
hold eight 174 MB buffers, about 1.4 GB idle. Keep the largest (its reuse is
what saves the millions of page faults the 64 KiB pages were chosen for) and
further buffers only up to 256 MiB.

Measured on a 16 GB machine, world load, release build: the process reached
18.0 GB private and died at `memory allocation of 70426816 bytes failed` with
the system commit at 48.8 GB of a 49.0 GB limit. It is not a fix for that
limit, and not comparable to earlier runs (those measured the page file, not
`PrivateMemorySize64`) — the counters to compare it against do not exist yet.

Refs Marvisak#407: this is bridge work, which that issue proposes to delete. It stays
valid until stage 2 there (xmem asks a GPU-visible arena first) replaces the
copies.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Idle snapshot pool memory capped (#472 rebased, 2 GB RAM) — memory-cap fix for long sessions; mixed gpu+hle, Windows-only.

@yualk

yualk commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Independent Windows x64 CPU-only probe on parent 940d32f69 vs this exact d23e5d9, using each pin's real pooled/give_back, touched pages, and GetProcessMemoryInfo(PROCESS_MEMORY_COUNTERS_EX). Only the idle shadow pool was exercised; no title, GPU dispatch, texture staging, or DirectStorage inputs.

  • Twelve 8 MiB leases, ABBA: both first and second live sets stayed 96 MiB; retained idle capacity 96 → 64 MiB. Process-private charge fell 32.08 MiB between arms at the idle sample. The refill touched all 12 again: observed page faults 9 → 8,222 and median measured lease/fill/check 3.52 → 5.11 ms.
  • Four 96 MiB leases, ABBA: both live sets stayed 384 MiB; idle 384 → 288 MiB and Windows private charge fell 96.19 MiB. Refill faults 9 → 24,635, measured 14.46 → 18.17 ms.
  • Small fit [8,32,96] kept and reused all leases on both. [384,128,128] kept all 640 MiB on both (largest + 256 MiB spare), so this is not a hard 256 MiB total-idle cap. The absolute values include allocator overhead. These are controlled refill costs, not an FPS or live game commit claim. AI-assisted.

zhaibingye added a commit to zhaibingye/VIctory that referenced this pull request Nov 25, 2026
Adapt the snapshot-pool part of Marvisak#2153 (also Marvisak#472). Do not import the unrelated texture or storage changes. Cap total idle capacity including the largest buffer, preserve the best-fitting allocation path, and limit idle entries to eight. Active snapshot correctness is unchanged.

Portable pool regression and Windows all-feature workspace test source checks passed.

Co-authored-by: Davi Andrei <67552001+SaymonMaster14@users.noreply.github.com>
… fails

A pool miss allocated with Vec::with_capacity, which aborts the process
when the allocation fails, even while the pool still holds idle buffers
(too small to fit, but up to POOL_BYTES plus the largest one). Try the
allocation with try_reserve_exact first; when it fails and the pool is
not empty, drop the idle buffers and make one last attempt with
reserve_exact, which aborts as before if that fails too. Pool hits and
successful misses are unchanged.

Idea from fxceless/victory-fork#360.

Co-Authored-By: avt0m4t48 <56218626+avt0m4t48@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@desplmfao

Copy link
Copy Markdown
Contributor Author

Added d389937: when a new snapshot buffer can't be allocated, the idle pool is freed and the allocation tried once more (try_reserve_exact first, so a failed attempt returns instead of aborting). The idea comes from fxceless/victory-fork#360 (avt0m4t48, credited in the commit). It only changes the out-of-memory path. cargo test -p victory-gpu shadow: 4 passed. Clippy is clean.

@desplmfao

Copy link
Copy Markdown
Contributor Author

@yualk thanks for the independent probe. That matches the intent: the largest idle buffer is always kept, and others only up to 256 MiB more, so [384,128,128] keeping 640 MiB is by design, not a total cap. Keeping the largest is what saves most of the page faults on the hot path (the world's big heap snapshot). The extra faults you measured are the cost of refilling the smaller ones. In the game that cost didn't show up in frame time: 428–452 ms per world frame with this PR against 442–451 ms without it (RTX 5080, Linux/Proton), while the idle pool went from 2,040 MB to 111–214 MB.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Verdict: merge candidate. Thanks for splitting it as suggested. The diff is now only crates/victory-gpu/src/shadow.rs pooled: when no idle buffer fits, it calls try_reserve_exact; on failure it logs, clears the idle pool, and calls reserve_exact once more, which still aborts if memory is really gone. give_back and POOL_BYTES are main's, so main's 512 MiB cap is unchanged.

Checked: when the first attempt succeeds the second reserve_exact is a no-op (capacity already ≥ n), and with an empty pool the behaviour is the same as main's Vec::with_capacity. So this only changes the out-of-memory path, as described.

CI: passing (check; one earlier run on the same head failed and the later run passed).


Generated by Claude Code

The pool cap doesn't need it, and counting a zlib-then-BCPack request's
intermediate buffer against the staging budget changes when the title's
reads start, without evidence that the console counts it. As asked in the
triage, it goes from this PR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@desplmfao

Copy link
Copy Markdown
Contributor Author

Thanks for the triage. Done in 0f13780: staging_bytes in dstorage.rs is back to main's version, and this PR no longer touches victory-hle. It came along with the #472 rebase. As you say, the pool cap doesn't need it, and I have no evidence that the console counts the intermediate buffer, so it's out rather than moved. What's left is the pool cap (give_back, POOL_BYTES), the idle-pool retry before reserve_exact aborts, and the shared texture SCRATCH. Clippy is clean.

… the pool at 512 MB

The texture part of this PR (one shared read buffer) is superseded by
main's 8f2a6f1 and is dropped here; what stays is the snapshot pool:
grow a snapshot in place, free the idle pool and retry when an
allocation fails, and keep the largest buffer plus at most 256 MB / 8
others instead of 512 MB in all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@desplmfao

Copy link
Copy Markdown
Contributor Author

Merged main in cb471dc (merge commit, no rebase). main has since covered part of this PR in its own way, so the PR is smaller now:

  • Dropped here: the shared texture read buffer. main's 8f2a6f14 (a texture refresh reads into its thread's own buffer) does the same job, and I took main's texture.rs as is.
  • What stays (only shadow.rs, about 40 lines against main): a snapshot grows in place instead of being made anew and copied; when an allocation fails, the idle pool is freed and it tries once more instead of aborting; and the pool keeps its largest buffer plus at most 256 MB / 8 others, where main now keeps up to 512 MB in all.

The 2 GB in the title was measured against main before its own 512 MB cap. Against today's main, the saving is the difference between the two caps, which I haven't measured yet. shadow:: tests: 8 passed under Wine; clippy clean.

@desplmfao

Copy link
Copy Markdown
Contributor Author

The check failure on cb471dc isn't from this PR. The failing test is emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index in victory-shader, and this PR only changes victory-gpu/src/shadow.rs. main's own CI fails the same way at da82e444, the commit I merged. main has since added 67e8ae14 ("Shader tests: the compute harness zeroes its UAVs"), whose CI is still running. Once that's green, I'll merge main again so this PR's CI follows.

desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 25, 2026
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 25, 2026
The previous main (old upstream base) is the first parent; the second is
upstream main with the current heads of our pull requests merged in:
Marvisak#2143, Marvisak#1849 (carried onto main's per-region runtime_pages), Marvisak#259, Marvisak#491,
Marvisak#2153, Marvisak#2099, Marvisak#2071 (main covers it), Marvisak#2162, Marvisak#2218, Marvisak#2088, Marvisak#2389, Marvisak#177's
element-type key on main's three handle maps, and the README. Marvisak#2112
(opt-in, in place) stays in the renderer branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
desplmfao and others added 2 commits November 25, 2026 20:04
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main now caps the idle pool at 512 MiB in all. This PR's rule (the
largest buffer, plus others up to 256 MiB and at most 8) could hold more
than that, so it goes. What stays: a snapshot that fails to allocate
frees the idle pool and tries once more instead of aborting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@desplmfao desplmfao changed the title GPU: cap idle snapshot pool memory (#472 rebased; 2 GB RAM in the world) GPU: a snapshot that fails to allocate frees the idle pool and tries once more Nov 26, 2026
@desplmfao

Copy link
Copy Markdown
Contributor Author

Split as suggested, in c21a054. give_back and POOL_BYTES are main's again, so main's 512 MiB cap applies unchanged. As you pointed out, our rule could hold more than that. The PR is now only the allocation retry in pooled: try to reserve, and if that fails, free the idle pool and reserve once more. The diff is 15 lines in shadow.rs, shadow:: gives 10 passed under Wine, and clippy is clean. The title is updated to match.

desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 26, 2026
…'s pool cap

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 26, 2026
…'s pool cap

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@desplmfao
desplmfao force-pushed the fix/snapshot-pool-472 branch from 3bb37bc to c21a054 Compare November 26, 2026 02:41
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants