perf(host): a larger, four-way cache for the sync object lookups (HOS_OBJ_CACHE, 16384 by default) #1781
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
5 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1781
Loading…
Reference in a new issue
No description provided.
Delete branch "snaildev99/Longshot:perf/obj-cache"
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
Every time the game locks or unlocks one of its mutexes, Longshot has to find its own object for it. The small per-thread cache in front of that search (1,024 direct-mapped entries) missed at almost every step of one graphics routine's walk over a table of mutexes (
f_80364080, the game's graphics front end, at a fixed 0x80 stride that the cache's hash folds onto few entries), so the slower shared-table search ran instead.docs/cpu-profile.mdmeasures that search (obj_lookup) at 3.1 to 4.1 % of a core and the cache (obj) at 1.6 to 2.3 %. This PR makes that cache larger, 16,384 entries four ways a set with a mixing hash, so the walk fits in it: the cache then misses 0.7 % of 21 million lookups in the Market.HOS_OBJ_CACHE=<entries>sets another size, andHOS_OBJ_CACHE=0keeps the fixed cache as before.The goal is less work per lock and unlock for the same result. Frame rate is the check that nothing got slower; the effect is small but consistent: Castle Town's Market ran 2.0 % faster standing still and 2.5 % faster turning the camera on main
a433db32(three interleaved runs a side), and +1.5 % standing as the mean of eight on/off pairs in a 48-run matrix, a gain in every pair.How:
obj_cache_entriesreads the switch (unset, empty or not a number: 16,384;0: the fixed cache; else 256 to 65,536);obj_lookup_cachedis made per thread at its first lookup (64-byte sets of four, murmur3's finalizer on the key, the oldest way replaced); a hit is validated exactly as the fixed cache's (the object's key and kind);obj_nockpttakes it when the switch is set;obj()adds the threads' counts up and logssync: object cache (HOS_OBJ_CACHE=n a thread, k made): L lookups, M missed (p %)every 10 s. Nothingobj_nockptreaches calls outsidetests/test_multiwait_wake.py's allowlist (it runs under the multi-wait lock). Why the hash and the ways: with the fixed cache's multiply-and-shift, 16,384 direct-mapped entries still missed 1,880 of a 4,000-slot walk every pass; four ways with the mixing hash, 47.Read first:
host/nn_os_sync.c(obj_cache_entries,obj_lookup_cached,obj_nockpt);tests/test_obj_cache_size.py; the row ingpu/vk/switches.def.See #1505 (the signal side of the same file), #1020 (where
obj_lookupis lead 4).Testing
Tested: Windows 11, RTX 3090 (NVIDIA 610.88), Ryzen 9 5900X. In the game: this change at its pre-rebase head in a test tree on main
a433db32(test/perf-prs@ be5623e0 on my fork, built from scratch twice, 0 failed): 15 hands-free Market runs, 6 withHOS_OBJ_CACHE=16384(0.69 % of 22 million lookups missed); in a v3.0.0 test tree, 24 Market runs with it (15 s standing, 40 s turning each, 36 caches made a run); before that two dozen hands-free runs in the Market and Hyrule Field with the first version on main544229a2. No hang, noFATALand no frame of 50 ms or more in any log. The rebase onto main068fb8eb(#1802) changed no code: onlydocs/development.md, which #1802 removed, lost the switch's row (its row ingpu/vk/switches.defis the reference now). The default went to 16,384 in2361252d(the row atuneswitch, the test run unset,=0and=256): the code paths are the ones measured, only which is the default changed. Rebased onto main89c3d21e(7f55fbe2): one line ofgpu/vk/switches.defmet #1814's newHOS_OCB_HOLD_MSrow, both rows kept; the PR's diff is otherwise the same line for line. At7f55fbe2the tests that readhost/nn_os_sync.c(13),tests/test_switch_table.pyandtests/test_env_on.pypass, but fortests/test_tracy_funcs.py, which fails on main89c3d21ealike (it reads Linux objects). Game-free at2361252d:tests/test_obj_cache_size.py(compiles the real table, the cache,obj_nockptand the flush: the value's parsing; the walk unset (47 of 4,000 slots miss its last pass), at=0and at=256(all 4,000 miss); 200,000 lookups among objects buried, re-kinded and reused, compared with the table; the log line)tests/test_multiwait_wake.py, the other checks that readnn_os_sync.c,tests/test_switch_table.pyandtests/test_env_on.pypass.tests/run_all.pyon Windows at2361252d: 708 passed, 47 failed, 116 skipped; main068fb8ebon this PC: 704 passed, 50 failed, 116 skipped. No failure that main does not have: main's, apart from timing tests that failed in one run only and pass when run alone (test_invalidate_vcore,test_compile_stops_on_exception,test_longshot_ctl,test_mesh_args_cleanup);test_vbytestimes out at 600 s in both. Main's failures on this PC:test_appimage_player_build,test_auto_handheld,test_bug_report_defaults,test_capture_read_once,test_crash_dialog,test_crt_options,test_damage_debug_menu,test_dev_ab,test_dev_build,test_dev_migrate,test_dev_packs,test_display_startup_failure,test_dlss_feature_size,test_failed_compile_keeps_objects,test_frame_overlap,test_frame_wait_bounds,test_game_menu_mouse_buttons,test_guest_imported,test_high_fps_fixes,test_host_guest_writes,test_hud_layout_row,test_job_queue,test_libc_hooks,test_lift_cache,test_linux_preflight,test_linux_pressure,test_make_game_ngx,test_mise_build,test_missing_nup,test_mods_list,test_ngx_sdk,test_pads_after_menu,test_pending_guest_import,test_pipeline_play_cache,test_portable_builder_home,test_portable_builder_startup,test_prepare_cache_save,test_present_thread,test_profiling_mode,test_psychov35_fsr3_fg,test_readback_timeout_source,test_sdiv_overflow,test_seed_reader,test_slow_frames,test_song_note_cue,test_upscaler_rows,test_vcore_escape,test_walk_key.Still to test:
sync: object cacheline with a miss rate under a few percent (@Beanstalk linux, reporting that line).Measurements
Verdict: less work per lock and unlock: 0.7 % of lookups miss the cache and fall through to the shared table. Frame rate is the check that nothing got slower: the Market +2.0 % standing and +2.5 % turning on main
a433db32; in the matrix +1.5 % standing (a gain in every pair, +0.3 to +1.7 fps) and +0.7 % turning (inside the runs' spread). No change in memory beyond the runs' spread. Nothing in Hyrule Field, where the renderer sets the frame.Baseline: one executable on both sides, the test tree
test/perf-prs@ be5623e0 on my fork (maina433db32with this PR and its siblings #1780 and #1782 merged, the default hot list,A64_VECFP=0so the lifted code is main's). Before is every switch unset, which then ran main's code path (the switch was opt-in when measured; with this PR's default the same two sides areHOS_OBJ_CACHE=0and unset); After sets only this one. Main has since moved ten commits, to89c3d21e. The first four (#1802 docs, tools and the PR template; #1774 and #1807 the Insert menu's rows; #1792 dev packs) change, outside comments, docs and the dev tools, the Insert menu's code and the version string and some log texts. The six after them are other performance work (#1843 ASTC re-encoding, #1814HOS_OCB_HOLD_MS, #1158 VRAM given back, #1848 capture buffers, #1782 the lift's float paths) and a CI change (#1850). None toucheshost/nn_os_sync.cor the lookups it makes, and each runs alike on both sides of an A/B that changes only this cache. They may move the Market's frame rate as a whole, so the percentages above are a433db32's; the farm request measures on current main.Setup: Windows 11 (build 22631), GeForce RTX 3090 (NVIDIA 610.88), Ryzen 9 5900X (12 cores), 128 GB. Child Link at the Market's save point by day; 3840x2160 full screen, frame rate cap Uncapped, no mods; the GPU wait 0.0 to 0.1 ms in every run (the processor sets the frame). Hands-free through
scripts/longshot_ctl.py: the save loads, 10 s to settle, 15 s standing (the FPS counter's value once a second), then 40 s with the right stick held to one side. Three runs a side, interleaved, the median.HOS_OBJ_CACHE=16384: test/perf-prs @ be5623e0With its two siblings on as well, the same runs: 76.0 standing (+10.5 %) and 68.1 turning (+8.1 %). No hang, no
FATALand no frame of 50 ms or more in any of the 15 logs.The 48-run matrix
The change was measured beside every combination of #1780, #1782 and main's
RX_VK_CAPTURE_NT, in one test treetest/perf-matrix@ 2c30c326 on my fork (v3.0.009cbdd97with the three changes, every lifted file at -O2 so that the hot list is not a variable, built withA64_VECFP=0and on). Sixteen variants, three interleaved runs each, 48 runs in 74 minutes with nobody at the PC. Before and After are the same executable; only this switch differs.HOS_OBJ_CACHE=16384RX_VK_CAPTURE_NT=1HOS_NVN_REC_HANDOFF=1RX_VK_CAPTURE_NT=1HOS_NVN_REC_HANDOFF=1The gain is about the size of the spread in any one pair, and shows as a gain because all eight pairs agree. Peak working set, unset against set: 9,944 to 10,030 MB either way. The run file is
docs/tested/2026-11-19-obj-cache-snaildev99-matrix-o.md.Offline (not a frame rate; the table sits in this process's caches, so a miss is cheap here): a walk of 4,000 mutexes at a 0x80 stride among 60,000 objects: misses a pass 4,000 (fixed cache) to 47 (16,384 entries); 17.4 to 7.4 ns a lookup.
The switch
HOS_OBJ_CACHE(new, run time, atuneswitch): entries of each thread's cache of sync objects (256 to 65,536, a power of two). 16,384 by default, as the measurements chose (the Market +2.0 % standing, a gain in every pair of the matrix, 0.7 % of lookups missed);HOS_OBJ_CACHE=0is the fixed 1,024-entry cache, main's path.Risks
What could break: a stale hit would lock the wrong object (a hang, or a race far away); a hit is checked exactly as the fixed cache's is (objects are never freed, only reused, and each hit checks key and kind). Memory: entries x 16 bytes for each thread that makes sync lookups, never freed when a thread ends: 256 KB a thread at 16,384, 36 threads in the Market, about 9 MB; not visible in the working set above.
Turning it off:
HOS_OBJ_CACHE=0(in the environment orlongshot_env.txt): the fixed cache, as on main;HOS_NO_OBJ_CACHEstill turns both caches off.After updating: each game thread that locks has a 256 KB cache (about 9 MB in the Market); saves,
longshot_options.iniand the caches do not change.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).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. (tests/test_obj_cache_size.py, new;tests/test_multiwait_wake.pyand the other checks that readnn_os_sync.cpass unchanged;tests/test_switch_table.pyreads the new row.)Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
Tested in game: Windows 11, RTX 3090 (NVIDIA 610.88), Ryzen 9 5900X: 24 hands-free runs in the child Market with HOS_OBJ_CACHE=16384 (15 s standing, then 40 s turning the camera), in a test build of v3.0.0 with this change (test/perf-matrix @ 2c30c326 on my fork). 36 caches made a run, 0.67 to 0.69 % of about 21 million lookups missed, no hang, no FATAL and no frame of 50 ms or more in any log; +0.3 to +1.7 fps standing in all eight on/off pairs. Not built or played at this head on current main yet: that is why this is WIP.
Possibly related, for context:
Built and played at this head. The test tree is
test/perf-prs@ be5623e0 on my fork: maina433db32with #1780, #1781 and #1782 merged, the default hot list, built from scratch twice (A64_VECFP=0, where the lifted code is main's, and the float paths on). Windows 11, RTX 3090 (NVIDIA 610.88), Ryzen 9 5900X, 3840x2160 uncapped, the child Market: 15 s standing, then 40 s turning the camera; five variants, three interleaved runs each, nobody at the PC, medians.HOS_NVN_REC_HANDOFF=1(#1780)HOS_OBJ_CACHE=16384(#1781)No frame of 50 ms or more, no
FATALand nonot a live recordingin any of the 15 logs; the object cache missed 0.69 % of 22 million lookups.Build cost of the float paths on a default build (both from scratch, 23 jobs): the lifted files' compiler time 7,374 s to 7,646 s (+3.7 %), the compile's wall time 458 s to 422 s (the slowest hot piece sets it, and it is not slower), lifted objects 679 to 687 MB.
The description has these numbers; the full 16-variant matrix on v3.0.0 is still its first table.
@Beanstalk linux: build this branch and play a few minutes in the Market with HOS_OBJ_CACHE=16384 set, expecting no hang and a "sync: object cache" log line with a miss rate under a few percent. This is the one box left before ready: the host code has only been built on Windows.
@snaildev99 only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was forf49be7c, and the PR has changed since (458a68b), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.f49be7cbe8458a68bc79Rebased onto main
068fb8eb(458a68bc). #1802 removeddocs/development.md, which held this switch's row; its row ingpu/vk/switches.defis the reference now, so that edit is dropped and no code changed. The description now uses the new template, and says plainly what this PR is for: less work on the game's threads (far fewer searches of the shared table on each lock and unlock), with frame rate as the check that nothing got slower.Why the numbers still hold on
068fb8eb: they were measured on maina433db32, and the four commits since (#1802 docs, tools and the PR template; #1774 and #1807 the Insert menu's rows; #1792 dev packs): outside comments, docs and the dev tools they change the Insert menu's code (#1774, #1807) and the version string and some log texts (#1802): nothing inhost/nn_os_sync.c, and nothing the Market runs each frame but the FPS counter's overlay, which runs alike on both sides. So the numbers hold.The farm request is reworded below, to measure the game's CPU time at a frame cap besides the frame times. A ~2 % change is close to what two runs a side can resolve, and the point of the change is less CPU time for the same frames.
@Beanstalk linux: the Market for a few minutes, HOS_OBJ_CACHE=16384 against unset. Measure the game's CPU seconds with the frame rate capped at 60 (on a PC that holds 60 there), and the frame times uncapped. Expect the same frame rate or better with less CPU time, no hang, and a "sync: object cache" log line with a miss rate under a few percent; report that line.
@snaildev99 only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was for458a68b, and the PR has changed since (2361252), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.WIP: perf(host): a larger, four-way cache for the sync object lookups (HOS_OBJ_CACHE, opt-in)to WIP: perf(host): a larger, four-way cache for the sync object lookups (HOS_OBJ_CACHE, 16384 by default)The object cache is now 16,384 entries by default (
2361252d): docs/dev/standards.md says a switch ships at the value its measurements chose, and never off just because it is new or unmeasured, and the measurements here choose 16,384 (the Market +2.0 % standing on maina433db32, a gain in all eight pairs of the matrix, 0.7 % of lookups missed).HOS_OBJ_CACHE=0is the fixed 1,024-entry cache, main's path; another number sets another size;HOS_NO_OBJ_CACHEstill turns both off. The row is now atuneswitch, andtests/test_obj_cache_size.pychecks unset,=0and=256. Only the default changed: both code paths are the ones measured. The Linux run below is now main against this PR.@Beanstalk linux: main against this PR in the Market for a few minutes (the 16384-entry cache is the default now). Measure the game's CPU seconds with the frame rate capped at 60 (on a PC that holds 60 there), and the frame times uncapped. Expect the same frame rate or better with less CPU time, no hang, and a "sync: object cache" log line with a miss rate under a few percent; report that line.
@snaildev99 only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.@Beanstalk
Beanstalk test farm · an earlier run: commit
2361252(the newer results are in a comment below)Commit
2361252· tier reviewed · approved by HumanBean · dashboardWhat will be tested · AI plan (
gpt-6.1-terra)On Linux, compare main and the PR in the child Market, with a 60 FPS cap and uncapped to expose CPU and frame-time effects. The run takes about 22 minutes per tester and records CPU seconds, FPS, frame times, memory, and the cache miss-rate log. Crashes, missing cache logs, a settled miss rate above 3%, or substantial performance regressions fail the plan; the capped FPS check also verifies that the test PC holds approximately 60 FPS.
0or6.kokiri-start(settings vsync=2, frame_generation=0), then teleport to Market, measure market for 40 s (after 15 s warm-up); 2 runs on each side for each of 2 cells (1 game start a run).Done (started 08:11 UTC). Comment
@Beanstalkto run it again.frame_rate_cap=0frame_rate_cap=62361252d037f55fbe26bBeanstalk test farm
Commit
7f55fbe· approved by nobody yet · dashboardA member of
BigBeanscan review this commit and comment@Beanstalkto test it.Rebased onto main
89c3d21e(7f55fbe2). #1814 addedHOS_OCB_HOLD_MSon the line ofgpu/vk/switches.defwhere this PR's row goes, so the PR no longer merged; both rows are kept, ours first, and the rest of the diff is the same line for line. The tests that readhost/nn_os_sync.c, the switch table andenv_onpass at the new head (test_tracy_funcsfails on main alike: it reads Linux objects).WIP: perf(host): a larger, four-way cache for the sync object lookups (HOS_OBJ_CACHE, 16384 by default)to perf(host): a larger, four-way cache for the sync object lookups (HOS_OBJ_CACHE, 16384 by default)Data on the same lookup from a different approach, in case it helps this PR. HeyListen 86d68f05 (Sacrappy/HeyListen, GPL-3.0) skips the search for mutexes altogether. It stores the host object's number in the guest
nn::os::MutexType's nesting-count word (+8), which this layer never uses, and takes it only while that object is still the address's mutex. A copied or cleared mutex falls back to the normal lookup and gets a new number. That covers exactly thef_80364080slot walk you describe (one different mutex per slot).On an RX 9070 XT / Ryzen 7 5800X (Linux, RADV), with our build on
main@89c3d21eand the port on by default:hf48rununcapped, 2 interleaved runs a side.HOS_NO_MUTEX_IX=1(the lookup as onmain) gives 79.9 / 81.0 fps against 82.3 / 83.1 with it on (run_a medians), about +3 %. Every other change in that build measured within noise of it in the same battery. I haven't measured it together withHOS_OBJ_CACHE, so I can't say whether the two add up or cover each other.Your PR owns the lookup, so I'm not opening a competing one. If you want the index as a commit on top of this, or as a separate small PR afterwards, say so and I'll send it with a main-vs-PR run.
Thanks, that's a good lead, and the slot walk is the right target. #1781 was merged a few hours ago (
2210931c), so a separate small PR on current main is the clean way; I'd be glad to see it, with HeyListen credited.Two things would help it:
HOS_OBJ_CACHE(16384) is on by default and answers about 99.3 % of the Market's lookups, so much of the walk's cost is already gone there. Your +3 % was against89c3d21e, before the cache. Main against main plus the index, and the same pair withHOS_OBJ_CACHE=0on both sides, would show how much each adds.MutexTypetoday, so whatever game or SDK code reads there sees what the game wrote. A note that no lifted code loads aMutexType's +8 outside thenn::osimports would settle it.