GPU: use VirtualQuery region fallback in copy_mapped and write_mapped #943

Open
github-bot wants to merge 2 commits from github-bot/gh-943 into main AGit
Owner

@⁠MartinIndra02 on GitHub · 2026-11-24 14:24 UTC · original
MartinIndra02:perf/gpu-copy-mapped-region-fallback → main · +222 −9 · 1 files · 2 commits · open

Closes #783

Summary

Replaces the 64 KiB → 4 KiB page-by-page fallback in copy_mapped and write_mapped (crates/victory-gpu/src/host.rs) with a race-safe VirtualQuery region walk:

  1. Per-region kernel copy with piece fallback: Each committed, accessible region is copied in a single ReadProcessMemory call; if a page is concurrently unmapped/reprotected between VirtualQuery and the copy, only that region falls back to the existing 64 KiB → 4 KiB for_pieces loop. Unmapped / PAGE_NOACCESS / PAGE_GUARD regions are zeroed in one write_bytes(0, n) (or skipped in write_mapped).
  2. Reuse initial partial-copy progress (done): When the initial whole-range ReadProcessMemory stops at the first unmapped page (ERROR_PARTIAL_COPY), the region walk starts at start + done instead of re-copying the committed prefix.

Verification (Windows 11, RTX 3080 621.24, Rust 1.98.1)

  • New unit test host::batch::copy_mapped_and_write_mapped_handle_holes_across_regions: 2 MiB reserved range with alternating 512 KiB committed regions and 512 KiB unmapped holes.
  • Release microbenchmark (copy_mapped_hole_benchmark, 16 runs each):
    • 64 MiB (4 KiB hole): 136.35 ms → 81.03 ms (1.68×)
    • 64 MiB (32 MiB hole): 608.11 ms → 49.82 ms (12.21×)
  • Independent in-title A/B by @rocobalt1 on 61474f6 vs ffe310e: -29% copies (792 -> 564 ms), -19% frame time (1,484 -> 1,196 ms).
  • In-title A/B on r4tedx/integration/testers (d6897fa, RTX 3080): preparing 2.0–3.1 s → 0.63–0.84 s, world 0.2–0.4 fps → 0.7–0.9 fps, 0 device removals.
  • cargo test -p victory-gpu --lib -- --test-threads=1: 51 passed, 0 failed, 2 ignored
  • cargo clippy -p victory-gpu --all-targets -- -D warnings (default + --features diagnostics): 0 warnings
  • cargo xtask policy: 154 files ok

No game data used. AI-assisted; all builds, tests, and benchmarks executed on my machine.

**@⁠MartinIndra02** on GitHub · 2026-11-24 14:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/943) `MartinIndra02:perf/gpu-copy-mapped-region-fallback` → `main` · +222 −9 · 1 files · 2 commits · open Closes #783 ### Summary Replaces the 64 KiB → 4 KiB page-by-page fallback in `copy_mapped` and `write_mapped` (`crates/victory-gpu/src/host.rs`) with a race-safe `VirtualQuery` region walk: 1. **Per-region kernel copy with piece fallback:** Each committed, accessible region is copied in a single `ReadProcessMemory` call; if a page is concurrently unmapped/reprotected between `VirtualQuery` and the copy, only that region falls back to the existing 64 KiB → 4 KiB `for_pieces` loop. Unmapped / `PAGE_NOACCESS` / `PAGE_GUARD` regions are zeroed in one `write_bytes(0, n)` (or skipped in `write_mapped`). 2. **Reuse initial partial-copy progress (`done`):** When the initial whole-range `ReadProcessMemory` stops at the first unmapped page (`ERROR_PARTIAL_COPY`), the region walk starts at `start + done` instead of re-copying the committed prefix. ### Verification (Windows 11, RTX 3080 `621.24`, Rust `1.98.1`) - New unit test `host::batch::copy_mapped_and_write_mapped_handle_holes_across_regions`: 2 MiB reserved range with alternating 512 KiB committed regions and 512 KiB unmapped holes. - Release microbenchmark (`copy_mapped_hole_benchmark`, 16 runs each): - `64 MiB (4 KiB hole)`: `136.35 ms` → **`81.03 ms`** (**1.68×**) - `64 MiB (32 MiB hole)`: `608.11 ms` → **`49.82 ms`** (**12.21×**) - Independent in-title A/B by `@rocobalt1` on `61474f6` vs `ffe310e`: **`-29%` copies** (`792 -> 564 ms`), **`-19%` frame time** (`1,484 -> 1,196 ms`). - In-title A/B on `r4tedx/integration/testers` (`d6897fa`, RTX 3080): `preparing` `2.0–3.1 s` → **`0.63–0.84 s`**, world `0.2–0.4 fps` → **`0.7–0.9 fps`**, `0` device removals. - `cargo test -p victory-gpu --lib -- --test-threads=1`: **51 passed, 0 failed, 2 ignored** - `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default + `--features diagnostics`): **0 warnings** - `cargo xtask policy`: **154 files ok** *No game data used. AI-assisted; all builds, tests, and benchmarks executed on my machine.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:37 +00:00
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 15:40 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 61474f6: cargo test -p victory-gpu passes, 51 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 15:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/943#issuecomment-7136428885) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 61474f6: `cargo test -p victory-gpu` passes, 51 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠TG0000 on GitHub · 2026-11-24 16:26 UTC · original

