perf(xmem): add opt-in 2 MiB physical page sections (VICTORY_SECTION_MB) #261

Closed
github-bot wants to merge 652 commits from github-bot/gh-261 into main AGit
Owner

@⁠Vladimir-Human on GitHub · 2026-11-27 12:46 UTC · original
Vladimir-Human:perf/xmem-2m-sections → main · +60109 −5445 · 206 files · 652 commits · closed without merging

Summary

Adds opt-in 2 MiB physical page section splitting (VICTORY_SECTION_MB=2; default 0 = single section per allocation on both Windows and Wine so baseline behavior stays unchanged, with Wine always staying on the single-section fallback) to avoid Windows section-lock view-walk stalls during heavy 64 KiB page mapping/unmapping, and upgrades victory-gpu::inplace section tracking to BTreeMap so thousands of sections stay O(log N).

Changes

  • crates/victory-hle/src/modules/xmem.rs
    • Split XMemAllocatePhysicalPages into sections of at most VICTORY_SECTION_MB MiB when explicitly enabled on Windows (opt-in, default 0 = single section on both Windows and Wine; Wine always stays on the single-section fallback to match Wine's SEC_COMMIT sparse-section path).
    • Add coalesce() in xmem::map using VirtualQueryEx (AllocationBase) + MEM_COALESCE_PLACEHOLDERS + split() so multi-page remappings across adjacent split placeholders (such as partial unmaps straddling a 2 MiB section boundary or earlier scatter-unmapped 64 KiB holes) succeed reliably.
    • Add unit and OS stress tests (section_size_parsing_and_fallback, physical_allocations_split_into_two_mebibyte_sections_with_fallback, os_map_unmap_remap_stress_across_two_mebibyte_sections) plus a release microbenchmark (xmem_section_2m_vs_64k_bench).
  • crates/victory-gpu/src/inplace.rs
    • Change Views::sections and Views::whole from Vec to BTreeMap for O(log N) lookups and cleanup.
    • Remove freed section handles from v.sections in title_memory_section_gone.
    • Group census_of section summaries by (bytes, committed) and add census_groups_sections_and_removes_freed_handles.

Upstream Credit

Adapted from voidopcode/vilocity (0f44a17), building on the Windows section-lock root-cause findings of RyanCase94, CuteBooty, and KainanP.

Verification

  • cargo clippy -p victory-hle -p victory-gpu --all-targets -- -D warnings (default & --features diagnostics): 0 warnings
  • cargo test -p victory-hle --features diagnostics --lib modules::xmem -- --test-threads=1: 21/21 passed (default unset) and 21/21 passed (VICTORY_SECTION_MB=2)
  • cargo test -p victory-gpu --features diagnostics --lib inplace::tests -- --test-threads=1 (VICTORY_D3D12_DEBUG=1): 9/9 HW passed & 9/9 WARP (VICTORY_D3D12_WARP=1) passed, 0 D3D12 debug errors
  • xtask policy --staged & cargo test -p xtask (11/11 passed)
  • Release microbenchmark (cargo test --release -p victory-hle --lib xmem_section_2m_vs_64k_bench -- --ignored --nocapture --test-threads=1, 2048 pages = 128 MiB):
    • 64 KiB sections (2048 sections): alloc+free 7.0 ms, 64 x 2 MiB map+unmap 14.79 ms (231.1 us/op)
    • 2 MiB sections (64 sections): alloc+free 0.7 ms, 64 x 2 MiB map+unmap 3.60 ms (56.3 us/op)
    • Speedup (2 MiB vs 64 KiB): 9.89x alloc+free, 4.11x map+unmap
**@⁠Vladimir-Human** on GitHub · 2026-11-27 12:46 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/261) `Vladimir-Human:perf/xmem-2m-sections` → `main` · +60109 −5445 · 206 files · 652 commits · closed without merging ## Summary Adds opt-in 2 MiB physical page section splitting (`VICTORY_SECTION_MB=2`; default `0` = single section per allocation on both Windows and Wine so baseline behavior stays unchanged, with Wine always staying on the single-section fallback) to avoid Windows section-lock view-walk stalls during heavy 64 KiB page mapping/unmapping, and upgrades `victory-gpu::inplace` section tracking to `BTreeMap` so thousands of sections stay O(log N). ### Changes - **`crates/victory-hle/src/modules/xmem.rs`** - Split `XMemAllocatePhysicalPages` into sections of at most `VICTORY_SECTION_MB` MiB when explicitly enabled on Windows (opt-in, default `0` = single section on both Windows and Wine; Wine always stays on the single-section fallback to match Wine's `SEC_COMMIT` sparse-section path). - Add `coalesce()` in `xmem::map` using `VirtualQueryEx` (`AllocationBase`) + `MEM_COALESCE_PLACEHOLDERS` + `split()` so multi-page remappings across adjacent split placeholders (such as partial unmaps straddling a 2 MiB section boundary or earlier scatter-unmapped 64 KiB holes) succeed reliably. - Add unit and OS stress tests (`section_size_parsing_and_fallback`, `physical_allocations_split_into_two_mebibyte_sections_with_fallback`, `os_map_unmap_remap_stress_across_two_mebibyte_sections`) plus a release microbenchmark (`xmem_section_2m_vs_64k_bench`). - **`crates/victory-gpu/src/inplace.rs`** - Change `Views::sections` and `Views::whole` from `Vec` to `BTreeMap` for O(log N) lookups and cleanup. - Remove freed section handles from `v.sections` in `title_memory_section_gone`. - Group `census_of` section summaries by `(bytes, committed)` and add `census_groups_sections_and_removes_freed_handles`. ### Upstream Credit Adapted from `voidopcode/vilocity` (`0f44a17`), building on the Windows section-lock root-cause findings of RyanCase94, CuteBooty, and KainanP. ### Verification - `cargo clippy -p victory-hle -p victory-gpu --all-targets -- -D warnings` (default & `--features diagnostics`): **0 warnings** - `cargo test -p victory-hle --features diagnostics --lib modules::xmem -- --test-threads=1`: **21/21 passed** (default unset) and **21/21 passed** (`VICTORY_SECTION_MB=2`) - `cargo test -p victory-gpu --features diagnostics --lib inplace::tests -- --test-threads=1` (`VICTORY_D3D12_DEBUG=1`): **9/9 HW passed** & **9/9 WARP (`VICTORY_D3D12_WARP=1`) passed**, 0 D3D12 debug errors - `xtask policy --staged` & `cargo test -p xtask` (**11/11 passed**) - Release microbenchmark (`cargo test --release -p victory-hle --lib xmem_section_2m_vs_64k_bench -- --ignored --nocapture --test-threads=1`, 2048 pages = 128 MiB): - 64 KiB sections (2048 sections): alloc+free `7.0 ms`, 64 x 2 MiB map+unmap `14.79 ms` (`231.1 us/op`) - 2 MiB sections (64 sections): alloc+free `0.7 ms`, 64 x 2 MiB map+unmap `3.60 ms` (`56.3 us/op`) - **Speedup (2 MiB vs 64 KiB): `9.89x` alloc+free, `4.11x` map+unmap**
Split `XMemAllocatePhysicalPages` requests into pagefile-backed sections
of at most `VICTORY_SECTION_MB` MiB (default 2 MiB = 32 pages on Windows,
0 = single section under Wine or when explicitly set to 0), and upgrade
`victory-gpu::inplace` section/whole-view registries from `Vec` to
`BTreeMap` with grouped section census output and full handle cleanup on
`title_memory_section_gone`.

