WIP: perf(host): the write marks' collection reads the marks in vector loads from any page, on every platform #936

Draft
mooki wants to merge 1 commit from mooki/Longshot:perf/page-scan-all into main
Member

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 takes
each 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=0 gives the word walk on the same build.

  • host: marks_set64() loads 64 marks in inline asm: SSE2's movdqu (four 16-byte loads) on x86-64, AVX2's vmovdqu (two
    32-byte loads) in a build for AVX2 (__AVX2__: build.py's -march=x86-64-v3 or native on a CPU that has it, as
    copy_nt in gpu/vk/vk_memory.cpp chooses), NEON's ld1 on arm64. One test of the OR, then a 64-bit mask of the marks that
    are set (pmovmskb against zero; on arm64 cmtst, each byte's bit and three pairwise adds). marks_from() reads the window at
    any 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_in keeps a window's set marks and takes them one by one; the
    word walk is unchanged inside an else. Built with GCC or clang (the asm is theirs) for x86-64 and arm64; another compiler or
    CPU builds the word walk alone (HOS_MARKS_VECTOR).
    • Why inline asm: a memcpy or 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-byte
      loads; arm64's ld1 of .16b elements is sixteen single-copy atomic byte reads). They only choose which pages the walk
      reads again and clears, so they order nothing and lose no mark.
    • The switch is read once, at the first collection, and logged once: mem: the write marks' walk: 64 marks a test, in 32-byte loads (AVX2) (HOS_COLLECT_VECTOR=0: word loads) (or 16-byte loads (SSE2), (NEON)), or mem: the write marks' walk: word loads (HOS_COLLECT_VECTOR=0).
  • tests: tests/test_collect_wide.py compiles the walk twice in one program, as the word walk (HOS_MARKS_VECTOR 0) and as
    built, 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_VECTOR on and off (each with main's HOS_COLLECT_WIDE on and off, which
    only 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: scripts/collect_bench.py times both walks game-free on mark arrays shaped like the game's (an indication only).
  • docs: HOS_COLLECT_VECTOR's row and the benchmark's (docs/development.md); main's HOS_COLLECT_WIDE row now says it applies
    to 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 instructions
where 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 collected and vk: capture: a submit ... scan ... over N ranges, M MB; four runs in the order A B B A (A HOS_COLLECT_VECTOR=0, B the default), 8 lines a run, the median; each run's mem: the write marks' walk: line names its arm
How 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=0 and once without

Before, HOS_COLLECT_VECTOR=0: this PR @ cab0f820 After: this PR @ cab0f820
Windows, writes collected, ms a submit (median)
Windows, the capture's scan, ms a submit (median)
Linux, writes collected, ms a submit (median)
Linux, the capture's scan, ms a submit (median)

Game-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=1 for the AVX2 program); -O2; the real mark array (1 << 27
marks); 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, ×):

shape ranges, MB, marks arm64 (NEON) x86_64 Rosetta (SSE2) x86_64 Rosetta (AVX2)
capture (the submit-time capture's ranges, as measured in a busy scene on the macOS port) 2,728, 1,765 MB, 165 67.8 → 13.8 (4.9×) 60.8 → 16.9 (3.6×) 71.7 → 21.1 (3.4×)
collection (long ranges) 60, 6,000 MB, 400 23.5 → 23.0 (1.0×) 25.8 → 30.7 (0.8×) 37.7 → 44.4 (0.8×)
short (1-8 pages each, unaligned) 2,728, 58 MB, 165 30.8 → 9.3 (3.3×) 29.8 → 12.3 (2.4×) 32.0 → 11.9 (2.7×)
dense (every mark set) 1, 256 MB, 65,536 123.9 → 122.3 (1.0×) 119.2 → 110.3 (1.1×) 115.5 → 76.5 (1.5×)
half (half the marks set) 1, 256 MB, 32,768 229.8 → 48.3 (4.8×) 231.8 → 43.1 (5.4×) 194.3 → 34.5 (5.6×)

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's HOS_COLLECT_WIDE keeps its meaning for the word walk (with HOS_COLLECT_VECTOR=0 or where the vector walk is not built); the vector walk does not read it.

Flag What it is Before this PR After this PR Effect Helps / doesn't help / untested Recommendation
HOS_COLLECT_VECTOR A/B switch for the write marks' walk (collect_in, host/rx_backend.c), read once at the first collection (none: word loads) unset: vector loads from any page, 64 marks a test (SSE2, AVX2 or NEON); =0: the word walk, as before less replay-thread time a submit; the same pages, order and marks either way helps on macOS (memory sync −3.0 ms a frame against its byte walk, #852); game-free 3.4-4.9× on the capture's ranges and 0.8-1.0× on long ones against the word walk; untested in the game on Windows and Linux on, as it ships; =0 only for A/B

Testing

Tested: on macOS 26.7.1, Apple M4 Max, game-free, at this PR's head cab0f820 (on main 7bacf846):

  • tests/test_collect_wide.py on arm64 (NEON) and x86_64 under Rosetta, the SSE2 build and, with ROSETTA_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.
  • That it is #852's walk: preprocessed for macOS (arm64, x86_64) and for aarch64-linux-gnu, x86_64-linux-gnu,
    x86_64-w64-mingw32 and x86_64-pc-windows-msvc, the vector walk (its loads, marks_set64, marks_from, the window and
    the 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.
  • 14 of the vector walk's 24 mutants, a sample (each a one-line change: the step, the window's marks, the tail window, the
    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.
  • What Windows and Linux compile, without their SDKs (stand-in C library headers, clang 23.1.5's own intrinsic headers):
    the walk cut out of rx_backend.c for x86_64-linux-gnu, x86_64-w64-mingw32 and x86_64-pc-windows-msvc at
    -march=x86-64, x86-64-v2 and x86-64-v3, and aarch64-linux-gnu, with the runtime's flags and -Wall -Wextra -Werror:
    -fsyntax-only and objects exit 0 on all 10; the asm's instructions are movdqu (SSE2), vmovdqu (v3) and ld1 .16b.
    With HOS_MARKS_VECTOR 0 the word walk is main's token for token (one { } more) and instruction for instruction on all 10.
  • The whole game-free suite (python -I -B tests/run_all.py --jobs 6) on this Mac: 559 passed, 33 failed, 17 skipped (main
    7bacf846: 558, 34, 17). By name the one difference is test_collect_wide, which fails on main on macOS (its fixture's
    #define memcpy collides with the SDK's fortified memcpy) 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:

  • Windows (llvm-mingw) and Linux, any GPU: build this PR and main (7bacf846) the same way and run python -I -B tests/run_all.py on each, then start each to the title, expecting test_collect_wide to pass with the AVX2 and SSE2 walks, no other check to change status against main, and the line mem: 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)
  • Windows and Linux, any GPU: this PR's build in Kokiri Forest on the pristine save with default options, standing still for 5 minutes, four runs A B B A (A with HOS_COLLECT_VECTOR=0), expecting writes collected and the capture's scan lower in B and the same picture; fill in the Measurements table (by hand: the vk: a submit's memory work and vk: capture: a submit lines, 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 -march does not allow).

Turning it off: HOS_COLLECT_VECTOR=0, or revert the commit.

Compatibility: Nothing changes: no cache, save, longshot_options.ini key or vk ABI is touched.

Reviewer notes

  • One commit, cab0f820, on main 7bacf846; it merges with current main (fe3659d6) without conflict. Read collect_in and
    marks_from in host/rx_backend.c first, then marks_set64's three load forms, then the test's step counts.
  • With #852 (the macOS walk) it conflicts in host/rx_backend.c, tests/test_collect_wide.py and docs/development.md, and
    #852's tests/test_darwin_guest_memory.py finds 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=0 walk. A resolution is ready, checked
    on the merged tree (both tests, arm64 and x86_64).
  • A test of 256 marks at once while none is set (the word walk's widest step, in the same vector loads) was tried on this branch:
    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

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched).
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main.
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named).
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
  • Numbers from the same PC, scene and settings, at least 2 interleaved runs each, the median reported.
  • Switches documented (docs/development.md; the README for a menu row) and shipped at their recommended value.
  • A check under tests/ that reads changed source updated in this PR, not weakened.
  • Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • Docs updated.
**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 takes each 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=0` gives the word walk on the same build. - host: `marks_set64()` loads 64 marks in inline asm: SSE2's `movdqu` (four 16-byte loads) on x86-64, AVX2's `vmovdqu` (two 32-byte loads) in a build for AVX2 (`__AVX2__`: build.py's `-march=x86-64-v3` or `native` on a CPU that has it, as `copy_nt` in `gpu/vk/vk_memory.cpp` chooses), NEON's `ld1` on arm64. One test of the OR, then a 64-bit mask of the marks that are set (`pmovmskb` against zero; on arm64 `cmtst`, each byte's bit and three pairwise adds). `marks_from()` reads the window at any 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_in` keeps a window's set marks and takes them one by one; the word walk is unchanged inside an `else`. Built with GCC or clang (the asm is theirs) for x86-64 and arm64; another compiler or CPU builds the word walk alone (`HOS_MARKS_VECTOR`). - Why inline asm: a `memcpy` or 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-byte loads; arm64's `ld1` of `.16b` elements is sixteen single-copy atomic byte reads). They only choose which pages the walk reads again and clears, so they order nothing and lose no mark. - The switch is read once, at the first collection, and logged once: `mem: the write marks' walk: 64 marks a test, in 32-byte loads (AVX2) (HOS_COLLECT_VECTOR=0: word loads)` (or `16-byte loads (SSE2)`, `(NEON)`), or `mem: the write marks' walk: word loads (HOS_COLLECT_VECTOR=0)`. - tests: `tests/test_collect_wide.py` compiles the walk twice in one program, as the word walk (`HOS_MARKS_VECTOR 0`) and as built, 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_VECTOR` on and off (each with main's `HOS_COLLECT_WIDE` on and off, which only 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: `scripts/collect_bench.py` times both walks game-free on mark arrays shaped like the game's (an indication only). - docs: `HOS_COLLECT_VECTOR`'s row and the benchmark's (`docs/development.md`); main's `HOS_COLLECT_WIDE` row now says it applies to 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 instructions where 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 collected` and `vk: capture: a submit ... scan ... over N ranges, M MB`; four runs in the order A B B A (A `HOS_COLLECT_VECTOR=0`, B the default), 8 lines a run, the median; each run's `mem: the write marks' walk:` line names its arm How 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=0` and once without | | Before, HOS_COLLECT_VECTOR=0: this PR @ cab0f820 | After: this PR @ cab0f820 | |---|---|---| | Windows, writes collected, ms a submit (median) | | | | Windows, the capture's scan, ms a submit (median) | | | | Linux, writes collected, ms a submit (median) | | | | Linux, the capture's scan, ms a submit (median) | | | **Game-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=1` for the AVX2 program); -O2; the real mark array (1 << 27 marks); 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, ×): | shape | ranges, MB, marks | arm64 (NEON) | x86_64 Rosetta (SSE2) | x86_64 Rosetta (AVX2) | |---|---|---|---|---| | capture (the submit-time capture's ranges, as measured in a busy scene on the macOS port) | 2,728, 1,765 MB, 165 | 67.8 → 13.8 (4.9×) | 60.8 → 16.9 (3.6×) | 71.7 → 21.1 (3.4×) | | collection (long ranges) | 60, 6,000 MB, 400 | 23.5 → 23.0 (1.0×) | 25.8 → 30.7 (0.8×) | 37.7 → 44.4 (0.8×) | | short (1-8 pages each, unaligned) | 2,728, 58 MB, 165 | 30.8 → 9.3 (3.3×) | 29.8 → 12.3 (2.4×) | 32.0 → 11.9 (2.7×) | | dense (every mark set) | 1, 256 MB, 65,536 | 123.9 → 122.3 (1.0×) | 119.2 → 110.3 (1.1×) | 115.5 → 76.5 (1.5×) | | half (half the marks set) | 1, 256 MB, 32,768 | 229.8 → 48.3 (4.8×) | 231.8 → 43.1 (5.4×) | 194.3 → 34.5 (5.6×) | 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's `HOS_COLLECT_WIDE` keeps its meaning for the word walk (with `HOS_COLLECT_VECTOR=0` or where the vector walk is not built); the vector walk does not read it. | Flag | What it is | Before this PR | After this PR | Effect | Helps / doesn't help / untested | Recommendation | |---|---|---|---|---|---|---| | `HOS_COLLECT_VECTOR` | A/B switch for the write marks' walk (`collect_in`, `host/rx_backend.c`), read once at the first collection | (none: word loads) | unset: vector loads from any page, 64 marks a test (SSE2, AVX2 or NEON); `=0`: the word walk, as before | less replay-thread time a submit; the same pages, order and marks either way | helps on macOS (memory sync −3.0 ms a frame against its byte walk, #852); game-free 3.4-4.9× on the capture's ranges and 0.8-1.0× on long ones against the word walk; untested in the game on Windows and Linux | on, as it ships; `=0` only for A/B | ## Testing **Tested:** on macOS 26.7.1, Apple M4 Max, game-free, at this PR's head `cab0f820` (on main `7bacf846`): - `tests/test_collect_wide.py` on arm64 (NEON) and x86_64 under Rosetta, the SSE2 build and, with `ROSETTA_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. - That it is #852's walk: preprocessed for macOS (arm64, x86_64) and for `aarch64-linux-gnu`, `x86_64-linux-gnu`, `x86_64-w64-mingw32` and `x86_64-pc-windows-msvc`, the vector walk (its loads, `marks_set64`, `marks_from`, the window and the 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. - 14 of the vector walk's 24 mutants, a sample (each a one-line change: the step, the window's marks, the tail window, the 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. - What Windows and Linux compile, without their SDKs (stand-in C library headers, clang 23.1.5's own intrinsic headers): the walk cut out of `rx_backend.c` for `x86_64-linux-gnu`, `x86_64-w64-mingw32` and `x86_64-pc-windows-msvc` at `-march=x86-64`, `x86-64-v2` and `x86-64-v3`, and `aarch64-linux-gnu`, with the runtime's flags and `-Wall -Wextra -Werror`: `-fsyntax-only` and objects exit 0 on all 10; the asm's instructions are `movdqu` (SSE2), `vmovdqu` (v3) and `ld1 .16b`. With `HOS_MARKS_VECTOR 0` the word walk is main's token for token (one `{ }` more) and instruction for instruction on all 10. - The whole game-free suite (`python -I -B tests/run_all.py --jobs 6`) on this Mac: 559 passed, 33 failed, 17 skipped (main `7bacf846`: 558, 34, 17). By name the one difference is `test_collect_wide`, which fails on main on macOS (its fixture's `#define memcpy` collides with the SDK's fortified `memcpy`) 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:** - Windows (llvm-mingw) and Linux, any GPU: build this PR and main (`7bacf846`) the same way and run `python -I -B tests/run_all.py` on each, then start each to the title, expecting `test_collect_wide` to pass with the AVX2 and SSE2 walks, no other check to change status against main, and the line `mem: 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) - Windows and Linux, any GPU: this PR's build in Kokiri Forest on the pristine save with default options, standing still for 5 minutes, four runs A B B A (A with `HOS_COLLECT_VECTOR=0`), expecting `writes collected` and the capture's `scan` lower in B and the same picture; fill in the Measurements table (by hand: the `vk: a submit's memory work` and `vk: capture: a submit` lines, 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 `-march` does not allow). **Turning it off:** `HOS_COLLECT_VECTOR=0`, or revert the commit. **Compatibility:** Nothing changes: no cache, save, `longshot_options.ini` key or vk ABI is touched. ## Reviewer notes - One commit, `cab0f820`, on main `7bacf846`; it merges with current main (`fe3659d6`) without conflict. Read `collect_in` and `marks_from` in `host/rx_backend.c` first, then `marks_set64`'s three load forms, then the test's step counts. - With #852 (the macOS walk) it conflicts in `host/rx_backend.c`, `tests/test_collect_wide.py` and `docs/development.md`, and #852's `tests/test_darwin_guest_memory.py` finds 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=0` walk. A resolution is ready, checked on the merged tree (both tests, arm64 and x86_64). - A test of 256 marks at once while none is set (the word walk's widest step, in the same vector loads) was tried on this branch: 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 - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [ ] Rebased on current `main`. - [ ] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. - [ ] Numbers from the same PC, scene and settings, at least 2 interleaved runs each, the median reported. - [x] Switches documented (docs/development.md; the README for a menu row) and shipped at their recommended value. - [x] A check under `tests/` that reads changed source updated in this PR, not weakened. - [ ] Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths. - [x] Docs updated.
perf(host): the write marks' collection reads the marks in vector loads from any page, on every platform
Some checks failed
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
CI / Conventional Commit title (pull_request) Failing after 3s
CI / Workflow lint (actionlint) (pull_request) Successful in 5s
Website / Website on Cloudflare (pull_request_target) Has been skipped
Guide screenshots / Guide images and local UI source review (pull_request) Failing after 5s
Third-party parts / As their notes say (pull_request) Successful in 8s
PR description / Description follows the template (pull_request_target) Successful in 4s
cab0f82099
collect_in (host/rx_backend.c) reads the write marks 64 to a test, from any page, in inline-asm vector loads: SSE2's
16-byte loads on x86-64, AVX2's 32-byte ones in a build for AVX2 (x86-64-v3 or native), NEON's on arm64. Then it
takes each mark read set in turn with the same relaxed load and acquire exchange as before. The word loads tested only
from a page aligned to their width, so each range's head and tail and the pages around each set mark went 32, 8 and
1 at a time. The pages found, their order, the marks cleared and the memory ordering are the word walk's. Built with
GCC or clang for x86-64 and arm64; another compiler or CPU builds the word walk alone. HOS_COLLECT_VECTOR=0 gives the
word walk on the same build (read once, logged once: "mem: the write marks' walk:"). The walk is the macOS port's
(#852), for every platform, with an AVX2 form of its loads.

On the macOS port this walk took memory sync from 3.25 to 0.25 ms a frame against its byte walk (same build, switch
on and off). Against the word walk, game-free on mark arrays shaped like the game's (an indication only): about 3 to 5
times faster on the submit-time capture's many short ranges; on long clear ranges, where the word walk already tests 256
marks at once, no faster.

- tests/test_collect_wide.py: both walks in one program (the word walk, HOS_MARKS_VECTOR 0, and as built), checked
  against a plain walk and each other over 1,600 rounds of random marks and ranges, with HOS_COLLECT_WIDE and
  HOS_COLLECT_VECTOR on and off; step counts (64 vector tests for 4096 marks, 3 for 130 from page 5, 64 for 4096 set
  marks), the log line, and which loads are built. On x86-64 the SSE2 walk (-mno-avx2) and the AVX2 one (-mavx2, run
  where the CPU has it).
- scripts/collect_bench.py: both walks timed game-free on mark arrays shaped like the game's; an indication only.
- docs/development.md: HOS_COLLECT_VECTOR's row, the bench's, and HOS_COLLECT_WIDE's row scoped to the word walk.

Assisted-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
dyst left a comment

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 - w is 1..63 (0..62 near page 0) and p1 - p is 1..63. set/at reset 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:

  1. The new switch doesn't follow the on/off rule. e && e[0] == '0' (rx_backend.c:181) means HOS_COLLECT_VECTOR=off, false or no leaves the vector walk on. docs/development.md (~198) says off is 0/false/off/no through env_value_off, and host/hos_thread.c already includes gpu/vk/compat/longshot_env.h for exactly this. HOS_COLLECT_WIDE sets a bad precedent here, but a new switch should follow the rule. The test's fixture would need the header too.
  2. The long clear ranges are the one shape where this is slower, and that shape is the full collection ("writes collected"). Your bench has it at 0.8x, AVX2 37.7 to 44.4 µs. Most PCs will run the AVX2 form, because nx2/build.py builds v3 or native wherever the CPU has it. There it takes four window tests for main's one 256-mark skip. You've already tried a 256 test. Since neither form has run on a PC yet, I'd fold it in here rather than leave it for a follow-up. Then the A/B can't trade a slower collection for a faster capture scan.
  3. Before this leaves WIP, the most useful run is test_collect_wide with -mavx2 on 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

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 - w` is 1..63 (0..62 near page 0) and `p1 - p` is 1..63. `set`/`at` reset 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: 1. **The new switch doesn't follow the on/off rule.** `e && e[0] == '0'` (rx_backend.c:181) means `HOS_COLLECT_VECTOR=off`, `false` or `no` leaves the vector walk on. docs/development.md (~198) says off is `0`/`false`/`off`/`no` through `env_value_off`, and `host/hos_thread.c` already includes `gpu/vk/compat/longshot_env.h` for exactly this. `HOS_COLLECT_WIDE` sets a bad precedent here, but a new switch should follow the rule. The test's fixture would need the header too. 2. **The long clear ranges are the one shape where this is slower, and that shape is the full collection ("writes collected").** Your bench has it at 0.8x, AVX2 37.7 to 44.4 µs. Most PCs will run the AVX2 form, because nx2/build.py builds v3 or native wherever the CPU has it. There it takes four window tests for main's one 256-mark skip. You've already tried a 256 test. Since neither form has run on a PC yet, I'd fold it in here rather than leave it for a follow-up. Then the A/B can't trade a slower collection for a faster capture scan. 3. Before this leaves WIP, the most useful run is `test_collect_wide` with `-mavx2` on 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](https://claude.com/claude-code)
@ -89,0 +178,4 @@
if (vector < 0) {
const char *e = getenv("HOS_COLLECT_VECTOR");
int unread = -1;
vector = !(e && e[0] == '0');
Member

Only a leading 0 turns this off, so HOS_COLLECT_VECTOR=off, false or no keeps the vector walk on. The documented rule (docs/development.md, the on/off paragraph) is env_value_off from gpu/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.

Only a leading `0` turns this off, so `HOS_COLLECT_VECTOR=off`, `false` or `no` keeps the vector walk on. The documented rule (docs/development.md, the on/off paragraph) is `env_value_off` from `gpu/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;
Member

On a long clear range this takes 64 pages per test, where main's marks_clear_256 (with HOS_COLLECT_WIDE on) 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, while set == 0 and p + 256 <= p1, would keep that shape at least as fast as main, and the window logic wouldn't change.

On a long clear range this takes 64 pages per test, where main's `marks_clear_256` (with `HOS_COLLECT_WIDE` on) 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, while `set == 0` and `p + 256 <= p1`, would keep that shape at least as fast as main, and the window logic wouldn't change.
First-time contributor

Independent check for T-G2L4 (the card is not approved; no claim). Draft PR head cab0f820, main 7bacf846. Linux, NVIDIA RTX 4090 24 GB.

Suite, source, game-free

  • main 7bacf846: 530 passed, 14 failed, 65 skipped.
  • PR cab0f820: 530 passed, 14 failed, 65 skipped.
  • The same 14 test files fail on both, so this PR changes no other check's status:
test_appimage_player_build.py
test_crash_dialog.py
test_env_on.py
test_lift_cleanup_error.py
test_linux_preflight.py
test_make_game_cache.py
test_missing_nup.py
test_mods_size_table.py
test_options_budget_once.py
test_pipeline_tour.py
test_readback_late.py
test_vk_occlusion_queries.py
test_vk_pgo.py
test_walk_key.py
  • test_collect_wide itself:
    • main: ok: the same pages 256 marks a step as 32
    • PR: vector walk: built, 32-byte loads (AVX2) three times, then
      ok: 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 neither 7bacf846 nor 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

Independent check for T-G2L4 (the card is not approved; no claim). Draft PR head `cab0f820`, main `7bacf846`. Linux, NVIDIA RTX 4090 24 GB. **Suite, source, game-free** - main `7bacf846`: 530 passed, 14 failed, 65 skipped. - PR `cab0f820`: 530 passed, 14 failed, 65 skipped. - The same 14 test files fail on both, so this PR changes no other check's status: ``` test_appimage_player_build.py test_crash_dialog.py test_env_on.py test_lift_cleanup_error.py test_linux_preflight.py test_make_game_cache.py test_missing_nup.py test_mods_size_table.py test_options_budget_once.py test_pipeline_tour.py test_readback_late.py test_vk_occlusion_queries.py test_vk_pgo.py test_walk_key.py ``` - `test_collect_wide` itself: - main: `ok: the same pages 256 marks a step as 32` - PR: `vector walk: built, 32-byte loads (AVX2)` three times, then `ok: 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 neither `7bacf846` nor 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
Some checks failed
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
CI / Conventional Commit title (pull_request) Failing after 3s
CI / Workflow lint (actionlint) (pull_request) Successful in 5s
Website / Website on Cloudflare (pull_request_target) Has been skipped
Guide screenshots / Guide images and local UI source review (pull_request) Failing after 5s
Third-party parts / As their notes say (pull_request) Successful in 8s
PR description / Description follows the template (pull_request_target) Successful in 4s
This pull request is marked as a work in progress.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'perf/page-scan-all:mooki-perf/page-scan-all'
git switch 'mooki-perf/page-scan-all'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!936
No description provided.