Find the destination heap once per descriptor copy, not per descriptor #62

Open
romainhedouin wants to merge 1 commit from upstream-pr/62 into a-publier
romainhedouin commented 2026-11-25 13:14:44 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by romainhedouin on 2026-11-25T13:14:44Z.


When the title copies N descriptors (CopyDescriptorsSimple), each copied slot was mirrored to the host on its own: it took the SHADER_HEAPS lock, scanned the list for the heap holding the slot, and locked that heap's pending list. The heap is now found once for each run of slots in the same heap (a run stops where another heap starts, so overlapping heaps give the same answer as before), and CopyDescriptors no longer builds two Vecs of ranges per call.

Per copy of N descriptors into one heap Before After
SHADER_HEAPS read-lock acquisitions N 1
Heap-list scans N 1
pending locks N 1
Vec allocations per CopyDescriptors 2 0

Tested: counts from the code; a standalone model of the per-slot lookup against the run lookup agreed on 3,000,000 random cases (overlapping and adjacent heaps, mismatched steps); type-checked for Windows (cargo clippy --target x86_64-pc-windows-msvc, with and without --all-features, no new warnings), not run.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:issue:5492039168 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/62). Originally posted by [romainhedouin](https://github.com/romainhedouin) on 2026-11-25T13:14:44Z. --- When the title copies N descriptors (`CopyDescriptorsSimple`), each copied slot was mirrored to the host on its own: it took the `SHADER_HEAPS` lock, scanned the list for the heap holding the slot, and locked that heap's `pending` list. The heap is now found once for each run of slots in the same heap (a run stops where another heap starts, so overlapping heaps give the same answer as before), and `CopyDescriptors` no longer builds two `Vec`s of ranges per call. | Per copy of N descriptors into one heap | Before | After | |---|---|---| | `SHADER_HEAPS` read-lock acquisitions | N | 1 | | Heap-list scans | N | 1 | | `pending` locks | N | 1 | | `Vec` allocations per `CopyDescriptors` | 2 | 0 | Tested: counts from the code; a standalone model of the per-slot lookup against the run lookup agreed on 3,000,000 random cases (overlapping and adjacent heaps, mismatched steps); type-checked for Windows (`cargo clippy --target x86_64-pc-windows-msvc`, with and without `--all-features`, no new warnings), not run. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gut0to commented 2026-11-25 15:09:09 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by gut0to on 2026-11-25T15:09:09Z.

Review: COMMENTED


AI-assisted review: I checked the changed behavior and the associated test coverage.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review:6060187663 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/62#pullrequestreview-6060187663). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-25T15:09:09Z. **Review:** COMMENTED --- AI-assisted review: I checked the changed behavior and the associated test coverage.
gut0to commented 2026-11-25 15:09:09 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by gut0to on 2026-11-25T15:09:09Z.

Code review location: crates/victory-gpu/src/descriptor.rs; line 162

@@ -136,30 +136,57 @@ const SHADER_VISIBLE: u32 = 1;
 static SHADER_HEAPS: std::sync::Mutex<Vec<(u64, u64, usize)>> = std::sync::Mutex::new(Vec::new());
 
 /// Makes the host descriptor for what the title wrote at `handle`, if it
-/// is in a shader-visible heap. Keeps `SHADER_HEAPS` locked for the duration
-/// so `Heap::drop` cannot free the heap concurrently.
+/// is in a shader-visible heap.
 #[cfg(windows)]
 fn mirror(handle: u64) {
+    mirror_range(handle, 1, 1);
+}
+
+/// Makes the host descriptors for what the title wrote at the `count`
+/// slots `step` bytes apart from `handle`, finding the heap once for each
+/// run of them in one heap. Keeps `SHADER_HEAPS` locked for the duration
+/// so `Heap::drop` cannot free the heap concurrently.
+#[cfg(windows)]
+fn mirror_range(handle: u64, count: u32, step: u64) {
     let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner());
-    let Some(&(_, _, at)) = heaps
-        .iter()
-        .find(|&&(start, end, _)| (start..end).contains(&handle))
-    else {
-        return;
-    };
-    // SAFETY: registered heaps stay alive until `Heap::drop`, which takes
-    // the `SHADER_HEAPS` lock we hold here.
-    let heap = unsafe { &*(at as *const Heap) };
-    let Some(index) = heap.index(handle) else {
-        return;
-    };
-    let mut pending = heap.pending.lock().unwrap_or_else(|e| e.into_inner());
-    match heap.host.get() {
-        None => pending.push(index),
-        Some(host) => {
-            drop(pending);
-            if let Some(host) = host {
-                host.mirror(index, handle, false);
+    let mut done = 0;
+    while done < count {
+        let at = handle + u64::from(done) * step;
+        // The first heap that holds `at` is also the first to hold every
+        // slot up to its end, unless another heap starts sooner: the run
+        // stops there, as when each slot is looked up alone.
+        let mut found = None;
+        let mut next = u64::MAX;
+        for &(start, end, heap) in heaps.iter() {
+            if (start..end).contains(&at) {
+                found.get_or_insert((end, heap));

The standalone model covers overlapping heaps, but could we keep one overlapping-heap case as a Windows unit test for mirror_range? The found.get_or_insert choice and run cutoff intentionally preserve the old first-containing-heap behavior, which is subtle and otherwise has no repository regression test. AI-assisted review; I checked the production path and the test coverage described in the PR.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review-comment:4696900111 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/62#discussion_r4696900111). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-25T15:09:09Z. **Code review location:** `crates/victory-gpu/src/descriptor.rs`; line 162 ~~~~diff @@ -136,30 +136,57 @@ const SHADER_VISIBLE: u32 = 1; static SHADER_HEAPS: std::sync::Mutex<Vec<(u64, u64, usize)>> = std::sync::Mutex::new(Vec::new()); /// Makes the host descriptor for what the title wrote at `handle`, if it -/// is in a shader-visible heap. Keeps `SHADER_HEAPS` locked for the duration -/// so `Heap::drop` cannot free the heap concurrently. +/// is in a shader-visible heap. #[cfg(windows)] fn mirror(handle: u64) { + mirror_range(handle, 1, 1); +} + +/// Makes the host descriptors for what the title wrote at the `count` +/// slots `step` bytes apart from `handle`, finding the heap once for each +/// run of them in one heap. Keeps `SHADER_HEAPS` locked for the duration +/// so `Heap::drop` cannot free the heap concurrently. +#[cfg(windows)] +fn mirror_range(handle: u64, count: u32, step: u64) { let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner()); - let Some(&(_, _, at)) = heaps - .iter() - .find(|&&(start, end, _)| (start..end).contains(&handle)) - else { - return; - }; - // SAFETY: registered heaps stay alive until `Heap::drop`, which takes - // the `SHADER_HEAPS` lock we hold here. - let heap = unsafe { &*(at as *const Heap) }; - let Some(index) = heap.index(handle) else { - return; - }; - let mut pending = heap.pending.lock().unwrap_or_else(|e| e.into_inner()); - match heap.host.get() { - None => pending.push(index), - Some(host) => { - drop(pending); - if let Some(host) = host { - host.mirror(index, handle, false); + let mut done = 0; + while done < count { + let at = handle + u64::from(done) * step; + // The first heap that holds `at` is also the first to hold every + // slot up to its end, unless another heap starts sooner: the run + // stops there, as when each slot is looked up alone. + let mut found = None; + let mut next = u64::MAX; + for &(start, end, heap) in heaps.iter() { + if (start..end).contains(&at) { + found.get_or_insert((end, heap)); ~~~~ --- The standalone model covers overlapping heaps, but could we keep one overlapping-heap case as a Windows unit test for mirror_range? The found.get_or_insert choice and run cutoff intentionally preserve the old first-containing-heap behavior, which is subtle and otherwise has no repository regression test. AI-assisted review; I checked the production path and the test coverage described in the PR.
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/62:upstream-pr/62'
git switch 'upstream-pr/62'

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 'a-publier'
git merge --no-ff 'upstream-pr/62'
git switch 'upstream-pr/62'
git rebase 'a-publier'
git switch 'a-publier'
git merge --ff-only 'upstream-pr/62'
git switch 'upstream-pr/62'
git rebase 'a-publier'
git switch 'a-publier'
git merge --no-ff 'upstream-pr/62'
git switch 'a-publier'
git merge --squash 'upstream-pr/62'
git switch 'a-publier'
git merge --ff-only 'upstream-pr/62'
git switch 'a-publier'
git merge 'upstream-pr/62'
git push origin 'a-publier'
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!62
No description provided.