GPU: a submission's write ranges deduplicated through a set, not a scan #10

Open
LICObr wants to merge 1 commit from upstream-pr/10 into main
LICObr commented 2026-11-25 11:37:20 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by LICObr on 2026-11-25T11:37:20Z.


Problem

HostQueue::execute collects the GPU write ranges of every list in a submission (Step::GpuWrite, then batch.bindless_writes) into card_writes and cpu_back. It keeps each range once by scanning the ranges kept so far (target.iter().any(...)), so the cost grows with steps × distinct ranges on the submitting thread. defer_gpu_write does not dedupe at push time (unlike defer_refresh with refreshed). Every list of a world frame declares its UAV ranges again, so the same ranges show up many times per submission.

Change

  • A HashSet of (Arc::as_ptr(shadow), address, len) beside the two vectors. A range is pushed only the first time it is seen.
  • Same ranges, same order, same split between card and CPU copy-back as before. Only the lookup changes.
  • 11 lines in place of 20, in host.rs only.

Verification

  • cargo check -p victory-gpu --all-targets --all-features --target x86_64-pc-windows-msvc: builds.
  • cargo clippy -p victory-gpu --all-targets: no new warning in the changed code. The crate does not build under -D warnings today because of victory-shader (#2).
  • Not measured in a game run. I don't know how many write ranges a world submission declares. With thousands it should show on the submitting thread; with dozens it won't. If you can, a VICTORY_PROFILE run before and after would tell. I'm happy to adjust or drop it if it doesn't show up.
<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:issue:5490581294 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/10). Originally posted by [LICObr](https://github.com/LICObr) on 2026-11-25T11:37:20Z. --- ## Problem `HostQueue::execute` collects the GPU write ranges of every list in a submission (`Step::GpuWrite`, then `batch.bindless_writes`) into `card_writes` and `cpu_back`. It keeps each range once by scanning the ranges kept so far (`target.iter().any(...)`), so the cost grows with steps × distinct ranges on the submitting thread. `defer_gpu_write` does not dedupe at push time (unlike `defer_refresh` with `refreshed`). Every list of a world frame declares its UAV ranges again, so the same ranges show up many times per submission. ## Change - A `HashSet` of `(Arc::as_ptr(shadow), address, len)` beside the two vectors. A range is pushed only the first time it is seen. - Same ranges, same order, same split between card and CPU copy-back as before. Only the lookup changes. - 11 lines in place of 20, in `host.rs` only. ## Verification - `cargo check -p victory-gpu --all-targets --all-features --target x86_64-pc-windows-msvc`: builds. - `cargo clippy -p victory-gpu --all-targets`: no new warning in the changed code. The crate does not build under `-D warnings` today because of `victory-shader` (#2). - **Not measured in a game run.** I don't know how many write ranges a world submission declares. With thousands it should show on the submitting thread; with dozens it won't. If you can, a `VICTORY_PROFILE` run before and after would tell. I'm happy to adjust or drop it if it doesn't show up.
Ekabusto commented 2026-11-25 12:47:24 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by Ekabusto on 2026-11-25T12:47:24Z.


Checked locally: victory-gpu builds and cargo test -p victory-gpu --lib passes (106 passed, 4 ignored; this branch is on 4cbe9d7, which has 3 fewer tests than main). It merges into current main without conflicts, and clippy has one warning fewer than on main.

The change keeps behaviour: a shadow is either card or CPU for its whole life, so keying the set on (shadow, address, len) without the target picks the same vector the scan did, and the first occurrence keeps its place, so order is unchanged. Looks good to me; as you say, whether it shows up depends on how many ranges a world submission declares.

Reviewed on Windows 11 (build 26300), Rust 1.98.1, each PR in its own worktree; no game data, not run in the game. AI-assisted (Claude Code).

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7162859532 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/10#issuecomment-7162859532). Originally posted by [Ekabusto](https://github.com/Ekabusto) on 2026-11-25T12:47:24Z. --- Checked locally: `victory-gpu` builds and `cargo test -p victory-gpu --lib` passes (106 passed, 4 ignored; this branch is on `4cbe9d7`, which has 3 fewer tests than `main`). It merges into current `main` without conflicts, and clippy has one warning fewer than on `main`. The change keeps behaviour: a shadow is either card or CPU for its whole life, so keying the set on `(shadow, address, len)` without the target picks the same vector the scan did, and the first occurrence keeps its place, so order is unchanged. Looks good to me; as you say, whether it shows up depends on how many ranges a world submission declares. Reviewed on Windows 11 (build 26300), Rust 1.98.1, each PR in its own worktree; no game data, not run in the game. AI-assisted (Claude Code).
gut0to commented 2026-11-27 14:48:26 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by gut0to on 2026-11-27T14:48:26Z.

Code review location: crates/victory-gpu/src/host.rs; line 1206

@@ -1196,36 +1196,27 @@ impl Host {
         let first = early.record(self);
         // Deduplicate GPU write ranges once before execution so VRAM card
         // shadows get pre-uploaded / Wave64-hashed in the same GPU wait.
+        // A set beside the lists: every list of a world frame declares the
+        // same ranges again, and a scan of the list per range was quadratic.
         let mut card_writes: Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, usize)> =
             Vec::new();
         let mut cpu_back: Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, usize)> = Vec::new();
