memory: landing a structure asks our table whether its page is on the card #382

Manually merged
github merged 2 commits from github-bot/gh-382 into main 2026-12-05 17:31:22 +00:00 AGit
Owner

@⁠mateozariquiey on GitHub · 2026-12-03 21:59 UTC · original
mateozariquiey:pr/land-table → main · +11 −3 · 2 files · 2 commits · merged 2026-12-05 17:26 UTC

Cause. With ray tracing on, Memory::land runs for each address the title's structure builder is given, some 133 times a frame, on the submitting thread and under the mutex. It asked committed whether the address's 64 KiB page is there. A page on the card is not in the working set, so committed ends up reading a byte of each of its 16 kernel pages over the bus: 56 us a call, about 8 ms a frame (stats: copies run_ms).

Change. One line: land asks texture::gpu_mapped, which looks in the table of pages on the card first (um::resident) and asks committed about everything else. Only the question whether the page is there is answered otherwise: the header is read from title memory at every call, as before, and alone decides whether a structure is known, new or another one.

Tested. i7-10700K, RX 9070, RADV, 3840x2160 output, ray tracing on, an interior scene, three runs a side interleaved, on a tree based on 842767a:

before after
copies run_ms 8.3 to 8.7 0.45 to 0.48
preparing_ms (mean) 13.9 to 19.5 5.6 to 7.2
fps at 4,000 to 5,000 draws 27.2 27.6
fps at 5,000 to 6,000 draws 25.1 24.2

No errors, no dropped draws, captures alike. A diagnostic run asked both ways at every call: committed never said no where the table said yes. The memory:: tests pass on 6064c04.

Limits. No gain in frame rate here: the card is the limit in that scene, and the submitting thread waits for it instead. It frees CPU time, which may show where the processor is the limit. Where a view goes (unalias), its bits are cleared just after the mapping is removed: in between land reads a header of zeros, which is no structure, and may take zeros into the young copy of a page the title has just freed. The same could happen before between the question and the read.

**@⁠mateozariquiey** on GitHub · 2026-12-03 21:59 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/382) `mateozariquiey:pr/land-table` → `main` · +11 −3 · 2 files · 2 commits · merged 2026-12-05 17:26 UTC **Cause.** With ray tracing on, `Memory::land` runs for each address the title's structure builder is given, some 133 times a frame, on the submitting thread and under the mutex. It asked `committed` whether the address's 64 KiB page is there. A page on the card is not in the working set, so `committed` ends up reading a byte of each of its 16 kernel pages over the bus: 56 us a call, about 8 ms a frame (`stats: copies run_ms`). **Change.** One line: `land` asks `texture::gpu_mapped`, which looks in the table of pages on the card first (`um::resident`) and asks `committed` about everything else. Only the question whether the page is there is answered otherwise: the header is read from title memory at every call, as before, and alone decides whether a structure is known, new or another one. **Tested.** i7-10700K, RX 9070, RADV, 3840x2160 output, ray tracing on, an interior scene, three runs a side interleaved, on a tree based on 842767a: | | before | after | |---|---|---| | `copies run_ms` | 8.3 to 8.7 | 0.45 to 0.48 | | `preparing_ms` (mean) | 13.9 to 19.5 | 5.6 to 7.2 | | fps at 4,000 to 5,000 draws | 27.2 | 27.6 | | fps at 5,000 to 6,000 draws | 25.1 | 24.2 | No errors, no dropped draws, captures alike. A diagnostic run asked both ways at every call: `committed` never said no where the table said yes. The `memory::` tests pass on 6064c04. **Limits.** No gain in frame rate here: the card is the limit in that scene, and the submitting thread waits for it instead. It frees CPU time, which may show where the processor is the limit. Where a view goes (`unalias`), its bits are cleared just after the mapping is removed: in between `land` reads a header of zeros, which is no structure, and may take zeros into the young copy of a page the title has just freed. The same could happen before between the question and the read.
`Memory::land` asked `committed` for the page of each address the structure
builder was given, some 133 times a frame, on the submitting thread and
under the mutex. A page on the card is not in the working set, so `committed`
read a byte of each of its 16 kernel pages over the bus: 56 us a call, 8 ms a
frame with ray tracing. It asks `gpu_mapped` now, which looks in the table of
4 KiB pages on the card first and asks `committed` about everything else.

Only the question whether the page is there is answered otherwise: the
header is read from title memory at every call, as before, and alone decides
whether a structure is known, new or another one.

Three runs a side, interleaved (i7-10700K, RX 9070, 4K output, ray tracing
on): copies run_ms 8.3-8.7 -> 0.45-0.48, preparing_ms 13.9-19.5 -> 5.6-7.2.
At equal draws the frame rate is the same within the noise: the card is the
limit there, and the thread waits for it instead.

Where a view goes (`unalias`), its bits go just after the mapping does: in
between `land` reads a header of zeros, no structure, and may take zeros
into the young copy of a page the title has just freed. The same could
happen before between the question and the read.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-03 22:07:22 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 07:30 UTC · original

Looked at fabe229 against main. I think this is fine.

gpu_mapped is um::resident(..) || committed(..) (texture.rs:2309-2311), so it can only accept more pages than before, never fewer. No structure that landed on main stops landing here.

The extra pages it accepts are the ones whose bits are set in VRAM_PTES. Those bits are only set in um::map after show_as has succeeded (um.rs:1306-1310), and only for non-paged pools (um.rs:1166). Mapped through alias that means a READ_WRITE CPU view of the card memory, which main also accepted, just through the slow one-byte read per 4 KiB page in readable_plain. The lazily bound case maps with protection 0 when the page hasn't been touched (lazy.rs:496-497), but committed already said yes there too because lazy::there counts Pending as Through (lazy.rs:356). So I couldn't find a steady state where the new check says yes and main said no on a page the CPU can't read.

The one gap is a stale bit. unalias hides the mapping first and clears the bits after, in forget (um.rs:1404-1405, cleared at um.rs:1379), and resident reads the atomics without the um state lock (um.rs:1473-1480). A land landing in that window passes the check on a page that is already gone. That doesn't fault, because packed_at goes through copy_mapped (memory.rs:959) and a failed read falls back to read_pieces, which writes zeros (host.rs:7363-7378). The header then fails packed_len and only young pages are re-taken. Main has the same race between committed and packed_at, and the record path at memory.rs:1702 already reads the header with no check at all. So this doesn't add a new class of problem, it just moves the window a little.

To make sure the zero fallback really holds when the table is wrong, I wrote a throwaway local test (not pushed) that sets the VRAM bits by hand over a 64 KiB committed PAGE_NOACCESS block and over a reserve-only block. In both cases committed stayed false, gpu_mapped went true, and an 8-byte copy_mapped read back 0 without faulting. On a normal RW block both stayed true and the header read back correctly.

Small note, not blocking. When put_in_place does not take it, place still calls committed (memory.rs:1046), so the first landing of a new structure on a card page still pays the slow path. Only repeat landings get faster.

Tests ran natively on Windows (MSVC, not Wine). cargo test -p victory-gpu --lib memory gave 23 passed, 0 failed, 2 ignored. The committed, um:: and texture:: filters gave 3, 2 and 11 passed with no failures.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 07:30 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/382#issuecomment-7404757554) Looked at fabe229 against main. I think this is fine. `gpu_mapped` is `um::resident(..) || committed(..)` (texture.rs:2309-2311), so it can only accept more pages than before, never fewer. No structure that landed on main stops landing here. The extra pages it accepts are the ones whose bits are set in VRAM_PTES. Those bits are only set in `um::map` after `show_as` has succeeded (um.rs:1306-1310), and only for non-paged pools (um.rs:1166). Mapped through `alias` that means a READ_WRITE CPU view of the card memory, which main also accepted, just through the slow one-byte read per 4 KiB page in `readable_plain`. The lazily bound case maps with protection 0 when the page hasn't been touched (lazy.rs:496-497), but `committed` already said yes there too because `lazy::there` counts Pending as Through (lazy.rs:356). So I couldn't find a steady state where the new check says yes and main said no on a page the CPU can't read. The one gap is a stale bit. `unalias` hides the mapping first and clears the bits after, in `forget` (um.rs:1404-1405, cleared at um.rs:1379), and `resident` reads the atomics without the um state lock (um.rs:1473-1480). A `land` landing in that window passes the check on a page that is already gone. That doesn't fault, because `packed_at` goes through `copy_mapped` (memory.rs:959) and a failed read falls back to `read_pieces`, which writes zeros (host.rs:7363-7378). The header then fails `packed_len` and only young pages are re-taken. Main has the same race between `committed` and `packed_at`, and the record path at memory.rs:1702 already reads the header with no check at all. So this doesn't add a new class of problem, it just moves the window a little. To make sure the zero fallback really holds when the table is wrong, I wrote a throwaway local test (not pushed) that sets the VRAM bits by hand over a 64 KiB committed PAGE_NOACCESS block and over a reserve-only block. In both cases `committed` stayed false, `gpu_mapped` went true, and an 8-byte `copy_mapped` read back 0 without faulting. On a normal RW block both stayed true and the header read back correctly. Small note, not blocking. When `put_in_place` does not take it, `place` still calls `committed` (memory.rs:1046), so the first landing of a new structure on a card page still pays the slow path. Only repeat landings get faster. Tests ran natively on Windows (MSVC, not Wine). `cargo test -p victory-gpu --lib memory` gave 23 passed, 0 failed, 2 ignored. The `committed`, `um::` and `texture::` filters gave 3, 2 and 11 passed with no failures. AI-assisted: yes
Author
Owner

@⁠fsrajner on GitHub · 2026-12-04 08:14 UTC · original

Tested on top of main together with the other PRs listed below. RT is on in this scene, but we could not see a change in ms/frame. Our frames here are GPU-bound, so time saved on the submitting thread may not show up. Our stats: frame lines do not report copies run_ms, so I could not check that counter directly.

Setup: RX 9070 XT 16 GB, Ryzen 7 7800X3D, CachyOS, RADV from this repo's setup.sh. Series X, 3840x2160 output, FSR 4 Quality (4.1.2), RT at defaults, headless gamescope. Same save (the intro); the game stops 120 s after the first press. The figures come from the 5 s stats: frame windows after loading. 2 runs per variant; main = 0fd7614. The PR build is main + #382 + #392 + #393 + #408 + #445 + #369 together. #398 was left out because it conflicts with #369 in the residency planning. The #369 merge conflicted only with local counters.

variant ms/frame (runs) 5 s windows > 40 ms worst 5% worst window long frames (longest)
main 47 (48, 45) 86.5% 59.3 ms 62.8 ms 1184 (275 ms)
PR build, defaults 46 (46, 46) 85.7% 61.3 ms 82.9 ms 1176 (254 ms)
PR build + VICTORY_TEXTURE_BARRIERS=1 46 (46, 46) 88.9% 59.0 ms 61.5 ms 1137 (172 ms)
PR build + VICTORY_CARD_KEPT_MB=9216 45 (44, 46) 83.8% 63.4 ms 65.1 ms 1171 (201 ms)

The first run of main and of the PR build each began by re-preparing shaders after a translator change. That is likely where the 48 ms run and the 82.9 ms worst window come from. Otherwise the spread between runs is about ±1 ms/frame, so all of these are within noise. In this scene the card is the limit (about 21-24 ms of GPU wait per frame).

**@⁠fsrajner** on GitHub · 2026-12-04 08:14 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/382#issuecomment-7405688700) Tested on top of main together with the other PRs listed below. RT is on in this scene, but we could not see a change in ms/frame. Our frames here are GPU-bound, so time saved on the submitting thread may not show up. Our `stats: frame` lines do not report `copies run_ms`, so I could not check that counter directly. **Setup:** RX 9070 XT 16 GB, Ryzen 7 7800X3D, CachyOS, RADV from this repo's `setup.sh`. Series X, 3840x2160 output, FSR 4 Quality (4.1.2), RT at defaults, headless gamescope. Same save (the intro); the game stops 120 s after the first press. The figures come from the 5 s `stats: frame` windows after loading. 2 runs per variant; main = 0fd7614. The PR build is main + #382 + #392 + #393 + #408 + #445 + #369 together. #398 was left out because it conflicts with #369 in the residency planning. The #369 merge conflicted only with local counters. | variant | ms/frame (runs) | 5 s windows > 40 ms | worst 5% | worst window | long frames (longest) | |---|---|---|---|---|---| | main | 47 (48, 45) | 86.5% | 59.3 ms | 62.8 ms | 1184 (275 ms) | | PR build, defaults | 46 (46, 46) | 85.7% | 61.3 ms | 82.9 ms | 1176 (254 ms) | | PR build + `VICTORY_TEXTURE_BARRIERS=1` | 46 (46, 46) | 88.9% | 59.0 ms | 61.5 ms | 1137 (172 ms) | | PR build + `VICTORY_CARD_KEPT_MB=9216` | 45 (44, 46) | 83.8% | 63.4 ms | 65.1 ms | 1171 (201 ms) | The first run of main and of the PR build each began by re-preparing shaders after a translator change. That is likely where the 48 ms run and the 82.9 ms worst window come from. Otherwise the spread between runs is about ±1 ms/frame, so all of these are within noise. In this scene the card is the limit (about 21-24 ms of GPU wait per frame).
Author
Owner

