GPU: bound shadow buffer lookup by maximum registered size instead of 64 entries #2094
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2096"
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 fuzeofficlal on 2026-11-25T04:49:28Z.
Refs #906.
Summary
.take(64)cap inshadow::find(crates/victory-gpu/src/shadow.rs) with a reverse scan bounded by the maximum buffer size currently registered inBUFFERS(lowest = end.saturating_sub(max_size), returningNoneimmediately whenlowest > address).max_sizeonBuffers::insertand recompute it onBuffers::removewhen the previous maximum-size entry is unregistered, so misses reachingloose()stop at the live maximum buffer bound while lookups still reach a covering outer buffer past more than 64 smaller inner buffers.Verification
shadow::tests::find_reaches_covering_buffer_past_many_inner_buffersshadow::tests::find_stops_at_max_size_bound_and_tightens_on_unregistercargo clippy -p victory-gpu --tests --locked -- -D warningsandcargo xtask policy: passed.Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T04:56:57Z.
Review: APPROVED
Verified locally merged onto
origin/main(940d32f, Windows 11 x64, Rust 1.98.1 MSVC):cargo test -p victory-gpu --lib shadow::— 6 passed, 0 failed (includingoverflowing_ranges_are_rejected_without_panicandfind_scans_past_sixty_four_siblings_and_picks_tightest_equal_start)cargo clippy -p victory-gpu --all-targets— 0 warningscargo xtask policy— ok (183 files ok)Clean overflow guards on shadow range arithmetic and complete sibling scan in
find(). Looks good.AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T05:02:21Z.
Review: COMMENTED
Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.
Shadow range arithmetic overflow-guarded + scan — memory-safety hardening on the shadow-memory path.
[PR #2096] GPU: guard shadow range arithmetic against overflow and scan all buffers in find()to [PR #2096] GPU: bound shadow buffer lookup by maximum registered size instead of 64 entriesImported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T10:36:25Z.
Automated triage for the maintainers.
Merge candidate.
shadow::findnow scans backwards only fromend - max_size(find_buffer), whereBufferstracks the largest registered length and recomputes it when the largest entry is removed or replaced; a buffer that covers[address, end)must start at or after that bound, so lookups still reach an outer buffer past many smaller ones while misses reachingloose()stop early. The overflow guards and the equal-start tie-breaker are gone. #402 overlaps but currently has merge conflicts; this one merges cleanly. CI passes.On your reply: confirmed in
2f43424, this is what was asked. A minor follow-up, not a blocker:Buffers::removerescans the whole map each time an entry of the maximum size goes, which is O(n) per release if many buffers share that size; a size-count map would make it O(log n).Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T10:51:58Z.
Updated in
2f434243per triage feedback:find_bufferby the maximum currently registered buffer size (lowest = end.saturating_sub(max_size)over(lowest, 0)..=(address, usize::MAX)), tighteningmax_sizeonunregisterwhen the largest buffer is removed so misses stop early instead of walking the map.u64::MAXoverflow/saturating guards and the equal-start tie-breaker.Imported from Marvisak/VIctory. Originally posted by Desamodeth on 2026-11-26T21:10:37Z.
Code review location:
crates/victory-gpu/src/shadow.rs; line 831Keep the bound exact on replacement and removal. This is the size-count variant measured above.
Imported from Marvisak/VIctory. Originally posted by Desamodeth on 2026-11-26T21:10:37Z.
Review: COMMENTED
The full rescan mentioned in triage is visible in both removal and in-place replacement on
2f434243. With 4,096 equal 4 KiB buffers, removing them one by one visits 8,386,560 remaining entries under the registry mutex. Shrinking all 4,096 at the same keys visits 16,777,216 entries.CPU time per batch of 4,096 mutations, across two opposite-order runs:
63bcca43The suggestion keeps the PR's exact lookup bound and stores one count per distinct length. The extra live heap requested by the index was 192 B for one length, 2,016 B for 64 lengths and 139,968 B for 4,096 lengths. Registering the all-distinct batch also took 0.34–0.36 ms vs 0.16–0.21 ms here. I tested a single counter for ties too. It fixed the equal-size case, but largest-first removal still took 9.27–9.30 ms.
Isolated Windows x64 registry benchmark, Ryzen 7 9800X3D, rustc 1.98.1 release, production mutex and map operations. Each sample started empty, registered disjoint buffers and performed one hit per buffer before timing mutations. Eleven timed samples per case in each run. Separate allocation-counting binary. Checked exact maxima after 6,000 mixed mutations and 18,000 lookups against an exhaustive reference, including shared start addresses and replacements. The covering buffer beyond 64 siblings is still found. The suggestion applies to this head and to this PR applied over current main. Full crate, D3D12 and in-game frequency were outside this CPU measurement. AI-assisted with Codex.
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T21:34:23Z.
@Desamodeth Thanks for benchmarking those equal-size and descending-removal workloads — the 16x–125x slowdown under the
BUFFERSmutex was a great catch!I rebased onto current
main(63bcca43) and updatedBuffersin91104672to track live registration counts per buffer size insizes: BTreeMap<u64, usize>, withmax_size(&self) -> u64readingself.sizes.keys().next_back().copied().unwrap_or(0). Inserts, in-place replacements (equal/grow/shrink), and removals are now O(log n) without any full-map rescan while preserving the exact lookup bound.Added
shadow::tests::max_size_tracks_equal_sizes_replacements_and_descending_removalscovering equal-size ties, in-place replacements, descending removals, and empty reset; verified all 13shadow::tests on both NVIDIA GeForce RTX 5090 and WARP underVICTORY_D3D12_DEBUG=1, plusclippy(default and--all-features) andxtask policy.Imported from Marvisak/VIctory. Originally posted by Desamodeth on 2026-11-26T21:48:53Z.
Code review location:
crates/victory-gpu/src/shadow.rs; line 831Verified the fix in
91104672on Windows x64. On the same 4,096-buffer CPU benchmark, equal-size teardown fell from 6.36 ms to 0.13–0.15 ms, replacing 4 KiB registrations with 2 KiB fell from 12.58–12.68 ms to 0.102 ms, and largest-first teardown fell from 6.37 ms to 0.29–0.31 ms. The two CPU-only registry tests passed. The new code also matched an exhaustive reference across 6,000 mutations and 18,000 lookups, including a covering buffer behind 96 siblings.One trade-off for maintainers: with 4,096 distinct sizes, fetching the maximum from the tree costs ~6 ns per scattered hit (47 ns vs 41 ns with a cached maximum). At one size the difference was ~1 ns. Both variants used the same size-index heap. The original teardown concern is addressed. Synthetic registry timings only, 11 samples in each of two opposite-order runs. AI-assisted with Codex Astra 6.2 High.
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.