GPU: store HostHeap kept/made sparsely and allocate strides/marks lazily #2969
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2971"
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-26T01:48:52Z.
Summary
HostHeap::keptandHostHeap::madesparsely inHashMap<u32, ...>instead of dense ~1.26M-elementVecs per view heap.HostHeap::stridesandHostHeap::marksupload buffers lazily viaOnceLockon first non-null write or root-view bind (matchingHostHeap::addresses), avoiding ~26 MiB of upload memory on empty/unused heaps.HostHeap::evictnow iterates only occupied entries inkeptinstead of scanning all 1M+ slots.Testing
cargo xwin check --tests --profile diag -p victory-gpu --features diagnostics --target x86_64-pc-windows-msvc --lockedpasses cleanly.descriptor::host::testsunder Wine (RTX 3060): 15 passed (including newempty_view_heap_defers_stride_and_mark_tables); the 1 failure (dynamic_slots_are_held_until_released) is the pre-existing NVIDIA 1M view heap cap.Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T02:01:10Z.
Review: COMMENTED
Reviewed and verified against
main(63bcca43) on Windows 11 (RTX 5090 + WARP,VICTORY_D3D12_DEBUG=1) — solid and ready to merge:HashMapstorage formade/keptand lazyOnceLockinitialization ofstrides(zero-filled) andmarks(0xFF-filled) incrates/victory-gpu/src/descriptor/host.rspreserve all bounds checks (keep,mirror,write_stride,set_mark),sync_slotsdiffing,retirelifetime handling on overwrite/null,evict, anddiagnosticsslot inspection.98c38907(write_stride(index, if buffer.is_some() { 4 } else { 0 })) cleanly avoids eagerly allocating thestridesbuffer for null raw buffer views while still zeroing out any previously stored stride whenstridesalready exists.Local verification:
victory-gpu(descriptor::,host::tests::,memory::tests::withVICTORY_D3D12_DEBUG=1, tested at638b3353plus verified 1-line98c38907diff): 45/45 passed on both RTX 5090 and WARP (excluding the 2 pre-existing NVIDIA 1M view-heap baseline tests), includingempty_view_heap_defers_stride_and_mark_tablescargo clippy --locked -p victory-gpu --tests -- -D warnings(default &--features diagnostics): 0 warningscargo xtask policy: 191 files okImported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T02:30:50Z.
Review: COMMENTED
Verified the sparse conversion end to end: absence in made/kept now means never-made (matching the old zero entries), the dirty-table filter's Some(_)=>true / None=>current mapping preserves both branches, and keep() gained a bounds check it lacked. Lazy strides/marks is the clever part — write_stride(0)/set_mark(None) early-out before OnceLock creation because a fresh table reads exactly that. The addresses() replay now zero-fills first, covering slots the old dense iteration wrote None into. Real memory win on multi-million-slot heaps.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T05:43:45Z.
Automated triage for the maintainers.
Verdict: needs work. Making the stride and mark tables lazy (
strides_buf/marks_bufon aOnceLock, likeaddresses) is reasonable for view heaps that are never used. The other parts change behaviour on the hottest path incrates/victory-gpu/src/descriptor/host.rs, and the PR gives no measurement for them:madeas aHashMap.HostHeap::sync_slotslooks upmadefor every slot it syncs. Its own comment says a world frame looks at about 700,000 slots (~75 ms in run perf3). In the title the heap is dense, so a hash map saves little memory there and turns each indexed load into a hash lookup inside that loop. Please measuresync_slotsbefore and after in a world frame (the diagnostics frame report is enough). If it's slower, keepmadedense and make only the unused heaps cheap.sync_slots,None => truebecameNone => current. For a slot not in the map this matches the oldm == 0case. But the oldNonearm covered indices pastmade's length, which were always re-synced; now they're skipped unless current. Please confirmfirst..lastnever goes pastcount, or keep the old result for those indices.bufferisNone. Translated shaders read the stride table (VIEW_STRIDES), and 0 there means "no buffer view". That changes what a shader sees at a null raw slot, not only memory use. Either keep 4, or say why 0 is right for these shaders and add a test.CI: passing (
check).Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T08:50:31Z.
Independent local verification of head
98c3890on Windows 11, Rust 1.100.0/MSVC:VICTORY_D3D12_WARP=1 cargo test --locked -p victory-gpu --lib descriptor::host::tests::empty_view_heap_defers_stride_and_mark_tables— 1 passed, 0 failed.git diff --check origin/main HEAD— clean.Host GPU present: RTX 5060 Laptop (driver 621.24), AMD Radeon 780M; the test ran through WARP. Synthetic inputs only; no game assets or DRM material used. AI-assisted (Hy4 flash).
Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-26T12:09:29Z.
Addressed all triage points:
HostHeap::madeas a denseVec<u64>(lazily allocated on first mirrored view):sync_slotskeeps O(1) indexed lookups on active heaps (0 hashing overhead in the hot loop), while unused view heaps still take 0 bytes.sync_slotsout-of-bounds behavior:None if i < self.count => current, None => truematches the previousNone => truefori >= countand skips unallocated slots belowcountunlesscurrent.raw_view_at):buffer.is_none()is only used for null fallback/sentinel slots (NULL_BUFFER_VIEWand null raw pairs), where stride0("no buffer view") is accurate and avoids allocatingstrideson empty heaps. Added an assertion inempty_view_heap_defers_stride_and_mark_tables.mainvs combined branch): menu FPS remained 30.0 fps (33.3 ms/frame);system memorydropped 1,427 -> 1,407 MB (-20 MB) in menus and 6,622 -> 6,466 MB working set at world load start with 0 FPS regression.Imported from Marvisak/VIctory. Originally posted by Desamodeth on 2026-11-27T06:51:12Z.
Review: COMMENTED
Independent RTX 5070 + WARP verification, diff audit, and head-to-head Release benchmark against
main(63bcca43) and competing PR #3456 (28fde825) — synthetic fixtures only, no game files.Test & Lint Verification (
ba430844)cargo clippy --locked -p victory-gpu --lib --tests -- -D warnings(default and--features diagnostics): clean;cargo xtask policy: 191 files ok.--test-threads=1):descriptor::22/23 passed (empty_view_heap_defers_stride_and_mark_tablespasses; only failure is the pre-existing NVIDIA 1M view-heap cap assertion indynamic_slots_are_held_until_released, identical tomain),bindless::3/3 passed.VICTORY_D3D12_WARP=1,--features diagnostics,--test-threads=1):descriptor::23/23 passed,bindless::3/3 passed.Deterministic Memory & Interleaved Release Timings (RTX 5070 1M-slot heap:
count=934,464,dynamic=65,536, 5 forward+reverse pairs = 10 runs each, median [min..max])0 Beager forkept/made/strides/marks(vs46.39 MiBonmainand31.13 MiBon#3456).kept(HashMap<u32, KeptEntry>): Identical to #3456 (6.25 MiBat 130k live slots =-59.0%vsmain's15.26 MiB; crosses over at458,753slots =45.9%occupancy jumping to25.00 MiB; reaches50.00 MiBat 1M slots and never shrinks after clear).main(63bcca43)#2971(ba430844)#3456(28fde825)HostHeap::new(false, 1M)strides()+marks()[3,347..4,214]evict()scan, 1k live (0 hits)evict()scan, 130k live (0 hits)evict()500 hits, 130k liveevict()scan, 500k live (0 hits)keep()50k overwrites (130k live)keep()50k remove+insert churntextures_at()4,096 slots (130k live)Concrete Audit Notes (vs
mainand#3456)raw_pair_at(..., None, ...)still eagerly allocates the 8 MiBstridesbuffer:raw_view_atwas updated to write stride0whenbuffer.is_none(), butraw_pair_at(descriptor/host.rs:665-666) still callsself.write_stride(index, 4)unconditionally. Verified directly on RTX 5070: callingraw_pair_at(pair, None, 0)on a fresh heap flipsstridesfromfalsetotrue(marks=false,made=0 B).self.madelock: On active view heaps,HostHeap::new(6.82 ms) + firststrides()/marks()(3.71 ms) totals10.53 ms(near parity withmain's11.19 ms), and if the first trigger is a mirrored buffer view,strides_buf()/marks_buf()(upload_buffer+write_bytesover 8/16 MiB WC memory) andmade.resize(self.count, 0)execute while holdingself.made.lock(). Lazy allocation therefore only saves the 24 MiB + 7.1 MiB on instantiated view heaps that never receive buffer views or view-marking draws.keep(): Unlike #3456, #2971 callscrate::host::retire(old)and dropsArc<Shadow>while still holdingself.kept.lock(), and performs an extrakept.get(&index)lookup under--features diagnostics. Dropping the lock before retiring/droppingold(as #3456 does) is cleaner and avoids lock-order nesting.AI-assisted with Gemini 4 Neon.
Imported from Marvisak/VIctory. Originally posted by 0xKaji on 2026-11-27T11:20:08Z.
Thanks @Desamodeth for the thorough audit and benchmarks! Updated PR #2971 to address both concrete points:
HostHeap::raw_pair_at(..., None, ...)now writes stride0instead of4, avoiding eager 8 MiBstridesbuffer allocation on null pairs (and updatedempty_view_heap_defers_stride_and_mark_tablesto cover it).HostHeap::keepnow dropsself.keptlock before diagnostics checks,crate::host::retire(old), and dropping the previous entry, and avoids the extrakept.get(&index)lookup.Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-27T13:06:23Z.
I ran this through a heap-sync bench in the shape #3425 reports (
HostHeap::sync_slots, 18.1 ms/frame for ~384k slots) to check whether the sparsekeptmap costs anything on the hot path.Bench:
sync_slots_benchonbench/sync-slots(f233390e, on top off34bcfb8). Each frame, 192 random 64 KiB pages (393,216 slots, the "384k slots" of #3425) of a 2^20-slot placed, write-watched view heap are written. 0, ~9,500 or ~85,000 records among them change, and onesync_heapsruns. Each shape runs without and with 5 title threads writing a view every 20 µs. It checks every changed slot's view address after each sync, and every slot at the end. RTX 4060 Laptop. Base63bcca4and the PRs ran interleaved (7 runs for the ~9.5k row, 3 for the others). Ratios are per-run against base. #3265, which doesn't touch the sync, gave ×0.81-1.04 in the same runs, so read anything within ~±15% as noise.sync_heapsSo it's neutral for speed within noise. If anything, it's slightly slower when many slots are made (a
HashMapinsert/remove perkeep), but the ×1.03-1.12 is inside the run-to-run spread. This is fine for a memory PR, but it isn't a sync speed-up. 0 lost and 0 stale slots in every run, so the dense-lazymadeand theNone if i < self.count => currentbound behaved correctly here. In the same copy bench (copy_simple_bench, same records again), it was also within noise of base (medians 28-44 ms vs 27-43 ms alone).AI use: Claude Code (Claude Opus 5.5).
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.