GPU: find a buffer holding an address past many smaller ones; reuse loose windows that hold a range #402
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/402"
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 kxiserino on 2026-11-24T02:57:18Z.
Summary
Two ways
shadow::findmakes duplicate host copies of title memory.findgives up after 64 entries. It walkedBUFFERSbackwards fromaddresswith.take(64). If a big parent buffer (a heap with its placed buffers) has more than 64 registered sub-buffers between its start andaddress, the parent is never reached. The range then falls through toloose(), 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.MAX_SPAN: the longest buffer registered so far, updated underBUFFERS' lock inregister. It is a monotonic upper bound, so it stays correct afterunregister.enclosing(map, span, address, end)scans(end - span, 0)..=(address, usize::MAX)backwards. A buffer starting further below than that can't reachend. It returnsNoneup front when the range is longer than any buffer, soBTreeMap::rangenever gets an inverted range.loosereused 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.enclosing_windowreturns any window inLOOSEthat holds[address, end). Likeenclosing, it's bounded by the longest window made (LOOSE_SPAN).loosenow returnsNonefor addresses below0x10000(the null page and guard area). It also returnsNonewhen no window holds the range andaddressisn'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.VirtualQuery. Nothing negative is cached, so memory committed later still gets its window.endrounds up withchecked_next_multiple_of, so a range at the top of the address space returnsNoneinstead 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 thatenclosingfinds 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:registerraisesMAX_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 getNone.shadow::findorloosewith fake addresses that expect a copy (checked with grep andcargo test --workspace).Results:
cargo test --workspace: all pass.victory-gpuhas 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 missingSAFETYcomment athost.rs:267onmain, 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
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T06:10:39Z.
Review: APPROVED
Tested
022e9c8on Windows 11, RTX 5070, MSVC:cargo test --workspace --no-fail-fastpasses (265 passed, 0 failed). I could not measure the duplicate copies without a game package.enclosing:MAX_SPANis correct. A buffer starting belowend - MAX_SPANwould be longer than the longest buffer ever registered.fetch_maxunder the map lock keeps it monotonic acrossunregister.lowest > addressearly-out avoids an invertedBTreeMap::range, which would panic..take(64)found them, at the same position in the walk.loose:Nonebelow 0x10000 and for uncommitted memory stops it from allocating 4 MiB zero copies that are never freed. Thecommittedcheck runs only before a new window is made, as the description says.checked_next_multiple_ofcloses the overflow at the top of the address space.Nit:
enclosing_window's doc says "the innermost if several do". For two windows with the same start, the reverse walk over(start, len)meets the longer one first. In practice a window shares its start with the shorter one only when a cross-window range starts in the same 4 MiB block, and either copy holds the range. So this is the doc, not the behaviour.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:31:11Z.
Thanks @kxiserino for the wider shadow lookup and loose-window reuse. I tested head
022e9c82f0ee147a16b5a06c391de5a8d9beeab3with: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.
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T06:10:48Z.
Correction to the count in my review: the run at
022e9c8was 268 passed, 0 failed (not 265). The verdict is unchanged.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T16:59:55Z.
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_windowfrom022e9c8and 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.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:16:11Z.
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
Imported from Marvisak/VIctory. Originally posted by desplmfao on 2026-11-25T04:34:52Z.
@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, inshadow.rs: the imports,changed()next to yourMAX_SPAN,Shadow::newnow returning anArc, 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.upstream-sync referenced this pull request2026-11-25 11:05:31 +00:00
Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T03:01:40Z.
Review: APPROVED
LGTM — this fixes a real lookup bug, not just a heuristic widening.
enclosing's bound is tight: an entry holding[address,end)must start in[end-span, address]where span is the longest registered buffer, so the.rev().findyields the innermost holder — the.take(64)scan it replaces provably missed a parent past 64+ placed children (the test demonstrates exactly that).enclosing_windowapplies the same trick to LOOSE keyed (start,len), correctly picking the innermost among overlapping windows (spanning window vs smaller one inside it).committedcheck beforeshared_buffermeans a stale/null address no longer fabricates a window of zeros and reports success — returning None is the honest answer.LOWEST_MAPPED,saturating_add,checked_next_multiple_ofcover the boundary cases; span tracking is monotonic under the right locks.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.