Skip to content

GPU: copy loose title memory in 256 KiB windows instead of 4 MiB (stacked on #402) - #2088

Open
desplmfao wants to merge 5 commits into
Marvisak:mainfrom
desplmfao:perf/loose-window-size
Open

desplmfao wants to merge 5 commits into
Marvisak:mainfrom
desplmfao:perf/loose-window-size

Conversation

@desplmfao

@desplmfao desplmfao commented Nov 25, 2026 •

Copy link
Copy Markdown

Stacked on #402 by @kxiserino: its commit is the first one here, unchanged apart from resolving its conflict with current main (940d32f), and keeps its author. The second commit is the change below. If #402 lands first, this reduces to one line plus its comment.

Problem

Memory outside every buffer object (shadow::loose) gets host copies of the aligned windows that lookups touch, and those copies are never freed. A window was 4 MiB. While Grand Theft Auto VI loads its world (Series X 1.0.2.1), about 700 windows are live, 3.4 GB of host memory. They sit in MEMORY_POOL_L0 system memory, so it's RAM on top of the title memory they copy, and every page-table submission refreshes them.

Measured

I counted which parts of each window lookups ask for (64 KiB pieces, diagnostics build, temporary counters not part of this PR): at the same point of loading, 968 MB of 3,416 MB. That's about 30% of the bytes copied.

at the same point of loading 4 MiB windows (main + #402) 256 KiB windows (this PR)
loose windows 687, 3,416 MB 5,713, 2,168 MB
process RSS 17.3–20.4 GB 15.4 GB
no descriptor slots for a title memory copy's views 0 0

That's about 1.2 GB less while loading. Each window is a host buffer and an entry in the title-memory page table, so smaller windows mean more of them. 256 KiB was chosen to keep that count moderate. 64 KiB would cut more bytes but multiply the windows again.

In the open world

Added later, once the world loaded here (the hang was a single compute pass, skipped with VICTORY_SKIP_PIPELINES=72f552f4553fb5f9; see #844). Same stack otherwise (940d32f + other open PRs), same save, about 2 min into the open world. Host copies are counted from host copies of title memory on the heap views line; the world uses one page table, and it holds one view pair per copy.

in the open world 4 MiB windows (main) 256 KiB windows (this PR)
host copies = descriptor pairs 5,858 16,956
memory held in those copies 11.4 GB 6.5 GB
process RSS 29.2 GB (still growing after 3 min) ~15.8 GB

So in the world the smaller windows save about 5 GB of copies and about 13 GB of RSS. They cost about 3× the descriptor pairs, which is still far below the view heap's 2^21 slots.

Also tried, and not part of this PR: freeing windows no lookup has used for 5 s. During loading, almost all windows are used again within seconds (680 of 734), and others are held by lists or heap slots, so it barely freed anything.

Tested

Related

AI-assisted (Claude Code).

🤖 Generated with Claude Code

kxiserino and others added 2 commits November 24, 2026 23:32
…oose windows that hold a range

`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 <noreply@anthropic.com>
Memory outside every buffer object gets host copies of the aligned
windows lookups touch. While Grand Theft Auto VI loads its world, about
700 such 4 MiB windows (3.4 GB of host memory) were live; the lookups
touched about 30% of their bytes (in 64 KiB pieces). With 256 KiB
windows, at the same point of loading: 5,713 windows, 2.2 GB, and the
process 15.4 GB instead of 17.3 to 20.4 GB. No descriptor-slot warnings
for the extra windows' page-table entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremygold02

Copy link
Copy Markdown
Contributor

Triage verdict — Keep the window-size experiment stacked on #402 (b670a33)

Evidence

Checks and limits

  • Verified the two-commit ancestry and compared the lookup and page-table paths. The memory figures and the four passing stack tests are author-reported. I did not run the loading benchmark or reproduce a coherence failure.

Next step

This verification was AI-assisted.

@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:: — 8 passed, 0 failed (including a_parent_is_found_past_many_children and registered_buffers_widen_the_search)
  • cargo clippy -p victory-gpu --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean 256 KiB loose title-memory copy windowing and enclosing-entry lookup with solid unit tests. 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.

Loose title memory copied in 256 KiB windows instead of 4 MiB — finer copy granularity, less over-copy; check the window loop's tail handling.

A range in one window, then a range across its end, which gets a window
of its own over both: the title's writes reach both copies at their
refresh, and a shader's write through the spanning copy goes back to
title memory and into the first copy at its next refresh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@desplmfao

Copy link
Copy Markdown
Author

Thanks, good catch. I added the synthetic case you described (f83faa7, overlapping_windows_agree_through_title_memory):

  • A range inside one window gets window A. A range across A's end then gets its own window B over both, so A lies inside B.
  • A title write reaches both copies at their refresh.
  • A shader write through B goes back to title memory at copy-back, and A picks it up at its next refresh.

It passes on the RTX 5080 under Proton. The full victory-gpu suite gives 134 passed, with the same 13 NVIDIA/Wine failures as main here.

What the test can't make safe is two overlapping copies within one submission. If a shader writes through B and another reads the same bytes through A before the copy-back, it reads the old bytes. That's true of any two copies of the same memory, title buffers and windows included, and on main with 4 MiB windows too. Smaller windows put boundaries 16× as often, so a range crosses one more often. I haven't seen a symptom I could tie to it, but I haven't looked for one specifically.

On descriptor-pair pressure: in Grand Theft Auto VI's world with 256 KiB windows, I've seen 6,000–8,300 live windows in the host copies live lines, and one page table. I haven't logged pairs per table against the 4 MiB baseline yet. I'll add that number from the next world run.

@desplmfao

Copy link
Copy Markdown
Author

@jeremygold02 here's the number I promised: descriptor pairs per page table with 4 MiB windows (without this PR) vs. 256 KiB (with it). Upstream's page table writes one view pair per host copy of title memory, and the world uses one table, so pairs = host copies (host copies of title memory on the heap views diagnostics line).

Same stack otherwise (upstream 940d32f + our other open PRs), GTA VI Series X 1.0.2.1, same save, about 2 min into the open world, RTX 5080, Linux/Proton:

4 MiB windows (main) 256 KiB windows (#2088)
Host copies = descriptor pairs 5,858 16,956
Memory held in those copies 11.4 GB 6.5 GB
Game process RAM (RSS) 29.2 GB ~15.8 GB

So the smaller windows do cost about 3× the pairs, but 17k is still far below the view heap's 2^21 slots. In exchange, the copies hold about 5 GB less, and the process uses about 13 GB less RAM. Without this PR, the run was at 29 GB after 3 minutes and still growing, so I stopped it.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. Still carries #402 rebased (author kept) plus the WINDOW drop from 4 MiB to 256 KiB in crates/victory-gpu/src/shadow.rs, with the overlapping_windows_agree_through_title_memory test from the previous review.

On your reply: the new commit b73bb8b answers the unbounded-miss point. Buffers of BIG_LEN (1 MiB) or more are also kept in BIG, and enclosing_split first scans BUFFERS only within SMALL_SPAN below end, then only the big buffers starting further below, back to MAX_SPAN. I checked that it returns the same result as enclosing: anything starting more than SMALL_SPAN below end that still reaches end must be longer than every small buffer, so it is in BIG. The two ranges meet without a gap ([end - small, address], then [end - span, min(end - small, address + 1))). A buffer in the near range starts higher than any in the far one, so "innermost" is kept. BIG is always locked after BUFFERS (register, unregister, holding), so the lock order is consistent. The new test compares the two-step and one-step lookups at a single point and checks the others against expected keys, which is enough for this.

One small edge case: if a (start, resource) key were ever registered a second time with a shorter length and no unregister in between, BIG would keep the old end. The far-range step could then return a buffer that no longer holds the range. Removing the key from BIG in register before the insert would close this. That is unlikely in practice and does not block the merge. The open point from before still applies: two overlapping copies can disagree within one submission. CI: passing.


Generated by Claude Code

… ones

A miss in the buffer lookup scanned every buffer starting within the
longest buffer's length below the address (MAX_SPAN, 174 MB in the
world): about 480 entries per miss on average, at most 2,842, and some
330,000 misses every 5 s, all under BUFFERS' lock (measured in Grand
Theft Auto VI's world, RTX 5080 under Proton).

Buffers of 1 MiB or more are now also kept in a small map of their own.
A lookup scans BUFFERS only within the longest smaller buffer's length
below the address, then the big buffers further below, back to
MAX_SPAN. Only a big buffer can hold the range from further below, so
the result is the same innermost buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@desplmfao

Copy link
Copy Markdown
Author

On the triage's first point, unbounded misses in enclosing: measured, and addressed in b73bb8b.

Before (title's world, RTX 5080, Proton, our stack on top of this PR): every 5 s, about 1.4 million lookups hit after looking at 1 entry, and about 330,000 missed. Each miss scanned every buffer starting within MAX_SPAN below the address. MAX_SPAN was 174 MB, so a miss looked at 480 entries on average and up to 2,842, all under BUFFERS' lock.

Change: buffers of 1 MiB or more are also kept in a small map of their own. A lookup first scans BUFFERS only within the longest smaller buffer's length below the address (1008 KB here), then only the big buffers further below, back to MAX_SPAN. Only a big buffer can hold the range from further below, so the result is the same innermost buffer. A new test checks the two-step lookup against the one-step enclosing on a heap holding hundreds of placed buffers.

After, same place: misses look at 4.2 entries on average, at most 218. Hits are unchanged at 1. Frame time didn't move measurably (675 vs 725 ms, within run-to-run noise), so the lookups weren't a big share of a frame, but they no longer hold the lock long.

cargo test -p victory-gpu --lib shadow:: under Proton: 10 passed. Clippy is clean with and without --features diagnostics.

The second point (two overlapping copies within one submission can disagree, and smaller windows make boundary crossings more frequent) stands as described. overlapping_windows_agree_through_title_memory covers agreement across submissions, not within one.

… the big ones"

This reverts commit b73bb8b. With it, Grand Theft Auto VI's world showed
spikes out of the player character's head and triangles stretched across
the scene in two runs; the same build without it is clean. A stale entry
in the map of big buffers (a key registered again shorter) explains the
spikes, but with that fixed the triangles stayed, so the change goes until
it is understood. The lookup cost it targeted is back as before (about 480
entries per miss).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@desplmfao

Copy link
Copy Markdown
Author

A correction to my earlier comment: I've reverted the lookup change in 76cdef6 (a new commit, no force-push), so this PR is back to exactly what it was before b73bb8b.

In the title, the two-step lookup broke the picture: spikes out of the player character's head and triangles stretched across the scene, in two runs. The same build without it is clean. I found one real bug, a stale entry in the big-buffer map when a key is registered again shorter, and fixing it removed the spikes, but the stretched triangles stayed, so I don't understand the change well enough to keep it here.

The triage's first point (misses in enclosing scan every buffer within MAX_SPAN, about 480 entries per miss in the world) is therefore open again. It costs lock time but doesn't change results. If I come back to it, it'll be a separate PR that has been checked in the title first. Sorry for the noise.

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.

6 participants