WIP: perf(host): the write marks' asm walk beside the vector walk (lsasm_collect_marks, HOS_COLLECT_ASM) #2130
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
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2130
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/asm-collect-marks"
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
Before each submit the host collects the pages the game wrote since the last one by reading and clearing the lifted
code's write marks over the GPU-visible ranges (
collect_ininhost/rx_backend.c). Main has two walks for it since#936: the vector walk (
HOS_COLLECT_VECTOR, the default) and the word walk before it.This adds a third walk beside them, on the asm kit:
host/asm/collect.Slsasm_collect_marks. It tests 256 marks atonce from any page while none is set, a range's last 256 in one more test (reaching back below the range, read only),
then walks 32-mark windows by their set bits, each mark read set taken with an
xchg; SSE2's 16-byte loads, or AVX2's32-byte ones in a build for AVX2. C keeps the list: the kernel stops at a mark it has no room for, the list grows and
it goes on. #936's review rule is kept: the reads that decide the pages after one taken come after its
xchg(thebranch's kernel used a window's whole mask read before the takes; here the mask is used on only while its next set
mark is the next page, else the marks are read again from the page after the take, as main's vector walk does). The
mem: the write marks' walk:log line names the walk in use.scripts/collect_bench.pyis meant to time the asm walkbeside main's two, but it does not build at this head since the rebase onto the kit (its stand-ins lack
hos_xrealloc, whichcollect_innow calls); the fix follows with the next push. A copy of it with themissing stand-in ran here (Measurements). The result expected was the same pages collected, the walk faster than the vector walk or as fast; the micro-benchmark finds it slower on the capture-shaped walks and faster only with many marks set.
The switch:
HOS_COLLECT_ASM(new, a kill switch, bool, its row ingpu/vk/switches.def), on by default as thebranch shipped the kernel, read once with
HOS_COLLECT_VECTORat the first collection: withHOS_COLLECT_VECTORon,the asm walk;
HOS_COLLECT_ASM=0gives back main's code, the vector walk;HOS_COLLECT_VECTOR=0stays main's word walkwhatever
HOS_COLLECT_ASMsays. Built on x86-64 with GCC or clang on Windows and Linux, elsewhere main's walks alone.From 971f8074 on
exp/x86-asm-sync. Depends on #1947 (the asm kit, which now carries the GPU-visible ranges' sort,lsasm_sort_ranges): its commits show in this diff until #1947 merges.Not the same idea as the open PRs on write marks: #1558 sets fewer marks (none outside GPU-mapped groups), #1597 marks
fewer pages after a zstd stream, #1034 adds time spans and marks four unmarked guest writes, #1780 hands NVN recordings
over without a copy; none changes how the marks are read and cleared, which is all this walk does.
Read first:
host/asm/collect.S, thencollect_ininhost/rx_backend.candtests/test_asm_collect.py.Testing
Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23 (
CC/CXX,RX_CPMset); head4545cecaon #1947's head
cffa2f46(the asm kit, on main9dd41326). Through the PC's job queue, each run in a hidden console,tests/run_all.py --strict --exact --jobs 2 --timeout 1200: the change's own checks (light jobpr-tb-AsmCut5-light,log
t-AsmCut5-asm-collect):test_asm_collect.py,test_collect_wide.py,test_hos_ab.pypass (3 of 3).--changed cffa2f46(build jobpr-tb-AsmCut5-build, logt-AsmCut5-chg-asm-collect): 61 checks, 47 pass, 14 fail, each of whichfails on #1947's head @
cffa2f46too (a worktree atcffa2f46in the queue's jobs, logst-AsmCut5-base-kit-changedandt-AsmCut5-base-kit-more):test_crash_dialog.py,test_dev_ab.py,test_dev_build.py,test_device_lost_exit.py,test_frame_overlap.py,test_host_guest_writes.py,test_linux_pressure.py,test_menu_text_field.py,test_perf_report.py,test_present_queue_default.py,test_renderer_new_handler.py,test_slow_frames.py,test_target_reuse_memory.py,test_vk_occlusion_queries.py.It has run in the game on this PC: the in-play A/B in Measurements (held job
fb-g2130, the Market, 19 runs),with no FATAL, device lost or Vulkan error in any run.
Still to test:
uncapped (
HOS_FPS_MAX=240), on #1947's head, on this PR withHOS_COLLECT_ASM=0and on this PR with it unset, eachwarmed a minute, then at least three interleaved three-minute runs of each (ABC ABC ABC), and one run of each with
HOS_PROFILE=1(python -I scripts/perf.py reporton its folder:collect_in's share of its thread), expecting thelog line naming
lsasm_collect_marksunset, the same picture all three ways, no FATAL, that share lower and fps andframe times of this PR unset at least those of the other two; a difference beyond the runs' spread is a gain or a
loss, one within it is no difference measured (by hand)
PR with
HOS_COLLECT_ASM=0and on this PR with it unset, warmed and interleaved, expecting the same picture andframe times within the runs' spread or better unset (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel,
steamdeck: Kokiri Forest three minutes, #1947's head, HOS_COLLECT_ASM=0 and unset, warmed and interleaved, frame
times and a screenshot)
scripts/collect_bench.py(a copy with the missingstand-in until the branch's fix is pushed), the asm walk against main's two in interleaved rounds, expecting the asm
walk at least as fast as the vector walk; ranges that overlap are no difference measured (by hand)
python3 -I -B tests/test_asm_collect.py --cxx g++and--cxx clang++, expecting PASS (by hand)Measurements
Verdict: in play, no gain and no loss shown in the walk's own time. In the Market at an 800x600 desktop (the
output pinned at 2560x1440), one runtime, the capture's scan (the walk, once a submit, about 6 a frame over about 1,050
ranges) took 0.076 ms a scan with the switch unset and
=0alike in steady play (means +2.5 %, p = 0.72) and 0.083against 0.080 in the load before it (+2.8 %, p = 0.26). The render thread's figures lean against the kernel (descriptors
+7.8 %, p = 0.016; draw +5.3 %, p = 0.051), which the walk's own time does not explain; not claimed either way. The
walk's other call, the collection of every GPU-visible range, is inside memory sync, which is level (+1.3 %, p = 0.62)
(In play, below).
The micro-benchmark: no gain on the shapes measured from the game's submits: against main's vector walk the asm walk is
as fast on the capture shape in the AVX2 build (1.00×) and slower in the SSE2 build (0.93×), slower on the collection of
every GPU-visible range (0.83-0.90×) and on short ranges (0.73-0.80×); it is faster only with many marks set (1.14-1.15×
with every mark of a 256 MB range set, 1.18-1.38× with half of them). A micro-benchmark on authored input. The Market's
scans are its capture shape (about 1,050 ranges over about 2,840 MB and 213 marks a scan, against its 2,728 ranges, 1,765
MB and 165 marks) in the AVX2 build, the 1.00× row, and play agrees with it. What this PR is then worth: parity on the
walk that runs every submit, a gain only on heavily written ranges.
The switch:
HOS_COLLECT_ASM, on by default as this PR proposes; level with=0in the capture's scan in play, with therender thread's figures leaning against it (below).
=0gives back main's vector walk for the A/B.Baseline: main's vector walk (
HOS_COLLECT_VECTOR, the default since #936), cut from this PR'shost/rx_backend.cand run withHOS_COLLECT_ASM=0(main's code), as #1947's headcffa2f46and main have it.The micro-benchmark:
scripts/collect_bench.pyof this branch, which does not build at this head (its stand-ins lackhos_xrealloc, whichcollect_innow calls); the figures come from a copy of it with the missinghos_xreallocstand-inadded (realloc, aborting on NULL), its timing unchanged: a script on this PC, not in this PR (
asm_collect_bench.py,PrTrials'
trials/tools/bench). Run alone in the PC's job queue on 2026-11-22: 3 rounds of 301 repetitions per shape,i7-9700, llvm-mingw clang 23.1.5
-O2, arms SSE2 (-mno-avx2) and AVX2 (-mavx2), the mark array the real one's size(1 << 27 marks). Each repetition sets the shape's marks and times one walk; the walks take turns at going first; their
pages were compared at every repetition and were equal. Each cell: the median of the three rounds' medians,
[lowest–highest round median], in µs per walk. Change: before ÷ after (above 1: this PR faster). Main's older word walk,
for scale: 139 µs (SSE2) and 137 µs (AVX2) on the capture shape.
4545ceca4545cecaIn play (held job
fb-g2130, 11:21 to 12:03 UTC on 2026-11-23, one runtime of #2129's67603ce7on #2128'sbb9161d5and #1947's
cffa2f46, with this head4545cecamerged in; #2128's and #2129's switches at their defaults on both sides):the Market, where the capture's scan runs every submit (6 a frame) over about 1,050 ranges. Each run was 60 s, a warm-up a
side, then
HOS_COLLECT_ASM=0(the vector walk) and unset (lsasm_collect_marks) interleaved (off on on off, fourtimes). Every run loaded its programs at create (
RX_VK_LOAD=create), the output pinned at 2560x1440 on a desktop at800x600 at 30 Hz (the PC's only screen then); no run was held, no load marker or strike file appeared, and no run's log
has a commit-limit line, a FATAL or a Vulkan error. Reach: the walk's own line, logged once at the first collection,
names
mem: the write marks' walk: lsasm_collect_marks: 256 marks a test, else 32, in 32-byte loads (AVX2)on the unsetside and the vector walk on the
=0side: the AVX2 build. The scans a frame are the capture's, one a submit: its600-submit line (
vk: capture: a submit ... scan S over N ranges) times them, about 5.8 submits a frame here. Thefigures are those lines in the measured stretch (the last 40 lines a run, about 58 s of play).
HOS_COLLECT_ASM=0(vector walk), 8 runslsasm_collect_marks), 8 runsa frame's replay time (ms): submitBefore the measured stretch (the title, the load into the Market and its first seconds: the 600-submit lines before
those 40 with 5 or more pages copied, 77 to 109 a run, about 1,420 ranges a scan), the scan took 0.083 against 0.080 ms
(+2.8 %, p = 0.26), 7.00 against 6.94 µs per 100 ranges (+2.3 %, p = 0.56): not shown either way.
What a frame is: the scans are about 0.45 ms a frame of the submitting threads' time (the capture runs at submit, on the
game's threads). Twice the standard error of the difference of the two sides' means is 0.0089 ms a scan (11 %) in steady
play and 0.0038 ms (4.7 %) in the load window, so a loss of the micro-benchmark's collection or short-range size (0.83× and 0.73×: a fifth to a
third more time) on these scans would have shown in the load window and about at the margin in steady play; it did not.
The walk's other call, the collection of every GPU-visible range (the micro-benchmark's collection shape), runs inside
memory sync (
memory_collect_writteninsubmit_memory_work), which is level (+1.3 %, p = 0.62). Calm game time's 2 SE is 0.21 ms(1.7 %). The render thread's descriptors (+7.8 %, p = 0.016) and draw (+5.3 %, p = 0.051) lean against the kernel, as do
main_blocked and the tail; those figures are timed apart from the scan and from memory sync, and a change of the scan's
size (a few µs a scan) cannot make the 0.13 ms a frame of descriptors' difference; one figure of 14 at p 0.016 is not claimed. One run of #1947's kit
cffa2f46(no switch, the vector walk) is a reference only: another build.Risks
What could break: a mark the walk misses would leave a written page uncaptured (stale data on the GPU) until the next
write to it; the test runs the three walks against a plain walk on random marks and ranges between faulting pages,
single-steps the kernel to publish a mark right after each take, and runs a writer thread that must lose no mark. Turn
it off with
HOS_COLLECT_ASM=0. Host files: the runtime is rebuilt; nothing changes for players' saves, options orcaches.
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.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.@Beanstalk This walks the write marks before each submit with a hand-written x86-64 kernel (host), busy scenes most; HOS_COLLECT_ASM=0 turns it off. It sits on #1947 (
cffa2f46, the asm kit), so its diff against its merge base includes that PR's commits; the farm cannot take it as the comparison, so please compare with main. Please start from the kokiri-start save and take the picture right after it loads and settles, before any input, with a main-against-itself picture on the same PC at that point; then, for frame times only, play Kokiri Forest and Hyrule Field, two minutes each; at least two starts a side, interleaved, the PR against main on the same PC, on the PR's side unset (the default, on) and with HOS_COLLECT_ASM=0 (off), on Windows with a Ryzen 9 7900X, Windows with a Core i9-12900K, Linux with a Ryzen 5 5600X and the Steam Deck. The same picture as main within main's own difference from itself; with it unset, frame time p99 equal to main's or better, within the run-to-run spread, and equal to HOS_COLLECT_ASM=0 or better. The log must have no FATAL, device lost or validation error line. Unset, the log must have the line naming lsasm_collect_marks; with HOS_COLLECT_ASM=0 it must not. No stale textures.@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.Description corrected:
scripts/collect_bench.pydoes not build at this head since the rebase onto the kit (its stand-ins lackhos_xrealloc); it has not run here. The fix follows with the next push; the micro-benchmark in Still to test will use a copy with the stand-in until then.Measurements: the micro-benchmark (trials script, run alone on this PC 2026-11-22, 3 rounds, SSE2 and AVX2): figures and Verdict, from a copy of scripts/collect_bench.py with the missing hos_xrealloc stand-in
@Beanstalk One correction to my request 69741 above, for its picture point: please take each picture 5 s (300 frames at 60 fps) after the first frame whose picture is not a single uniform colour, with no input in between, and take the main-against-itself picture from the same point. A picture taken during the load's fade-in from black is not comparable: run 70269 on #2156 caught the fade in 3 of 4 starts.
@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.@Beanstalk This replaces my request 69741 on this head and the corrections under it, in a form the farm can run: it cannot time a picture from the first frame that is not a uniform colour (its answer on #2131), so each picture here follows a fixed wait after the map event. This walks the write marks before each submit with a hand-written x86-64 kernel (host), busy scenes most; HOS_COLLECT_ASM=0 turns it off. It sits on #1947 (
cffa2f46, the asm kit), so its diff against its merge base includes that PR's commits; the farm cannot take it as the comparison, so please compare with main. Please start from the kokiri-start save and take the picture 10 s (600 frames at 60 fps) after the map event that ends the load, a fixed wait with no input in between (the farm cannot time it from the first frame that is not a uniform colour), with a main-against-itself picture on the same PC from the same point; then, for frame times only, play Kokiri Forest and Hyrule Field, two minutes each; at least two starts a side, interleaved, the PR against main on the same PC, on the PR's side unset (the default, on) and with HOS_COLLECT_ASM=0 (off), on Windows with a Ryzen 9 7900X, Windows with a Core i9-12900K, Linux with a Ryzen 5 5600X and the Steam Deck. The same picture as main within main's own difference from itself; with it unset, frame time p99 equal to main's or better, within the run-to-run spread, and equal to HOS_COLLECT_ASM=0 or better. The log must have no FATAL, device lost or validation error line. Unset, the log must have the line naming lsasm_collect_marks; with HOS_COLLECT_ASM=0 it must not. No stale textures.@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.