Look up views once per range in watch::writing, and drop ended pins #67

Closed
romainhedouin wants to merge 1 commit from upstream-pr/67 into main
romainhedouin commented 2026-11-25 13:16:34 +00:00 (Migrated from github.com)

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


watch::writing runs for every copy into title memory, every copy-back, every readback row, every ReadFile and every raw DirectStorage request. For each 64 KiB block of the range it locked the views table and copied that block's list of views into a new Vec, even for pages with a single view that it then skipped. It now locks the views table once for the whole range and only collects the other views of pages that have several. It also drops pins that have ended when adding a new one (they end in the order they are made, so this is a pop from the front); before, they were only dropped on the slow path of guard() and could pile up, making every pin check scan them. Same pins, same blocks opened.

Per writing() call over N blocks Before After
Views-table locks N 1
Allocations (single-view pages) 1 per block 0
Model benchmark, 16 blocks, single-view pages 640–800 ns 330–415 ns
Model benchmark, 256 MiB, no views 91 µs 58 µs

"Model benchmark" = the same logic copied into a standalone program and timed in release mode on a Mac, not in the game. The model asserts identical pins and opened blocks for the old and new code. 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:5492068336 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/67). Originally posted by [romainhedouin](https://github.com/romainhedouin) on 2026-11-25T13:16:34Z. --- `watch::writing` runs for every copy into title memory, every copy-back, every readback row, every `ReadFile` and every raw DirectStorage request. For each 64 KiB block of the range it locked the views table and copied that block's list of views into a new `Vec`, even for pages with a single view that it then skipped. It now locks the views table once for the whole range and only collects the other views of pages that have several. It also drops pins that have ended when adding a new one (they end in the order they are made, so this is a pop from the front); before, they were only dropped on the slow path of `guard()` and could pile up, making every pin check scan them. Same pins, same blocks opened. | Per `writing()` call over N blocks | Before | After | |---|---|---| | Views-table locks | N | 1 | | Allocations (single-view pages) | 1 per block | 0 | | Model benchmark, 16 blocks, single-view pages | 640–800 ns | 330–415 ns | | Model benchmark, 256 MiB, no views | 91 µs | 58 µs | "Model benchmark" = the same logic copied into a standalone program and timed in release mode on a Mac, not in the game. The model asserts identical pins and opened blocks for the old and new code. 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:08:54 +00:00 (Migrated from github.com)

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

Review: COMMENTED


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

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

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

Code review location: crates/victory-gpu/src/watch.rs; line 664

@@ -643,25 +644,38 @@ pub fn writing(at: usize, len: usize) {
     }
     let (first, last) = (at / BLOCK, (at + len).div_ceil(BLOCK));
     let mut g = guards();
-    let until = std::time::Instant::now() + PIN;
-    g.pins.push((first, last, until));
+    let now = std::time::Instant::now();
+    let until = now + PIN;
+    // The pins that ended are dropped here too, or they pile up (and every
+    // check of a block scans them) until a first look at some range.
+    while g.pins.front().is_some_and(|&(_, _, end)| end <= now) {
+        g.pins.pop_front();
+    }
+    g.pins.push_back((first, last, until));
     open(&mut g, at, at + len);
     if !view_guard() {
         return;
     }
     // The same pages at their other views: the write lands in them too, so
-    // their copies must take them again, now and when it has landed.
-    for block in first..last {
-        let shown = aliases(block * BLOCK);
-        if shown.len() < 2 || shown.len() > MANY_VIEWS {
-            continue;
-        }
-        for alias in shown {
-            if alias / BLOCK != block {
-                g.pins.push((alias / BLOCK, alias / BLOCK + 1, until));
-                open_here(&mut g, alias, alias + BLOCK);
+    // their copies must take them again, now and when it has landed. One look
+    // at the views for the whole range, and a list only of the other views of
+    // pages with several (a lock and a list for each block before, which
+    // every copy into title memory waited for under the guards' lock).
+    let others = {

The existing alias tests write through a mapped view and exercise the exception path, but they do not call writing() on a range spanning aliased blocks. Could one test verify that this bulk alias collection leaves each corresponding other view writable and reports it on the next guard check? AI-assisted review; I checked the changed range walk against the existing alias tests.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review-comment:4696896650 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/67#discussion_r4696896650). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-25T15:08:54Z. **Code review location:** `crates/victory-gpu/src/watch.rs`; line 664 ~~~~diff @@ -643,25 +644,38 @@ pub fn writing(at: usize, len: usize) { } let (first, last) = (at / BLOCK, (at + len).div_ceil(BLOCK)); let mut g = guards(); - let until = std::time::Instant::now() + PIN; - g.pins.push((first, last, until)); + let now = std::time::Instant::now(); + let until = now + PIN; + // The pins that ended are dropped here too, or they pile up (and every + // check of a block scans them) until a first look at some range. + while g.pins.front().is_some_and(|&(_, _, end)| end <= now) { + g.pins.pop_front(); + } + g.pins.push_back((first, last, until)); open(&mut g, at, at + len); if !view_guard() { return; } // The same pages at their other views: the write lands in them too, so - // their copies must take them again, now and when it has landed. - for block in first..last { - let shown = aliases(block * BLOCK); - if shown.len() < 2 || shown.len() > MANY_VIEWS { - continue; - } - for alias in shown { - if alias / BLOCK != block { - g.pins.push((alias / BLOCK, alias / BLOCK + 1, until)); - open_here(&mut g, alias, alias + BLOCK); + // their copies must take them again, now and when it has landed. One look + // at the views for the whole range, and a list only of the other views of + // pages with several (a lock and a list for each block before, which + // every copy into title memory waited for under the guards' lock). + let others = { ~~~~ --- The existing alias tests write through a mapped view and exercise the exception path, but they do not call writing() on a range spanning aliased blocks. Could one test verify that this bulk alias collection leaves each corresponding other view writable and reports it on the next guard check? AI-assisted review; I checked the changed range walk against the existing alias tests.
romainhedouin commented 2026-11-26 22:24:34 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by romainhedouin on 2026-11-26T22:24:34Z.


Closing: on a-publier, watch::writing no longer pins or walks views block by block. It takes the guards' lock only when the PROTECTED bitmap shows a protected block (watch.rs:3222).

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7204651217 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/67#issuecomment-7204651217). Originally posted by [romainhedouin](https://github.com/romainhedouin) on 2026-11-26T22:24:34Z. --- Closing: on `a-publier`, `watch::writing` no longer pins or walks views block by block. It takes the guards' lock only when the `PROTECTED` bitmap shows a protected block (watch.rs:3222).

Pull request closed

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!67
No description provided.