HLE: mapping the pages a range already shows changes nothing #3845

Open
Ingenioeren wants to merge 1 commit from upstream-pr/3845 into main
Ingenioeren commented 2026-11-29 15:15:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Ingenioeren on 2026-11-29T15:15:24Z.


What

XMemMapPhysicalPages returns at once when every address in the range already shows the requested page. XMemMapPhysicalPagesScatter calls it per page, so it gets the same short cut.

Why

Before, re-mapping the page an address already shows still unmapped the range and mapped it again:

  • it changes no byte, but it costs an UnmapViewOfFile2, a VirtualFree split, a MapViewOfFile3 and a commit per call;
  • a view of several pages is cut into single-page views around it, so later runs over them need more views;
  • victory_gpu::title_memory_remapped is called, so the GPU side treats the range as showing new memory and its copies stop guarding it, even though nothing changed.

Unknown pages, null pages (unmap) and n == 0 take the old path unchanged.

The idea of skipping identical page mappings comes from crossducking/RAZDA4A-XEN (wave 15 notes, GPL-3.0). It publishes no source, so this code is written from scratch.

How tested

  • New test xmem::tests::mapping_the_pages_already_shown_keeps_the_view: maps 3 pages as one view, maps each page again at its own address (scatter) and as a 2-page run, and checks the view is still one 3-page view, shown has one address for the page, and the bytes survive. It then maps another page there and checks that still replaces it. With the early return disabled it fails (the one view stays whole: Some(65536) vs Some(196608)). With the fix it passes.
  • cargo test --locked --offline -p victory-hle --lib: 153 passed.
  • cargo clippy --locked --offline -p victory-hle --all-targets: no warnings.
  • rustfmt --config-path rustfmt.toml --check on the file: clean. cargo run -p xtask -- policy --staged: ok.
  • Timing with a throwaway release-mode test (not committed): 256 pages mapped as one view, then XMemMapPhysicalPagesScatter of the same 256 pages 20 times, 3 runs each. Before: 6.46-6.59 µs per page. After: 17 ns per page. This harness has no GPU behind title_memory_remapped, so the GPU-side cost that is also skipped is not included.

Not tested: no game run. I don't know how often the title re-maps a page that is already shown, so I'm not claiming any in-game gain.

