Avoid yielding on zero-timeout frame polls #538

Open
holoslicer wants to merge 1 commit from holoslicer/Darner:fix/nonblocking-frame-polls into Main
Contributor

Contributor role (optional)

Please credit Fluxer ID 1577921940104949761 for the Contributor role on merge.

What it changes

On Windows, WaitFrameEventX can give up the calling thread's time slice when the title requests a zero-timeout poll. If the next frame tick is in the future, main calls std::thread::sleep(Duration::ZERO) before returning S_FALSE. This change skips the sleeper when the requested duration is zero. Positive waits still sleep for the shorter of the time to the tick and the timeout.

How it was tested

I compared c8258f9 (A) and 6186d7b (B) on Windows 11 x64 with an Intel Core i7-12700K. The measurements use an emulated device and synthetic calls. Gameplay and in-game frame times remain untested.

  1. Set the frame interval to 33,333 µs, pin the caller to one logical processor, and run one equal-priority spinning thread on that same processor.
  2. Prime one origin poll, then time WaitFrameEventX with event kind 0, timeout 0, no handles and flags 2. Check for S_FALSE and a token increment of one on every measured call.
  3. Run the builds in A/B/B/A order with five rounds of 96 contended polls per run, for 960 polls per revision. The table reports the median of the ten round percentiles. The maximum covers every observed poll.
Contended poll duration A · main B · this PR
p50 17.31 ms 0.1 µs
p95 23.39 ms 0.1 µs
p99 28.46 ms 0.1 µs
Maximum 52.61 ms 1.4 µs

With the worker stopped, both builds recorded a 0.1 µs p50 over 30,720 polls per revision. At that scale the timer rounds readings to 0.1 µs. Blocking controls used event kind 2, a 1,000 ms timeout and 72 measured waits per revision at each interval. Their means were 33.34 → 33.32 ms at 30 Hz and 16.73 → 16.67 ms at 60 Hz. Contention and the Windows scheduler affect these timings. Settings and cap defaults stay the same.

I ran these checks on 6186d7b.

  • cargo test --workspace --offline --locked -- --test-threads=1 --skip oracle64::legacy_mul_forms_are_exact passed, including the new deterministic test for polls, elapsed ticks and positive sleeps.
  • I ran the excluded shader oracle separately using the same shader sources at optimization level 3 with debug assertions enabled. oracle64::legacy_mul_forms_are_exact passed in 229.36 seconds.
  • cargo clippy --workspace --all-targets --offline --locked -- -D warnings passed. The same command with --all-features passed.
  • cargo xtask policy passed for all 576 files.

Checklist

  • In-game A/B. The measurements above exercise the frame API offline.
  • One change based on main.
  • The diff follows docs/asset-policy.md.
  • No prerequisite PRs.

Checked for overlap

  • Searched open and closed issues and PRs, including open PRs touching frame.rs.
  • #518 adds event-handle waits in the same function. If it lands first, apply the zero-sleep guard only to the no-handle fallback. In a local integration with 0249058, a zero-timeout call on a manual-reset event returned S_FALSE, then S_OK after signaling, then S_FALSE after resetting. The token advanced only on the two timeouts.
  • #154 also touches frame.rs for menu and reset handling.
