WIP: perf(vi): the vsync ticker waits on a high-resolution timer instead of yielding #1235
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1235
Loading…
Reference in a new issue
No description provided.
Delete branch "Molier/Longshot:perf/vsync-ticker-timer"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type: perf · Risk: low · Tested on: Linux (CachyOS, kernel 7.4.0-rc1), Ryzen 7 5800X, 32 GB, Radeon RX 9070 XT, RADV (Mesa 26.2.6), gamescope; host-only synthetic test (posix shim) · Needs: Windows (its own
CreateWaitableTimerExW; only the posix shim ran here), Intel, NVIDIA, a Steam DeckWhy
The vsync ticker (
vsync_maininhost/nn_vi.c) advances the frame-rate cap's clock on its own thread. Its wait went throughhos_sleep_ns, which yields in a loop for anything under a millisecond (host/hos_thread.c). Uncapped, the ticker's period is 1 ms, so that spun a whole core doing no work. The game's own sleeps already wait on a high-resolution waitable timer; the ticker's wait did not use it.What changed
The ticker now waits on a thread-local high-resolution waitable timer instead of yielding, and falls back to the old yield wait when the timer cannot be made. A switch restores the old wait.
host/nn_vi.c:vsync_wait_nscreates once per thread (_Thread_local) and waits on aCreateWaitableTimerExWhigh-resolution timer, arms it withSetWaitableTimerEx, and blocks inWaitForSingleObject; if no timer was made orSetWaitableTimerExfails, it callshos_sleep_ns.vsync_sleeppicks the timer wait or the yield loop byHOS_VSYNC_YIELD;vsync_main's single wait call per tick now goes throughvsync_sleep.docs/development.md: the switch's row in the switch table.Measurements
Verdict: At 1440p the switch lowers median frame time on the moving legs of the Hyrule Field route: -4.3% (17.87 -> 17.11 ms) and -5.2% (18.06 -> 17.12 ms), each beyond the 3-vs-3 run noise band. The load leg (-2.1%) and the 800p rerun are inside noise either way; no regression measured. Host-only, over 2,000 ticks of a 1 ms period, the ticker thread's own CPU use drops from 99.9% of a core (yield) to 1.9% (timer), at a cost of 0.079 ms mean lateness per tick against 0.001 ms.
Baseline: one build of nightly-189 (main @
aa95d35f) plus local patches (this change among them), withHOS_VSYNC_YIELD=1in one arm and unset in the other. It is not main against this PR, and it is 150+ commits behind current main; the run on current main is listed under Must be tested.Hardware: Linux (CachyOS, kernel 7.4.0-rc1), Ryzen 7 5800X, 32 GB, Radeon RX 9070 XT, RADV (Mesa 26.2.6), gamescope
Scene:
hf48run— a save in Hyrule Field, the character running a fixed route, 1440p output, render scale 1, frame generation off, uncapped; plus an 800p rerun of the same route.Method: median frame time per segment (
run_a,run_b,load), averaged over 3 runs per arm, interleaved (on, off, on, off, ...) after a warm-up; a claim is "beyond noise" only when every on-run beats every off-run (non-overlapping 3-vs-3 ranges). The ticker thread's own CPU use and lateness come from a host-only synthetic run of the real wait path (posix shim, 2,000 ticks of a 1 ms period).How to reproduce: the
hf48runsave and route above,HOS_VSYNC_YIELD=1(off) against unset (on, the default), 3 or more interleaved runs, uncapped.HOS_VSYNC_YIELD=1: nightly-189 + local patchesrun_arun_bload(teleport)run_a/run_b/loadHost-only, same build:
HOS_VSYNC_YIELD=1: nightly-189 + local patchesFlags and settings
One new switch, on by default (the timer path).
HOS_VSYNC_YIELD=1Testing
Tested: Linux (CachyOS, kernel 7.4.0-rc1), Ryzen 7 5800X, Radeon RX 9070 XT, RADV (Mesa 26.2.6), gamescope: the
hf48runroute above (3 interleaved runs per arm at 1440p, one rerun at 800p), the spinner check and the game-clock speed check. A host-only compile against the realhost/nn_vi.cbuild flags (-fsyntax-only, real generated headers) passes, and the host-only synthetic wait test (2,000 ticks of a 1 ms period) runs clean.Not tested: Windows' own
CreateWaitableTimerExW(only the posix shim was exercised here); Intel and NVIDIA GPUs (this machine is RADV); a Steam Deck or other low-power device, where the yield-versus-timer trade-off may differ;python -I -B tests/run_all.pywas not run in full on this rebase.Must be tested:
ac778751against this PR @2435256a, 2 interleaved runs each: thehf48runroute at 1440p with default settings, expecting the frame-time result above to hold against a real main build.HOS_VSYNC_YIELD=1against unset, 3 or more interleaved runs each, expecting the same gain on the moving legs and no regression (Test Farm: @Beanstalk windows, a moving-route A/B with and withoutHOS_VSYNC_YIELD=1).Risks
What could break: If
CreateWaitableTimerExW/SetWaitableTimerExmisbehaves on a driver or kernel (a wrong wakeup time, a timer that never fires), the ticker's clock would drift or stall, which paces every frame's present. The code falls back tohos_sleep_nswhenever the timer cannot be made, so a system where the call itself fails is unaffected; a system where it succeeds but behaves wrongly is not covered by the testing above.Turning it off:
HOS_VSYNC_YIELD=1, or revert the commit.Compatibility: Nothing changes: no save, option, cache or ABI change.
Checklist
Everyone
type(scope): description) of the status line's type.can't be run by hand is in Testing's Not testable by hand, with why.
in Risks. Not ticked yet: Testing says the yield-versus-timer trade-off on a Steam Deck or low-power device was not measured.
main.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named). Not run in full on this rebase; the PR's own tests pass (named in Testing).third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.Tested in game: this commit has been part of the local build the measurements come from (Linux, CachyOS, Ryzen 7 5800X, RX 9070 XT, RADV Mesa 26.2.6, gamescope) since 2026-11-16, on nightly-189 and then nightly-338 bases: several hours of the two benchmark routes (uncapped and capped 60) and a short play session (about 15 minutes) at 4K. The game clock stayed in the 0.99-1.01 band (samples in the description) and no thread spins at the ticker's period. Not run: this branch built from its own commit on current main, and Windows.
Windows numbers for this, since the description asks for them. I had found the same spinning thread in a played session (an unnamed thread at 97-101 % of a core for as long as the cap was Uncapped; its start address is
vsync_mainin the build's map) and was about to write the same fix when I found this PR.Verdict: on Windows the ticker's own CPU goes from 96-97 % of a core to under 0.5 %, and the whole game's CPU from about 6.1 to 4.8 cores. The frame rate is the same within the runs' spread, on all cores and held to four. The game clock's speed is 1.000 on both sides. No regression seen.
Hardware: Windows 11 Pro 26200, Ryzen 9 9950X3D (16 cores, SMT off; the game's threads on the 8 cores of the larger cache, as the log says), 32 GB, RTX 4070 SUPER 12 GB, NVIDIA 617.42
Build: this PR's head,
longshot v3.0.0-rc.5-41-g2435256a, clean tree, built here with clang for the MSVC target (so Windows' ownCreateWaitableTimerExW, not the shim)Scene: Castle Town, the walk from the south gate into the market, the turns and the walk back (a scripted route on the wall clock, 100 s), 2560x1440, DLAA, frame generation off, uncapped (
HOS_FPS_MAX=1000)Method: one build,
HOS_VSYNC_YIELD=1against unset, the two in turn, three runs each; headless. Frame figures from the frame trace (RX_VK_FRAME_TRACE=1, both sides) from 40 s on. CPU: every thread's CPU time read from outside over 20 s of the route (from 60 s on), nothing in the process touched. Then the same with the process held to four cores (affinity0xF); there the log says "the game's sleeps under a millisecond wait on the timer ... (4 logical processors", so only the host's own short sleeps still yield.All cores:
HOS_VSYNC_YIELD=1: this PR @2435256a2435256avsync_main)HOS_FPS_LOG), medianHeld to four cores:
HOS_VSYNC_YIELD=1: this PR @2435256a2435256aWhat I would not claim from this: a frame rate gain. This route does not show the same view every run: one run in three on each side sat in lighter views (about 1,950-2,000 draws a frame in the log's windows against 2,250-2,320), and those are the 62-70 fps ones. Like for like the sides are level: on all cores 60.5 and 61.1 against 57.3 and 61.6 in the heavier views and 70.3 against 69.2 in the lighter; on four cores 51.4 and 51.2 against 51.5 and 53.8, and 62.4 against 62.9. The CPU saving is there in every run.
One thing for the reviewers rather than a request:
host/hos_thread.c'ssleep_nsalready holds this timer (one per thread, with the same fallback), so the ticker could ask it for "the timer whatever the length" instead of keeping a second copy innn_vi.c. I had it that way locally, with cases added totests/test_short_sleep_timer.py; glad to send those if useful.🤖 Generated with Claude Code
Brought up to the ready-for-review standard (#1454): the checklist's Everyone list has two new boxes, added here from what this PR's description and comments show (please check they match what you know).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.