WIP: perf(vk): a mirror sync's walk over the page write generations in x86-64 assembly #2134
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!2134
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/asm-mirror-walk"
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
A mirror sync (
mirror_syncingpu/vk/vk_memory.cpp) finds the pages of a pool written since its last sync bywalking the page write generations: every page of a region written since then was tested one by one.
This adds
gpu/vk/asm/mirror.Son the asm kit:lsasm_mirror_next, the first page of [page, end) written after thepool's sync. A region whose generation is the sync or older is passed over whole; inside a newer one it compares 2
(SSE2) or 4 (AVX2) page generations at a time, each the larger of its chunk's entry and its region's, as
page_genreads them; unsigned 64-bit compares, and no entry read past a region's or the pool's end.
next_written_pagecallsit on x86-64 (the same walk in C elsewhere), and
mirror_syncgoes from one written page to the next instead oftesting every page of a written region, its per-page decisions as they were.
gen_chunksholds plain pointers (madeonce and kept for the process, as before) so the kernel reads the table as it is. The result expected: the same pages
synced, the walk faster in pools with few written pages.
No switch: the same pages in the same order.
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.Read first:
gpu/vk/asm/mirror.S, thennext_written_page,page_genandmirror_syncingpu/vk/vk_memory.cpp.Testing
Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23 (
CC/CXX,RX_CPMset); headd18f85d8on #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-mirror):test_asm_mirror.py,test_wb_learn.pypass (2 of 2).--changed cffa2f46(build jobpr-tb-AsmCut5-build, logt-AsmCut5-chg-asm-mirror): 78 checks, 70 pass, 8 fail, each of which fails on #1947's head@
cffa2f46too (a worktree atcffa2f46in the queue's jobs, logst-AsmCut5-base-kit-changedandt-AsmCut5-base-kit-more):test_capture_read_once.py,test_frame_overlap.py,test_guest_imported.py,test_linux_pressure.py,test_pending_guest_import.py,test_replay_write_order.py,test_sparse_mirror.py,test_texel_buffer_ownership.py.It has run in the game on this PC once: the in-play A/B in Measurements (held job
fb-g2134, the Market, 18 runs), withno FATAL, device lost or Vulkan error in any run.
Still to test:
uncapped (
HOS_FPS_MAX=240), on #1947's head and on this PR, each warmed a minute, then at least three interleavedthree-minute runs of each (AB AB AB), and one run of each with
HOS_PROFILE=1(python -I scripts/perf.py reporton its folder: the share of
mirror_syncandnext_written_pageon their threads), expecting the same picture, noFATAL, that share lower and fps and frame times of this PR at least the base's; a difference beyond the runs' spread
is a gain or a loss, one within it is no difference measured (by hand)
this PR, warmed and interleaved, expecting the same picture and frame times within the runs' spread or better on
this PR (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri Forest three minutes,
#1947's head against this PR, warmed and interleaved, frame times and a screenshot)
python3 -I -B tests/test_asm_mirror.py --cxx g++and--cxx clang++, expecting PASS (by hand)Measurements
Verdict: a gain in pools with few written pages, a loss in pools with many: over a 1 GB pool with 300 pages
written the walk takes 4.8× (SSE2) and 9.7× (AVX2) less time at random and 3.5× and 5.7× less in runs of 8; with none
written both pass over whole regions in under a µs (at the timer's 0.1 µs resolution). Slower with every page written
(0.17-0.18×: 0.72 ms against 4.0-4.3 ms, a kernel call per page) and with whole regions stamped through
region_full(0.33-0.35×); on 1,024 small pools of 16 pages 0.93× (SSE2) and 1.13× (AVX2). A micro-benchmark on authored input.
In play (the Market, this head against its parent, the asm kit
cffa2f46, 8 runs each, below): no gain shown in thememory sync, the stage the walk is part of (-2.3 % in the frame trace, p = 0.53; -3.8 % in the replay's own line,
p = 0.33). The Market is the sparse case: about 0.46 % of the mirrored memory is synced a frame, about 90 pages a
submit. Of the frame figures only main_blocked_ms differs at p under 0.05 (-3.8 %, p = 0.012, lower with this head),
one of 14 figures tested, and not in the walk's stage, so it is not claimed.
Baseline: the C this PR replaces, as #1947's head
cffa2f46has it (the asm kit on main9dd41326, which this PR depends on), not main itself.The micro-benchmark:
mirror_sync's walk ascffa2f46has it (cut from its loop up to its first per-page decision) against this PR's loop overnext_written_page(lsasm_mirror_next), with the C path other CPUs run beside them; the tables at the production sizes, authored generations, synced at 100, the pages found collected. A script on this PC, not in this PR (asm_mirror_bench.py, PrTrials'trials/tools/bench), run alone in the PC's job queue on 2026-11-22: 3 rounds of 201 repetitions per case and arm, i7-9700, llvm-mingw clang 23.1.5-O2 -ffp-contract=off, arms SSE2 (-march=x86-64) and AVX2 (-march=x86-64-v3, what the release builds the back end for). The arms take turns at going first; their outputs 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).cffa2f46d18f85d8d18f85d8In play (held job
fb-g2134, 00:56 to 01:41 UTC on 2026-11-23): the Market, chosen because its memory sync is thelongest of the scenes on disk (0.61-0.67 ms a frame against 0.54-0.58 in Kokiri Forest). Each run was 60 s. The two builds
are this head
d18f85d8(rt-amw-d18f85d8) and its parent, the asm kitcffa2f46(rt-kit-cffa2f46): a warm-up each,then kit and head interleaved (kit head head kit, four times), every run loading at create, at the game's 2560x1440
output on a 2560x1440 desktop at 60 Hz. No run was left out, and no load marker or strike file appeared. Reach: the
mirror syncs' 600-submit line counted dirty pages in every one of the head's warm-up lines, each a page
next_written_pagereturned, which islsasm_mirror_nextin this head. No log line times the walk alone, so the memorysync (the frame trace's and the replay line's) is the finest figure that holds it.
Which case of the micro-benchmark play is (the logs' own counts, the same on both sides): about 985 MB in 198 mirrored
pools, of which 4.5 MB a frame is synced in about 890 copies (0.46 % of the mirrored memory a frame, about 5 KB a copy).
The 600-submit line finds about 53,500 dirty pages in the measured stretch (about 90 a submit, 6 submits a frame) and
about 27,500 before it (the title and the load into the Market). That is few written pages in large pools: the sparse
cases of the micro-benchmark (4.8-9.7×), not every page written (0.17×).
cffa2f46(C walk), 8 runsd18f85d8, 8 runsa frame's replay time (ms): submit / drawThe tail (p99, frames of 50 ms or more) leans to this head because the burst runs fell more on the kit's side this
sitting: runs with bursts were 3 kit and 1 head, and runs with 20 or more frames of 50 ms were 4 kit and 2 head. It is
not shown (p 0.10 and 0.12). This is this head against its parent: two builds,
not one build's switch (this PR has none). The play profile the back end is built with was recorded before this
change, so functions this change edits can build without their profile in the head's build (a compile with clang's
profile warnings on showed that for #1994's file; not checked for this PR's). A difference between the builds can come
from that as well as from the kernel. What is shown in play: nothing in the walk's stage, and nothing claimed in frame
time.
Risks
What could break: a written page the walk skips would leave stale data in a mirror the GPU reads (wrong textures or
geometry); the test runs
next_written_page,page_genandmirror_sync's loop cut from the source against main'swalk over the production table sizes, between pages that fault, with syncs around the 32-bit limit, the sign bit and
UINT64_MAX. A revert is enough. Renderer files: the runtime is rebuilt; nothing changes for players' saves, options or
caches.
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.@Beanstalk This syncs the mirrors of written GPU pages with a hand-written x86-64 kernel (gpu/vk), in every scene. 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. No switch: a revert gives back main's code. 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 Windows with an RTX 4070, Windows with an Arc B580, Linux with an RX 6600 (RADV) and the Steam Deck. The same picture as main within main's own difference from itself; frame time p99 equal to main's or better, within the run-to-run spread. The log must have no FATAL, device lost or validation error line. No stale buffers.@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.Measurements: the micro-benchmark (trials script, run alone on this PC 2026-11-22, 3 rounds, SSE2 and AVX2): figures and Verdict
@Beanstalk One correction to my request 69763 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 69763 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 syncs the mirrors of written GPU pages with a hand-written x86-64 kernel (gpu/vk), in every scene. 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. No switch: a revert gives back main's code. 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 Windows with an RTX 4070, Windows with an Arc B580, Linux with an RX 6600 (RADV) and the Steam Deck. The same picture as main within main's own difference from itself; frame time p99 equal to main's or better, within the run-to-run spread. The log must have no FATAL, device lost or validation error line. No stale buffers.@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.