Also add placeholder coalescing in `xmem::map` (`coalesce`) so multi-page
remappings across adjacent split placeholders (such as a partial unmap
straddling a 2 MiB section seam or earlier scatter-unmapped 64 KiB holes)
succeed reliably via `MEM_COALESCE_PLACEHOLDERS`.

Upstream credit: adapted from `voidopcode/vilocity` (`0f44a17`), building
on the Windows section-lock root-cause findings of RyanCase94, CuteBooty,
and KainanP.

Verified:
- `clippy -p victory-hle -p victory-gpu --all-targets -- -D warnings` (default & `diagnostics`): 0 warnings
- `victory-hle` `modules::xmem`: 21 passed, 0 failed (incl. 2 MiB split, sub-section offsets, fallback, partial free, and OS map/unmap/remap + placeholder-coalescing stress tests)
- `victory-gpu` `inplace::tests` (`VICTORY_D3D12_DEBUG=1`): 9/9 HW & 9/9 WARP passed
- Release microbenchmark (`xmem_section_2m_vs_64k_bench`, 2048 pages = 128 MiB):
  - 64 KiB sections (2048 sections): alloc+free 8.1 ms, 64 x 2 MiB map+unmap 16.37 ms (255.7 us/op)
  - 2 MiB sections (64 sections): alloc+free 0.8 ms, 64 x 2 MiB map+unmap 3.62 ms (56.5 us/op)
  - Speedup: 10.01x alloc+free, 4.53x map+unmap
