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

Manually merged
github merged 2 commits from github-bot/gh-145 into main 2026-11-28 19:42:36 +00:00 AGit
Owner

@⁠lido772 on GitHub · 2026-11-27 18:51 UTC · original
lido772:avx2-refresh-compare → main · +2 −2 · 1 files · 2 commits · merged 2026-11-28 19:39 UTC

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.

**@⁠lido772** on GitHub · 2026-11-27 18:51 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/145) `lido772:avx2-refresh-compare` → `main` · +2 −2 · 1 files · 2 commits · merged 2026-11-28 19:39 UTC 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.
Shadow::take() -- the copy a refresh makes for a host copy, measured this session at
71.6 MB compared and 47.2 MB copied a frame over 1077 runs -- was calling the scalar
dword_runs(at, held, title, 0) even though word_runs() dispatches to word_runs_avx2()
for exactly this case (gap 0, word-aligned, equal lengths) and falls back to that same
scalar call when it cannot. Its only other user was send_changed, behind words_under().
Shadow::take gets the wide compare now.

Correctness: word_runs already has a round-trip test in this file (word_runs ==
dword_runs over random rounds) and it passes; the fallback is the identical scalar call,
so only the instruction used changes, never the runs reported.

Expected: the refresh's cost this session measured is dominated by comparing ~71.6 MB of
title memory against the CPU copy every frame; upstream's findings.md measured 14 -> 4-7 ms
from doing this compare with AVX2 on a sibling path. Verify with VICTORY_REFRESH_SPLIT=1:
the MB copied must not move, the ms in the submitting thread must.
put_changed(), which writes what shaders wrote back into title memory, also computed its
runs with the scalar dword_runs(.., 0) although word_runs() is that same call with the
AVX2 implementation when it can. Same fallback, same runs, wider instruction. The
copy-back path is the one measured at 34.8 -> 17.2 ms by WC_NO_BACK, so it is worth the
wide compare too.

Tests: 423 passed, 2 failed - the two pre-existing upstream ones. Build clean.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 18:51:52 +00:00
github-bot referenced this pull request from a commit 2026-11-28 19:41:56 +00:00
github manually merged commit d9167f679f into main 2026-11-28 19:42:36 +00:00
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

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