Intel: mwaitx waits sleep on the fence value (VICTORY_MWAITX_SLEEP=1) #1029

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

@⁠alexis27yt on GitHub · 2026-11-25 16:44 UTC · original
alexis27yt:intel-mwaitx → main · +308 −3 · 2 files · 1 commits · open

On an Intel processor every monitorx and mwaitx of the title's fence wait is an illegal-instruction exception (README: ~280,000 a second in game), so one hardware thread is kept busy in Windows' exception dispatch for the whole wait. On a 6-core part that thread may share a core with the layer's submit thread.

Two switches, both off by default:

  • VICTORY_MWAITX_SLEEP=1: once a wait has spun VICTORY_MWAITX_SPIN_US (50 by default), mwaitx sleeps with WaitOnAddress on the monitored 8 bytes (up to 1 ms); fence::set calls WakeByAddressAll on the fence value when it moves. Any other writer is seen within 1 ms (timeBeginPeriod(1)). The 5-second line now counts sleeps woken and timed out.
  • VICTORY_MWAITX_PATCH=1: monitorx / mwaitx patched in place at their first exception (nop dword [rax]; pause + nop), as clwb already is.

Measured on an i5-12400F + RTX 4070 SUPER with a new hand-run test that runs the title's loop shape (monitorx, check, mwaitx, again) through the same emulation, 5 waits of 1 s each (cargo test --release -p victory-loader the_fence_wait_loop_costs -- --ignored --nocapture):

mode waiting thread's CPU exceptions/s seen late
base 99.7 % 948,544 1 µs
SLEEP 0.3 % 1,629 80 µs (worst 307 µs)
PATCH 98.4 % 0 6 µs

Not measured in game yet (frame rate effect unknown); the tester's machine has no game files. Unit tests added for the stand-in bytes, a patched page running, and a wake on a moving word; all pass on the i5-12400F. Builds clean with clippy, no new warnings.

