GPU: Speed up shadow buffer writeback merging #2054

Manually merged
github merged 1 commit from gh-pr/2054/head into gh-pr/2054/base 2026-11-26 04:53:08 +00:00
Owner

@⁠GaussianGuaicai on GitHub · 2026-11-25 04:11 UTC · original
GaussianGuaicai:gpu/shadow-writeback-merge → main · +41 −18 · 1 files · 1 commits · merged 2026-11-25 21:53 UTC

Share a branch-free byte merge between the two shadow-buffer writeback paths so the compiler can vectorize the loop. The GPU byte still wins only when it differs from the saved byte; otherwise the guest byte is preserved. The helper reports whether guest memory needs to be written, as before.

Validation

  • Added a bytewise equivalence test for sparse and dense updates, conflicting writes, and short or unaligned lengths. It passed locally.
  • cargo test --locked -p victory-gpu --lib --features diagnostics: 146 passed, 1 failed, 2 ignored. Unmodified base 940d32f: 145 passed, 1 failed, 2 ignored. Both failed the same assertion in draw::tests::a_texture_written_in_one_format_reads_in_another_through_a_placed_view.
  • git diff --check passed.

An earlier diagnostic comparison measured merge cost of 0.392 vs. 0.088 ms/MB. The workloads differed; this measures only the merge step and does not establish an end-to-end frame-time improvement.

Prepared with assistance from GitHub Copilot.

**@⁠GaussianGuaicai** on GitHub · 2026-11-25 04:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/2054) `GaussianGuaicai:gpu/shadow-writeback-merge` → `main` · +41 −18 · 1 files · 1 commits · merged 2026-11-25 21:53 UTC Share a branch-free byte merge between the two shadow-buffer writeback paths so the compiler can vectorize the loop. The GPU byte still wins only when it differs from the saved byte; otherwise the guest byte is preserved. The helper reports whether guest memory needs to be written, as before. ### Validation - Added a bytewise equivalence test for sparse and dense updates, conflicting writes, and short or unaligned lengths. It passed locally. - `cargo test --locked -p victory-gpu --lib --features diagnostics`: 146 passed, 1 failed, 2 ignored. Unmodified base `940d32f`: 145 passed, 1 failed, 2 ignored. Both failed the same assertion in `draw::tests::a_texture_written_in_one_format_reads_in_another_through_a_placed_view`. - `git diff --check` passed. An earlier diagnostic comparison measured merge cost of 0.392 vs. 0.088 ms/MB. The workloads differed; this measures only the merge step and does not establish an end-to-end frame-time improvement. Prepared with assistance from GitHub Copilot.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:53:07 +00:00
github manually merged commit 857f630861 into gh-pr/2054/base 2026-11-26 04:53:08 +00:00
Author
Owner

@⁠RKouchoo on GitHub · 2026-11-25 04:19 UTC · original

On 7800X3D, #2054 cut a 4 MiB writeback merge from 4.22 ms to 0.34 ms when about one byte in 64 changed. In a separate replay of Story’s buffer-copy workload, #2051 cut the median from 54.1 ms to 30.5 ms.

**@⁠RKouchoo** on GitHub · 2026-11-25 04:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2054#issuecomment-7152732431) On 7800X3D, #2054 cut a 4 MiB writeback merge from 4.22 ms to 0.34 ms when about one byte in 64 changed. In a separate replay of Story’s buffer-copy workload, #2051 cut the median from 54.1 ms to 30.5 ms.
Author
Owner

@⁠Dathinel on GitHub · 2026-11-25 04:33 UTC · original

The 64 KiB piece size used in shadow.rs shows a clear gain on a Ryzen 7 7435HS. The direction reverses for very short, sparsely changed spans. I compared the merge loop from 940d32f6 with merge_writeback from 857f630 on identical synthetic buffers.

Span length Host bytes changed from snapshot Base, ms per MiB PR, ms per MiB
64 KiB ~1/64 0.797–0.886 0.076–0.082
64 KiB ~1/2 4.347–4.490 0.077–0.085
4 bytes ~1/64 0.997–1.087 1.726–1.819

Each row processes 1 MiB total, split into equal spans. Windows/MSVC, rustc 1.98.1 -O, standalone CPU harness with direct calls. Each range is the min/max of three process medians (30 paired samples per process, alternating order). Buffer resets were outside the timer. Both output buffers and the count of changed spans matched.

The four-byte row deliberately calls the merge on every span, including unchanged ones. The saved-page path does that after clipping; the other caller first checks host != old. Actual clipping frequency, GPU readback and frame time were outside this measurement. AI-assisted.

**@⁠Dathinel** on GitHub · 2026-11-25 04:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/2054#issuecomment-7152950167) The 64 KiB piece size used in `shadow.rs` shows a clear gain on a Ryzen 7 7435HS. The direction reverses for very short, sparsely changed spans. I compared the merge loop from `940d32f6` with `merge_writeback` from `857f630` on identical synthetic buffers. | Span length | Host bytes changed from snapshot | Base, ms per MiB | PR, ms per MiB | |---|---:|---:|---:| | 64 KiB | ~1/64 | 0.797–0.886 | 0.076–0.082 | | 64 KiB | ~1/2 | 4.347–4.490 | 0.077–0.085 | | 4 bytes | ~1/64 | 0.997–1.087 | 1.726–1.819 | Each row processes 1 MiB total, split into equal spans. Windows/MSVC, rustc 1.98.1 `-O`, standalone CPU harness with direct calls. Each range is the min/max of three process medians (30 paired samples per process, alternating order). Buffer resets were outside the timer. Both output buffers and the count of changed spans matched. The four-byte row deliberately calls the merge on every span, including unchanged ones. The saved-page path does that after clipping; the other caller first checks `host != old`. Actual clipping frequency, GPU readback and frame time were outside this measurement. AI-assisted.
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 04:41 UTC · original

Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at 857f630. victory-gpu: 142 passed, 2 failed, 2 ignored; the new writeback_merge_matches_bytewise_oracle passes. The 2 failures are #1744's heap-cap failures on main. Same as main on this hardware. Not run: a title.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-25 04:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/2054#issuecomment-7153079221) Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at `857f630`. `victory-gpu`: 142 passed, 2 failed, 2 ignored; the new `writeback_merge_matches_bytewise_oracle` passes. The 2 failures are #1744's heap-cap failures on `main`. Same as `main` on this hardware. Not run: a title. AI-assisted (Claude Code).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 09:23 UTC · original

Automated triage for the maintainers.
Merge candidate.

In crates/victory-gpu/src/shadow.rs, the two copies of the byte-merge loop (in copy_back_or_not and the saved-page path) become one branch-free merge_writeback. I checked that it does the same thing as the old loop. Where the host byte differs from the snapshot, the title byte takes the host byte; the copy then takes the title byte; it returns true when any title byte changed. The oracle test compares against that rule written out byte by byte, not against the new code. Two people measured it in the thread: about 10x faster on 64 KiB pieces, a little slower on 4-byte spans on the saved-page path.

Overlap: #1867 does the same merge with AVX2, unsafe and an added doc file. This PR is the simpler, portable version of that. #1986 (also marked merge candidate) restructures the same loop in copy_back_or_not to skip withheld ranges, and #1998/#2242 rewrite the copy-back too. Whichever of those lands second will need a rebase. Optional nit: the new test could go in the existing #[cfg(test)] mod tests at the end of the file.

CI: passing (check).

Recommending a look, not approving.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 09:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/2054#issuecomment-7158258080) <!-- victory-triage v1 head=857f630861ab350ca45fadb1ba76ddb5c4aeb24e verdict=merge-candidate at=2026-11-25T09:23:01Z --> Automated triage for the maintainers. **Merge candidate.** In `crates/victory-gpu/src/shadow.rs`, the two copies of the byte-merge loop (in `copy_back_or_not` and the saved-page path) become one branch-free `merge_writeback`. I checked that it does the same thing as the old loop. Where the host byte differs from the snapshot, the title byte takes the host byte; the copy then takes the title byte; it returns true when any title byte changed. The oracle test compares against that rule written out byte by byte, not against the new code. Two people measured it in the thread: about 10x faster on 64 KiB pieces, a little slower on 4-byte spans on the saved-page path. Overlap: #1867 does the same merge with AVX2, `unsafe` and an added doc file. This PR is the simpler, portable version of that. #1986 (also marked merge candidate) restructures the same loop in `copy_back_or_not` to skip withheld ranges, and #1998/#2242 rewrite the copy-back too. Whichever of those lands second will need a rebase. Optional nit: the new test could go in the existing `#[cfg(test)] mod tests` at the end of the file. CI: passing (`check`). Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
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/Marvisak--VIctory!2054
No description provided.