WIP: perf(host): the write marks' collection reads the marks in vector loads from any page, on every platform #936
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!936
Loading…
Reference in a new issue
No description provided.
Delete branch "mooki/Longshot:perf/page-scan-all"
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: macOS 26.7.1, Apple M4 Max (arm64; x86_64 under Rosetta), game-free; the same walk in the game on the macOS port · Needs: Windows and Linux, any GPU
Why
At every submit the renderer collects the pages of guest memory the game wrote since the last one, so they reach the GPU. It
walks one write mark per 4 KB page over every range the GPU can see, on the thread that replays the frame (and on the game's
main thread when it submits). Almost every mark is clear, so the walk's own steps are the cost. On main the walk tests 256, 32
and 8 marks at a time with word loads (#63), but only from a page aligned to that width: each range's unaligned head and tail,
and the pages around each mark that is set, go 32, 8 and 1 at a time. The macOS port's walk tests 64 marks at once from any page
in vector loads (#852, macOS only); there it took the replay's memory sync from 3.25 to 0.25 ms a frame against the Mac's byte
walk. This PR gives every platform that walk, so Windows and Linux spend less of the replay thread on it. Against main's word
loads the gain is smaller than on the Mac (whose walk before it read a byte at a time): game-free it is 3.4 to 4.9 times
faster on the capture's many short ranges, and no faster on the long clear ones, where main's walk already tests 256 marks at
once. The Windows and Linux numbers in the game are asked for. See #420, #63, #852.
What changed
The write marks' walk (
collect_in,host/rx_backend.c) reads the marks 64 at a time in vector loads from any page, then takeseach mark read set in turn with the same relaxed load and acquire exchange as before. It is #852's walk, for every platform, with
an AVX2 form of its loads. The pages found, their order, the marks cleared and the memory ordering are the word walk's.
HOS_COLLECT_VECTOR=0gives the word walk on the same build.marks_set64()loads 64 marks in inline asm: SSE2'smovdqu(four 16-byte loads) on x86-64, AVX2'svmovdqu(two32-byte loads) in a build for AVX2 (
__AVX2__: build.py's-march=x86-64-v3ornativeon a CPU that has it, ascopy_ntingpu/vk/vk_memory.cppchooses), NEON'sld1on arm64. One test of the OR, then a 64-bit mask of the marks thatare set (
pmovmskbagainst zero; on arm64cmtst, each byte's bit and three pairwise adds).marks_from()reads the window atany page; near a range's end it reads the 64 marks that end there (or begin at page 0) and drops those outside, all inside the
mark array (
_Static_assert(A64_DIRTY_PAGES >= 64)).collect_inkeeps a window's set marks and takes them one by one; theword walk is unchanged inside an
else. Built with GCC or clang (the asm is theirs) for x86-64 and arm64; another compiler orCPU builds the word walk alone (
HOS_MARKS_VECTOR).memcpyor intrinsic load of the marks is a plain C read of bytes the game's threads store atomically(
A64_MARK,hos_mark_written); the asm's loads are not C accesses, and each reads every byte whole (x86's 16- and 32-byteloads; arm64's
ld1of.16belements is sixteen single-copy atomic byte reads). They only choose which pages the walkreads again and clears, so they order nothing and lose no mark.
mem: the write marks' walk: 64 marks a test, in 32-byte loads (AVX2) (HOS_COLLECT_VECTOR=0: word loads)(or16-byte loads (SSE2),(NEON)), ormem: the write marks' walk: word loads (HOS_COLLECT_VECTOR=0).tests/test_collect_wide.pycompiles the walk twice in one program, as the word walk (HOS_MARKS_VECTOR 0) and asbuilt, and checks both against a plain walk and each other (pages, order, marks left) over 1,600 rounds of random marks and
ranges (aligned or not, short, empty, overlapping, near page 0, at and past the array's end, past the guest's end, marks just
outside each range, dense runs), with
HOS_COLLECT_VECTORon and off (each with main'sHOS_COLLECT_WIDEon and off, whichonly the word walk reads). It counts the steps (4096 clear marks: 16 tests of 256 in the word walk, 64 vector tests; 130 clear
marks from page 5: 3 vector tests, the last one the 64 marks that end at page 135; 4096 set marks: 64 vector tests, each
window's marks taken from one read), reads the log line, and requires the vector walk on x86-64 and arm64. On x86-64 it builds
the SSE2 walk (
-mno-avx2) and the AVX2 one (-mavx2, run where the CPU has AVX2).scripts/collect_bench.pytimes both walks game-free on mark arrays shaped like the game's (an indication only).HOS_COLLECT_VECTOR's row and the benchmark's (docs/development.md); main'sHOS_COLLECT_WIDErow now says it appliesto the word walk.
Measurements
Verdict: not measured in the game on Windows or Linux yet: the table below is owed, from the test request. Game-free on
this Mac (an indication only, below), against main's word walk: 4.9 times faster on the capture's 2,728 short ranges (arm64),
3.6 and 3.4 times under Rosetta (SSE2, AVX2); the collection's 60 long ranges 1.0 times on arm64 and 0.8 under Rosetta (main's
walk tests 256 clear marks at once there, this walk 64); dense ranges the same or faster; the pages identical at every
repetition. On the macOS port this walk took memory sync from 3.25 to 0.25 ms a frame (#852, E9b against E9c), but against the
Mac's byte walk, a slower baseline than main's word walk.
Baseline: the same build with
HOS_COLLECT_VECTOR=0, which is main's word walk (the same tokens, and the same instructionswhere the vector walk is not built), so the two columns differ only by the walk, as in #63.
Hardware: Windows and Linux PCs, any GPU: owed (test request T-G2L4)
Scene: Kokiri Forest on the pristine save, standing still, default options (#63's scene); and any busy scene the tester plays
Method: the renderer's own lines, every 600 submits:
vk: a submit's memory work (ms): ... writes collectedandvk: capture: a submit ... scan ... over N ranges, M MB; four runs in the order A B B A (AHOS_COLLECT_VECTOR=0, B the default), 8 lines a run, the median; each run'smem: the write marks' walk:line names its armHow to reproduce: build this PR; start it into Kokiri Forest on the pristine save with default options and stand still; read those lines in longshot_log.txt, once with
HOS_COLLECT_VECTOR=0and once withoutcab0f820cab0f820Game-free, an indication only (
python -I scripts/collect_bench.py, this PR @cab0f820): macOS 26.7.1, Apple M4 Max;arm64 native, x86_64 under Rosetta (
ROSETTA_ADVERTISE_AVX=1for the AVX2 program); -O2; the real mark array (1 << 27marks); warm caches, one thread, no writer; the Mac busy with other lanes' jobs (load average 15-29); median of 301
repetitions, in µs (word walk → vector walk, ×):
Rosetta runs x86 code translated to Arm code, so its columns indicate the walks' relative work, not an x86 PC's times. [INFERENCE]
With #63's Linux figure for the collection (0.03-0.04 ms a submit) and the bench's ratio for the capture, a PC might save a
few hundredths of a millisecond a submit, a few tenths a frame: the requested runs say.
Flags and settings
One new A/B switch,
HOS_COLLECT_VECTOR. Main'sHOS_COLLECT_WIDEkeeps its meaning for the word walk (withHOS_COLLECT_VECTOR=0or where the vector walk is not built); the vector walk does not read it.HOS_COLLECT_VECTORcollect_in,host/rx_backend.c), read once at the first collection=0: the word walk, as before=0only for A/BTesting
Tested: on macOS 26.7.1, Apple M4 Max, game-free, at this PR's head
cab0f820(on main7bacf846):tests/test_collect_wide.pyon arm64 (NEON) and x86_64 under Rosetta, the SSE2 build and, withROSETTA_ADVERTISE_AVX=1(without it Rosetta's CPUID says no AVX2), the AVX2 build: every switch setting passes. The same test on main's
rx_backend.c(--source) fails: no vector walk is built.aarch64-linux-gnu,x86_64-linux-gnu,x86_64-w64-mingw32andx86_64-pc-windows-msvc, the vector walk (its loads,marks_set64,marks_from, the window andthe step) is #852's token for token, and the switch's read differs only in the log's text (the loads' name, and the walk
with the switch off); an AVX2 build differs by its AVX2 load form alone.
switch, the log line, both gates, NEON's, SSE2's and AVX2's lanes, AVX2 left out), 19 mutant/arch runs: every one fails the
test, none by a build error alone.
the walk cut out of
rx_backend.cforx86_64-linux-gnu,x86_64-w64-mingw32andx86_64-pc-windows-msvcat-march=x86-64,x86-64-v2andx86-64-v3, andaarch64-linux-gnu, with the runtime's flags and-Wall -Wextra -Werror:-fsyntax-onlyand objects exit 0 on all 10; the asm's instructions aremovdqu(SSE2),vmovdqu(v3) andld1 .16b.With
HOS_MARKS_VECTOR 0the word walk is main's token for token (one{ }more) and instruction for instruction on all 10.python -I -B tests/run_all.py --jobs 6) on this Mac: 559 passed, 33 failed, 17 skipped (main7bacf846: 558, 34, 17). By name the one difference istest_collect_wide, which fails on main on macOS (its fixture's#define memcpycollides with the SDK's fortifiedmemcpy) and passes here; the 33 failures are main's own on this Mac.Not tested: Windows and Linux (no PC here): neither a build of the whole file or the game, nor the numbers. An x86 CPU
running the AVX2 walk (Rosetta translated it here). The game on this branch (main does not start on a Mac); the macOS port plays
this walk (#852).
Must be tested:
7bacf846) the same way and runpython -I -B tests/run_all.pyon each, then start each to the title, expectingtest_collect_wideto pass with the AVX2 and SSE2 walks, no other check to change status against main, and the linemem: the write marks' walk: 64 marks a test, in 32-byte loads (AVX2)in longshot_log.txt (Test Farm: @Beanstalk windows and linux: build unchanged, run_all, start to the title)HOS_COLLECT_VECTOR=0), expectingwrites collectedand the capture'sscanlower in B and the same picture; fill in the Measurements table (by hand: thevk: a submit's memory workandvk: capture: a submitlines, every 600 submits)Risks
What could break: the collection of written pages on every platform, x86-64 and arm64 builds with GCC or clang: a page left
uncollected would show as stale geometry or textures on the GPU. The test compares the two walks page for page and mark for
mark, and the macOS port has played this walk (#852: the same steps and loads; 12 of 12 must-shows in a 1,197 s hold). The AVX2
form of the loads is new and has run only under Rosetta here. A CPU without AVX2 runs the SSE2 walk, chosen at build time as the
rest of the build (no instruction the build's
-marchdoes not allow).Turning it off:
HOS_COLLECT_VECTOR=0, or revert the commit.Compatibility: Nothing changes: no cache, save,
longshot_options.inikey or vk ABI is touched.Reviewer notes
cab0f820, on main7bacf846; it merges with current main (fe3659d6) without conflict. Readcollect_inandmarks_frominhost/rx_backend.cfirst, thenmarks_set64's three load forms, then the test's step counts.host/rx_backend.c,tests/test_collect_wide.pyanddocs/development.md, and#852's
tests/test_darwin_guest_memory.pyfinds the walk by #852's comment; whichever lands second takes one vector walk(this PR's, for every platform) with #852's byte walk as macOS's
HOS_COLLECT_VECTOR=0walk. A resolution is ready, checkedon the merged tree (both tests, arm64 and x86_64).
game-free it took the long clear ranges from 0.8-1.0 to 1.1-1.2 times the word walk's speed, but it never ran in the game, so
it is left for a follow-up with game numbers.
Issues
See #420
See #852
Checklist
Everyone
type(scope): description) of the status line's type.main.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named).third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.tests/that reads changed source updated in this PR, not weakened.The walk reads well and I couldn't break it.
marks_from's tail window stays inside the array. Its shifts stay in range:p - wis 1..63 (0..62 near page 0) andp1 - pis 1..63.set/atreset per range. Each set bit still goes through the same relaxed load and acquire exchange. I also checked the NEON pairwise-add mask by hand and it is correct. Three points:e && e[0] == '0'(rx_backend.c:181) meansHOS_COLLECT_VECTOR=off,falseornoleaves the vector walk on. docs/development.md (~198) says off is0/false/off/nothroughenv_value_off, andhost/hos_thread.calready includesgpu/vk/compat/longshot_env.hfor exactly this.HOS_COLLECT_WIDEsets a bad precedent here, but a new switch should follow the rule. The test's fixture would need the header too.test_collect_widewith-mavx2on a real x86 CPU, plus whole-file builds on llvm-mingw and Linux gcc. Rosetta's AVX2 says little about either.This is from reading the code only. I didn't build or run anything.
🤖 Generated with Claude Code
@ -89,0 +178,4 @@if (vector < 0) {const char *e = getenv("HOS_COLLECT_VECTOR");int unread = -1;vector = !(e && e[0] == '0');Only a leading
0turns this off, soHOS_COLLECT_VECTOR=off,falseornokeeps the vector walk on. The documented rule (docs/development.md, the on/off paragraph) isenv_value_offfromgpu/vk/compat/longshot_env.h, which host/hos_thread.c already includes:vector = !(e && env_value_off(e));. The test fixture would need the header as well.@ -105,0 +200,4 @@set = marks_from(p, p1);at = p;if (!set) {p += 64;On a long clear range this takes 64 pages per test, where main's
marks_clear_256(withHOS_COLLECT_WIDEon) takes 256. That is the 0.8x in the bench's collection row, and it lands on the full collection every submit. A 256-wide clear test here, whileset == 0andp + 256 <= p1, would keep that shape at least as fast as main, and the window logic wouldn't change.Independent check for T-G2L4 (the card is not approved; no claim). Draft PR head
cab0f820, main7bacf846. Linux, NVIDIA RTX 4090 24 GB.Suite, source, game-free
7bacf846: 530 passed, 14 failed, 65 skipped.cab0f820: 530 passed, 14 failed, 65 skipped.test_collect_wideitself:ok: the same pages 256 marks a step as 32vector walk: built, 32-byte loads (AVX2)three times, thenok: the same pages, order and marks left in the word walk and the walk built here (x86_64 SSE2, x86_64 AVX2)Build/start/before-after: not run. The local shared lift tree expects the host symbol
hos_occlusion_off, which neither7bacf846nor this head defines; the link fails on main and on the PR. That is a local test-environment blocker, not a PR failure, and I did not fake a build or a before/after.The request remains open; this is only the suite half.
Generated by opencode with deepseek-v4.1-flash-1017
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.