## Contributor role (optional) Please credit Fluxer ID `1577921940104949761` for the Contributor role on merge. ## What it changes On Windows, `WaitFrameEventX` can give up the calling thread's time slice when the title requests a zero-timeout poll. If the next frame tick is in the future, `main` calls `std::thread::sleep(Duration::ZERO)` before returning `S_FALSE`. This change skips the sleeper when the requested duration is zero. Positive waits still sleep for the shorter of the time to the tick and the timeout. ## How it was tested I compared `c8258f9` (A) and `6186d7b` (B) on Windows 11 x64 with an Intel Core i7-12700K. The measurements use an emulated device and synthetic calls. Gameplay and in-game frame times remain untested. 1. Set the frame interval to 33,333 µs, pin the caller to one logical processor, and run one equal-priority spinning thread on that same processor. 2. Prime one origin poll, then time `WaitFrameEventX` with event kind `0`, timeout `0`, no handles and flags `2`. Check for `S_FALSE` and a token increment of one on every measured call. 3. Run the builds in A/B/B/A order with five rounds of 96 contended polls per run, for 960 polls per revision. The table reports the median of the ten round percentiles. The maximum covers every observed poll. | Contended poll duration | A · main | B · this PR | |---|---:|---:| | p50 | 17.31 ms | 0.1 µs | | p95 | 23.39 ms | 0.1 µs | | p99 | 28.46 ms | 0.1 µs | | Maximum | 52.61 ms | 1.4 µs | With the worker stopped, both builds recorded a 0.1 µs p50 over 30,720 polls per revision. At that scale the timer rounds readings to 0.1 µs. Blocking controls used event kind `2`, a 1,000 ms timeout and 72 measured waits per revision at each interval. Their means were 33.34 → 33.32 ms at 30 Hz and 16.73 → 16.67 ms at 60 Hz. Contention and the Windows scheduler affect these timings. Settings and cap defaults stay the same. I ran these checks on `6186d7b`. - `cargo test --workspace --offline --locked -- --test-threads=1 --skip oracle64::legacy_mul_forms_are_exact` passed, including the new deterministic test for polls, elapsed ticks and positive sleeps. - I ran the excluded shader oracle separately using the same shader sources at optimization level 3 with debug assertions enabled. `oracle64::legacy_mul_forms_are_exact` passed in 229.36 seconds. - `cargo clippy --workspace --all-targets --offline --locked -- -D warnings` passed. The same command with `--all-features` passed. - `cargo xtask policy` passed for all 576 files. ## Checklist - [ ] In-game A/B. The measurements above exercise the frame API offline. - [x] One change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] No prerequisite PRs. ## Checked for overlap - [x] Searched open and closed issues and PRs, including open PRs touching `frame.rs`. - #518 adds event-handle waits in the same function. If it lands first, apply the zero-sleep guard only to the no-handle fallback. In a local integration with `0249058`, a zero-timeout call on a manual-reset event returned `S_FALSE`, then `S_OK` after signaling, then `S_FALSE` after resetting. The token advanced only on the two timeouts. - #154 also touches `frame.rs` for menu and reset handling.
holoslicer changed title from WIP: [pr-fix][rendering] Avoid yielding on zero-timeout frame polls to [pr-fix][rendering] Avoid yielding on zero-timeout frame polls 2026-12-05 09:35:14 +00:00
TrustNo1 changed title from [pr-fix][rendering] Avoid yielding on zero-timeout frame polls to Avoid yielding on zero-timeout frame polls 2026-12-06 03:27:33 +00:00
Owner

Test needed — commit 6186d7ba; what to check: On Windows, WaitFrameEventX can give up the calling thread's time slice when the title requests a zero-timeout poll. If the next frame tick is in the future, main calls std::thread::sleep(Duration::ZERO) before returning S_FALSE. This change skips the sleeper when the requested duration is zero. Positive waits still sleep for the shorter of the time to the tick and the timeout.

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `6186d7ba`; what to check: On Windows, `WaitFrameEventX` can give up the calling thread's time slice when the title requests a zero-timeout poll. If the next frame tick is in the future, `main` calls `std::thread::sleep(Duration::ZERO)` before returning `S_FALSE`. This change skips the sleeper when the requested duration is zero. Positive waits still sleep for the shorter of the time to the tick and the timeout. Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
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 'fix/nonblocking-frame-polls:holoslicer-fix/nonblocking-frame-polls'
git switch 'holoslicer-fix/nonblocking-frame-polls'

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 'holoslicer-fix/nonblocking-frame-polls'
git switch 'holoslicer-fix/nonblocking-frame-polls'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'holoslicer-fix/nonblocking-frame-polls'
git switch 'holoslicer-fix/nonblocking-frame-polls'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'holoslicer-fix/nonblocking-frame-polls'
git switch 'Main'
git merge --squash 'holoslicer-fix/nonblocking-frame-polls'
git switch 'Main'
git merge --ff-only 'holoslicer-fix/nonblocking-frame-polls'
git switch 'Main'
git merge 'holoslicer-fix/nonblocking-frame-polls'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!538
No description provided.