AI assistance: prepared with Claude Code, reviewed by Ingenioeren.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:5552075833 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3845). Originally posted by [Ingenioeren](https://github.com/Ingenioeren) on 2026-11-29T15:15:24Z. --- ## What `XMemMapPhysicalPages` returns at once when every address in the range already shows the requested page. `XMemMapPhysicalPagesScatter` calls it per page, so it gets the same short cut. ## Why Before, re-mapping the page an address already shows still unmapped the range and mapped it again: - it changes no byte, but it costs an `UnmapViewOfFile2`, a `VirtualFree` split, a `MapViewOfFile3` and a commit per call; - a view of several pages is cut into single-page views around it, so later runs over them need more views; - `victory_gpu::title_memory_remapped` is called, so the GPU side treats the range as showing new memory and its copies stop guarding it, even though nothing changed. Unknown pages, null `pages` (unmap) and `n == 0` take the old path unchanged. The idea of skipping identical page mappings comes from crossducking/RAZDA4A-XEN (wave 15 notes, GPL-3.0). It publishes no source, so this code is written from scratch. ## How tested - New test `xmem::tests::mapping_the_pages_already_shown_keeps_the_view`: maps 3 pages as one view, maps each page again at its own address (scatter) and as a 2-page run, and checks the view is still one 3-page view, `shown` has one address for the page, and the bytes survive. It then maps another page there and checks that still replaces it. With the early return disabled it fails (`the one view stays whole`: `Some(65536)` vs `Some(196608)`). With the fix it passes. - `cargo test --locked --offline -p victory-hle --lib`: 153 passed. - `cargo clippy --locked --offline -p victory-hle --all-targets`: no warnings. - `rustfmt --config-path rustfmt.toml --check` on the file: clean. `cargo run -p xtask -- policy --staged`: ok. - Timing with a throwaway release-mode test (not committed): 256 pages mapped as one view, then `XMemMapPhysicalPagesScatter` of the same 256 pages 20 times, 3 runs each. Before: 6.46-6.59 µs per page. After: 17 ns per page. This harness has no GPU behind `title_memory_remapped`, so the GPU-side cost that is also skipped is not included. Not tested: no game run. I don't know how often the title re-maps a page that is already shown, so I'm not claiming any in-game gain. AI assistance: prepared with Claude Code, reviewed by Ingenioeren. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
cloph-dsp commented 2026-11-29 16:29:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-29T16:29:36Z.


Independent local verification (Windows, Rust 1.100.0/MSVC, no game data):

  • PR #3845 at head 8427125961f501c9f1ee251e1ae18071b4ecc0d2, base/parent 63bcca43c6a00a758510398fa43096ce7e4f7df0.
  • cargo test --locked -p victory-hle --lib -- modules::xmem::tests::mapping_the_pages_already_shown_keeps_the_view --exact --test-threads=1 --quiet → 1 passed, 0 failed, 152 filtered, exit 0.
  • git grep -F for the test name on the base returned no match (test is new on this PR); git diff --check 63bcca4 HEAD clean (1 file, +74/-4).
  • Used a per-worktree CARGO_TARGET_DIR (never shared) with CARGO_BUILD_JOBS=2.
  • Only the named synthetic test was replayed; full workspace suite and clippy were not run locally.
  • GPU/driver: N/A (CPU-only HLE XMapping test). No game data, assets, extracted shaders, DRM or decrypt involved.

AI-assisted (Hy4 flash).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7258946176 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3845#issuecomment-7258946176). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-29T16:29:36Z. --- Independent local verification (Windows, Rust 1.100.0/MSVC, no game data): - PR #3845 at head `8427125961f501c9f1ee251e1ae18071b4ecc0d2`, base/parent `63bcca43c6a00a758510398fa43096ce7e4f7df0`. - `cargo test --locked -p victory-hle --lib -- modules::xmem::tests::mapping_the_pages_already_shown_keeps_the_view --exact --test-threads=1 --quiet` → 1 passed, 0 failed, 152 filtered, exit 0. - `git grep -F` for the test name on the base returned no match (test is new on this PR); `git diff --check 63bcca4 HEAD` clean (1 file, +74/-4). - Used a per-worktree `CARGO_TARGET_DIR` (never shared) with `CARGO_BUILD_JOBS=2`. - Only the named synthetic test was replayed; full workspace suite and clippy were not run locally. - GPU/driver: N/A (CPU-only HLE XMapping test). No game data, assets, extracted shaders, DRM or decrypt involved. AI-assisted (Hy4 flash).
cloph-dsp commented 2026-11-30 10:52:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-30T10:52:56Z.


Verified independently on Windows / Rust 1.100.0 / MSVC, source + synthetic inputs only.

  • Worktree D:\Victory-Contrib\pr-3845-watch, dedicated CARGO_TARGET_DIR=D:\Victory-Contrib\target-3845 (never shared).
  • Negative control: git grep -F mapping_the_pages_already_shown_keeps_the_view 63bcca43c6a00a758510398fa43096ce7e4f7df0 -- crates/victory-hle/src/modules/xmem.rs → no match (test is new on the base).
  • git diff --check "$base..HEAD" → exit 0.
  • cargo test --locked -p victory-hle --lib -- modules::xmem::tests::mapping_the_pages_already_shown_keeps_the_view --exact --test-threads=1 --quiet → 1 passed; 0 failed; 152 filtered out, exit 0 (rustc 1.100.0).

GPU/driver: N/A (CPU-only synthetic unit test). No game data, assets, extracted shaders, DRM or decrypt used or uploaded. AI-assisted (Hy4 flash).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7276212171 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3845#issuecomment-7276212171). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-30T10:52:56Z. --- Verified independently on Windows / Rust 1.100.0 / MSVC, source + synthetic inputs only. - Worktree `D:\Victory-Contrib\pr-3845-watch`, dedicated `CARGO_TARGET_DIR=D:\Victory-Contrib\target-3845` (never shared). - Negative control: `git grep -F mapping_the_pages_already_shown_keeps_the_view 63bcca43c6a00a758510398fa43096ce7e4f7df0 -- crates/victory-hle/src/modules/xmem.rs` → no match (test is new on the base). - `git diff --check "$base..HEAD"` → exit 0. - `cargo test --locked -p victory-hle --lib -- modules::xmem::tests::mapping_the_pages_already_shown_keeps_the_view --exact --test-threads=1 --quiet` → `1 passed; 0 failed; 152 filtered out`, exit 0 (rustc 1.100.0). GPU/driver: N/A (CPU-only synthetic unit test). No game data, assets, extracted shaders, DRM or decrypt used or uploaded. AI-assisted (Hy4 flash).
passerbyyhx commented 2026-12-01 12:30:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-12-01T12:30:23Z.


On 84271259 I observed calls to title_memory_remapped from the Windows HLE mapping paths. Reapplying the same three pages, both as one range and as a scatter, produced zero notifications.

A scatter that replaced only the middle page of an existing three-page view produced four notifications, in this order, (0,3), (0,1), (2,1), (1,1), expressed as (page offset, page count). The three mapped bytes read back as [49,52,51]. After writing 116 through the new middle mapping, the four-page alias read [49,50,51,116]. The alias data checks passed. The assertion expecting notifications only for the middle page failed. This measures the notification ranges on this head. Attribution to a change in this PR would require a parent-side comparison.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7311227922 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3845#issuecomment-7311227922). Originally posted by [passerbyyhx](https://github.com/passerbyyhx) on 2026-12-01T12:30:23Z. --- On `84271259` I observed calls to `title_memory_remapped` from the Windows HLE mapping paths. Reapplying the same three pages, both as one range and as a scatter, produced zero notifications. A scatter that replaced only the middle page of an existing three-page view produced four notifications, in this order, `(0,3)`, `(0,1)`, `(2,1)`, `(1,1)`, expressed as (page offset, page count). The three mapped bytes read back as `[49,52,51]`. After writing 116 through the new middle mapping, the four-page alias read `[49,50,51,116]`. The alias data checks passed. The assertion expecting notifications only for the middle page failed. This measures the notification ranges on this head. Attribution to a change in this PR would require a parent-side comparison.
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/3845:upstream-pr/3845'
git switch 'upstream-pr/3845'

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/3845'
git switch 'upstream-pr/3845'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3845'
git switch 'upstream-pr/3845'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3845'
git switch 'main'
git merge --squash 'upstream-pr/3845'
git switch 'main'
git merge --ff-only 'upstream-pr/3845'
git switch 'main'
git merge 'upstream-pr/3845'
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!3845
No description provided.