perf(hle): avoid remap run allocations inside mapping locks #582

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

@⁠tommiehansen on GitHub · 2026-12-05 11:31 UTC · original
tommiehansen:perf/remap-borrowed-runs → main · +101 −14 · 2 files · 1 commits · open

Why

Per-run remap vectors allocate while preparing/applying mapping operations.

What changes

Borrow consecutive compatible runs from the existing slice and prepare alias-operation groups before acquiring the physical mapping lock. Preserve order and compatibility checks.

Benchmark results

4096 nonconsecutive empty-unmap groups, 32 iterations/sample, 8 alternating blocks, 16 samples/arm: allocations/iteration 4096→0; median 1.9218→1.07955 ms; paired −44.22%.

Why this should be merged / current decision

Removes grouping allocations and work under existing locks; ready for review.

Validation

Native CI passed debug/release and benchmark; contiguous, repeated and nonconsecutive addresses covered. Generic Mapping is regression coverage only.

Compatibility

Not a driver mapping or FPS benchmark; distinct from #392/#334/#217.

Archived detailed evidence and review history.

**@⁠tommiehansen** on GitHub · 2026-12-05 11:31 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/582) `tommiehansen:perf/remap-borrowed-runs` → `main` · +101 −14 · 2 files · 1 commits · open ## Why Per-run remap vectors allocate while preparing/applying mapping operations. ## What changes Borrow consecutive compatible runs from the existing slice and prepare alias-operation groups before acquiring the physical mapping lock. Preserve order and compatibility checks. ## Benchmark results 4096 nonconsecutive empty-unmap groups, 32 iterations/sample, 8 alternating blocks, 16 samples/arm: allocations/iteration 4096→0; median 1.9218→1.07955 ms; paired −44.22%. ## Why this should be merged / current decision Removes grouping allocations and work under existing locks; ready for review. ## Validation [Native CI](https://github.com/tommiehansen/VIctory-xmem/actions/runs/47514963984/job/145658338903) passed debug/release and benchmark; contiguous, repeated and nonconsecutive addresses covered. Generic Mapping is regression coverage only. ## Compatibility Not a driver mapping or FPS benchmark; distinct from #392/#334/#217. [Archived detailed evidence and review history](https://github.com/tommiehansen/VIctory-xmem/blob/04dd44857abe222961157663b89a4dfed4c7277c/archive/pr-descriptions-20261206.md).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 11:35:00 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 12:55 UTC · original

Checked 07ce1a9 natively on Windows with MSVC, on top of current main (8bf4878, which is also the merge base).

I read the change against the real mapping path and I don't see a behaviour change. In bind.rs the new group_end classifies every member of a run with mode() against the views as they are before that run is applied, which is exactly what the old peek closure did, and the next run is only classified after run() has updated the map. The slice handed to run() has the same pages in the same order as the old Vec, so um::lazy::bind and unbind get the same base, length, pool and offset, and the placeholder handling inside them (VirtualFree with MEM_PRESERVE_PLACEHOLDER, map, unalias) is untouched. Each run is still one bind or unbind call made under PHYSICAL and MAPPING, so I couldn't find a window where another thread sees a half remapped run that wasn't already there. Nothing is reused across calls or threads, the slice borrows the caller's own Vec.

In plan_aliases, building ops and the touched set before taking physical() is safe because it only reads planned, which the closure owns, and the lock order stays PHYSICAL then title_memory_remapping(). The lock was already dropped between groups before this change, so there's no new interleaving point.

One small difference worth knowing about. The Op values used to be dropped at the end of each run and now live until apply returns. They hold extra references to their sections, and both the old and the new drop happen under the same two locks, only the point inside apply moves. On the plan_aliases path planned keeps the sections alive anyway. On XMemMapPhysicalPages a concurrent XMemFreePhysicalPages could in theory leave an op holding the last reference if its bind fails, and then the section would go a few runs later than before, still under the locks. I don't think it needs changing.

The victory-hle lib tests in debug give 159 passed, 3 failed, 1 ignored on main and 160 passed, 3 failed, 2 ignored on the PR. The extra pass is bind_slice_groups_preserve_order_and_boundaries and the extra ignore is the bench. The same three gameinput tests fail on both (a_host_pad_feeds_the_one_pad_which_never_goes, keys_feed_the_keyboard_and_the_stand_in_pad, pc_kbm_maps_pc_style_keys_and_smooths_mouse_look), identically and also with one test thread, so they have nothing to do with this change. Filtering on xmem gives 6 passed, 1 ignored. victory-gpu isn't touched so I didn't run it.

The new test is narrower than the description suggests. It only uses empty slices on an empty map, so every op is Empty and run() returns straight away. Open, Replace and Close, a boundary caused by a mode change, and the section and index half of compatible() aren't exercised, and apply_original is only used by the ignored bench, never asserted against apply. A test that compares the two groupings over a map with some views present and mixed slices would cover the part that actually matters.

I ran the ignored bench in release three times. Candidate over baseline median comes out at 0.47, 0.49 and 0.48, with the paired per block ratio between about 0.42 and 0.54 in the two steady runs (the third had a couple of noisy early samples). That's a bit better than the -44% in the description and points the same way. It's the best case though, 4096 single page Empty runs, so it measures the Vec allocation and nothing else. Real runs are longer and dominated by the mapping calls, and lazy::bind and unbind still build their own per run Vec under the locks, so in practice the gain will be much smaller. The 4096 to 0 figure is printed as a constant by the bench, not measured, though reading the code it is right.

