GPU: find a buffer holding an address past many smaller ones; reuse loose windows that hold a range #402

Open
github-bot wants to merge 1 commit from github-bot/gh-402 into main AGit
Owner

@⁠kxiserino on GitHub · 2026-11-24 02:57 UTC · original
kxiserino:gpu/shadow-find-enclosing-and-loose-reuse → main · +176 −11 · 1 files · 1 commits · open

Summary

Two ways shadow::find makes duplicate host copies of title memory.

  1. find gives up after 64 entries. It walked BUFFERS backwards from address with .take(64). If a big parent buffer (a heap with its placed buffers) has more than 64 registered sub-buffers between its start and address, the parent is never reached. The range then falls through to loose(), which makes a separate 4 MiB host copy that isn't aliased with the parent's copy. That copy is GPU memory the parent's copy already covers, and the two copies' contents can diverge.

    • New MAX_SPAN: the longest buffer registered so far, updated under BUFFERS' lock in register. It is a monotonic upper bound, so it stays correct after unregister.
    • New enclosing(map, span, address, end) scans (end - span, 0)..=(address, usize::MAX) backwards. A buffer starting further below than that can't reach end. It returns None up front when the range is longer than any buffer, so BTreeMap::range never gets an inverted range.
    • Hits are found exactly as before: the innermost buffer, at the same point in the walk. Only misses look further back, and never further than the longest buffer.
  2. loose reused a window only for an exact (start, len) match. A range spanning two windows gets a window of its own. A later range inside that window, or inside a plain window when a longer key exists, then got yet another copy.

    • New enclosing_window returns any window in LOOSE that holds [address, end). Like enclosing, it's bounded by the longest window made (LOOSE_SPAN).
    • loose now returns None for addresses below 0x10000 (the null page and guard area). It also returns None when no window holds the range and address isn't committed (texture::committed(address, 1)). Stale, null or unmapped addresses used to get 4 MiB of zeros in a new host buffer that was never freed.
    • The committed check runs only before a new window is made, so lookups of existing windows don't pay for a VirtualQuery. Nothing negative is cached, so memory committed later still gets its window.
    • end rounds up with checked_next_multiple_of, so a range at the top of the address space returns None instead of overflowing.

Testing

Synthetic only: local maps, made-up addresses and the test's own reservations.

  • a_parent_is_found_past_many_children: a 256 MiB parent with 200 child buffers. It checks that the old .take(64) walk misses the parent and that enclosing finds it, including near its end. A range inside a child still finds the child (innermost). A range across two children finds the parent. Ranges past, before and longer than every buffer find none.
  • registered_buffers_widen_the_search: register raises MAX_SPAN, and a parent with 100 children registered in the global map is found. Everything is unregistered afterwards.
  • loose_windows_are_reused_for_ranges_inside_them: covers sub-ranges of a plain window and of a window spanning two windows, ranges that cross a window's end, and ranges past, before or longer than every window.
  • loose_memory_must_be_committed: null-page addresses, a reserved but uncommitted range, and the top of the address space all get None.
  • No existing test calls shadow::find or loose with fake addresses that expect a copy (checked with grep and cargo test --workspace).

Results:

  • cargo test --workspace: all pass. victory-gpu has 47 passed, 1 ignored.
  • cargo test -p victory-gpu -- --test-threads=1: 47 passed. With --features diagnostics: 49 passed.
  • cargo clippy -p victory-gpu [--features diagnostics] -- -D warnings: clean.
  • --all-targets: clean apart from the existing missing SAFETY comment at host.rs:267 on main, which #355 fixes.
  • cargo xtask policy: asset policy: 151 files ok.

Tradeoff: a lookup for memory that no buffer holds now looks at every buffer starting within the longest buffer's length below it, where it used to stop at 64. Those are the lookups that then go to LOOSE.

🤖 Generated with Claude Code

