WIP: perf(host): four switches read on per-call paths are read once #1952
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
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1952
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/switches-read-once"
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
Four switches were looked up in the environment every time the code that reads them ran, and the C runtime's
getenvwalks the whole environment at each call:
HOS_NO_MOVIESat everynn::fs::OpenFilethat opens a file for reading (host/nn_fs.c);HOS_RES_SCALEat every present while the Insert menu's Resolution row is set (realtime_step,host/nn_vi.c);HOS_VIDEO_RGBAfor every decoded movie picture (queue_frame,host/nn_video.c);RX_VK_LOG_PARTIALat every partial texture upload (texture_touch,gpu/vk/vk_texture.cpp).The first three are now read once at the start:
hos_fs_env,hos_video_envandhos_vi_env, whichgame_maincallsnext to its
HOS_TRACEread, once the environment is set (longshot_env.txt) and before the game's main thread ismade;
HOS_RES_SCALEis also parsed there, andrealtime_stepapplies the kept value where it applied the looked-upone (at each present, the same value, with no first-use flag). The game's threads read
the values without a lock, written before those threads exist. The renderer has no such start, so
RX_VK_LOG_PARTIALstays a C++ function-local
static const(its initialisation is thread-safe), now looked up by the first partial uploadonly: a whole upload passes no guard. What each switch does is unchanged. (Main's
mirror_budgetalready readsRX_VK_MIRROR_MBonce; the change this comes from, d7dc45e2 onexp/x86-asm-sync, also did that one.)Read first:
hos_fs_env,hos_video_env,hos_vi_envand their call ingame_main(host/hos_main.c);tests/test_vcore_fs_open.py, which runsOpenFilewithHOS_NO_MOVIESflipped after the start's read, andtests/test_switches_read_at_start.py.Testing
Tested: Windows 11, i7-9700, RTX 4060 (NVIDIA 610.88), llvm-mingw clang 23.1.5; game-free checks and a
micro-benchmark only. This change has not run in the game on this PC yet (Still to test). Each check ran in a hidden
console of its own, in one job of the PC's job queue (fb-1952-4), on this branch rebased onto
main@6e1521a7, heade43ef403:python -I -B tests/test_vcore_fs_open.py: ok, in a fresh process each forHOS_NO_MOVIESunset,0,off,1and
yes, the switch flipped after the start's read: the movies follow the value at the start. It fails at thechecks' commit
641ace07(no start read yet), and on three wrong caches written into copies ofhost/nn_fs.c(0 takenfor not yet read, read at the first movie, read at the first
OpenFile).python -I -B tests/test_switches_read_at_start.py: ok (the three calls ingame_mainbefore the game's threads, noread left in
OpenFile,queue_frameorrealtime_step,RX_VK_LOG_PARTIALinside the partial branch; theResolution block with
HOS_RES_SCALEunset,0.5and empty, flipped after the start's read, and two live menuchanges without it). It fails at
641ace07.host/nn_fs.c,host/nn_vi.c,host/nn_video.c,host/hos_main.candgpu/vk/vk_texture.cppcompiled to objectswith the build's own commands (llvm-mingw, not linked; the game was not built or run).
python -I -B tests/run_all.py --strict --changed origin/main(the checks that read a changed file, 8 at once): 132passed, 31 failed, 12 skipped. 14 of the 31 fail on
main@6e1521a7too, run by name in the PC's job queue(fb-1952-2 and fb-1952-3):
test_crash_dialog.py,test_crash_record.py,test_dev_packs.py,test_display_startup_failure.py,test_host_guest_writes.py,test_libc_hooks.py,test_lift_cache.py,test_linux_pressure.py,test_mods_list.py,test_perf_report.py,test_profiling_mode.py,test_readback_timeout_source.py,test_slow_frames.py,test_vcore_escape.py. The other 17 compiled nothing inthat run nor in the runs on
main(no Vulkan headers on the jobs' path: "not found: vulkan_headers (--strict)").Rerun with the headers (job fb3-vk-2,
RX_CPMset) at the heade43ef403and onmain@6e1521a7: 11 pass at both(
test_device_policy.py,test_dump_readback.py,test_fsr3fi_hud_mask.py,test_image_view_retry.py,test_inline_write_order.py,test_pipeline_state_portable.py,test_sparse_bind_failure.py,test_texture_staging_publication.py,test_vulkan_clear_mip.py,test_widen_alpha.py,nx2/test_texture_init_barrier.py); 6 fail at both with the same first error:test_low_vram_texture_mips.py(srccut: "'bool low_vram_texture_mips(' is in gpu/vk/vk_texture.cpp ... more than once"),
test_pass_barrier.py("FAIL: vk_work.h's vkCmd* macros changed"),
test_staging_burst.py("vk_texture.cpp's arenas are no longer 64 MB:update this test"),
test_target_reuse_memory.py(2 of its 8 tests: "'vmaGetAllocationInfo2' unexpectedly found" inthe VMA source, and its fixture's "use of undeclared identifier 'destroy_image_later'"),
test_texel_buffer_ownership.py(anAssertionErrorat its count ofdestroy_import_buffer() andtest_ub_update_batch.py(srccut: "'namespace mkwvk {' is in gpu/vk/vk_work.h ... more than once"). So 20 of thatrun's 31 failures fail, all on
maintoo, and 11 pass; none fails on this branch alone. Among those that pass:test_pacing_policy.py,test_frame_carry_cap60.py,test_frame_rate_cap_step.py(which compilerealtime_step)and
test_close_waits_for_save.py(which readsOpenFile).Earlier, the first version of this change on
main@8859673e(e49530fb): the micro-benchmark in Measurements, and abuild of the game with it (
longshot.exeandlongshot_vk.dll, llvm-mingw).Still to test:
main@6e1521a7, warmed andinterleaved, the same settings, no switches set: Castle Town's market for 60 s (average fps, 1 % lows, median and
p99 frame times), a cold and a warm area load, a movie played with
HOS_VIDEO_RGBAunset and=1, and a live changeof the Insert menu's Resolution, expecting frame times within the runs' spread or better and the same pictures (by
hand)
python3 -I -B tests/test_vcore_fs_open.pyandpython3 -I -B tests/test_switches_read_at_start.py, expecting both to pass (by hand)area load, this PR against
main, expecting the same frame times asmainor better and no crash (by hand, or aTest Farm request)
Measurements
Verdict: micro-benchmark only, not measured in play yet: a
getenvof a switch that is not set took 3253 ns withan environment of 136 variables, against 1.34 ns for the kept value. That is the lookup alone, not a file open, a
present, a movie picture or an upload, and nothing here shows a gain in the game.
Setup: Windows 11, i7-9700, llvm-mingw clang 23.1.5,
-O2 -march=nativeas the host is built; one UCRTgetenvof aswitch the game does not set, in the benchmark's own process (the job queue's environment, 136 variables), against a
static read; 21 interleaved rounds, the median and the rounds' range, in one job of the PC's job queue
(
microbench.py getenv). Measured onmain@8859673ewith the first version of this change (e49530fb), not againon this head; the kept value is a plain static read in both.
8859673e8859673e)getenvRisks
A switch set or changed while the game runs is no longer seen: the three host switches are read once, at the start,
after
longshot_env.txt; the host sets its environment once, at start-up, and noHOS_ABreader names these four, soa run sees the same values as before.
HOS_RES_SCALE's value is stored into the fixed path's scale at every presentwhile it is set (the same value each time), where it was stored once, at the first present. A revert is enough; players notice
nothing (no saves,
longshot_options.inior caches 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
tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Recommendation: Request changes. The file-open and movie-decode caches introduce data races, and the texture change adds work to whole uploads. The isolated lookup is faster, but the PR has yet to show a gain in play.
The PR caches four environment switches to cut CPU work during file opens, presents, movie decoding and texture uploads. The existing defaults are appropriate, but the caches need safe initialization, and the resolution override can use one lookup and one parse.
Does it help: Unproven. The author's Windows 11 UCRT microbenchmark on an i7-9700 with 136 environment variables reported a median of 3,253.10 ns per unset lookup versus 1.34 ns for a cached read over 21 interleaved rounds, using
8859673eand e49530fb. It measures the isolated lookup. There are no paired in-game results for frame times, file loading, movie decoding or texture uploads on the reviewed base.Other systems: Concurrent asset readers and separate movie decoders share the new host caches on Windows and Linux. The resolution path still reads the live Insert menu setting when the override is absent. The texture change is shared across Vulkan vendors and replay modes, including RADV and Steam Deck. It affects CPU-side diagnostic work on the upload path. The diff changes no upload sizes, decoder allocation sizes, saves, settings format or renderer API.
Switches: No switches added or removed. HOS_NO_MOVIES defaults to playing movies. HOS_RES_SCALE defaults to following the Insert menu, and presence still takes precedence. HOS_VIDEO_RGBA defaults to deferring RGBA conversion for NV12/YUV420. RX_VK_LOG_PARTIAL defaults to logging off. All four defaults are appropriate for a startup configuration and already appear in gpu/vk/switches.def. The diff freezes their values for the run. The startup loader and HOS_AB call sites show no supported live update for these four.
Reviewed:
c9d74d7845against2e1f6c32d3: Read the complete diff, commit, AGENTS.md, CONTRIBUTING.md, Forgejo template, developer standards and tests/test_vcore_fs_open.py. Read the changed C and C++ functions in full, their relevant callers, locks, startup environment loading, resolution readers, switch rows and test runner integration. Other code in those source files was outside the review path. Skimmed the author's run summaries. The raw benchmark and CI logs were unavailable, and docs/review-checklist.md was absent. The PR supplied no comments or prior reviews.; nothing run (a code review).Checked and fine:
Blocking (to change before merge)
Waiting on the author (a run, a measurement or a decision; no edit does it)
Testing: Read-only review. The author reports that the focused Windows test passed on b359f827. The changed-source run reported 83 passed, 9 failed and 5 skipped, with the same nine failures individually reproduced on
fc4a6769. The author also reports an earlier Windows build. Linux and in-game runs were pending, and there were no tester comments.Overlaps and merge order: No overlapping pull request found in the checkout or supplied discussion. The cited experimental commit is the source of the change. The forge's open PR list was unavailable.
Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.
@ -3352,3 +3352,3 @@return tex;}if (partial && env_on("RX_VK_LOG_PARTIAL")) {static const bool log_partial = env_on("RX_VK_LOG_PARTIAL"); // (read once: it was a getenv at every partial upload)Whole uploads now hit the static guard and can initialize it. Put the cached lookup behind the partial check so whole uploads skip the diagnostic.
@ -393,0 +391,4 @@game goes on without them (decoded by nn_video.c otherwise). Read once, at the first OpenFile that gets here: itholds for the run, and the C runtime's getenv walks the whole environment at every call */static int no_movies = -1;if (no_movies < 0) no_movies = env_on("HOS_NO_MOVIES");Concurrent opens can reach this plain static after releasing the virtual core and mount lock. Equal stores still race. Initialize it before guest threads or use a thread-safe once operation.
@ -475,1 +475,3 @@below stays over it) */below stays over it. Whether it is set is read once: it was a getenv at every present while the row is set) */static int res_env = -1;if (res_env < 0) res_env = getenv("HOS_RES_SCALE") != NULL;The first present still looks up this variable two or three times. Parse it once during startup and use that snapshot for both menu precedence and the override value.
@ -427,2 +427,2 @@const char *rgba_env = getenv("HOS_VIDEO_RGBA");const int force_rgba = rgba_env && !env_value_off(rgba_env);static int force_rgba = -1; /* (read once: it was a getenv for every decoded picture) */if (force_rgba < 0) { const char *rgba_env = getenv("HOS_VIDEO_RGBA"); force_rgba = rgba_env && !env_value_off(rgba_env); }Each decoder holds its own lock. Two decoders can race when setting this shared flag. Initialize it before decoding can start or use a thread-safe once operation.
@ -131,2 +131,4 @@check(r == R_PATH_NOT_FOUND && held == 1 && held_in_create == -1, "a movie left out (HOS_NO_MOVIES): the core back");set_env("HOS_NO_MOVIES", NULL);r = open_file("rom:/Movie/b.webm", 1, 1);check(r == R_PATH_NOT_FOUND && held_in_create == -1, "HOS_NO_MOVIES is read once, not at every OpenFile");This passes with a cache that only remembers true or starts on the first movie. In two fresh processes, flip the switch after the first ordinary read and verify both initial values.
Recommendation: Request changes. Concurrent file opens and movie decoders can race while initializing their new process-wide C flags. Initialize both after startup environment loading and before guest threads start, or use synchronized one-time initialization.
host/nn_fs.c:394runs afterOpenFilereleases its virtual core andhost_pathreleases the mount lock. Two ordinary asset opens can both write the cache even with the switch unset. If the fix keeps lazy initialization, preserve the read-only guard so a write-capable open on a writable mount skips this lookup. File-open discussionhost/nn_video.c:428shares the flag among separate decoders. Two first-frame calls can hold differentd->lockinstances and still write the same flag. This covers the FFmpeg and the AV1 callback paths. The predicate should preserve the existing mode selection. Decoder discussionDoes it help? Unproven in play. The author's Windows microbenchmark on earlier revisions reports median 3,253.10 ns for an unset lookup and 1.34 ns for a cached read over 21 interleaved rounds with 136 environment variables. Compare the corrected head and its base in interleaved gameplay runs that exercise presents, asset loading, movie decoding and texture uploads. Include average FPS, 1% lows, median and p99 frame times, and the relevant load, decode and upload timings.
Other findings
HOS_RES_SCALEtwice when absent and three times when present. Derive presence and the parsed override from one read. Keep live Resolution updates when the override is absent, and preserve the current presence rule for an empty value on Linux. Resolution discussionpartialfalse, reachingtexture_touch's upload block now runs the logging initializer on its first visit and checks the guard thereafter. Leave the cached lookup inside the partial branch so initial and whole uploads skip it. The existing upload ranges and failure handling are preserved. Texture discussionSwitches and scope
The default is to find movies, follow the live Resolution menu, defer RGBA conversion for NV12/YUV420 frames and leave partial-upload logging off. All four switches already have rows. Both host platforms and the shared renderer path are in scope. This diff leaves the save format, renderer API and generated files alone.
Review and testing
Reviewed all five changed files and their relevant call paths at
c9d74d784542against2e1f6c32d30e. The author reports 83 passing, 9 baseline-matching failures and 5 skips on the prior Windows revision, plus the focused OpenFile pass. The exact head still needs the author gameplay run, the applicable Windows and Linux checks, and affected Tier 1 coverage. Forgejo showed the description check passing and source jobs waiting on required conditions. Beanstalk showed no reports. No overlap requiring a merge order was found among the inspected branches.@ -393,0 +392,4 @@holds for the run, and the C runtime's getenv walks the whole environment at every call */static int no_movies = -1;if (no_movies < 0) no_movies = env_on("HOS_NO_MOVIES");if (!want_write && no_movies) {On a writable mount, a mode with write or append bits reaches the newly hoisted cache lookup, though the base short-circuited before the switch lookup. If initialization stays lazy after the race fix, put it under the read-only guard.
@ -476,2 +477,3 @@if (res_env < 0) res_env = getenv("HOS_RES_SCALE") != NULL;const int scale = rx_option(3, 0);if (scale > 0 && !getenv("HOS_RES_SCALE")) {if (scale > 0 && !res_env) {Preserve the distinction between unset and empty when consolidating the reads. Linux can retain an empty variable, and the existing presence test treats it as an override. With the override absent, verify that a second live menu change still takes effect.
c9d74d7845e43ef40379Thanks for the review. The branch is rebased onto
main@6e1521a7(the PR's own commit unchanged,git range-diffshows it equal), with two new commits on top:641ace07(the checks) ande43ef403(the change). The checks ran in the PC's job queue (fb-1952-4): both new checks fail at641ace07and pass ate43ef403.no_moviesraced by parallel opens): fixed ine43ef403.HOS_NO_MOVIESis read byhos_fs_env, whichgame_maincalls next to itsHOS_TRACEread, afterlongshot_env.txtand before the game's main thread is made;OpenFileonly reads the kept value, and no path of it looks the switch up, the write path included.force_rgbashared by decoders): fixed ine43ef403the same way (hos_video_env);queue_frameno longer reads the environment.HOS_RES_SCALElooked up 2-3 times, a first-use flag): fixed ine43ef403.hos_vi_envreads and parses it once at the start (presence!= NULL, as before);realtime_stephas no first-use flag and no lookup: the kept value is applied where it was (stored at each present while it is set), and the Insert menu's Resolution only when it is not set.tests/test_switches_read_at_start.pyruns that block withhos_vi_envin fresh processes (unset,0.5, empty), the variable flipped after the start's read: the override holds, and without it two live menu changes both take effect.e43ef403: thestatic constlookup is insideif (partial), so a whole upload neither initialises nor passes it.641ace07.tests/test_vcore_fs_open.pynow runs a fresh process for each ofHOS_NO_MOVIESunset,0,off,1andyes(the value checked inside the process), flips the switch right after the start's read and checks that the flip took, then opens an ordinary file and two movies, which must follow the value at the start. That is stricter than flipping after the first ordinary open: a read at the firstOpenFilefails too. Run against three wrong caches written into copies ofnn_fs.c(0 taken for not yet read, read at the first movie, read at the firstOpenFile), it fails on each.tests/test_switches_read_at_start.pychecks the three calls' place ingame_main, thatOpenFile,queue_frameandrealtime_stepno longer read these switches, and thatRX_VK_LOG_PARTIALis looked up inside the partial branch only.Also run at
e43ef403: the five changed translation units (nn_fs.c,nn_vi.c,nn_video.c,hos_main.c,vk_texture.cpp) compiled with the build's own commands, llvm-mingw, to objects (not linked);tests/run_all.py --strict --changed origin/main: 132 passed, 31 failed, 12 skipped; the 31 fail onmain@6e1521a7too, run by name (fb-1952-2, fb-1952-3), none on this branch alone.Waiting on me, not done yet (listed in Still to test): this head has not run in the game, so there are no frame-time, load, decode or upload timings. The only figure is the micro-benchmark of the lookup alone; nothing shows a gain in play yet. The paired runs (head and its base, warmed and interleaved, with a live Resolution change and both movie conversion modes), the Linux build and tests, the Tier 1 runs and CI on this head are pending.
@futondestroyer thanks for the review (62736). Fixed in
e43ef403(checks in641ace07), rebased ontomain@6e1521a7:HOS_NO_MOVIESandHOS_VIDEO_RGBAare no longer initialised lazily.hos_fs_envandhos_video_envread them ingame_mainafterlongshot_env.txtand before the game's main thread is made;OpenFileandqueue_frame(FFmpeg's and the AV1 callback's path both go through it) only read the kept values.HOS_VIDEO_RGBA's predicate isenv_on, which is the same test as before (set && !env_value_off).OpenFiledoes no lookup on any path, the write path included.hos_vi_envtakes presence and the parsed value from onegetenvat the start, presence being!= NULLas before, so an empty value still counts as set.realtime_stephas no lookup or first-use flag left.tests/test_switches_read_at_start.pycompiles that function and realtime_step's Resolution block and runs them in fresh processes (unset,0.5, empty), flipping the variable after the start's read. With the override absent, two later live menu changes both take effect. On this Windows PC the empty value reached the process and counted as set (the override, 0, held over the menu); the Linux run is pending.if (partial); fresh processes for unset,0,off,1andyes, with the flip checked).Pending on my side: the interleaved gameplay runs, the Linux checks, Tier 1 and CI on this head.
@Beanstalk This reads four switches once at startup instead of on every call: HOS_NO_MOVIES (each file open for reading), HOS_RES_SCALE (each present), HOS_VIDEO_RGBA (each movie frame) and one renderer switch read at each texture upload (gpu/vk/vk_texture.cpp). Behaviour must not change: movies play, a Resolution change in the Insert menu applies at once. It has not run in the game on these PCs yet. Please start from the title with its movie, then play Castle Town Market for two minutes with one area load and one change of the Insert menu's Resolution during play, the PR against main on the same PC, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. No crash or hang; the movie plays and the new resolution applies as on main; frame time p99 equal to main's or better, within the run-to-run spread.
@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.The 17 renderer checks that failed with "not found: vulkan_headers (--strict)" did not compile in the earlier run (no Vulkan headers on that job's path); rerun with them in job fb3-vk-2, at the head
e43ef403and onmain@6e1521a7: 11 pass at both and 6 fail at both with the same first error, so 20 of the 31 failures fail (all onmaintoo); Tested's sentence and counts corrected.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.