Skip to content

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

Open
matheusfrta wants to merge 1 commit into
Marvisak:mainfrom
matheusfrta:patch-1795537915
Open

matheusfrta wants to merge 1 commit into
Marvisak:mainfrom
matheusfrta:patch-1795537915

Conversation

@matheusfrta

@matheusfrta matheusfrta commented Nov 24, 2026 •

Copy link
Copy Markdown

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 mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 9fb6294 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).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
…map and group runs in map_alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Zipettte

Copy link
Copy Markdown

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

OsiPog commented Nov 24, 2026

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Author

@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

OsiPog commented Nov 24, 2026

Copy link
Copy Markdown

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.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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:

  • xmem: map a run of physical pages over several placeholders #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

@matheusfrta matheusfrta changed the title HLE: coalesce split placeholders on map and group runs in map_alias fix(hle): coalesce split placeholders when mapping physical pages Nov 25, 2026
@matheusfrta

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants