shadow: use the existing AVX2 compare for the refresh and copy-back runs #145

Merged
lido772 merged 2 commits from upstream-pr/145 into main 2026-11-28 19:39:39 +00:00
lido772 commented 2026-11-27 18:51:29 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by lido772 on 2026-11-27T18:51:29Z.


Two hot call sites compared whole pages with the scalar dword_runs(.., 0) although
word_runs() is exactly that call with an AVX2 implementation when it can use one, and a
fallback to the same scalar call when it cannot (unaligned, different lengths, no AVX2).
Its only other user was send_changed, behind words_under().

  • Shadow::take: the compare a refresh makes for a host copy, dword_runs(at, held, title, 0).
  • put_changed: the runs written back when shaders wrote into a copy, same call.

Measured (release build with diagnostics, same scene, same switch set)

The submitting thread's refreshes figure, normalised per span because the scene's span
count moves between samples:

before (scalar) after (AVX2)
sample 1 25.4 ms / 9,725 spans 22.7 ms / 9,436 spans
sample 2 27.0 ms / 10,276 spans 23.1 ms / 9,620 spans
per span 2.61-2.63 us 2.40-2.41 us
average run 43.8 KB 43.4 KB

-8.5 % per span, about 2.4 ms a frame off the submitting thread. The unchanged average
run size is the check that these are the same runs and only the compare got wider;
VICTORY_REFRESH_SPLIT=1 reports the same MB copied before and after (38-47 MB, 890-1077
runs a frame), as it must.

Why it is safe

word_runs is not new logic: it already existed for send_changed, and
shadow::merge_tests::word_runs_are_the_dword_runs asserts over random rounds that it
reports exactly what dword_runs(.., 0) does. Both tests pass on this branch. The fallback
is the identical scalar call, so the only thing that changes is the instruction used, never
the runs reported.

A third site (~3981, in the landing path) already uses word_runs behind
host::words_under() and was deliberately left alone: that guard is load-bearing, because
it pairs with the goes_as_words filter, so relaxing it would change which runs are
reported rather than just how fast they are found.

How it was found

Timing the refresh path showed ~71.6 MB of title memory compared per frame to find the
~47 MB that actually changed, with only ~32 % of sampled rewritten pages differing at all.
That is a comparison-bound path, not a copy-bound one - which is what made an unused AVX2
compare worth looking for.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5531638124 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/145). Originally posted by [lido772](https://github.com/lido772) on 2026-11-27T18:51:29Z. --- Two hot call sites compared whole pages with the scalar `dword_runs(.., 0)` although `word_runs()` is exactly that call with an AVX2 implementation when it can use one, and a fallback to the same scalar call when it cannot (unaligned, different lengths, no AVX2). Its only other user was `send_changed`, behind `words_under()`. - `Shadow::take`: the compare a refresh makes for a host copy, `dword_runs(at, held, title, 0)`. - `put_changed`: the runs written back when shaders wrote into a copy, same call. ## Measured (release build with diagnostics, same scene, same switch set) The submitting thread's `refreshes` figure, normalised per span because the scene's span count moves between samples: | | before (scalar) | after (AVX2) | |---|---|---| | sample 1 | 25.4 ms / 9,725 spans | 22.7 ms / 9,436 spans | | sample 2 | 27.0 ms / 10,276 spans | 23.1 ms / 9,620 spans | | **per span** | **2.61-2.63 us** | **2.40-2.41 us** | | average run | 43.8 KB | 43.4 KB | **-8.5 % per span, about 2.4 ms a frame off the submitting thread.** The unchanged average run size is the check that these are the same runs and only the compare got wider; `VICTORY_REFRESH_SPLIT=1` reports the same MB copied before and after (38-47 MB, 890-1077 runs a frame), as it must. ## Why it is safe `word_runs` is not new logic: it already existed for `send_changed`, and `shadow::merge_tests::word_runs_are_the_dword_runs` asserts over random rounds that it reports exactly what `dword_runs(.., 0)` does. Both tests pass on this branch. The fallback is the identical scalar call, so the only thing that changes is the instruction used, never the runs reported. A third site (`~3981`, in the landing path) already uses `word_runs` behind `host::words_under()` and was deliberately left alone: that guard is load-bearing, because it pairs with the `goes_as_words` filter, so relaxing it would change which runs are reported rather than just how fast they are found. ## How it was found Timing the refresh path showed ~71.6 MB of title memory compared per frame to find the ~47 MB that actually changed, with only ~32 % of sampled rewritten pages differing at all. That is a comparison-bound path, not a copy-bound one - which is what made an unused AVX2 compare worth looking for.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
trustno1-coder/VIctory-NVIDIA!145
No description provided.