Copy-back: look for changed runs a word at a time (same output, 4-6x faster search) #26

Open
spy0eye-beep wants to merge 77 commits from upstream-pr/26 into main
spy0eye-beep commented 2026-11-25 17:24:07 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by spy0eye-beep on 2026-11-25T17:24:07Z.


What

A CPU-only speed-up in copy-back (shadow.rs back_piece), with the same result byte for byte: nothing about what is drawn changes.

After the merge, back_piece looks for the runs of changed bytes in each 64 KiB piece's mask, one byte at a time. Where a shader wrote here and there in a piece, the mask is mostly zeros, and that search ran at 2-4 GB/s. It now passes over zero words 8 bytes at a time (mask_runs), then finishes byte by byte as before.

cargo test --release -p victory-gpu --lib mask_runs_speed -- --ignored --nocapture, i7-13620H:

64 KiB piece byte by byte by words
every byte written 4.0 GB/s 4.2 GB/s
every 16th dword written 2.4 GB/s 10.0 GB/s
1 byte per 4 KiB written 4.2 GB/s 27.3 GB/s

Same output

  • New test word_search_finds_the_byte_runs: random masks of every length 0-40 and 63, 64, 65, 4095, 4096, 65536 bytes, at five densities (dense to one byte per 4 KiB), runs touching word edges and the end. The runs equal those of the old byte-by-byte search, kept in the test as the reference.
  • The merge itself is unchanged. (I tried merging 8 bytes at a time too: identical bytes, but slower than the byte loop, which the compiler already vectorises. Left out.)
  • The kernel writes per run (write_mapped) are unchanged, so title bytes between runs are still not written (upstream VIctory #2009).

Testing

  • cargo test -p victory-gpu --all-features on the RTX 4060 Laptop: 163 passed, 0 failed (shadow tests: 29 passed).
  • Clippy: the same two existing errors as beta (draw.rs sort_unstable_by_key, indirect.rs depth_written), none new. rustfmt: no new differences.
  • Not measured in the title. How much of a frame this saves depends on how scattered the shaders' writes are; in fxceless/victory-fork#26 copy-back was 148-198 ms of a ~300 ms frame for 45-73 MB, most of it presumably the kernel writes, which this does not touch.

AI-assisted (Claude).

🤖 Generated with Claude Code

<!-- greyfinch-sync:PastiiJay/klbf-lab:issue:5495965278 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/26). Originally posted by [spy0eye-beep](https://github.com/spy0eye-beep) on 2026-11-25T17:24:07Z. --- ## What A CPU-only speed-up in copy-back (`shadow.rs` `back_piece`), with **the same result byte for byte**: nothing about what is drawn changes. After the merge, `back_piece` looks for the runs of changed bytes in each 64 KiB piece's mask, one byte at a time. Where a shader wrote here and there in a piece, the mask is mostly zeros, and that search ran at 2-4 GB/s. It now passes over zero words 8 bytes at a time (`mask_runs`), then finishes byte by byte as before. `cargo test --release -p victory-gpu --lib mask_runs_speed -- --ignored --nocapture`, i7-13620H: | 64 KiB piece | byte by byte | by words | |---|---|---| | every byte written | 4.0 GB/s | 4.2 GB/s | | every 16th dword written | 2.4 GB/s | **10.0 GB/s** | | 1 byte per 4 KiB written | 4.2 GB/s | **27.3 GB/s** | ## Same output - New test `word_search_finds_the_byte_runs`: random masks of every length 0-40 and 63, 64, 65, 4095, 4096, 65536 bytes, at five densities (dense to one byte per 4 KiB), runs touching word edges and the end. The runs equal those of the old byte-by-byte search, kept in the test as the reference. - The merge itself is unchanged. (I tried merging 8 bytes at a time too: identical bytes, but slower than the byte loop, which the compiler already vectorises. Left out.) - The kernel writes per run (`write_mapped`) are unchanged, so title bytes between runs are still not written (upstream VIctory #2009). ## Testing - `cargo test -p victory-gpu --all-features` on the RTX 4060 Laptop: 163 passed, 0 failed (`shadow` tests: 29 passed). - Clippy: the same two existing errors as `beta` (`draw.rs` `sort_unstable_by_key`, `indirect.rs` `depth_written`), none new. rustfmt: no new differences. - Not measured in the title. How much of a frame this saves depends on how scattered the shaders' writes are; in fxceless/victory-fork#26 copy-back was 148-198 ms of a ~300 ms frame for 45-73 MB, most of it presumably the kernel writes, which this does not touch. AI-assisted (Claude). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
PastiiJay commented 2026-11-25 18:38:10 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-25T18:38:10Z.


Thanks a lot @spy0eye-beep! Noted. It goes into tonight's RX 9070 XT test queue and I'll report the result here.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7171182504 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/26#issuecomment-7171182504). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-25T18:38:10Z. --- Thanks a lot @spy0eye-beep! Noted. It goes into tonight's RX 9070 XT test queue and I'll report the result here.
gmankab commented 2026-11-25 19:20:29 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by gmankab on 2026-11-25T19:20:29Z.


I ran word_search_finds_the_byte_runs and mask_runs_speed on the exact PR head. The correctness test passed. On the Xeon E5-2670 v3, the dense mask was 1.69 → 1.68 GB/s, one changed byte per 64 bytes was 1.16 → 4.03 GB/s, and one per 4 KiB was 1.64 → 9.48 GB/s.

I also ported the commit to FX with Rayane’s authorship preserved. All 30 of its shadow tests passed, and the mask scan showed a similar gain on sparse inputs. FX draft PR

Tested-on: Fedora Linux 45 x86_64, Wine in the Victory Flatpak with DRM hidden
Assisted-by: gpt-6.2 sol xhigh

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7172160924 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/26#issuecomment-7172160924). Originally posted by [gmankab](https://github.com/gmankab) on 2026-11-25T19:20:29Z. --- I ran `word_search_finds_the_byte_runs` and `mask_runs_speed` on the exact PR head. The correctness test passed. On the Xeon E5-2670 v3, the dense mask was 1.69 → 1.68 GB/s, one changed byte per 64 bytes was 1.16 → 4.03 GB/s, and one per 4 KiB was 1.64 → 9.48 GB/s. I also ported the commit to FX with Rayane’s authorship preserved. All 30 of its shadow tests passed, and the mask scan showed a similar gain on sparse inputs. [FX draft PR](https://github.com/fxceless/victory-fork/pull/1094) Tested-on: Fedora Linux 45 x86_64, Wine in the Victory Flatpak with DRM hidden Assisted-by: gpt-6.2 sol xhigh
PastiiJay commented 2026-11-25 19:21:57 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-25T19:21:57Z.


Thanks a lot @gmankab for measuring it, those are great numbers (up to ~6x on sparse changes), and for porting it to fxceless with Rayane's authorship kept. It's already in our next test build, and the results will be posted here.

<!-- greyfinch-sync:PastiiJay/klbf-lab:comment:7172193773 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/26#issuecomment-7172193773). Originally posted by [PastiiJay](https://github.com/PastiiJay) on 2026-11-25T19:21:57Z. --- Thanks a lot @gmankab for measuring it, those are great numbers (up to ~6x on sparse changes), and for porting it to fxceless with Rayane's authorship kept. It's already in our next test build, and the results will be posted here.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/26:upstream-pr/26'
git switch 'upstream-pr/26'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/26'
git switch 'upstream-pr/26'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/26'
git switch 'upstream-pr/26'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/26'
git switch 'main'
git merge --squash 'upstream-pr/26'
git switch 'main'
git merge --ff-only 'upstream-pr/26'
git switch 'main'
git merge 'upstream-pr/26'
git push origin 'main'
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
PastiiJay/klbf-lab!26
No description provided.