Skip to content

GPU: index withheld ranges during copy-back - #3216

Open
yumaste wants to merge 3 commits into
Marvisak:mainfrom
yumaste:perf/index-withheld-copyback
Open

yumaste wants to merge 3 commits into
Marvisak:mainfrom
yumaste:perf/index-withheld-copyback

Conversation

@yumaste

@yumaste yumaste commented Nov 26, 2026 •

Copy link
Copy Markdown

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 GPU: append only newly covered bytes to write snapshots #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).

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@AurumCarlos AurumCarlos left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@yumaste

yumaste commented Nov 26, 2026

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants