Skip to content

GPU: bound shadow buffer lookup by maximum registered size instead of 64 entries - #2096

Open
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-shadow-overflow-and-find-906
Open

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-shadow-overflow-and-find-906

Conversation

@fuzeofficlal

@fuzeofficlal fuzeofficlal commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Refs #906.

Summary

  • Replace the fixed .take(64) cap in shadow::find (crates/victory-gpu/src/shadow.rs) with a reverse scan bounded by the maximum buffer size currently registered in BUFFERS (lowest = end.saturating_sub(max_size), returning None immediately when lowest > address).
  • Track max_size on Buffers::insert and recompute it on Buffers::remove when the previous maximum-size entry is unregistered, so misses reaching loose() 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_buffers
  • shadow::tests::find_stops_at_max_size_bound_and_tightens_on_unregister
  • cargo clippy -p victory-gpu --tests --locked -- -D warnings and cargo xtask policy: passed.

@DenRakEiw DenRakEiw 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.

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 (including overflowing_ranges_are_rejected_without_panic and find_scans_past_sixty_four_siblings_and_picks_tightest_equal_start)
  • cargo clippy -p victory-gpu --all-targets — 0 warnings
  • cargo 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).

@catminou4 catminou4 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.

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.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. shadow::find now scans backwards only from end - max_size (find_buffer), where Buffers tracks 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 reaching loose() 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::remove rescans 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

@fuzeofficlal
fuzeofficlal force-pushed the fix/gpu-shadow-overflow-and-find-906 branch from 8b120e2 to 2f43424 Compare November 25, 2026 10:51
@fuzeofficlal fuzeofficlal changed the title GPU: guard shadow range arithmetic against overflow and scan all buffers in find() GPU: bound shadow buffer lookup by maximum registered size instead of 64 entries Nov 25, 2026
@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Updated in 2f434243 per triage feedback:

  • Bounded find_buffer by the maximum currently registered buffer size (lowest = end.saturating_sub(max_size) over (lowest, 0)..=(address, usize::MAX)), tightening max_size on unregister when the largest buffer is removed so misses stop early instead of walking the map.
  • Dropped all u64::MAX overflow/saturating guards and the equal-start tie-breaker.

@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 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:

Workload main 63bcca43 This PR Suggested size counts
Remove equal 4 KiB registrations 0.13–0.15 ms 6.48–6.64 ms 0.13–0.15 ms
Replace each 4 KiB registration with 2 KiB 0.09–0.10 ms 13.01–13.21 ms 0.105–0.106 ms
Remove 4,096 distinct sizes, largest first 0.14–0.15 ms 6.49–6.53 ms 0.40–0.42 ms

The 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.

Comment thread crates/victory-gpu/src/shadow.rs Resolved
… 64 entries

Replace the fixed .take(64) cap in shadow::find with a scan bounded by the maximum buffer size currently registered in BUFFERS:

- Track live registration counts per buffer size in Buffers::sizes (BTreeMap<u64, usize>), maintaining max_size() in O(log n) across inserts, in-place size replacements, and removals without rescanning the full map under the BUFFERS mutex.
- Bound the reverse scan in find_buffer using max_size(), allowing lookups to reach a covering outer buffer past more than 64 smaller inner buffers while stopping misses immediately outside max_size().
- Add unit tests covering lookup past 128 inner buffers, max_size() bounding and tightening on unregister, multiple equal max-size buffers, in-place replacements, and descending removals to empty.

Refs Marvisak#906.
@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

@Desamodeth Thanks for benchmarking those equal-size and descending-removal workloads — the 16x–125x slowdown under the BUFFERS mutex was a great catch!

I rebased onto current main (63bcca43) and updated Buffers in 91104672 to track live registration counts per buffer size in sizes: BTreeMap<u64, usize>, with max_size(&self) -> u64 reading self.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_removals covering equal-size ties, in-place replacements, descending removals, and empty reset; verified all 13 shadow:: tests on both NVIDIA GeForce RTX 5090 and WARP under VICTORY_D3D12_DEBUG=1, plus clippy (default and --all-features) and xtask policy.

@fuzeofficlal
fuzeofficlal force-pushed the fix/gpu-shadow-overflow-and-find-906 branch from 2f43424 to 9110467 Compare November 26, 2026 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants