GPU: index withheld ranges during copy-back #3216

Open
github-bot wants to merge 3 commits from github-bot/gh-3216 into main AGit
Owner

@⁠yumaste on GitHub · 2026-11-26 13:24 UTC · original
yumaste:perf/index-withheld-copyback → main · +134 −32 · 2 files · 3 commits · open

Problem

When a GPU submission withholds writes after a memory miss, the host scans all initially withheld ranges for each declared write. It then scans the final withheld ranges again for each marked page. Both scans include ranges owned by other shadows.

Change

Index larger sets by shadow identity and start address, with the maximum end seen so far for each shadow. Each write or page then needs one binary search. Sets of 32 ranges or fewer still use a linear scan. Declared writes are checked against the original withheld set, and marked pages see the final set after that pass. This preserves the existing overlap and whole-page rules.

Synthetic benchmark

Medians of 20 interleaved runs on Windows, Core Ultra 9 275HX, Rust 1.98.1 with -C opt-level=3. Each case distributes ranges and pages over 128 shadows. It measures building the range sets and checking declared writes and marked pages. Both implementations produced identical decisions. The benchmark excludes bitmap readback, waiting for the GPU and writing bytes to guest memory.

Declared writes Initially withheld Marked pages Scan This PR
8,192 0 4,096 6.8 us 5.3 us
1,024 8 512 5.4 us 5.4 us
4,096 256 2,048 517 us 101 us
8,192 2,048 4,096 4.53 ms 0.43 ms

Validation

  • Compared both search paths with the prior overlap predicate across nested, touching, empty and cross-shadow ranges.
  • The WARP copy-back test checks an unmarked page and a separate marked page overlapping a withheld range. Both retain the title's bytes, and all sampled bitmap bits clear.
  • WARP victory-gpu library suite with diagnostics: 247 passed, 1 ignored. Strict Clippy and the asset policy check passed.
  • Trial merge with #3211 succeeded.

Integration notes

A trial merge with #3211 succeeds. Trial merges with #3099 and #1998 conflict in host.rs and shadow.rs because they change the input and meaning of withheld ranges. Keep their original range data for the behavior change and use an index for overlap queries. #3145 and #3165 conflict in shadow.rs around mark processing. Their mark-owner lifetime fix must remain outside the marks lock when combining it with the indexed query.

Coordination claim. No game files used. AI-assisted (Codex).

**@⁠yumaste** on GitHub · 2026-11-26 13:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/3216) `yumaste:perf/index-withheld-copyback` → `main` · +134 −32 · 2 files · 3 commits · open ## Problem When a GPU submission withholds writes after a memory miss, the host scans all initially withheld ranges for each declared write. It then scans the final withheld ranges again for each marked page. Both scans include ranges owned by other shadows. ## Change Index larger sets by shadow identity and start address, with the maximum end seen so far for each shadow. Each write or page then needs one binary search. Sets of 32 ranges or fewer still use a linear scan. Declared writes are checked against the original withheld set, and marked pages see the final set after that pass. This preserves the existing overlap and whole-page rules. ## Synthetic benchmark Medians of 20 interleaved runs on Windows, Core Ultra 9 275HX, Rust 1.98.1 with `-C opt-level=3`. Each case distributes ranges and pages over 128 shadows. It measures building the range sets and checking declared writes and marked pages. Both implementations produced identical decisions. The benchmark excludes bitmap readback, waiting for the GPU and writing bytes to guest memory. | Declared writes | Initially withheld | Marked pages | Scan | This PR | | ---: | ---: | ---: | ---: | ---: | | 8,192 | 0 | 4,096 | 6.8 us | 5.3 us | | 1,024 | 8 | 512 | 5.4 us | 5.4 us | | 4,096 | 256 | 2,048 | 517 us | 101 us | | 8,192 | 2,048 | 4,096 | 4.53 ms | 0.43 ms | ## Validation - Compared both search paths with the prior overlap predicate across nested, touching, empty and cross-shadow ranges. - The WARP copy-back test checks an unmarked page and a separate marked page overlapping a withheld range. Both retain the title's bytes, and all sampled bitmap bits clear. - WARP `victory-gpu` library suite with diagnostics: 247 passed, 1 ignored. Strict Clippy and the asset policy check passed. - Trial merge with #3211 succeeded. ## Integration notes A trial merge with #3211 succeeds. Trial merges with #3099 and #1998 conflict in `host.rs` and `shadow.rs` because they change the input and meaning of withheld ranges. Keep their original range data for the behavior change and use an index for overlap queries. #3145 and #3165 conflict in `shadow.rs` around mark processing. Their mark-owner lifetime fix must remain outside the marks lock when combining it with the indexed query. [Coordination claim](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633). No game files used. AI-assisted (Codex).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:30:04 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 13:27 UTC · original · approved these changes

Verified on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib shadow:: -- --test-threads=1: 11/11 passed (including withheld_index_matches_overlap_scan).
  • cargo test -p victory-gpu --lib host::withholding -- --test-threads=1: passed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly indexes withheld ranges during copy-back to avoid repeated linear overlap scans.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 13:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/3216#pullrequestreview-6074934735) · **approved these changes** Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib shadow:: -- --test-threads=1`: 11/11 passed (including `withheld_index_matches_overlap_scan`). - `cargo test -p victory-gpu --lib host::withholding -- --test-threads=1`: passed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly indexes withheld ranges during copy-back to avoid repeated linear overlap scans. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-26 14:07 UTC · original · reviewed

The index is correct. Sorted by (shadow, start) with a running max end per shadow, the last entry with (copy, start) < (shadow, to) is the latest range of that shadow starting before to, and the overlap test reduces to from < max_end. That matches the old predicate, including empty ranges (start == end), which withheld_index_matches_overlap_scan covers. The host side also keeps the old two-pass behaviour: declared writes are checked against the initial set, marked pages against the final one, with no further propagation. Keeping identities as usize is fine because back holds the Arcs until after copy_back_marks, as your comment says.

Two remarks:

1. The edited test lost a case. In marked_pages_go_back_by_the_bytes_the_gpu_changed, page 2 used to be unmarked ("page 2 not marked"): it checked that a page nobody marked keeps the title's bytes and that the copy's byte there isn't put back. Now page 2 is marked and withheld, and the assertions are the same, so the test passes either way and the unmarked path is no longer exercised. Using a fourth page for the withheld case (the pool would need 4 pages) would keep both.

2. Other open PRs use the old signature. copy_back_marks now takes &Withheld instead of &[(Arc<Shadow>, usize, usize)]. #3099 ("drop only marked pages for withheld marked ranges") and #1998 ("keep valid marked writes beside withheld ranges") both change what a withheld range does in this same function, and both build and pass the slice. #3145 and #3165 rewrite the top of copy_back_marks. You've trial-merged with #3211. A note in the description about the ones that would conflict (or a quick git merge-tree against #3099/#1998) would help whoever merges them in sequence.

**@⁠AurumCarlos** on GitHub · 2026-11-26 14:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/3216#pullrequestreview-6075517892) · **reviewed** The index is correct. Sorted by `(shadow, start)` with a running max end per shadow, the last entry with `(copy, start) < (shadow, to)` is the latest range of that shadow starting before `to`, and the overlap test reduces to `from < max_end`. That matches the old predicate, including empty ranges (`start == end`), which `withheld_index_matches_overlap_scan` covers. The host side also keeps the old two-pass behaviour: declared writes are checked against the initial set, marked pages against the final one, with no further propagation. Keeping identities as `usize` is fine because `back` holds the `Arc`s until after `copy_back_marks`, as your comment says. Two remarks: **1. The edited test lost a case.** In `marked_pages_go_back_by_the_bytes_the_gpu_changed`, page 2 used to be *unmarked* ("page 2 not marked"): it checked that a page nobody marked keeps the title's bytes and that the copy's byte there isn't put back. Now page 2 is marked and withheld, and the assertions are the same, so the test passes either way and the unmarked path is no longer exercised. Using a fourth page for the withheld case (the pool would need 4 pages) would keep both. **2. Other open PRs use the old signature.** `copy_back_marks` now takes `&Withheld` instead of `&[(Arc<Shadow>, usize, usize)]`. #3099 ("drop only marked pages for withheld marked ranges") and #1998 ("keep valid marked writes beside withheld ranges") both change what a withheld range does in this same function, and both build and pass the slice. #3145 and #3165 rewrite the top of `copy_back_marks`. You've trial-merged with #3211. A note in the description about the ones that would conflict (or a quick `git merge-tree` against #3099/#1998) would help whoever merges them in sequence.
Author
Owner

@⁠yumaste on GitHub · 2026-11-26 14:22 UTC · original

The test now uses a fourth page for the withheld case (e4994f8, with the merge cleanup in 2a3b636). Page 2 stays unmarked and page 3 is marked and withheld. Both preserve the title's bytes. All 10 shadow tests passed under WARP. I also documented the four trial-merge conflicts in the description and rechecked that #3211 merges cleanly.

**@⁠yumaste** on GitHub · 2026-11-26 14:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/3216#issuecomment-7194729635) The test now uses a fourth page for the withheld case (`e4994f8`, with the merge cleanup in `2a3b636`). Page 2 stays unmarked and page 3 is marked and withheld. Both preserve the title's bytes. All 10 shadow tests passed under WARP. I also documented the four trial-merge conflicts in the description and rechecked that #3211 merges cleanly.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3216/head:github-bot/gh-3216'
git switch 'github-bot/gh-3216'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3216'
git switch 'github-bot/gh-3216'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3216'
git switch 'github-bot/gh-3216'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3216'
git switch 'main'
git merge --squash 'github-bot/gh-3216'
git switch 'main'
git merge --ff-only 'github-bot/gh-3216'
git switch 'main'
git merge 'github-bot/gh-3216'
git push origin 'main'
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!3216
No description provided.