GPU: a page's saved bytes are read into an unzeroed buffer #3260

Open
Creaa wants to merge 1 commit from upstream-pr/3262 into main
Creaa commented 2026-11-26 14:54:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Creaa on 2026-11-26T14:54:14Z.


watch.rs Changed::save keeps a page's bytes from before its first write (the copy-back of marked pages compares against them). @borosf measured it at about 4.2 us a page in #1633 (a 64 KiB alloc, zeroing it, and ReadProcessMemory under the lock); #3241 tried to avoid the read and was closed as unsound because the saved bytes must stay the pre-write bytes and the source may hold unmapped pages. This keeps both and drops the work that is not needed: the buffer is no longer zeroed before the kernel overwrites all of it.

What is in it

  • crates/victory-gpu/src/watch.rs: Changed::save reads the page into Box::<[u8]>::new_uninit_slice(PAGE) instead of a zeroed vec!, still under the tracker's lock (so the bytes belong to the arming it checked; an earlier version of this PR read outside the lock, and @AurumCarlos pointed out the page could be disarmed and armed again meanwhile). A read that fails keeps nothing (as before).
  • crates/victory-gpu/src/host.rs: read_into_uninit, read_into for a buffer not yet written (the same ReadProcessMemory call).
  • Test a_read_into_an_unwritten_buffer_fails_on_an_unreadable_source: the read fails on an unreadable source and, when it succeeds, writes every byte. The existing armed_ranges_keep_their_pages_bytes_from_before_the_first_write covers that the saved bytes are still the pre-write ones.

Tested

  • RTX 4070 12 GB (driver 617.14), i7-13700F, 32 GB RAM, Windows 11 build 26300, Rust 1.98.1 (pinned); on main 63bcca4.
  • cargo test -q --locked -p victory-gpu --lib --no-fail-fast: 230 passed, 2 failed, 1 ignored. The 2 (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views) fail the same way on main on this card (the capped view heap; #2461). The watch tests: 16 passed, also with --features diagnostics.
  • CI on the previous head failed once in descriptor::host::tests::a_slot_rewritten_while_its_view_is_prepared_views_the_new_record (not touched here); it passes 5 of 5 locally, so it was pushed again unchanged.
  • cargo clippy --workspace --all-targets --locked -- -D warnings and with --all-features: clean. cargo xtask policy: 191 files ok.
  • Timing (a local test-harness loop, not committed): 64 armed pool pages, already written, save on each, 20 rounds, release: median 21.1-21.7 us a page before, 16.0-17.2 after (three runs each, the same pages; the lock is not contended there, so this is the zeroing). A breakdown on the same machine: allocation plus ReadProcessMemory plus free is about 3.4 us; most of what is left is the first touch of the fresh heap memory when many saved pages are alive at once, which a pool of returned buffers would remove (not in this PR: it needs the consumers in shadow.rs to hand the buffers back).

Not tested

  • A game run with this change (the path is the write-fault handler; the numbers above are synthetic). Other hardware.

AI use

  • Claude Code (Claude Opus 5.5) wrote the change and the test, measured it and drafted this description; I checked it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:5512257577 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3262). Originally posted by [Creaa](https://github.com/Creaa) on 2026-11-26T14:54:14Z. --- `watch.rs` `Changed::save` keeps a page's bytes from before its first write (the copy-back of marked pages compares against them). @borosf measured it at about 4.2 us a page in #1633 (a 64 KiB alloc, zeroing it, and `ReadProcessMemory` under the lock); #3241 tried to avoid the read and was closed as unsound because the saved bytes must stay the pre-write bytes and the source may hold unmapped pages. This keeps both and drops the work that is not needed: the buffer is no longer zeroed before the kernel overwrites all of it. **What is in it** - `crates/victory-gpu/src/watch.rs`: `Changed::save` reads the page into `Box::<[u8]>::new_uninit_slice(PAGE)` instead of a zeroed `vec!`, still under the tracker's lock (so the bytes belong to the arming it checked; an earlier version of this PR read outside the lock, and @AurumCarlos pointed out the page could be disarmed and armed again meanwhile). A read that fails keeps nothing (as before). - `crates/victory-gpu/src/host.rs`: `read_into_uninit`, `read_into` for a buffer not yet written (the same `ReadProcessMemory` call). - Test `a_read_into_an_unwritten_buffer_fails_on_an_unreadable_source`: the read fails on an unreadable source and, when it succeeds, writes every byte. The existing `armed_ranges_keep_their_pages_bytes_from_before_the_first_write` covers that the saved bytes are still the pre-write ones. **Tested** - RTX 4070 12 GB (driver 617.14), i7-13700F, 32 GB RAM, Windows 11 build 26300, Rust 1.98.1 (pinned); on `main` `63bcca4`. - `cargo test -q --locked -p victory-gpu --lib --no-fail-fast`: 230 passed, 2 failed, 1 ignored. The 2 (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`) fail the same way on `main` on this card (the capped view heap; #2461). The `watch` tests: 16 passed, also with `--features diagnostics`. - CI on the previous head failed once in `descriptor::host::tests::a_slot_rewritten_while_its_view_is_prepared_views_the_new_record` (not touched here); it passes 5 of 5 locally, so it was pushed again unchanged. - `cargo clippy --workspace --all-targets --locked -- -D warnings` and with `--all-features`: clean. `cargo xtask policy`: 191 files ok. - Timing (a local test-harness loop, not committed): 64 armed pool pages, already written, `save` on each, 20 rounds, release: median **21.1-21.7 us a page before, 16.0-17.2 after** (three runs each, the same pages; the lock is not contended there, so this is the zeroing). A breakdown on the same machine: allocation plus `ReadProcessMemory` plus free is about 3.4 us; most of what is left is the first touch of the fresh heap memory when many saved pages are alive at once, which a pool of returned buffers would remove (not in this PR: it needs the consumers in `shadow.rs` to hand the buffers back). **Not tested** - A game run with this change (the path is the write-fault handler; the numbers above are synthetic). Other hardware. **AI use** - Claude Code (Claude Opus 5.5) wrote the change and the test, measured it and drafted this description; I checked it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Bj0rnis commented 2026-11-26 15:02:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Bj0rnis on 2026-11-26T14:56:59Z.


Verified head 30abde89 on Windows 11 (x64, Rust 1.98.1, NVIDIA GeForce RTX 5070 Ti + WARP):

  • Merge check vs origin/main (63bcca43): clean
  • RTX 5070 Ti (cargo test -p victory-gpu --lib --features diagnostics): 245 passed, 2 failed, 1 ignored (2.97s) — the 2 failures are the pre-existing #1744 NVIDIA 1M view-heap cap failures on main (descriptor::host::tests::dynamic_slots_are_held_until_released, memory::tests::lists_share_a_table_and_copies_keep_their_views); no regressions and the new test passes on hardware
  • WARP (VICTORY_D3D12_WARP=1): 247 passed, 0 failed, 1 ignored (1.97s)
  • Clippy (-p victory-gpu --lib --tests --features diagnostics -- -D warnings): clean (0 warnings)
  • cargo xtask policy: 191 files ok

Not tested: live title workload.

AI-assisted test run and summary.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7195564553 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3262#issuecomment-7195564553). Originally posted by [Bj0rnis](https://github.com/Bj0rnis) on 2026-11-26T14:56:59Z. --- Verified head `30abde89` on Windows 11 (x64, Rust 1.98.1, NVIDIA GeForce RTX 5070 Ti + WARP): - **Merge check vs `origin/main` (`63bcca43`):** clean - **RTX 5070 Ti (`cargo test -p victory-gpu --lib --features diagnostics`):** **245 passed**, 2 failed, 1 ignored (2.97s) — the 2 failures are the pre-existing #1744 NVIDIA 1M view-heap cap failures on `main` (`descriptor::host::tests::dynamic_slots_are_held_until_released`, `memory::tests::lists_share_a_table_and_copies_keep_their_views`); no regressions and the new test passes on hardware - **WARP (`VICTORY_D3D12_WARP=1`):** **247 passed**, 0 failed, 1 ignored (1.97s) - **Clippy (`-p victory-gpu --lib --tests --features diagnostics -- -D warnings`):** clean (0 warnings) - **`cargo xtask policy`:** 191 files ok Not tested: live title workload. *AI-assisted test run and summary.*
AurumCarlos commented 2026-11-26 15:08:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T15:08:40Z.

Review: COMMENTED


The uninit read is fine: ReadProcessMemory either fails (nothing kept) or writes the whole page, and your test pins both.

One question about dropping the lock: the re-check after the read only asks whether the page is armed and unsaved, not whether it's the same arming. If the page is disarmed and armed again by a new submission while the read is in flight (on the submitting thread, while a title thread is inside save), the bytes read before the re-arm get kept as the new arming's "before" bytes. If the title wrote the page in between, copy-back would then treat those title writes as GPU writes. The window is a few µs, but an arming generation checked at insert (or recorded at the first check) would close it.

Nit: the diff adds a blank line at the end of watch.rs.

<!-- greyfinch-sync:Marvisak/VIctory:review:6076487216 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3262#pullrequestreview-6076487216). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-26T15:08:40Z. **Review:** COMMENTED --- The uninit read is fine: `ReadProcessMemory` either fails (nothing kept) or writes the whole page, and your test pins both. One question about dropping the lock: the re-check after the read only asks whether the page is *armed* and unsaved, not whether it's the *same* arming. If the page is disarmed and armed again by a new submission while the read is in flight (on the submitting thread, while a title thread is inside `save`), the bytes read before the re-arm get kept as the new arming's "before" bytes. If the title wrote the page in between, copy-back would then treat those title writes as GPU writes. The window is a few µs, but an arming generation checked at insert (or recorded at the first check) would close it. Nit: the diff adds a blank line at the end of `watch.rs`.
Dukestar69 commented 2026-11-26 15:10:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T15:10:24Z.

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64:

  • Reading into Box::<[u8]>::new_uninit_slice(PAGE) via read_into_uninit (calling assume_init() only after read confirms all len bytes were written) avoids 64 KiB zero-initialization and drops the Saved mutex during the kernel read while re-checking armed/contains_key before insertion.
  • cargo test -p victory-gpu --lib watch:: -- --test-threads=1: 16 passed, 0 failed (including a_read_into_an_unwritten_buffer_fails_on_an_unreadable_source).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6076510676 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3262#pullrequestreview-6076510676). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T15:10:24Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64: - Reading into `Box::<[u8]>::new_uninit_slice(PAGE)` via `read_into_uninit` (calling `assume_init()` only after `read` confirms all `len` bytes were written) avoids 64 KiB zero-initialization and drops the `Saved` mutex during the kernel read while re-checking `armed`/`contains_key` before insertion. - `cargo test -p victory-gpu --lib watch:: -- --test-threads=1`: 16 passed, 0 failed (including `a_read_into_an_unwritten_buffer_fails_on_an_unreadable_source`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Creaa commented 2026-11-26 16:05:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Creaa on 2026-11-26T15:18:10Z.


@AurumCarlos thanks, right: the re-check only proved "armed", not "the same arming", and a disarm + re-arm during the read would keep bytes from before the new arming. Fixed in 7d5b8eb (amended) the simple way: the read is back under the tracker's lock, so only the unzeroed buffer remains. In the same synthetic loop that is still most of the gain (median 16.0-17.2 us a page against 21.1-21.7 before), since the lock was not contended there. The trailing blank line is gone too. watch tests (16, also with diagnostics) and victory-gpu clippy (default and --all-features) pass.

AI use: Claude Code (Claude Opus 5.5) made the change and drafted this reply.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7196073697 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3262#issuecomment-7196073697). Originally posted by [Creaa](https://github.com/Creaa) on 2026-11-26T15:18:10Z. --- @AurumCarlos thanks, right: the re-check only proved "armed", not "the same arming", and a disarm + re-arm during the read would keep bytes from before the new arming. Fixed in `7d5b8eb` (amended) the simple way: the read is back under the tracker's lock, so only the unzeroed buffer remains. In the same synthetic loop that is still most of the gain (median 16.0-17.2 us a page against 21.1-21.7 before), since the lock was not contended there. The trailing blank line is gone too. `watch` tests (16, also with `diagnostics`) and `victory-gpu` clippy (default and `--all-features`) pass. AI use: Claude Code (Claude Opus 5.5) made the change and drafted this reply.
neonbyte1 commented 2026-11-27 14:26:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-27T14:26:00Z.


Tested in game together with #3215 (both merged cleanly) on an RTX 5090 (driver 620.73.04), Linux, GE-Proton11-14 Wine with its vkd3d-proton, GTA VI's opening prison yard, on main plus the unmerged PRs this setup needs (listed in #3425). 420 s runs, medians of the last 30 frame-rate lines, against three adjacent controls of the same base:

frame preparing (CPU)
controls (3 runs) 196.1 / 196.9 / 202.2 ms 75.8 / 75.8 / 78.5 ms
#3262 + #3215 197.6 ms 74.7 ms

No measurable change in frame or preparing time here: whatever the two save is below our run-to-run spread (a few ms). No crash, same pictures. Not a reason against either; the work they remove is real, just small on this scene.

AI-assisted (Claude); measured on my machine, no game data shared.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7220775855 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3262#issuecomment-7220775855). Originally posted by [neonbyte1](https://github.com/neonbyte1) on 2026-11-27T14:26:00Z. --- Tested in game together with #3215 (both merged cleanly) on an RTX 5090 (driver 620.73.04), Linux, GE-Proton11-14 Wine with its vkd3d-proton, GTA VI's opening prison yard, on `main` plus the unmerged PRs this setup needs (listed in #3425). 420 s runs, medians of the last 30 frame-rate lines, against three adjacent controls of the same base: | | frame | preparing (CPU) | |---|---|---| | controls (3 runs) | 196.1 / 196.9 / 202.2 ms | 75.8 / 75.8 / 78.5 ms | | #3262 + #3215 | 197.6 ms | 74.7 ms | No measurable change in frame or preparing time here: whatever the two save is below our run-to-run spread (a few ms). No crash, same pictures. Not a reason against either; the work they remove is real, just small on this scene. _AI-assisted (Claude); measured on my machine, no game data shared._
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3262:upstream-pr/3262'
git switch 'upstream-pr/3262'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/3262'
git switch 'upstream-pr/3262'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3262'
git switch 'upstream-pr/3262'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3262'
git switch 'main'
git merge --squash 'upstream-pr/3262'
git switch 'main'
git merge --ff-only 'upstream-pr/3262'
git switch 'main'
git merge 'upstream-pr/3262'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3260
No description provided.