GPU: reclaim idle snapshot and texture staging pools and compact pipeline/heap metadata #3228
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3230"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-26T13:49:27Z.
Summary
Reclaims idle host memory held by internal pools and metadata after loading spikes, without lowering burst ceilings or regressing active frame performance.
Changes
shadow::POOL(shadow.rs,host.rs): Keeps the 512 MiB burst ceiling (POOL_BYTES), but tracks peak live demand across 128-submission windows (trim_snapshot_pool()called once per submission inHost::execute) and trims excess pooled buffers down to recent peak demand (floorPOOL_SPARE_BYTES = 32 MiB).texture::BUFFERSand bounded per-threadScratch(texture.rs): TrimsBUFFERS.freedown to recent 64-submission peak usage (floor8 MiB, ceiling64 MiB), and caps per-threadScratchretention at8 MiBcheckingcapacity() <= KEEP(avoiding 32 MiB/thread permanent retention).Heap::pendingvector (descriptor.rs): Usesstd::mem::take(&mut *pending)when initializingHostHeapso the boot-timeVec<u32>capacity is freed immediately.library::CREATED(pipeline/library.rs): Stores 16-byte BLAKE3 key prefixes ([u8; 16]) instead of heap-allocatedStrings for 60k+ created pipeline keys.BITMAP_POOL_CAP(memory.rs): Lowers the idle dirty-page bitmap pool cap from64(16 MiB) to16(4 MiB).Verification
cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warningscargo xwin clippy --workspace --all-targets --all-features --locked --target x86_64-pc-windows-msvc -- -D warningscargo test -p xtask --locked(6/6 passed)snapshot_pool_trims_after_idle_window,note_created_counts_total_and_distinct_keys,mark_runs_are_reused_and_joined,memory::tests::bitmap_pool_reuses_and_zeroes_across_resets+ 5 non-DXCmemory::testspassed (9/11 passed; the 2 DXC-dependentmemory::testsfail on Wine'sd3dcompiler_47due to pre-existingE5017: Unbounded resource arrays).Partial Overlaps / Related PRs
#2153and#472also touchshadow::POOL(lowering the hard cap or draining on allocation failure); this PR preserves the 512 MiB burst cap and trims only after idle submission windows.#2972trimstexture::Chunks,HostList::scratch, andshadow::Arena; this PR covers the complementarytexture::BUFFERS, per-threadtexture::Scratch,shadow::POOL,Heap::pending,library::CREATED, andmemory::BITMAP_POOL_CAP.Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T14:08:30Z.
Review: APPROVED
Verified on RTX 5090 against
origin/main(63bcca4):cargo test -p victory-gpu --lib shadow:: -- --test-threads=1: 11/11 passed (includingsnapshot_pool_trims_after_idle_window).cargo test -p victory-gpu --lib pipeline:: -- --test-threads=1: 24/24 passed (includingnote_created_counts_total_and_distinct_keys);texture::: 2/2 passed.cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Cleanly reclaims idle snapshot/staging pools and compacts pipeline/heap metadata.
Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by potatoe0100 on 2026-11-26T14:11:00Z.
Tested
d2d85cbmerged ontomain63bcca4on an RTX 4060 8 GB (driver 621.14, i5-13600KF, Windows 11 Pro 26300). No game files.cargo test -p victory-gpu --lib --locked -- --test-threads=1gives 231 passed and 2 failed. The newsnapshot_pool_trims_after_idle_windowandnote_created_counts_total_and_distinct_keyspass. The 2 failures are the known capped-heap ones (#1835), the same as onmainwith this card.Two things from reading it:
SnapshotPool::live_bytescan only drift upward.takeadds a buffer's capacity andgive_backsubtracts it. But snapshot segments only go back throughgive_backinShadow::snapshot(merging) and whenpendingempties.impl Drop for Shadowreturns its slice and marks, notsegments. So if a copy is dropped while a snapshot is still pending, its bytes stay counted as live forever.step_windowthen setspeak_bytes = live_bytes, so over a long session the trim budget creeps up toPOOL_BYTESand stops trimming. That's harmless, but it's the opposite of what the PR is for. One option is to count the bytes taken within each window instead of a running live total, so nothing has to be given back to keep the count right.Scratchkeep limit 32 MiB → 8 MiB. A 2048×2048 RGBA8 texture needs a 16 MiB scratch, so those are no longer reused. On this CPU, a fresh 16 MiBVecfilled once costs 4.8 ms, against 0.7 ms reused, because of first-touch page faults (8 MiB: 2.1 vs 0.5 ms; 32 MiB: 7.3 vs 1.8 ms; a standalonerustc -Oloop, median of 20). That's about 4 ms more per such texture on the thread preparing the frame, while streaming. Unless the 24 MiB per thread is a real problem, 16 MiB, or keeping the old 32 MiB with thelen→capacityfix, may be a safer default. Thecapacityfix itself looks right.AI-assisted (Claude)
Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-26T22:56:09Z.
Thanks for catching both! Updated PR #3230:
SnapshotPool::live_byteswith per-submissionsubmission_bytes(reset eachstep_window()), so a droppedShadowwith pending snapshots cannot leak intopeak_bytesand defeat idle trimming; updatedsnapshot_pool_trims_after_idle_windowto cover a dropped buffer.texture::ScratchKEEPfrom8 MiBto16 MiB(still checkingcapacity() <= KEEP) so 2048x2048 RGBA8 textures reuse their scratch buffer without page faults.Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T00:16:02Z.
Review: APPROVED
Reviewed and verified
bc4382251a2ec76003877fdd1f787acb4d9bb6c8on both RTX 5090 and WARP withVICTORY_D3D12_DEBUG=1.Checks run:
cargo xtask policy— passcargo clippy --tests -p victory-gpu -- -D warnings(default and--features diagnostics) — cleanshadow::tests(10/10),pipeline::library::tests(12/12),texture::tests(2/2),memory::tests(8/8 on WARP, 7/7 on RTX 5090 excluding the pre-existinglists_share_a_table_and_copies_keep_their_viewsbaseline failure),descriptor::tests(7/7 on WARP) — all pass with 0 D3D12 Debug Layer errorstexture::recycle()across two 64-submission windows (36 MiB -> 36 MiB -> 4 MiBunder the 8 MiB spare cap) andScratch's 16 MiBcapacity()retention check — passes cleanly on both RTX 5090 and WARP with 0 Debug Layer errors.Lifetimes and locking look sound:
texture::recycle()runs inbindless::uploads(bindless.rs:204) before the next submission's uploads once the prior submission has completed,shadow::trim_snapshot_pool()runs inhost.rs:1429after GPU completion andrelease_retired(), andstd::mem::take(&mut *pending)(descriptor.rs:101) releases the pending vector's capacity while holding the lock before publishingOnceLock.One non-blocking edge case worth polishing in a follow-up (
crates/victory-gpu/src/shadow.rs:132-139):SnapshotPool::trim_to,total += v.capacity(); total <= max_byteskeeps adding dropped buffers' capacities intototal. Since buffers are sorted descending, if a single pooled buffer exceedsbudget(e.g.[64 MiB, 16 MiB, 8 MiB]trimmed toPOOL_SPARE_BYTES = 32 MiB),totaljumps to 64 MiB on the first buffer and evicts the 16 MiB and 8 MiB buffers as well (emptying the pool to 0 bytes instead of retaining 24 MiB of warm spare buffers). Guarding the increment fixes it:LGTM!
Imported from Marvisak/VIctory. Originally posted by pickles976 on 2026-11-27T01:32:37Z.
Wine verification at clean exact head
bc4382251a2ec76003877fdd1f787acb4d9bb6c8, base63bcca43c6a00a758510398fa43096ce7e4f7df0:1 passed,0 failed/ignored,0.00s reported test time; fresh compilation8.12s from the intended checkout, total guarded command8.85s. Rust1.98.1 Windows-MSVC, Wine11/Linux6.17, i7-13620H/15693MiB RAM. CPU ownership/pool policy only: no D3D12 device or GPU execution in this fixture. No source changes; touching crate roots only forced correct shared-target provenance.
The fixture checks the first window retains its96MiB peak demand despite a taken buffer being dropped, and the next idle window trims the two retained32MiB buffers to one. It does not touch/measure all reserved pages, prove an RSS delta, or test title progress.
Evidence:
.compat-runtime/logs/verify-pr3230-idle-pool-20261127T012936Z/, including exact command/provenance and machine-readable build/test/resource observations. External9GiB scope/512MiB swap and2048MiB available-RAM floor remained intact.Not tested: matched base, title, full suite, physical RAM release or staging GPU fences. This bounds/reclaims idle snapshot capacity, not aggregate live shadows/XMem/process RAM. Our older main owner census sampled idle snapshots/standalone staging at zero; that is a different stack/stage, not proof they remain zero in the later scoped-backing stack. No new title trial justified by this isolated pass. The already-discussed oversized-leading-buffer over-trim remains a warm-spare-policy edge, not new evidence from this run.
AI assistance: omp (
openai-codex/gpt-6-astra) for verification and reporting.Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-27T11:19:27Z.
Thanks @fuzeofficlal for the review and catching that edge case! Fixed
SnapshotPool::trim_toso dropped oversized buffers are not added tototal(retaining smaller warm spare buffers within budget), and addedsnapshot_pool_trim_to_retains_smaller_buffers_past_oversizedto cover it.Imported from Marvisak/VIctory. Originally posted by farhankhan8571811-star on 2026-11-28T10:12:35Z.
I benchmarked the snapshot pool at this head (
40cbe842) against main (63bcca43), #3442 and #3384 in a CPU-only harness. Pool code is copied verbatim from each head; the caller (Shadow::writing's merge and the copy-back return) is ported once from main, as it is the same at all four heads apart from #3384'sPOOL.call syntax.trim_snapshot_pool()runs once per submission, as inHost::execute; every 128th call is a trim window (trim_tothe window's peak,clamp(32 MiB, 512 MiB)). The workload is synthetic: no game files, no GPU. Claim: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7226578104The idle trim releases memory in most cases I tried. It has a reload cost, and in one case its trim windows run but evict nothing.
The sequence is load (230 world submissions) → idle (1000 small-only submissions, eight trim windows) → reload (230):
The cost is re-allocation in the first one or two reload submissions. Observed reload p50 medians were within 0.9 ms of main's; no equivalence test was performed. Maxima varied up to 2.7× between repeated same-seed runs, so the last column is approximate.
Where it does not work: a large set in pieces that fills the 512 MiB cap (256 MiB in 4 growth steps: 64 + 128 + 192 + 256 MiB).
trim_toskips buffers that don't fit and keeps smaller ones, so the pool keeps 256 + 192 + 64 = 512 MiB and drops the small buffers returned afterwards. Main's rule stops at the first buffer that crosses the cap (256 + 192, then small buffers). For[400, 200, 64]MiB, main keeps 400 and this PR 400 + 64.takeaddsv.capacity()of what it hands out (self.submission_bytes += v.capacity();). Small requests get the large leftovers, so the window sum reaches the cap and the budget clamps to 512 MiB. All eight idle windows ran, none evicted anything, and 31% of small takes miss. At a smaller scale, an idle period of one 5 MiB snapshot per submission keeps 44 MiB.40cbe842added the skip rule on purpose, and it has a measured upside: when the harness returns a quarter of the small shadows one submission late, this PR had the lowest steady miss rate of the six policies I ran (0.1% median, 264 MiB allocated over 460 submissions; main 2.3%, seeds 0.1–11.1%, and 2.3 GiB).Hypothetical variants, to separate the two effects (not proposals):
ninstead of capacitygive_back, window unchangednEach has a measured cost, so I'm not suggesting one. Counting
nis closer to the description's "demand", but both counts sum every take in a submission, including growth pieces returned before it ends, so neither is live demand. A live-bytes window held 15–20 MiB at idle and re-allocated more than either count when large snapshots were rare. From the code, not measured: if submissions stop, the window never advances, so nothing is trimmed until they resume.Limitations: the submission mix is a model, not a capture; marked writes (which skip snapshots) aren't modelled; snapshot lifetimes are the harness's (returned at copy-back, or one submission late), not upstream's (returned when the shadow's last pending range completes, not measured); sizes are assumptions, and 174 MiB is the earlier-discussed heap size; page faults are process-wide, so none are attributed to the pool; pool figures are pool bytes, not process commit, unless "peak commit" is stated; one Windows machine at 79–95% memory load (at run end), single-threaded, 5 seeds per case, no significance test.
With real per-submission snapshot sizes I can rerun.
Harness, pins, scripts, raw results and reproduction steps: 1,240 original runs plus 631 targeted follow-up runs (36 superseded runs kept, labelled). Byte-for-byte snapshot verification: 6,288 submissions, then 96,096 snapshots, 0 failures. Corrections to my earlier write-ups:
CORRECTIONS.md.https://github.com/farhankhan8571811-star/victory-snapshot-pool-bench
AI-assisted (Claude Code).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.