Skip to content

GPU: deduplicate HostList::keep refs, stack-allocate user_data, and avoid double CBV shadow lookup - #3379

Open
Texbirds wants to merge 1 commit into
Marvisak:mainfrom
Texbirds:perf/gpu-draw-keep-dedup-and-zero-alloc
Open

Texbirds wants to merge 1 commit into
Marvisak:mainfrom
Texbirds:perf/gpu-draw-keep-dedup-and-zero-alloc

Conversation

@Texbirds

Copy link
Copy Markdown
Contributor

Problem

Three strictly redundant per-draw/per-stage operations run on the command-recording hot path in crates/victory-gpu/src/draw.rs and crates/victory-gpu/src/host.rs:

  1. Duplicate COM keep references per draw (host.rs:2262, called from draw.rs:676 and draw.rs:818): State::prepare calls list.keep(table) for the bound heap's mark table on every draw and for VIEW_STRIDES on every shader stage. HostList::keep unconditionally calls COM QueryInterface::<IUnknown>() (atomic AddRef) and pushes into Deferred::kept without deduplication, accumulating thousands of duplicate IUnknown references to the same heap buffers per list and running thousands of redundant Release calls on HostList::reset.
  2. Per-stage heap allocation for root constants (draw.rs:751): State::prepare allocates vec![0u32; dwords as usize] on every shader stage of every draw/dispatch, even though D3D12 root constants are capped at 64 dwords.
  3. Double global-lock shadow::find for table CBVs (draw.rs:1894-1897): readable calls crate::shadow::find(address, 1) to get s, computes len = u64::from(size).min(s.left(address)) (which is already bounded by s), and then calls crate::shadow::find(address, len) a second time — re-locking global BUFFERS and scanning the BTreeMap to return the same Arc<Shadow>.

Change

  • HostList::keep (crates/victory-gpu/src/host.rs): Track kept raw COM pointers in Deferred::kept_ptrs: HashSet<usize> (cleared alongside kept by Deferred::default() in HostList::reset). Duplicate keep calls on the same live interface return immediately without QueryInterface/AddRef or growing kept. Because kept holds the IUnknown until reset, the raw pointer cannot be reused by another object while the list is open.
  • Stack buffer for user_data (crates/victory-gpu/src/draw.rs): Pack root constants into a stack [0u32; 64] slice when dwords <= 64, falling back to Vec only if dwords > 64.
  • Single shadow::find for View::Constant (crates/victory-gpu/src/draw.rs): Return (s, address, len) directly from the first crate::shadow::find(address, 1) call.

Test

  • host::tests::keep_deduplicates_repeated_objects_until_reset (Windows, synthetic HostList + upload buffers, no game data): calls list.keep(&a) 1,000 times and asserts kept.len() == 1 / kept_ptrs.len() == 1, verifies a second distinct buffer increments to 2, and verifies reset() clears both so keep(&a) inserts again on the next use.

Verification & scope

  • Zero-tradeoff removal of redundant work on the recording path; no retail FPS delta is claimed without a live capture.
  • Not run locally (no Rust toolchain installed on this machine); relying on CI (check) for Windows build, Clippy -D warnings, and unit tests.

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM — verified on RTX 5090 (Windows / D3D12) against origin/main (63bcca4).

  • Deduplicating HostList::keep references until reset, stack-allocating user_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 (including keep_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 fuzeofficlal 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.

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::keep deduplication (crates/victory-gpu/src/host.rs:1038, 2289-2300):
    • Keeping the strong IUnknown in Deferred::kept ensures the object stays alive (preventing COM pointer reuse) for the entire life of the open command list; HostList::reset clears both kept and kept_ptrs together via *self.deferred() = Deferred::default(); and a failed cast::<IUnknown>() rolls back kept_ptrs.remove(&raw).
    • Verified with an extra COM lifetime/interface probe keeping a buffer via both ID3D12Resource and IUnknown, dropping all caller references before host.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.
  • 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 Vec for dwords > 64.
  • Single shadow::find for View::Constant (crates/victory-gpu/src/draw.rs:1902-1905):
    • Since len = u64::from(size).min(s.left(address)) is already bounded by s's range [start, end), a second shadow::find(address, len) on both BUFFERS (innermost matching interval) and loose (4 MiB aligned window) is guaranteed to resolve to the same Arc<Shadow>.
  • Verification: cargo xtask policy (191 files ok), cargo clippy --locked -p victory-gpu --tests -- -D warnings (default and --features diagnostics), and all 22/22 draw::tests::* + keep tests/probes pass on both RTX 5090 and WARP.

LGTM!

@Jackster

Copy link
Copy Markdown

Windows 11, 63bcca4 + this PR, Radeon RX 7900 XTX:

  • host::tests::keep_deduplicates_repeated_objects_until_reset by name: 1 passed.
  • cargo test -p victory-gpu --lib: 232 passed / 0 failed / 1 ignored (unpatched 63bcca4 here: 231).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.

Synthetic only. AI-assisted.

@Desamodeth Desamodeth left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@KainanP

KainanP commented Nov 27, 2026 •

Copy link
Copy Markdown

List-call numbers for this PR (head 9e13b51), ns per draw:

main main + #3379 stack stack + #3379
1 thread 2506 / 2557 2479 / 2452 (−1 % / −4 %) 1110 / 1114 1061 / 1047 (−4.4 % / −6.0 %)
5 threads 8959 / 9047 8993 / 8774 (noise) 1488 / 1460 1431 / 1419 (−3.8 % / −2.8 %)

Method. pipeline::list_bench from #2586 (fork branch perf/list-call-bench) with VICTORY_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 --lib test 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 on main is 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::find for table CBVs, is not on this bench's path. On main, 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 on main here. keep_deduplicates_repeated_objects_until_reset passes. On the stack: 238 passed, 3 failed, the same 3 as the stack without this PR (the stack itself fails 3: the #1744 pair, plus a_list_lends_a_read_copy_only_while_it_is_the_textures in the stack's port of #3405. That test passes on #3405's own head, 232/2 here). I checked the dedup: kept is only ever emptied by *self.deferred() = Deferred::default() at reset, which clears kept_ptrs with 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 in readable: the stack calls list.find_shadow (#1356's per-list cache) where main calls crate::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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants