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

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

@⁠Creaa on GitHub · 2026-11-26 14:54 UTC · original
Creaa:gpu/watch-save-unzeroed → main · +25 −3 · 2 files · 1 commits · open

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

**@⁠Creaa** on GitHub · 2026-11-26 14:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/3262) `Creaa:gpu/watch-save-unzeroed` → `main` · +25 −3 · 2 files · 1 commits · open `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)
`Changed::save` keeps a page's bytes from before its first write (the
copy-back of marked pages compares against them). It zeroed a fresh 64 KiB
buffer and then had `ReadProcessMemory` overwrite all of it, holding the
tracker's lock throughout. The read now goes into an uninitialised buffer
(`host::read_into_uninit`): a read that succeeds writes every byte, one
that fails keeps none of it. It also runs outside the tracker's lock, which
is taken again to insert only if the page is still armed and unsaved.

What is saved is unchanged: still the page's own bytes before the write,
read by the kernel (so an unmapped page fails instead of faulting), as the
review of #3241 requires.

Measured with 64 armed, already-written pages and 20 rounds of `save` in
the test harness (release, RTX 4070 machine, i7-13700F): median 21.1-21.7
us a page before, 16.6 after.

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

@⁠Bj0rnis on GitHub · 2026-11-26 14:56 UTC · original

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.

**@⁠Bj0rnis** on GitHub · 2026-11-26 14:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/3262#issuecomment-7195564553) 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.*
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-26 15:08 UTC · original · reviewed

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.

**@⁠AurumCarlos** on GitHub · 2026-11-26 15:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/3262#pullrequestreview-6076487216) · **reviewed** 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`.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 15:10 UTC · original · approved these changes

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).

**@⁠Dukestar69** on GitHub · 2026-11-26 15:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/3262#pullrequestreview-6076510676) · **approved these changes** 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).
github-bot force-pushed github-bot/gh-3262 from 30abde8941 to 7d5b8eb5c6 2026-11-26 15:19:45 +00:00 Compare
Author
Owner

@⁠Creaa on GitHub · 2026-11-26 15:18 UTC · original

@⁠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.

**@⁠Creaa** on GitHub · 2026-11-26 15:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/3262#issuecomment-7196073697) @⁠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.
github-bot force-pushed github-bot/gh-3262 from 7d5b8eb5c6 to 461cfc8ff7 2026-11-26 15:31:06 +00:00 Compare
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3262'
git switch 'github-bot/gh-3262'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3262'
git switch 'github-bot/gh-3262'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3262'
git switch 'main'
git merge --squash 'github-bot/gh-3262'
git switch 'main'
git merge --ff-only 'github-bot/gh-3262'
git switch 'main'
git merge 'github-bot/gh-3262'
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!3262
No description provided.