**@⁠alexis27yt** on GitHub · 2026-11-25 16:44 UTC · [original](https://github.com/fxceless/victory-fork/pull/1029) `alexis27yt:intel-mwaitx` → `main` · +308 −3 · 2 files · 1 commits · open On an Intel processor every `monitorx` and `mwaitx` of the title's fence wait is an illegal-instruction exception (README: ~280,000 a second in game), so one hardware thread is kept busy in Windows' exception dispatch for the whole wait. On a 6-core part that thread may share a core with the layer's submit thread. Two switches, both **off by default**: - `VICTORY_MWAITX_SLEEP=1`: once a wait has spun `VICTORY_MWAITX_SPIN_US` (50 by default), `mwaitx` sleeps with `WaitOnAddress` on the monitored 8 bytes (up to 1 ms); `fence::set` calls `WakeByAddressAll` on the fence value when it moves. Any other writer is seen within 1 ms (`timeBeginPeriod(1)`). The 5-second line now counts sleeps woken and timed out. - `VICTORY_MWAITX_PATCH=1`: `monitorx` / `mwaitx` patched in place at their first exception (`nop dword [rax]`; `pause` + nop), as `clwb` already is. **Measured** on an i5-12400F + RTX 4070 SUPER with a new hand-run test that runs the title's loop shape (`monitorx`, check, `mwaitx`, again) through the same emulation, 5 waits of 1 s each (`cargo test --release -p victory-loader the_fence_wait_loop_costs -- --ignored --nocapture`): | mode | waiting thread's CPU | exceptions/s | seen late | |---|---|---|---| | base | 99.7 % | 948,544 | 1 µs | | SLEEP | **0.3 %** | 1,629 | 80 µs (worst 307 µs) | | PATCH | 98.4 % | 0 | 6 µs | Not measured in game yet (frame rate effect unknown); the tester's machine has no game files. Unit tests added for the stand-in bytes, a patched page running, and a wake on a moving word; all pass on the i5-12400F. Builds clean with clippy, no new warnings.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:56:16 +00:00
Author
Owner

@⁠alexis27yt on GitHub · 2026-11-25 16:54 UTC · original

Side note, not part of this PR: copies and compares on a 6-core part are bound by memory, not by thread count.

While looking at the submit thread I measured copy_jobs + find_pieces (through wide) on 256 MiB, as a frame does them, on the same i5-12400F (6 cores / 12 threads), with a local switch for the kept threads and an AVX2 equality in place of == (median of 8 rounds):

kept + calling threads compare copy GB/s compare GB/s ms per 80 MB (copy + compare)
4 (today's default here) == 24.1 25.5 6.3
7 AVX2 26.1 27.8 5.8
9 == 25.2 27.6 5.9
12 == 25.5 25.7 6.1
3 == 19.6 21.8 7.6

From 4 threads on, all settings sit at 25-27 GB/s, inside run-to-run noise: the default (available_parallelism / 4, 3 kept here) already reaches this machine's RAM. AVX2 gives nothing consistent over MSVC's memcmp. So I am not proposing either change.

What it does say: 80 MB of copy + compare costs ~6 ms of raw memory time here, against the ~80 ms a frame your README measures for those stages in game. If that holds, most of that time goes to per-range work (thousands of small ranges, locks, title reads, guard faults) rather than to moving bytes. I can't profile that without game files, but I hope the number helps.

**@⁠alexis27yt** on GitHub · 2026-11-25 16:54 UTC · [original](https://github.com/fxceless/victory-fork/pull/1029#issuecomment-7168718458) **Side note, not part of this PR: copies and compares on a 6-core part are bound by memory, not by thread count.** While looking at the submit thread I measured `copy_jobs` + `find_pieces` (through `wide`) on 256 MiB, as a frame does them, on the same i5-12400F (6 cores / 12 threads), with a local switch for the kept threads and an AVX2 equality in place of `==` (median of 8 rounds): | kept + calling threads | compare | copy GB/s | compare GB/s | ms per 80 MB (copy + compare) | |---|---|---|---|---| | 4 (today's default here) | `==` | 24.1 | 25.5 | 6.3 | | 7 | AVX2 | 26.1 | 27.8 | 5.8 | | 9 | `==` | 25.2 | 27.6 | 5.9 | | 12 | `==` | 25.5 | 25.7 | 6.1 | | 3 | `==` | 19.6 | 21.8 | 7.6 | From 4 threads on, all settings sit at 25-27 GB/s, inside run-to-run noise: the default (`available_parallelism / 4`, 3 kept here) already reaches this machine's RAM. AVX2 gives nothing consistent over MSVC's `memcmp`. So I am not proposing either change. What it does say: 80 MB of copy + compare costs ~6 ms of raw memory time here, against the ~80 ms a frame your README measures for those stages in game. If that holds, most of that time goes to per-range work (thousands of small ranges, locks, title reads, guard faults) rather than to moving bytes. I can't profile that without game files, but I hope the number helps.
Author
Owner

@⁠Proudnoob4 on GitHub · 2026-11-25 17:39 UTC · original

Tested in-game on Windows 10 Pro (22H2), Intel i5-13600K (6P+8E), RTX 4080 16 GB (driver 621.01, Series X profile), 420 s run in the prison cell scene (ca8829f + #1014 + #1015 + #1016 + #1029 + #1032 stacked, compared against our 600 s 465b94f run with the same base switches VICTORY_ASYNC_SUBMIT=1 VICTORY_QUEUE2=1 VICTORY_COPY_PLAIN=1 VICTORY_INDIRECT_WINDOW=1 VICTORY_JOIN_PARTS=1 VICTORY_BACK_OWN_VIEW=1 VICTORY_INDIRECT_GATHER=1 plus VICTORY_RELAY=2 VICTORY_LATE_WAIT=3 VICTORY_GROUP_SYNC=2 VICTORY_WIDE_BACK=1 VICTORY_CARD_BUFFERS_MIN_MB=1 VICTORY_CARD_BUFFERS_MB=1024).

Real-game Intel i5-13600K A/B (VICTORY_MWAITX_SLEEP=1 on vs off in the same cell scene):

  • Without VICTORY_MWAITX_SLEEP=1 (ca8829f + #1014, 420 s): monitorx 185k-231k/s, mwaitx 185k-231k/s, mwaitx waits ended: 3-4/s, 313-402 ms/s. (On 465b94f: 71k-95k/s.)
  • With VICTORY_MWAITX_SLEEP=1 (stacked 420 s run): monitorx 191-217/s, mwaitx 191-217/s, mwaitx slept/s: woken 16-17, timed out 45-49 | mwaitx waits ended: 4-6/s, 73.9-99.5 ms/s. Wait exceptions dropped by >99.9% (~1,000x) with zero hangs or regressions (8.50 fps median / 9.80 max).

AI-assisted (Gemini 4 Hydrogen).

**@⁠Proudnoob4** on GitHub · 2026-11-25 17:39 UTC · [original](https://github.com/fxceless/victory-fork/pull/1029#issuecomment-7169816308) Tested in-game on Windows 10 Pro (22H2), Intel i5-13600K (6P+8E), RTX 4080 16 GB (driver 621.01, Series X profile), 420 s run in the prison cell scene (ca8829f + #1014 + #1015 + #1016 + #1029 + #1032 stacked, compared against our 600 s 465b94f run with the same base switches VICTORY_ASYNC_SUBMIT=1 VICTORY_QUEUE2=1 VICTORY_COPY_PLAIN=1 VICTORY_INDIRECT_WINDOW=1 VICTORY_JOIN_PARTS=1 VICTORY_BACK_OWN_VIEW=1 VICTORY_INDIRECT_GATHER=1 plus VICTORY_RELAY=2 VICTORY_LATE_WAIT=3 VICTORY_GROUP_SYNC=2 VICTORY_WIDE_BACK=1 VICTORY_CARD_BUFFERS_MIN_MB=1 VICTORY_CARD_BUFFERS_MB=1024). Real-game Intel i5-13600K A/B (VICTORY_MWAITX_SLEEP=1 on vs off in the same cell scene): - Without VICTORY_MWAITX_SLEEP=1 (ca8829f + #1014, 420 s): monitorx 185k-231k/s, mwaitx 185k-231k/s, mwaitx waits ended: 3-4/s, 313-402 ms/s. (On 465b94f: 71k-95k/s.) - With VICTORY_MWAITX_SLEEP=1 (stacked 420 s run): monitorx 191-217/s, mwaitx 191-217/s, mwaitx slept/s: woken 16-17, timed out 45-49 | mwaitx waits ended: 4-6/s, 73.9-99.5 ms/s. Wait exceptions dropped by >99.9% (~1,000x) with zero hangs or regressions (8.50 fps median / 9.80 max). AI-assisted (Gemini 4 Hydrogen).
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/1029/head:github-bot/gh-1029'
git switch 'github-bot/gh-1029'

Merge

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