fix(hle): coalesce split placeholders when mapping physical pages #1224

Open
matheusfrta wants to merge 1 commit from upstream-pr/1224 into main
matheusfrta commented 2026-11-24 16:32:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by matheusfrta on 2026-11-24T16:32:00Z.


Rebases the placeholder coalescing and run-grouped map_alias changes onto main while keeping shown alias tracking, low_guards, and commit-failure reporting intact. Verified against cargo test -p victory-hle remapping_split_placeholders_and_coalesced_aliases.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6586582056 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1224). Originally posted by [matheusfrta](https://github.com/matheusfrta) on 2026-11-24T16:32:00Z. --- Rebases the placeholder coalescing and run-grouped `map_alias` changes onto `main` while keeping `shown` alias tracking, `low_guards`, and commit-failure reporting intact. Verified against `cargo test -p victory-hle remapping_split_placeholders_and_coalesced_aliases`.
mercuryVM commented 2026-11-24 18:04:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T17:54:28Z.

Review: APPROVED


Tested 9fb62942 on Windows 11, RTX 5070, MSVC:

  • cargo test -p victory-hle passes on the head (95 passed).
  • Merged into our fork: workspace tests, shader tests and clippy -D warnings (default and all features) are all clean.

The change is right:

  • The coalesce only ever widens the placeholder that map splits again around [base, base + len) just afterwards, with MEM_PRESERVE_PLACEHOLDER. A merged span bigger than the target is fine.
  • If [AllocationBase, base) holds a mapped view, the MEM_COALESCE_PLACEHOLDERS call simply fails, and the old path runs unchanged.
  • map_alias only joins a run while the slices stay in one section with consecutive indices (Arc::ptr_eq and index == first.index + run / PAGE), so each grouped view shows the same pages the per-page views did.

Notes:

  • coalesce_placeholders now runs a VirtualQueryEx walk on every map call, including single-page maps that never need it. It might be worth calling it only after MapViewOfFile3 has failed once.
  • A few doubled blank lines that cargo fmt will collapse.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6044555209 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1224#pullrequestreview-6044555209). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T17:54:28Z. **Review:** APPROVED --- Tested 9fb62942 on Windows 11, RTX 5070, MSVC: - `cargo test -p victory-hle` passes on the head (95 passed). - Merged into our fork: workspace tests, shader tests and `clippy -D warnings` (default and all features) are all clean. The change is right: - The coalesce only ever widens the placeholder that `map` splits again around `[base, base + len)` just afterwards, with `MEM_PRESERVE_PLACEHOLDER`. A merged span bigger than the target is fine. - If `[AllocationBase, base)` holds a mapped view, the `MEM_COALESCE_PLACEHOLDERS` call simply fails, and the old path runs unchanged. - `map_alias` only joins a run while the slices stay in one section with consecutive indices (`Arc::ptr_eq` and `index == first.index + run / PAGE`), so each grouped view shows the same pages the per-page views did. Notes: - `coalesce_placeholders` now runs a `VirtualQueryEx` walk on every `map` call, including single-page maps that never need it. It might be worth calling it only after `MapViewOfFile3` has failed once. - A few doubled blank lines that `cargo fmt` will collapse. AI-assisted (Claude Code).
Zipettte commented 2026-11-24 18:06:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T17:59:47Z.


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

Tested with AI assistance (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140005643 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1224#issuecomment-7140005643). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T17:59:47Z. --- Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 9fb6294: `cargo test -p victory-hle` passes, 96 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
OsiPog commented 2026-11-24 19:04:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by OsiPog on 2026-11-24T18:36:41Z.


Data point for this change under Wine, from an independent implementation of the same idea (map runs of consecutive pool pages in map_alias as one view, merging the per-page placeholders first with MEM_COALESCE_PLACEHOLDERS; page by page if the merge fails).

Why per-page views hurt under Wine. We profiled wineserver during the multi-minute zero-fps stalls at boot and story load. 91% of its CPU was in is_valid_view_addr (server/mapping.c). Each map_view request scans a linked list of every view the process has mapped. find_mapped_view on unmap is also O(n), and it walks from the head while new views are appended at the tail. With one view per 64 KiB page, n reaches the tens of thousands. Every other server request (waits, events, file I/O) then queues behind those scans. Every stall stack we dumped had a title thread in NtMapViewOfSectionEx. Before the change, single-page XMemMapPhysicalPages calls took 70-470 ms each in MapViewOfFile3, and story load had not finished after 12+ minutes.

Result. Wine-staging 11.21, vkd3d-proton 3.2, RADV, first-boot setup already done. Boot reached the start of the first cutscene in about 4 minutes. Caveat: that run also used a wineserver patched so that is_valid_view_addr skips the overlap scan and find_mapped_view walks from the tail. So it does not isolate this PR's effect. The point is that fewer views helps any stock Wine; the scan cost grows with the view count. The proper server-side fix (a tree of views instead of the list) belongs in Wine, and we are working on it separately.

We haven't run this PR's branch itself. If useful, we can do an A/B run with stock wineserver.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140887601 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1224#issuecomment-7140887601). Originally posted by [OsiPog](https://github.com/OsiPog) on 2026-11-24T18:36:41Z. --- Data point for this change under Wine, from an independent implementation of the same idea (map runs of consecutive pool pages in `map_alias` as one view, merging the per-page placeholders first with `MEM_COALESCE_PLACEHOLDERS`; page by page if the merge fails). **Why per-page views hurt under Wine.** We profiled wineserver during the multi-minute zero-fps stalls at boot and story load. 91% of its CPU was in `is_valid_view_addr` (`server/mapping.c`). Each `map_view` request scans a linked list of every view the process has mapped. `find_mapped_view` on unmap is also O(n), and it walks from the head while new views are appended at the tail. With one view per 64 KiB page, n reaches the tens of thousands. Every other server request (waits, events, file I/O) then queues behind those scans. Every stall stack we dumped had a title thread in `NtMapViewOfSectionEx`. Before the change, single-page `XMemMapPhysicalPages` calls took 70-470 ms each in `MapViewOfFile3`, and story load had not finished after 12+ minutes. **Result.** Wine-staging 11.21, vkd3d-proton 3.2, RADV, first-boot setup already done. Boot reached the start of the first cutscene in about 4 minutes. Caveat: that run also used a wineserver patched so that `is_valid_view_addr` skips the overlap scan and `find_mapped_view` walks from the tail. So it does not isolate this PR's effect. The point is that fewer views helps any stock Wine; the scan cost grows with the view count. The proper server-side fix (a tree of views instead of the list) belongs in Wine, and we are working on it separately. We haven't run this PR's branch itself. If useful, we can do an A/B run with stock wineserver.
matheusfrta commented 2026-11-24 19:04:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by matheusfrta on 2026-11-24T18:42:22Z.


@OsiPog Thanks for sharing those numbers! That wineserver bottleneck breakdown makes a lot of sense.

An A/B test of this branch on stock wineserver would definitely be interesting and super helpful to see the isolated impact of the PR. If you have the time to run it, I'd really appreciate it!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7141034236 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1224#issuecomment-7141034236). Originally posted by [matheusfrta](https://github.com/matheusfrta) on 2026-11-24T18:42:22Z. --- @OsiPog Thanks for sharing those numbers! That wineserver bottleneck breakdown makes a lot of sense. An A/B test of this branch on stock wineserver would definitely be interesting and super helpful to see the isolated impact of the PR. If you have the time to run it, I'd really appreciate it!
OsiPog commented 2026-11-24 21:03:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by OsiPog on 2026-11-24T20:51:20Z.


A/B follow-up to the data point above: same build (our run-coalescing map_alias change), same first-boot data and settings, only the wineserver differs.

  • Patched wineserver: reached the first chapter card about 4 minutes after launch. Menus held a steady 30 fps.
  • Stock wine-staging 11.21 wineserver: an 8.5-minute stall early in boot, with no frames presented and no log progress. Screen black for the whole run. No chapter card within the 10-minute cap. Single MapViewOfFile3 calls still took 50-517 ms.

So under stock Wine, fewer views from this change are not enough on their own. The view-list scan in wineserver stays the bottleneck, presumably because other mapping paths still create many views. We replaced our byte patch with a source patch that keeps each process's mapped views in a red-black tree instead of a list. In a 40k-view benchmark it also fixes the address queries the byte patch missed (0.07 s vs 1.33 s stock). It is being prepared for Wine upstream. Caveat: these are single runs on a shared, loaded machine.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144146454 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1224#issuecomment-7144146454). Originally posted by [OsiPog](https://github.com/OsiPog) on 2026-11-24T20:51:20Z. --- A/B follow-up to the data point above: same build (our run-coalescing `map_alias` change), same first-boot data and settings, only the wineserver differs. - **Patched wineserver:** reached the first chapter card about 4 minutes after launch. Menus held a steady 30 fps. - **Stock wine-staging 11.21 wineserver:** an 8.5-minute stall early in boot, with no frames presented and no log progress. Screen black for the whole run. No chapter card within the 10-minute cap. Single `MapViewOfFile3` calls still took 50-517 ms. So under stock Wine, fewer views from this change are not enough on their own. The view-list scan in wineserver stays the bottleneck, presumably because other mapping paths still create many views. We replaced our byte patch with a source patch that keeps each process's mapped views in a red-black tree instead of a list. In a 40k-view benchmark it also fixes the address queries the byte patch missed (0.07 s vs 1.33 s stock). It is being prepared for Wine upstream. Caveat: these are single runs on a shared, loaded machine.
upstream-sync changed title from [PR #1224] HLE: coalesce split placeholders on map and group runs in map_alias to [PR #1224] fix(hle): coalesce split placeholders when mapping physical pages 2026-11-25 02:03:39 +00:00
Marvisak commented 2026-11-25 02:06:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:26:41Z.


Automated triage for the maintainers.

Needs work: needs the maintainer's decision between this PR and #76. Rebased cleanly. The final newline and the test command in the description are fixed. In crates/victory-hle/src/modules/xmem.rs, map_alias now maps each run of consecutive slices of one section as one view (view_run). map merges adjacent reserve regions from the range's BaseAddress with MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS before it splits. remapping_split_placeholders_and_coalesced_aliases covers a split-and-remap, an alias across two sections, and a partial remap from page 1. By reading this is correct: a merge that is refused (a plain reservation, or another allocation) only logs a warning, and map goes on as before.

What is still open:

  • #76 works on the same function. The maintainer has to pick one, or say how they combine.
  • There is no measurement of the benefit on Windows, for example the view count or the time spent in map_alias during a load with and without this PR. @OsiPog's A/B under stock Wine found that fewer views from this approach did not remove the stall on their own, so a Windows number would show whether this is worth carrying.

On your reply: thanks for the rebase that keeps shown, low_guards and the commit-failure reporting. Those are intact in the diff.

CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149552539 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1224#issuecomment-7149552539). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:26:41Z. --- <!-- victory-triage v1 head=ac39370f881cab4d372fef5b811649659784ab3d verdict=needs-work at=2026-11-26T00:21:53Z --> Automated triage for the maintainers. **Needs work: needs the maintainer's decision between this PR and #76.** Rebased cleanly. The final newline and the test command in the description are fixed. In `crates/victory-hle/src/modules/xmem.rs`, `map_alias` now maps each run of consecutive slices of one section as one view (`view_run`). `map` merges adjacent reserve regions from the range's `BaseAddress` with `MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS` before it splits. `remapping_split_placeholders_and_coalesced_aliases` covers a split-and-remap, an alias across two sections, and a partial remap from page 1. By reading this is correct: a merge that is refused (a plain reservation, or another allocation) only logs a warning, and `map` goes on as before. What is still open: - #76 works on the same function. The maintainer has to pick one, or say how they combine. - There is no measurement of the benefit on Windows, for example the view count or the time spent in `map_alias` during a load with and without this PR. @OsiPog's A/B under stock Wine found that fewer views from this approach did not remove the stall on their own, so a Windows number would show whether this is worth carrying. On your reply: thanks for the rebase that keeps `shown`, `low_guards` and the commit-failure reporting. Those are intact in the diff. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
matheusfrta commented 2026-11-25 02:06:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by matheusfrta on 2026-11-25T01:50:10Z.


Updated coalesce_placeholders to start at first.BaseAddress and log a warning if VirtualFree fails. Also cleaned up the formatting (cargo fmt), added a partial-remap case to remapping_split_placeholders_and_coalesced_aliases, and added the test command to the PR description.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150013029 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1224#issuecomment-7150013029). Originally posted by [matheusfrta](https://github.com/matheusfrta) on 2026-11-25T01:50:10Z. --- Updated `coalesce_placeholders` to start at `first.BaseAddress` and log a warning if `VirtualFree` fails. Also cleaned up the formatting (`cargo fmt`), added a partial-remap case to `remapping_split_placeholders_and_coalesced_aliases`, and added the test command to the PR description.
matheusfrta commented 2026-11-25 23:06:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by matheusfrta on 2026-11-25T22:50:38Z.


Rebased onto main and ensured the trailing newline is in place.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176851954 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1224#issuecomment-7176851954). Originally posted by [matheusfrta](https://github.com/matheusfrta) on 2026-11-25T22:50:38Z. --- Rebased onto `main` and ensured the trailing newline is in place.
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/1224:upstream-pr/1224'
git switch 'upstream-pr/1224'

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