- `xtask policy --staged` (2 files ok) & `cargo test -p xtask` (11/11)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 13:00:09 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 12:53 UTC · original

@⁠Vladimir-Human The cross-boundary remap stress test is the useful check for the smaller sections. I’d keep the 2 MiB setting opt-in until we have a matched Windows load and a Wine fallback check. The release allocator benchmark gives the expected lookup and mapping cost on its own.

**@⁠TaynanGT** on GitHub · 2026-11-27 12:53 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/261#issuecomment-7218807715) @⁠Vladimir-Human The cross-boundary remap stress test is the useful check for the smaller sections. I’d keep the 2 MiB setting opt-in until we have a matched Windows load and a Wine fallback check. The release allocator benchmark gives the expected lookup and mapping cost on its own.
github-bot changed title from perf(xmem): split physical page allocations into 2 MiB sections to perf(xmem): add opt-in 2 MiB physical page sections (VICTORY_SECTION_MB) 2026-11-27 13:14:47 +00:00
github-bot force-pushed github-bot/gh-261 from 8269810b2c to 8915099bf7 2026-11-27 13:14:48 +00:00 Compare
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-27 13:04 UTC · original

@⁠TaynanGT Updated in 8915099: VICTORY_SECTION_MB is now opt-in (default 0 = single section per allocation on both Windows and Wine), while Wine unconditionally stays on the single-section fallback even if VICTORY_SECTION_MB is set.

  • parse_section_mb & section_size_parsing_and_fallback verify default unset/0/invalid -> single section, explicit VICTORY_SECTION_MB=2 / 4 on Windows -> 2 MiB / 4 MiB sections, and Wine fallback -> single section.
  • Re-verified modules::xmem 21/21 both with default unset and with VICTORY_SECTION_MB=2 (including os_map_unmap_remap_stress_across_two_mebibyte_sections), inplace::tests 9/9 HW & 9/9 WARP, clippy (default & diagnostics) 0 warnings, and release xmem_section_2m_vs_64k_bench (9.89x alloc+free, 4.11x map+unmap).
**@⁠Vladimir-Human** on GitHub · 2026-11-27 13:04 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/261#issuecomment-7219032460) @⁠TaynanGT Updated in `8915099`: `VICTORY_SECTION_MB` is now opt-in (default `0` = single section per allocation on both Windows and Wine), while Wine unconditionally stays on the single-section fallback even if `VICTORY_SECTION_MB` is set. - `parse_section_mb` & `section_size_parsing_and_fallback` verify default unset/`0`/invalid -> single section, explicit `VICTORY_SECTION_MB=2` / `4` on Windows -> 2 MiB / 4 MiB sections, and Wine fallback -> single section. - Re-verified `modules::xmem` **21/21** both with default unset and with `VICTORY_SECTION_MB=2` (including `os_map_unmap_remap_stress_across_two_mebibyte_sections`), `inplace::tests` **9/9 HW** & **9/9 WARP**, `clippy` (default & `diagnostics`) 0 warnings, and release `xmem_section_2m_vs_64k_bench` (`9.89x` alloc+free, `4.11x` map+unmap).
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 13:09 UTC · original

@⁠Vladimir-Human The new default matches the existing single-section path. The opt-in and Wine parsing tests cover the compatibility choice, and the cross-boundary remap test covers the Windows mechanism. I’ll keep the allocator microbenchmark separate from the matched game-load comparison.

**@⁠TaynanGT** on GitHub · 2026-11-27 13:09 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/261#issuecomment-7219122961) @⁠Vladimir-Human The new default matches the existing single-section path. The opt-in and Wine parsing tests cover the compatibility choice, and the cross-boundary remap test covers the Windows mechanism. I’ll keep the allocator microbenchmark separate from the matched game-load comparison.
github-bot force-pushed github-bot/gh-261 from 8915099bf7 to cbb87f609d 2026-11-27 20:18:43 +00:00 Compare
github-bot force-pushed github-bot/gh-261 from cbb87f609d to 14fc1c031d 2026-11-27 20:35:50 +00:00 Compare
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-30 17:14 UTC · original

main has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current main and open a new PR; I'll review it there.

**@⁠TaynanGT** on GitHub · 2026-11-30 17:14 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/261#issuecomment-7285941645) `main` has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current `main` and open a new PR; I'll review it there.
github-bot closed this pull request 2026-11-30 17:20:51 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.