Thanks, the region walk is worth having: ported into a lab build here (RX 6800, Windows 11), the read side gives the same submission time as a separate implementation of ours (551.0 against 551.2 ms per frame of 36 submissions, 8 paired frames in one launch), so we use this one as our base. That measures submission work only, not frame rate.

One thing to fix on the write side at 61474f6: write_mapped resumes at dst + done after a failed WriteProcessMemory. When the destination is what fails, done can be larger than what was delivered: in 99 of 60,000 attempts here it was up to 16 bytes too high. Example: a write starting 8 bytes before the hole returned a count of 8 and delivered nothing. Those bytes are then never written, so title memory keeps the old ones. (Windows 11 build 26300, not Wine.)

Suggestion: on the write side ignore the count and restart the walk at dst. A write of bytes already delivered repeats the same data, except if the title wrote them in between. (Edit: an earlier version of this comment also suggested stepping back to a page boundary; that would still skip the bytes of the example, please ignore it.)

The read side looks fine: when the source fails the count was never too high in about 30,000 failures. It often stops up to 64 KiB short of the hole, which the walk then covers.

AI-assisted (Claude Code); the write count was measured, the rest is from reading the diff.

**@⁠TG0000** on GitHub · 2026-11-24 16:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/943#issuecomment-7137677165) Thanks, the region walk is worth having: ported into a lab build here (RX 6800, Windows 11), the read side gives the same submission time as a separate implementation of ours (551.0 against 551.2 ms per frame of 36 submissions, 8 paired frames in one launch), so we use this one as our base. That measures submission work only, not frame rate. One thing to fix on the **write** side at 61474f6: `write_mapped` resumes at `dst + done` after a failed `WriteProcessMemory`. When the *destination* is what fails, `done` can be larger than what was delivered: in 99 of 60,000 attempts here it was up to 16 bytes too high. Example: a write starting 8 bytes before the hole returned a count of 8 and delivered nothing. Those bytes are then never written, so title memory keeps the old ones. (Windows 11 build 26300, not Wine.) Suggestion: on the write side ignore the count and restart the walk at `dst`. A write of bytes already delivered repeats the same data, except if the title wrote them in between. (Edit: an earlier version of this comment also suggested stepping back to a page boundary; that would still skip the bytes of the example, please ignore it.) The **read** side looks fine: when the *source* fails the count was never too high in about 30,000 failures. It often stops up to 64 KiB short of the hole, which the walk then covers. _AI-assisted (Claude Code); the write count was measured, the rest is from reading the diff._
Author
Owner

@⁠MartinIndra02 on GitHub · 2026-11-24 16:34 UTC · original

@⁠TG0000 Great catch — thank you for measuring that on native Windows 11!

Updated in 12cfd38:

  • write_mapped now ignores done on a failed WriteProcessMemory and restarts the VirtualQuery region walk from dst, so no committed prefix bytes right before an unmapped hole can be skipped.
  • Kept the done prefix skip on the copy_mapped read side.
  • Extended host::batch::copy_mapped_and_write_mapped_handle_holes_across_regions to test write_mapped starting 1, 4, 8, 16, 24, 32 bytes before an unmapped hole and assert every prefix byte is written.

Re-verified on Windows 11 / RTX 3080 (621.24): victory-gpu 51 passed, 0 failed, 2 ignored, both CI clippy configs (-D warnings) clean, cargo xtask policy 154 files ok.

**@⁠MartinIndra02** on GitHub · 2026-11-24 16:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/943#issuecomment-7137867035) @⁠TG0000 Great catch — thank you for measuring that on native Windows 11! Updated in `12cfd38`: - `write_mapped` now ignores `done` on a failed `WriteProcessMemory` and restarts the `VirtualQuery` region walk from `dst`, so no committed prefix bytes right before an unmapped hole can be skipped. - Kept the `done` prefix skip on the `copy_mapped` read side. - Extended `host::batch::copy_mapped_and_write_mapped_handle_holes_across_regions` to test `write_mapped` starting `1, 4, 8, 16, 24, 32` bytes before an unmapped hole and assert every prefix byte is written. Re-verified on Windows 11 / RTX 3080 (`621.24`): `victory-gpu` **51 passed, 0 failed, 2 ignored**, both CI clippy configs (`-D warnings`) clean, `cargo xtask policy` **154 files ok**.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:17 UTC · original

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-gpu/src/host.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/943#issuecomment-7149389541) <!-- victory-triage v1 head=12cfd38273db12bde8a7c48785f9f0f97c18472a verdict=needs-rebase at=2026-11-25T01:17:00Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-gpu/src/host.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/host.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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