WIP: perf(host): OoT's OptimizeChange thread sleeps 3 ms after each 300 us of running (HOS_BG_THROTTLE) #1642
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!1642
Loading…
Reference in a new issue
No description provided.
Delete branch "TrashPanda/Longshot:perf/bg-throttle"
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?
Summary
One of the game's background threads, OoT's OptimizeChangeBank (the lowest-priority one), never stops working on the
PC: on the console it only gets leftover time, here it takes about a third of a CPU core and slows whatever shares that
core. Hookshot made it pause for 3 ms after every 0.3 ms of work, which cut it to about 9% of a core. This ports that
throttle, on by default;
HOS_BG_THROTTLE=0givesmain's behaviour. In GrimVisage's in-game A/B at the Castle Townmarket it cut the long stutters, at about 1.5% of the average frame rate.
Read first:
host/hos_thread.c:bg_throttle(), called fromhos_vcore_checkpoint()(which every nn::os call and SVC alreadycalls). Each thread is sorted once by name (
HosThread.bg_kind); the OptimizeChange thread, after<run>since itlast woke (
bg_since), gives its virtual core up (hos_vcore_away), sleeps, and takes a core again(
hos_vcore_back). WithHOS_VCORES=0it still sleeps, holding no core.=1is 300:3000 (Hookshot's parser took=1as a 1 us run, against its ownrow);
=0is off.gpu/vk/switches.def: theHOS_BG_THROTTLErow (kindkill);docs/dev/reference/switches.md: its row underThreads and cores.
tests/test_bg_throttle.py(new);tests/test_vcore_escape.pygets aSleepstand-in, as the block it compiles nowcalls it.
The source is Hookshot's port of a fork's "native engine" (
git.notrexed.dev/Hookshot/Hookshot@60a53da7);the throttle is the one part with a number, so it is ported and measured alone. Prior work: none in Longshot (searched
BG_THROTTLE,OptimizeChange, "throttle", "background thread").Testing
Tested: in the game by GrimVisage on Linux: Fedora 45 (kernel 7.3.4), Radeon RX 9070 XT (RADV GFX1201, Mesa
26.2.7), Ryzen 5 5600X, 32 GB, this PR @
6f02fa5c, the switch unset against=0(numbers in Measurements). The onside's log had
threads: OptimizeChangeBanc runs 300 us, then sleeps 3 ms (HOS_BG_THROTTLE); no errors, hangs or devicelosses. The rebase onto
main@068fb8ebleft the game code identical to6f02fa5c(only the docs moved and the testgained
longshot_env.h). Game-free on Linux (g++, clang 21), before the rebase:tests/test_bg_throttle.pypasses here(the thread runs 9.1% of each second, never sleeps with
=0) and fails onmain; with the core kept while sleeping itfails. The full suite hasn't been run on this head yet; CI runs it on this push.
Still to test:
HOS_BG_THROTTLE=0against unset, interleaved, two runs each),expecting fewer frames of 50 ms or more with the throttle and the average within about 2%; Windows'
Sleep(3)depends on the timer resolution (@Beanstalk windows: Castle Town market A/B, HOS_BG_THROTTLE=0 against unset).
unset).
as quickly as with
=0, no pop-in or missing material (by hand).Measurements
Verdict: fewer long frames at the Castle Town market: frames of 50 ms or more 2 and 7 against 12 and 15, of 100 ms
or more 0 and 1 against 3 and 4, and a higher 1% low; the average about 1.5% lower (44.9 and 43.9 fps against 45.1 and
45.4). The sides don't overlap on any of these.
Baseline: the same build with
HOS_BG_THROTTLE=0, which ismain's behaviour; one build A/B'd by its switch, asinterleaved runs of two builds would add build-to-build noise.
Setup: Fedora 45, RX 9070 XT, Mesa 26.2.7, Ryzen 5 5600X; 2560x1440 fullscreen, HDR10, FSR 4 (1080p to 1440p), V-Sync
Auto (mailbox), no frame cap, frame generation off, threaded rendering on;
HOS_PROFILE=1on both sides. A scriptedroute through the control bridge from
kokiri-start: the debug teleport to the Market, the clock at 12:00, laps betweenthe gate and the fountain for 120 s. A warm-up run each, then interleaved (
=0, default,=0, default). Measured byGrimVisage.
6f02fa5c6f02fa5cRisks
The average frame rate is about 1.5% lower at the market with the throttle on (Measurements), the price of the fewer
long frames. The thread now sleeps at an nn::os call or SVC, including one that unlocks a mutex: if it sleeps holding a
game lock a frame thread wants, that thread waits up to 3 ms. The work it does (rebuilding model material blocks) gets
less time, so materials could arrive later after a scene change. Windows' sleep precision is untested. Turn it off with
HOS_BG_THROTTLE=0, or revert. Nothing changes in saves, settings, the caches or the vk interface.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named). (Not run on this head yet; CI's run on the push.)main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def.tests/that reads changed source is updated, not weakened.Sleep,getenv,sscanf,strstr.)Assisted-by:/Co-Authored-By:trailers (and Hookshot's author, NotRexed, asCo-Authored-By:).Possibly related, for context:
mooki referenced this pull request2026-11-18 23:27:04 +00:00
Tested in game on Linux, the first in-game run of this PR as far as I can see: Fedora 45 (kernel 7.3.4), AMD Radeon RX 9070 XT (RADV GFX1201, Mesa 26.2.7), Ryzen 5 5600X (12 logical processors), 32 GB RAM. This PR's head
6f02fa5cin one test build, the switch unset (the default, 300:3000) againstHOS_BG_THROTTLE=0. The on side's log hasthreads: OptimizeChangeBanc runs 300 us, then sleeps 3 ms (HOS_BG_THROTTLE). No errors, hangs or device losses.Castle Town market: fewer stutters, with a small cost in the average.
=01=02The frames of 50 ms and of 100 ms or more, the 1% low and the waits of 100 ms or more don't overlap between the sides. Neither does the average: about 1.5% lower with the throttle on, worth a line in Risks.
Settings: the player's own (Custom preset), 2560x1440 fullscreen, HDR10, AMD FSR 4 (1920x1080 to 2560x1440), V-Sync Auto (mailbox), no frame cap, frame generation off, FPS counter off; one kept options file for every run. Threaded rendering Auto (on).
HOS_PROFILE=1on both sides (frames from its presents trace, the waits from its virtual-core wait records).Method: a scripted route through the control bridge from the repository save
kokiri-start: the debug teleport to the Market, the clock at 12:00, then laps between the gate and the fountain, position-steered, 120 s. A warm-up run of each side first, then interleaved (=0, default,=0, default).6f02fa5c826ce0fc7d3cView command line instructions
Checkout
From your project repository, check out a new branch and test the changes.