GPU: deduplicate HostList::keep refs, stack-allocate user_data, and avoid double CBV shadow lookup #3379
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3379
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3379"
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?
@Texbirds on GitHub · 2026-11-26 20:58 UTC · original
Texbirds:perf/gpu-draw-keep-dedup-and-zero-alloc→main· +46 −4 · 2 files · 1 commits · openProblem
Three strictly redundant per-draw/per-stage operations run on the command-recording hot path in
crates/victory-gpu/src/draw.rsandcrates/victory-gpu/src/host.rs:keepreferences per draw (host.rs:2262, called fromdraw.rs:676anddraw.rs:818):State::preparecallslist.keep(table)for the bound heap's mark table on every draw and forVIEW_STRIDESon every shader stage.HostList::keepunconditionally calls COMQueryInterface::<IUnknown>()(atomicAddRef) and pushes intoDeferred::keptwithout deduplication, accumulating thousands of duplicateIUnknownreferences to the same heap buffers per list and running thousands of redundantReleasecalls onHostList::reset.draw.rs:751):State::prepareallocatesvec![0u32; dwords as usize]on every shader stage of every draw/dispatch, even though D3D12 root constants are capped at 64 dwords.shadow::findfor table CBVs (draw.rs:1894-1897):readablecallscrate::shadow::find(address, 1)to gets, computeslen = u64::from(size).min(s.left(address))(which is already bounded bys), and then callscrate::shadow::find(address, len)a second time — re-locking globalBUFFERSand scanning theBTreeMapto return the sameArc<Shadow>.Change
HostList::keep(crates/victory-gpu/src/host.rs): Track kept raw COM pointers inDeferred::kept_ptrs: HashSet<usize>(cleared alongsidekeptbyDeferred::default()inHostList::reset). Duplicatekeepcalls on the same live interface return immediately withoutQueryInterface/AddRefor growingkept. Becausekeptholds theIUnknownuntilreset, the raw pointer cannot be reused by another object while the list is open.user_data(crates/victory-gpu/src/draw.rs): Pack root constants into a stack[0u32; 64]slice whendwords <= 64, falling back toVeconly ifdwords > 64.shadow::findforView::Constant(crates/victory-gpu/src/draw.rs): Return(s, address, len)directly from the firstcrate::shadow::find(address, 1)call.Test
host::tests::keep_deduplicates_repeated_objects_until_reset(Windows, syntheticHostList+ upload buffers, no game data): callslist.keep(&a)1,000 times and assertskept.len() == 1/kept_ptrs.len() == 1, verifies a second distinct buffer increments to2, and verifiesreset()clears both sokeep(&a)inserts again on the next use.Verification & scope
check) for Windows build, Clippy-D warnings, and unit tests.@Dukestar69 on GitHub · 2026-11-26 21:07 UTC · original · approved these changes
LGTM — verified on RTX 5090 (Windows / D3D12) against
origin/main(63bcca4).HostList::keepreferences until reset, stack-allocatinguser_data, and avoiding the redundant CBV shadow lookup cleanly trim per-draw overhead.cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 20 passed, 0 failed.cargo test -p victory-gpu --lib host::tests:: -- --skip dynamic_slots_are_held_until_released --test-threads=1: all passed (includingkeep_deduplicates_repeated_objects_until_reset).cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@fuzeofficlal on GitHub · 2026-11-26 21:21 UTC · original · approved these changes
Clean, zero-tradeoff hot-path optimizations. Audited all three changes and verified merged onto
origin/main(63bcca43) on both NVIDIA GeForce RTX 5090 and Microsoft WARP withVICTORY_D3D12_DEBUG=1:HostList::keepdeduplication (crates/victory-gpu/src/host.rs:1038, 2289-2300):IUnknowninDeferred::keptensures the object stays alive (preventing COM pointer reuse) for the entire life of the open command list;HostList::resetclears bothkeptandkept_ptrstogether via*self.deferred() = Deferred::default(); and a failedcast::<IUnknown>()rolls backkept_ptrs.remove(&raw).ID3D12ResourceandIUnknown, dropping all caller references beforehost.execute, resetting, and allocating a fresh buffer (which actually reused the exact same raw COM pointer address on both RTX 5090 and WARP) — clean with zero D3D12 Debug Layer errors.user_data(crates/victory-gpu/src/draw.rs:751-759):Vecfordwords > 64.shadow::findforView::Constant(crates/victory-gpu/src/draw.rs:1902-1905):len = u64::from(size).min(s.left(address))is already bounded bys's range[start, end), a secondshadow::find(address, len)on bothBUFFERS(innermost matching interval) andloose(4 MiB aligned window) is guaranteed to resolve to the sameArc<Shadow>.cargo xtask policy(191 files ok),cargo clippy --locked -p victory-gpu --tests -- -D warnings(default and--features diagnostics), and all 22/22draw::tests::*+keeptests/probes pass on both RTX 5090 and WARP.LGTM!
@Jackster on GitHub · 2026-11-26 22:34 UTC · original
Windows 11,
63bcca4+ this PR, Radeon RX 7900 XTX:host::tests::keep_deduplicates_repeated_objects_until_resetby name: 1 passed.cargo test -p victory-gpu --lib: 232 passed / 0 failed / 1 ignored (unpatched63bcca4here: 231).cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.Synthetic only. AI-assisted.
@Desamodeth on GitHub · 2026-11-27 06:35 UTC · original · reviewed
The global
HostList::keepset pays off for repeated buffers. With 1,024 distinct buffers it nearly doubles CPU time and more than triples retained Rust heap. Please qualify the “zero-tradeoff” claim in the description.1,024 calls per command list on an RTX 5070. CPU time includes recording those references and resetting the list.
The direction held in all 12 pairs for each row. Reuse matters even with 1,024 distinct buffers: at 8,192 calls the PR wins (156.11 → 138.87 µs). A frame capture tallying total and distinct keeps per list would tell us how to weight these cases. If one-off keeps are common, freshly allocated texture-copy buffers are one place to bypass the set while preserving deduplication for mark/stride tables.
Reproduction and scope
Windows, Ryzen 7 9800X3D, NVIDIA 621.40, Rust 1.98.1, release. Compared
63bcca43with9e13b518merged onto that base. Precreate 1,024 realhost.default_buffer(256)resources. On an open list calllist.keep(&resources[i % distinct])in order, check the retained-reference count, close, reset and check that the count is zero. Time only the keep loop and reset. Four warm-ups per case, then 96 lists for the 1,024-call cases and 20 lists for 8,192 calls. Values are medians across 12 process pairs alternating main/PR and PR/main. Separate instrumented binaries measured net Rust allocation bytes on the recording thread immediately after the loop. The PR's existing keep/execute/reset test also passed on this adapter. This isolates resource bookkeeping; game frame time and VRAM consumption were not measured.AI-assisted with Codex Astra 6.2 High.
@KainanP on GitHub · 2026-11-27 12:02 UTC · original
List-call numbers for this PR (head
9e13b51), ns per draw:Method.
pipeline::list_benchfrom #2586 (fork branchperf/list-call-bench) withVICTORY_LIST_BENCH_SCALING=1: the draw sequence (root CBV, 4 root constants, index buffer, indexed draw that reads 6 textures through a table and 3 root CBVs). Each thread records its own list, best of 5 × 20,000 draws. Each row is the median of 7 interleaved runs, and there were two separate batches of 7 in opposite config order (shown as batch 1 / batch 2). Release--libtest build, RTX 4060 Laptop, Windows 11. Nothing else heavy was running (an editor and a browser, CPU 7–41 % between rounds, no game). The 5-thread row onmainis noisy (single runs from 7.4 to 12.8 µs), so deltas under ~5 % there are not resolvable.Bases.
main=63bcca4+ bench commits. Stack =main+ #1978 + #2821 + #3258 + #3274 + #3275 + rebased #1356 + #3405 (the stack in #2586).Reading it. Of the PRs I measured on this path (#3249, #1825, #3218, this one), this is the only one that improves both thread rows on the stack, in both batches. The 1-thread ranges barely overlap (stack 1056–1218, + #3379 1006–1078). The draw sequence keeps the marks table every draw and packs root constants every stage, which are two of the three things this PR removes. The third, the second
shadow::findfor table CBVs, is not on this bench's path. Onmain, the 5-thread row is dominated by shared locks that the stack removes, so the gain only shows clearly once those are gone.Correctness.
cargo test -p victory-gpu --lib(release) on the RTX: 230 passed, 2 failed. The failures are the #1744 heap-cap pair, also failing onmainhere.keep_deduplicates_repeated_objects_until_resetpasses. On the stack: 238 passed, 3 failed, the same 3 as the stack without this PR (the stack itself fails 3: the #1744 pair, plusa_list_lends_a_read_copy_only_while_it_is_the_texturesin the stack's port of #3405. That test passes on #3405's own head, 232/2 here). I checked the dedup:keptis only ever emptied by*self.deferred() = Deferred::default()atreset, which clearskept_ptrswith it. So a pointer in the set is always one the list still holds, and it can't be reused by another object while the list is open. Merging onto the stack conflicts with rebased #1356 inreadable: the stack callslist.find_shadow(#1356's per-list cache) wheremaincallscrate::shadow::find. The same one-lookup change applies there. #1825 does the stride-table half of the keep dedup. With this PR, that half is covered by the general dedup.Not measured. Lists with root constant parameters over 64 dwords (the Vec fallback), and any game frame.
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.