Stop re-zeroing the texture scratch buffer, and reuse the placed-texture set #63

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

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


Two small cuts in texture refresh:

  • Scratch buffer. Refreshing a texture whose memory is not write-watched copies it into a per-thread scratch buffer, after resize(size, 0). Every time a texture was bigger than the previous one, the extra part was zero-filled, then immediately overwritten by the copy (which writes every byte, zeros for unreadable pages). The buffer now keeps its length, as with_tiled_copy already does, so only a texture bigger than any before is zero-filled. The diagnostics mode that skips copies still hashes zeros.
  • placed_live. It rebuilt a set of every placed texture from PLACED_TEXTURES with one hash insert each, while PLACED_LIVE already holds exactly that set (both change together under the same lock). It now clones PLACED_LIVE.
Before After
Bytes zero-filled per refresh growth over the previous texture growth over the largest so far (≈0 in steady state)
Model benchmark, 8 textures/frame alternating 4 MiB / 64 KiB, 2,000 frames 20 GiB zeroed, 164 ms 4 MiB, 0.3 ms
placed_live, model benchmark, 20,000 placed textures 194 µs 4.7 µs

"Model benchmark" = the same logic copied into a standalone program and timed in release mode on a Mac, not in the game. 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:5492062559 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/63). Originally posted by [romainhedouin](https://github.com/romainhedouin) on 2026-11-25T13:16:13Z. --- Two small cuts in texture refresh: - **Scratch buffer.** Refreshing a texture whose memory is not write-watched copies it into a per-thread scratch buffer, after `resize(size, 0)`. Every time a texture was bigger than the previous one, the extra part was zero-filled, then immediately overwritten by the copy (which writes every byte, zeros for unreadable pages). The buffer now keeps its length, as `with_tiled_copy` already does, so only a texture bigger than any before is zero-filled. The diagnostics mode that skips copies still hashes zeros. - **`placed_live`.** It rebuilt a set of every placed texture from `PLACED_TEXTURES` with one hash insert each, while `PLACED_LIVE` already holds exactly that set (both change together under the same lock). It now clones `PLACED_LIVE`. | | Before | After | |---|---|---| | Bytes zero-filled per refresh | growth over the previous texture | growth over the largest so far (≈0 in steady state) | | Model benchmark, 8 textures/frame alternating 4 MiB / 64 KiB, 2,000 frames | 20 GiB zeroed, 164 ms | 4 MiB, 0.3 ms | | `placed_live`, model benchmark, 20,000 placed textures | 194 µs | 4.7 µs | "Model benchmark" = the same logic copied into a standalone program and timed in release mode on a Mac, not in the game. 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:03 +00:00 (Migrated from github.com)

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

Review: COMMENTED


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

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

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

Code review location: crates/victory-gpu/src/texture.rs; line 345

@@ -331,9 +331,20 @@ impl Staging {
         }
         // Read through our safe copy: the title may unmap the pages
         // (evicting a texture a big table lists) while we read them.
-        SCRATCH.with_borrow_mut(|tiled| {
-            tiled.resize(size, 0);
+        SCRATCH.with_borrow_mut(|scratch| {
+            // As in `with_tiled_copy`: the length stays, the copy writes all
+            // of `size` (zeros for what cannot be read), so only a bigger
+            // texture than any before is zeroed.
+            if size > scratch.len() {
+                scratch.resize(size, 0);
+            }
+            let tiled = &mut scratch[..size];
             crate::host::copy_mapped(tiled.as_mut_ptr(), self.memory, size, false);
+            // The diagnostics mode without copies writes nothing: it hashes zeros, as before.
+            #[cfg(feature = "diagnostics")]
+            if crate::copystat::mode() == 2 {

Could a diagnostics-mode regression test prefill the retained scratch buffer with nonzero bytes, run a no-copy refresh, and verify that the hash sees zeros? That explicit fill preserves the old behavior after scratch stops being resized to zero on every refresh. AI-assisted review; I checked the diagnostics-only zero-fill and scratch reuse path.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review-comment:4696898892 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/63#discussion_r4696898892). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-25T15:09:03Z. **Code review location:** `crates/victory-gpu/src/texture.rs`; line 345 ~~~~diff @@ -331,9 +331,20 @@ impl Staging { } // Read through our safe copy: the title may unmap the pages // (evicting a texture a big table lists) while we read them. - SCRATCH.with_borrow_mut(|tiled| { - tiled.resize(size, 0); + SCRATCH.with_borrow_mut(|scratch| { + // As in `with_tiled_copy`: the length stays, the copy writes all + // of `size` (zeros for what cannot be read), so only a bigger + // texture than any before is zeroed. + if size > scratch.len() { + scratch.resize(size, 0); + } + let tiled = &mut scratch[..size]; crate::host::copy_mapped(tiled.as_mut_ptr(), self.memory, size, false); + // The diagnostics mode without copies writes nothing: it hashes zeros, as before. + #[cfg(feature = "diagnostics")] + if crate::copystat::mode() == 2 { ~~~~ --- Could a diagnostics-mode regression test prefill the retained scratch buffer with nonzero bytes, run a no-copy refresh, and verify that the hash sees zeros? That explicit fill preserves the old behavior after scratch stops being resized to zero on every refresh. AI-assisted review; I checked the diagnostics-only zero-fill and scratch reuse path.
romainhedouin commented 2026-11-26 22:24:32 +00:00 (Migrated from github.com)

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


Closing: on a-publier the texture read buffer already only grows (with_read, texture.rs:433), and the placed-texture set this PR reused doesn't exist.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7204650646 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/63#issuecomment-7204650646). Originally posted by [romainhedouin](https://github.com/romainhedouin) on 2026-11-26T22:24:32Z. --- Closing: on `a-publier` the texture read buffer already only grows (`with_read`, texture.rs:433), and the placed-texture set this PR reused doesn't exist.

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