@⁠Luxuride on GitHub · 2026-12-04 12:03 UTC · original

Best of the batch: six lines, measured, and it removes ~8 ms a frame of ray-tracing work from the submitting thread. Take it after a rebase and one doc fix.

The doc fix: texture::gpu_mapped()'s own comment (texture.rs:2305-2309) says it is "For consumers asking if GPU backing exists, not for validation of a CPU load", and land() does then do a CPU load of the header (memory.rs:962, copy_mapped → ReadProcessMemory). On a card page that is not CPU-readable the read yields zeros and the code falls into the "anything else" branch (memory.rs:979-990), merging a page of bytes it never read. Update that comment in this PR, since the predicate now has a CPU-load caller by design.

The other one: your evidence is exactly right and it lives only in the commit message (copies 8.3-8.7 ms → 0.45-0.48 ms, preparing 13.9-19.5 ms → 5.6-7.2 ms, three interleaved runs a side on i7-10700K / RX 9070 / 4K / RT on). Move it into the PR body so it survives in git log where the reviewer looks.

The check-to-use window you disclosed in the commit message is unchanged and honestly stated — no action.

**@⁠Luxuride** on GitHub · 2026-12-04 12:03 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/382#issuecomment-7410926482) Best of the batch: six lines, measured, and it removes ~8 ms a frame of ray-tracing work from the submitting thread. Take it after a rebase and one doc fix. The doc fix: `texture::gpu_mapped()`'s own comment (`texture.rs:2305-2309`) says it is "For consumers asking if GPU backing exists, not for validation of a CPU load", and `land()` does then do a CPU load of the header (`memory.rs:962`, `copy_mapped` → `ReadProcessMemory`). On a card page that is not CPU-readable the read yields zeros and the code falls into the "anything else" branch (`memory.rs:979-990`), merging a page of bytes it never read. Update that comment in this PR, since the predicate now has a CPU-load caller by design. The other one: your evidence is exactly right and it lives only in the commit message (copies 8.3-8.7 ms → 0.45-0.48 ms, preparing 13.9-19.5 ms → 5.6-7.2 ms, three interleaved runs a side on i7-10700K / RX 9070 / 4K / RT on). Move it into the PR body so it survives in `git log` where the reviewer looks. The check-to-use window you disclosed in the commit message is unchanged and honestly stated — no action.
github-bot force-pushed github-bot/gh-382 from fabe229f32 to 8979da1e67 2026-12-04 19:21:42 +00:00 Compare
Author
Owner

@⁠mateozariquiey on GitHub · 2026-12-04 19:18 UTC · original

Rebased onto b2449c4 and reworded gpu_mapped's comment in a second commit (8979da1): it says whether the backing exists, not that a CPU load of it will succeed, and it now names Memory::land as the caller that reads a header after asking (a page gone by then reads as zeros, which is no structure).

The figures are in the PR body already, in the table under "Tested"; the commit message has the same ones.

The memory:: tests pass on the rebased branch (23 passed, under Wine with RADV).

**@⁠mateozariquiey** on GitHub · 2026-12-04 19:18 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/382#issuecomment-7421675863) Rebased onto b2449c4 and reworded `gpu_mapped`'s comment in a second commit (8979da1): it says whether the backing exists, not that a CPU load of it will succeed, and it now names `Memory::land` as the caller that reads a header after asking (a page gone by then reads as zeros, which is no structure). The figures are in the PR body already, in the table under "Tested"; the commit message has the same ones. The `memory::` tests pass on the rebased branch (23 passed, under Wine with RADV).
github manually merged commit a891636ae4 into main 2026-12-05 17:31:22 +00:00
github-bot changed target branch from main to gh-pr/382/base 2026-12-05 17:31:23 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.