The linked CI run passed but it ran 9a16820 on a separate audit branch that has diverged from this one, so I couldn't tie its numbers to 07ce1a9 itself. The related work section checks out. #392 is open and only touches the um side, #334 is merged and touches watch.rs and dstorage.rs, and #217 is a mesa patch.

On overlap with open PRs touching the same files, #581, #522, #488, #452 and #429 all merge cleanly with this branch according to git merge-tree. #581 touches XMemVirtualAlloc, XMemFreeDefault and allocate_unified, nowhere near plan_aliases. #452 and #429 wrap the same bind::apply call in plan_aliases with hwrt::freed_gathered. That's textually adjacent but merges without conflict, and the two changes compose since the wrap is still inside both locks.

A minor thing, rustfmt would reflow the iterator chains in the new test module (bind.rs around lines 150, 166 and 187). The non-test code is fmt clean.

Looks good to me.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 12:55 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/582#issuecomment-7438315696) Checked 07ce1a9 natively on Windows with MSVC, on top of current main (8bf4878, which is also the merge base). I read the change against the real mapping path and I don't see a behaviour change. In bind.rs the new group_end classifies every member of a run with mode() against the views as they are before that run is applied, which is exactly what the old peek closure did, and the next run is only classified after run() has updated the map. The slice handed to run() has the same pages in the same order as the old Vec, so um::lazy::bind and unbind get the same base, length, pool and offset, and the placeholder handling inside them (VirtualFree with MEM_PRESERVE_PLACEHOLDER, map, unalias) is untouched. Each run is still one bind or unbind call made under PHYSICAL and MAPPING, so I couldn't find a window where another thread sees a half remapped run that wasn't already there. Nothing is reused across calls or threads, the slice borrows the caller's own Vec. In plan_aliases, building ops and the touched set before taking physical() is safe because it only reads planned, which the closure owns, and the lock order stays PHYSICAL then title_memory_remapping(). The lock was already dropped between groups before this change, so there's no new interleaving point. One small difference worth knowing about. The Op values used to be dropped at the end of each run and now live until apply returns. They hold extra references to their sections, and both the old and the new drop happen under the same two locks, only the point inside apply moves. On the plan_aliases path planned keeps the sections alive anyway. On XMemMapPhysicalPages a concurrent XMemFreePhysicalPages could in theory leave an op holding the last reference if its bind fails, and then the section would go a few runs later than before, still under the locks. I don't think it needs changing. The victory-hle lib tests in debug give 159 passed, 3 failed, 1 ignored on main and 160 passed, 3 failed, 2 ignored on the PR. The extra pass is bind_slice_groups_preserve_order_and_boundaries and the extra ignore is the bench. The same three gameinput tests fail on both (a_host_pad_feeds_the_one_pad_which_never_goes, keys_feed_the_keyboard_and_the_stand_in_pad, pc_kbm_maps_pc_style_keys_and_smooths_mouse_look), identically and also with one test thread, so they have nothing to do with this change. Filtering on xmem gives 6 passed, 1 ignored. victory-gpu isn't touched so I didn't run it. The new test is narrower than the description suggests. It only uses empty slices on an empty map, so every op is Empty and run() returns straight away. Open, Replace and Close, a boundary caused by a mode change, and the section and index half of compatible() aren't exercised, and apply_original is only used by the ignored bench, never asserted against apply. A test that compares the two groupings over a map with some views present and mixed slices would cover the part that actually matters. I ran the ignored bench in release three times. Candidate over baseline median comes out at 0.47, 0.49 and 0.48, with the paired per block ratio between about 0.42 and 0.54 in the two steady runs (the third had a couple of noisy early samples). That's a bit better than the -44% in the description and points the same way. It's the best case though, 4096 single page Empty runs, so it measures the Vec allocation and nothing else. Real runs are longer and dominated by the mapping calls, and lazy::bind and unbind still build their own per run Vec under the locks, so in practice the gain will be much smaller. The 4096 to 0 figure is printed as a constant by the bench, not measured, though reading the code it is right. The linked CI run passed but it ran 9a16820 on a separate audit branch that has diverged from this one, so I couldn't tie its numbers to 07ce1a9 itself. The related work section checks out. #392 is open and only touches the um side, #334 is merged and touches watch.rs and dstorage.rs, and #217 is a mesa patch. On overlap with open PRs touching the same files, #581, #522, #488, #452 and #429 all merge cleanly with this branch according to git merge-tree. #581 touches XMemVirtualAlloc, XMemFreeDefault and allocate_unified, nowhere near plan_aliases. #452 and #429 wrap the same bind::apply call in plan_aliases with hwrt::freed_gathered. That's textually adjacent but merges without conflict, and the two changes compose since the wrap is still inside both locks. A minor thing, rustfmt would reflow the iterator chains in the new test module (bind.rs around lines 150, 166 and 187). The non-test code is fmt clean. Looks good to me. AI-assisted: yes
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/582/head:github-bot/gh-582'
git switch 'github-bot/gh-582'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-582'
git switch 'github-bot/gh-582'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-582'
git switch 'github-bot/gh-582'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-582'
git switch 'main'
git merge --squash 'github-bot/gh-582'
git switch 'main'
git merge --ff-only 'github-bot/gh-582'
git switch 'main'
git merge 'github-bot/gh-582'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.