**@⁠kxiserino** on GitHub · 2026-11-24 02:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/402) `kxiserino:gpu/shadow-find-enclosing-and-loose-reuse` → `main` · +176 −11 · 1 files · 1 commits · open ## Summary Two ways `shadow::find` makes duplicate host copies of title memory. 1. **`find` gives up after 64 entries.** It walked `BUFFERS` backwards from `address` with `.take(64)`. If a big parent buffer (a heap with its placed buffers) has more than 64 registered sub-buffers between its start and `address`, the parent is never reached. The range then falls through to `loose()`, which makes a separate 4 MiB host copy that isn't aliased with the parent's copy. That copy is GPU memory the parent's copy already covers, and the two copies' contents can diverge. - New `MAX_SPAN`: the longest buffer registered so far, updated under `BUFFERS`' lock in `register`. It is a monotonic upper bound, so it stays correct after `unregister`. - New `enclosing(map, span, address, end)` scans `(end - span, 0)..=(address, usize::MAX)` backwards. A buffer starting further below than that can't reach `end`. It returns `None` up front when the range is longer than any buffer, so `BTreeMap::range` never gets an inverted range. - Hits are found exactly as before: the innermost buffer, at the same point in the walk. Only misses look further back, and never further than the longest buffer. 2. **`loose` reused a window only for an exact `(start, len)` match.** A range spanning two windows gets a window of its own. A later range inside that window, or inside a plain window when a longer key exists, then got yet another copy. - New `enclosing_window` returns any window in `LOOSE` that holds `[address, end)`. Like `enclosing`, it's bounded by the longest window made (`LOOSE_SPAN`). - `loose` now returns `None` for addresses below `0x10000` (the null page and guard area). It also returns `None` when no window holds the range and `address` isn't committed (`texture::committed(address, 1)`). Stale, null or unmapped addresses used to get 4 MiB of zeros in a new host buffer that was never freed. - The committed check runs only before a new window is made, so lookups of existing windows don't pay for a `VirtualQuery`. Nothing negative is cached, so memory committed later still gets its window. - `end` rounds up with `checked_next_multiple_of`, so a range at the top of the address space returns `None` instead of overflowing. ## Testing Synthetic only: local maps, made-up addresses and the test's own reservations. - `a_parent_is_found_past_many_children`: a 256 MiB parent with 200 child buffers. It checks that the old `.take(64)` walk misses the parent and that `enclosing` finds it, including near its end. A range inside a child still finds the child (innermost). A range across two children finds the parent. Ranges past, before and longer than every buffer find none. - `registered_buffers_widen_the_search`: `register` raises `MAX_SPAN`, and a parent with 100 children registered in the global map is found. Everything is unregistered afterwards. - `loose_windows_are_reused_for_ranges_inside_them`: covers sub-ranges of a plain window and of a window spanning two windows, ranges that cross a window's end, and ranges past, before or longer than every window. - `loose_memory_must_be_committed`: null-page addresses, a reserved but uncommitted range, and the top of the address space all get `None`. - No existing test calls `shadow::find` or `loose` with fake addresses that expect a copy (checked with grep and `cargo test --workspace`). Results: - `cargo test --workspace`: all pass. `victory-gpu` has 47 passed, 1 ignored. - `cargo test -p victory-gpu -- --test-threads=1`: 47 passed. With `--features diagnostics`: 49 passed. - `cargo clippy -p victory-gpu [--features diagnostics] -- -D warnings`: clean. - `--all-targets`: clean apart from the existing missing `SAFETY` comment at `host.rs:267` on `main`, which #355 fixes. - `cargo xtask policy`: `asset policy: 151 files ok`. Tradeoff: a lookup for memory that no buffer holds now looks at every buffer starting within the longest buffer's length below it, where it used to stop at 64. Those are the lookups that then go to `LOOSE`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
`shadow::find` looked at only the 64 buffers starting closest below an
address. A big buffer with more than 64 smaller buffers placed in it
before the address was missed, and the range fell through to `loose`,
which made a second 4 MiB host copy of memory the big buffer's copy
already had, not aliased with it. The search now goes back as far as the
longest buffer registered so far (`MAX_SPAN`): one starting further below
cannot hold the range.

`loose` reused a window only for the exact same (start, length), so a
range inside a window made for a range spanning two windows got another
copy. It now reuses any window holding the range. It also gives no copy
for null-page addresses, or for memory that is not committed when a new
window would be made, instead of 4 MiB of zeros.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:02:35 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:31 UTC · original

Thanks @⁠kxiserino for the wider shadow lookup and loose-window reuse. I tested head 022e9c82f0ee147a16b5a06c391de5a8d9beeab3 with:

cargo test --locked -p victory-gpu --lib shadow:: -- --test-threads=1

Result: 4 passed, 0 failed (44 filtered out). The tests cover finding a parent past many smaller children, widening search for registered buffers, reusing loose windows for contained ranges, and requiring loose memory to be committed. These exercise address-range lookup behavior without a title workload.

**@⁠jeremygold02** on GitHub · 2026-11-24 04:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/402#issuecomment-7122093053) Thanks @⁠kxiserino for the wider shadow lookup and loose-window reuse. I tested head `022e9c82f0ee147a16b5a06c391de5a8d9beeab3` with: ``` cargo test --locked -p victory-gpu --lib shadow:: -- --test-threads=1 ``` Result: 4 passed, 0 failed (44 filtered out). The tests cover finding a parent past many smaller children, widening search for registered buffers, reusing loose windows for contained ranges, and requiring loose memory to be committed. These exercise address-range lookup behavior without a title workload.
Author
Owner

@⁠mercuryVM on GitHub · 2026-11-24 06:10 UTC · original

Correction to the count in my review: the run at 022e9c8 was 268 passed, 0 failed (not 265). The verdict is unchanged.

AI-assisted (Claude Code).

**@⁠mercuryVM** on GitHub · 2026-11-24 06:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/402#issuecomment-7123666717) Correction to the count in my review: the run at 022e9c8 was 268 passed, 0 failed (not 265). The verdict is unchanged. AI-assisted (Claude Code).
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 16:59 UTC · original

I checked the loose-window reuse part against the allocation order in a saved Windows 11 / RTX 5070 mission-loading trace. The instrumented build (6538a86) used the exact (start, length) cache for loose windows. The trace logged 322 successful shared-buffer creations there: 301 at 4 MiB and 21 at 8 MiB. The cache held those buffers for the process lifetime. Their sizes sum to 1,372 MiB, covering 1,244 MiB of distinct guest address ranges. These are buffer sizes, not a measurement of resident RAM or VRAM.

For an offline comparison I passed the 322 rounded window extents, in creation order, through enclosing_window from 022e9c8 and checked each lookup against a linear containment scan. Five extents already fit a previously retained window. The replay therefore created 317 windows totaling 1,352 MiB (20 MiB fewer). Overlap alone accounted for more bytes than this because many ranges overlapped only partially or the larger window was created later.

The trace records successful window allocations, not every original unrounded request or registered-buffer lifetime. This checks the lookup on shapes and ordering from the title; it does not measure what the whole PR saves in a live run or test the wider registered-buffer search. AI-assisted.

**@⁠jeremygold02** on GitHub · 2026-11-24 16:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/402#issuecomment-7138529440) I checked the loose-window reuse part against the allocation order in a saved Windows 11 / RTX 5070 mission-loading trace. The instrumented build (`6538a86`) used the exact `(start, length)` cache for loose windows. The trace logged 322 successful shared-buffer creations there: 301 at 4 MiB and 21 at 8 MiB. The cache held those buffers for the process lifetime. Their sizes sum to 1,372 MiB, covering 1,244 MiB of distinct guest address ranges. These are buffer sizes, not a measurement of resident RAM or VRAM. For an offline comparison I passed the 322 **rounded window extents**, in creation order, through `enclosing_window` from `022e9c8` and checked each lookup against a linear containment scan. Five extents already fit a previously retained window. The replay therefore created 317 windows totaling 1,352 MiB (20 MiB fewer). Overlap alone accounted for more bytes than this because many ranges overlapped only partially or the larger window was created later. The trace records successful window allocations, not every original unrounded request or registered-buffer lifetime. This checks the lookup on shapes and ordering from the title; it does not measure what the whole PR saves in a live run or test the wider registered-buffer search. AI-assisted.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:16 UTC · original

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-gpu/src/shadow.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/402#issuecomment-7148331897) <!-- victory-triage v1 head=022e9c82f0ee147a16b5a06c391de5a8d9beeab3 verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-gpu/src/shadow.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠desplmfao on GitHub · 2026-11-25 04:34 UTC · original

@⁠kxiserino #2088 is stacked on this PR. Your commit is in it unchanged, and you're still its author. I only resolved its conflict with current main (940d32f, in shadow.rs: the imports, changed() next to your MAX_SPAN, Shadow::new now returning an Arc, and the new tests next to yours). That might save you the rebase.

Measured with this PR on an RTX 5080 under Proton, before the world (menu): 55 loose windows (264 MB), against 66 (308 MB) on main. While loading, the windows still grew to about 700 (3.4 GB). #2088 makes them 256 KiB instead.

**@⁠desplmfao** on GitHub · 2026-11-25 04:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/402#issuecomment-7152973939) @⁠kxiserino #2088 is stacked on this PR. Your commit is in it unchanged, and you're still its author. I only resolved its conflict with current `main` (`940d32f`, in `shadow.rs`: the imports, `changed()` next to your `MAX_SPAN`, `Shadow::new` now returning an `Arc`, and the new tests next to yours). That might save you the rebase. Measured with this PR on an RTX 5080 under Proton, before the world (menu): 55 loose windows (264 MB), against 66 (308 MB) on `main`. While loading, the windows still grew to about 700 (3.4 GB). #2088 makes them 256 KiB instead.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/shadow.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/402/head:github-bot/gh-402'
git switch 'github-bot/gh-402'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-402'
git switch 'github-bot/gh-402'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-402'
git switch 'github-bot/gh-402'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-402'
git switch 'main'
git merge --squash 'github-bot/gh-402'
git switch 'main'
git merge --ff-only 'github-bot/gh-402'
git switch 'main'
git merge 'github-bot/gh-402'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!402
No description provided.