Repository navigation
GPU: deduplicate HostList::keep refs, stack-allocate user_data, and avoid double CBV shadow lookup - #3379
GPU: deduplicate HostList::keep refs, stack-allocate user_data, and avoid double CBV shadow lookup#3379Texbirds wants to merge 1 commit into
Conversation
…void double CBV shadow lookup
Dukestar69
left a comment
There was a problem hiding this comment.
LGTM — verified on RTX 5090 (Windows / D3D12) against origin/main (63bcca4).
- Deduplicating
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
left a comment
There was a problem hiding this comment.
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 with VICTORY_D3D12_DEBUG=1:
HostList::keepdeduplication (crates/victory-gpu/src/host.rs:1038, 2289-2300):- Keeping the strong
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). - Verified with an extra COM lifetime/interface probe keeping a buffer via both
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.
- Keeping the strong
- Stack-allocated
user_data(crates/victory-gpu/src/draw.rs:751-759):- Correctly zero-initializes up to 64 dwords on the stack and falls back to
Vecfordwords > 64.
- Correctly zero-initializes up to 64 dwords on the stack and falls back to
- Single
shadow::findforView::Constant(crates/victory-gpu/src/draw.rs:1902-1905):- Since
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>.
- Since
- Verification:
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!
|
Windows 11,
Synthetic only. AI-assisted. |
Desamodeth
left a comment
There was a problem hiding this comment.
The global HostList::keep set 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.
| Distinct buffers | main → PR, µs/list | Retained Rust heap, bytes |
|---|---|---|
| 1 | 20.89 → 15.16 | 8,192 → 84 |
| 128 | 20.76 → 18.88 | 8,192 → 3,344 |
| 256 | 20.79 → 22.08 | 8,192 → 6,672 |
| 1,024 | 20.85 → 40.92 | 8,192 → 26,640 |
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 63bcca43 with 9e13b518 merged onto that base. Precreate 1,024 real host.default_buffer(256) resources. On an open list call list.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.
|
List-call numbers for this PR (head
Method. Bases. 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 Correctness. 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). |
Problem
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.