+        let mut seen = std::collections::HashSet::new();
+        let mut add = |shadow: &std::sync::Arc<crate::shadow::Shadow>, address: usize, len: usize| {
+            if seen.insert((std::sync::Arc::as_ptr(shadow) as usize, address, len)) {

Could the range-key logic get a deterministic helper test? The set spans both per-list GpuWrite entries and batch.bindless_writes, so correctness depends on deduplicating only the full (Shadow identity, address, length) tuple. A test could feed the same tuple from both sources, then reuse the same bounds with a different Shadow and a different range on the original, asserting only the true duplicate is removed. That would guard against silently dropping write or copy-back ranges if this key changes later.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review-comment:4720768271 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/10#discussion_r4720768271). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-27T14:48:26Z. **Code review location:** `crates/victory-gpu/src/host.rs`; line 1206 ~~~~diff @@ -1196,36 +1196,27 @@ impl Host { let first = early.record(self); // Deduplicate GPU write ranges once before execution so VRAM card // shadows get pre-uploaded / Wave64-hashed in the same GPU wait. + // A set beside the lists: every list of a world frame declares the + // same ranges again, and a scan of the list per range was quadratic. let mut card_writes: Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, usize)> = Vec::new(); let mut cpu_back: Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, usize)> = Vec::new(); + let mut seen = std::collections::HashSet::new(); + let mut add = |shadow: &std::sync::Arc<crate::shadow::Shadow>, address: usize, len: usize| { + if seen.insert((std::sync::Arc::as_ptr(shadow) as usize, address, len)) { ~~~~ --- Could the range-key logic get a deterministic helper test? The set spans both per-list `GpuWrite` entries and `batch.bindless_writes`, so correctness depends on deduplicating only the full `(Shadow identity, address, length)` tuple. A test could feed the same tuple from both sources, then reuse the same bounds with a different `Shadow` and a different range on the original, asserting only the true duplicate is removed. That would guard against silently dropping write or copy-back ranges if this key changes later.
gut0to commented 2026-11-27 14:48:26 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by gut0to on 2026-11-27T14:48:26Z.

Review: COMMENTED


AI-assisted static review (ChatGPT); I inspected the diff but did not run tests.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review:6089879259 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/10#pullrequestreview-6089879259). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-27T14:48:26Z. **Review:** COMMENTED --- AI-assisted static review (ChatGPT); I inspected the diff but did not run tests.
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/10:upstream-pr/10'
git switch 'upstream-pr/10'

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/10'
git switch 'upstream-pr/10'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/10'
git switch 'upstream-pr/10'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/10'
git switch 'main'
git merge --squash 'upstream-pr/10'
git switch 'main'
git merge --ff-only 'upstream-pr/10'
git switch 'main'
git merge 'upstream-pr/10'
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
Belobug/VIctory-Belobug-AMD!10
No description provided.