fix(gpu): expire hot textures by submission serial instead of probe count #252

Merged
Sheffy007 merged 1 commit from upstream-pr/252 into main 2026-11-27 13:35:46 +00:00
Sheffy007 commented 2026-11-27 05:18:46 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T05:18:46Z.


Summary

  • Track hot-texture lifetime by submission serial (Title::sync_serial and Tracked::hot_until = sync_serial + HOT) instead of decrementing Tracked::hot only when probe_round visits a texture (#179, #15). When world streaming puts ~18,900 textures into hot_keys and PROBE_BUDGET visits ~60 per submission, unchanged streamed textures previously stayed hot for 600 * (18900 / 60) submissions (> 1 hour) instead of 600 submissions.
  • Evict entries whose hot_until < sync_serial in probe_round without calling probe_hash (mach_vm_read_overwrite) or consuming MIN_SYNC_HOT / PROBE_BUDGET probes.
  • Fix probe_round's wraparound removal (wrapped prefix swap before swap_remove) so removing an entry after wrapping from cursor..len into 0..start never pulls an already-visited suffix entry back into the unvisited prefix or skips an unvisited entry.
  • Add native::texture_tests::hot_textures_cool_by_submissions_even_when_probe_round_is_capped and document the change in docs/native-host.md.

Verification

  • cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5521626890 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/252). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T05:18:46Z. --- ## Summary - Track hot-texture lifetime by submission serial (`Title::sync_serial` and `Tracked::hot_until = sync_serial + HOT`) instead of decrementing `Tracked::hot` only when `probe_round` visits a texture (`#179`, `#15`). When world streaming puts ~18,900 textures into `hot_keys` and `PROBE_BUDGET` visits ~60 per submission, unchanged streamed textures previously stayed hot for `600 * (18900 / 60)` submissions (> 1 hour) instead of 600 submissions. - Evict entries whose `hot_until < sync_serial` in `probe_round` without calling `probe_hash` (`mach_vm_read_overwrite`) or consuming `MIN_SYNC_HOT` / `PROBE_BUDGET` probes. - Fix `probe_round`'s wraparound removal (`wrapped` prefix swap before `swap_remove`) so removing an entry after wrapping from `cursor..len` into `0..start` never pulls an already-visited suffix entry back into the unvisited prefix or skips an unvisited entry. - Add `native::texture_tests::hot_textures_cool_by_submissions_even_when_probe_round_is_capped` and document the change in `docs/native-host.md`. ## Verification - `cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask` - `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo xtask policy`
LakshanSivakumar commented 2026-11-27 10:32:10 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T10:32:10Z.


I had not reviewed this one yet. Before it can merge:

  1. Please update the branch on main.
  2. It changes how long a streamed texture stays probed (by submission serial, not by visits). Please say what you measured in the world, before and after: probes per submission, the time of the texture sync, and the frame rate if you have it.
  3. The removal of an entry after the cursor wrapped (swap with start, then swap_remove) is the delicate part and has one test. Please add a test where an entry is removed after the wrap, and say what keeps an entry that was already visited from being skipped or visited twice.

I have not run it.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7216001216 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/252#issuecomment-7216001216). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T10:32:10Z. --- I had not reviewed this one yet. Before it can merge: 1. Please update the branch on main. 2. It changes how long a streamed texture stays probed (by submission serial, not by visits). Please say what you measured in the world, before and after: probes per submission, the time of the texture sync, and the frame rate if you have it. 3. The removal of an entry after the cursor wrapped (swap with `start`, then `swap_remove`) is the delicate part and has one test. Please add a test where an entry is removed after the wrap, and say what keeps an entry that was already visited from being skipped or visited twice. I have not run it.
Sheffy007 commented 2026-11-27 11:53:07 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T11:53:07Z.


Addressed all three points:

  1. Rebased onto main (e2f211a8): Updated cleanly onto current main.
  2. Measurements (synthetic --release benchmark vs world run):
    • I do not have game files locally, so I have not measured an in-game world frame rate with this change (the 18,900 hot_keys figure came from VICTORY_TRACE_TEXTURE_SYNC=1 world logs in #179).
    • Added native::textures::tests::bench_hot_texture_serial_cooling_under_probe_budget (--release -- --ignored --nocapture on Apple M5 Pro, 48 GB, macOS 27.2.1) over 5,000 hot 64 KiB textures across 800 submissions under the 2 ms PROBE_BUDGET:
      • Submissions 1..=600: 2.003 ms/submission (~200–250 probes/submission, capped by PROBE_BUDGET).
      • After HOT = 600 submissions (601..=800): expired entries are evicted in a single pass without reading memory, dropping hot_keys to 0 and hot-probe lock hold time from 2.003 ms/submission to 0.0004 ms/submission (at ~30 submissions per world frame, ~60 ms/frame of host-lock time freed once a streamed wave ages past 600 submissions, whereas per-visit decrement kept hot_keys at 5,000 for ~15,000 submissions).
  3. Wraparound removal invariant & tests (probe_round_wraparound_removals_visit_each_live_entry_once):
    • Why no visited entry is skipped or visited twice: probe_round maintains a 3-way partition of keys:
      • Before wrap (!wrapped, start <= idx <= keys.len()): keys[0..start] is unvisited, keys[start..idx] was visited and kept, and keys[idx..keys.len()] is unvisited (swap_remove(idx) pulls an unvisited tail element into idx, keeping keys.len() >= start).
      • After wrap (wrapped, 0 <= idx <= start <= keys.len()): keys[0..idx] was visited after wrap, keys[idx..start] is unvisited, and keys[start..keys.len()] was visited before wrap. When keys[idx] (idx < start) is removed, *start -= 1 names the last unvisited slot (idx <= *start), keys.swap(idx, *start) moves that unvisited entry into idx (or hits idx == *start and exits at idx >= start) while placing the removed entry at *start, and keys.swap_remove(*start) fills *start from keys.len() - 1 (an already-visited pre-wrap survivor, or pops *start itself when *start == keys.len() - 1). Both visited slices [0..idx] and [*start..keys.len()] remain strictly disjoint from keys[idx..*start].
    • Added native::textures::tests::probe_round_wraparound_removals_visit_each_live_entry_once, testing both an explicit post-wrap removal trace (cursor = 4 on [0..6], removing entries at indices 0 and 2 after wraparound) and an exhaustive sweep of all 6 start cursors across all 3^6 = 729 combinations of (already expired, expires after probe, stays hot) (4,374 schedules), asserting via Tracked::skipped that every live entry is visited once and only once and every expired entry is evicted with 0 probes.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7217586668 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/252#issuecomment-7217586668). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T11:53:07Z. --- Addressed all three points: 1. **Rebased onto `main` (`e2f211a8`):** Updated cleanly onto current `main`. 2. **Measurements (synthetic `--release` benchmark vs world run):** - I do not have game files locally, so I have not measured an in-game world frame rate with this change (the 18,900 `hot_keys` figure came from `VICTORY_TRACE_TEXTURE_SYNC=1` world logs in #179). - Added `native::textures::tests::bench_hot_texture_serial_cooling_under_probe_budget` (`--release -- --ignored --nocapture` on Apple M5 Pro, 48 GB, macOS 27.2.1) over `5,000` hot `64 KiB` textures across `800` submissions under the `2 ms` `PROBE_BUDGET`: - **Submissions `1..=600`:** `2.003 ms/submission` (~200–250 probes/submission, capped by `PROBE_BUDGET`). - **After `HOT = 600` submissions (`601..=800`):** expired entries are evicted in a single pass without reading memory, dropping `hot_keys` to **`0`** and hot-probe lock hold time from **`2.003 ms/submission` to `0.0004 ms/submission`** (at ~30 submissions per world frame, ~`60 ms/frame` of host-lock time freed once a streamed wave ages past 600 submissions, whereas per-visit decrement kept `hot_keys` at `5,000` for ~`15,000` submissions). 3. **Wraparound removal invariant & tests (`probe_round_wraparound_removals_visit_each_live_entry_once`):** - **Why no visited entry is skipped or visited twice:** `probe_round` maintains a 3-way partition of `keys`: - *Before wrap (`!wrapped`, `start <= idx <= keys.len()`):* `keys[0..start]` is unvisited, `keys[start..idx]` was visited and kept, and `keys[idx..keys.len()]` is unvisited (`swap_remove(idx)` pulls an unvisited tail element into `idx`, keeping `keys.len() >= start`). - *After wrap (`wrapped`, `0 <= idx <= start <= keys.len()`):* `keys[0..idx]` was visited after wrap, `keys[idx..start]` is unvisited, and `keys[start..keys.len()]` was visited before wrap. When `keys[idx]` (`idx < start`) is removed, `*start -= 1` names the last **unvisited** slot (`idx <= *start`), `keys.swap(idx, *start)` moves that unvisited entry into `idx` (or hits `idx == *start` and exits at `idx >= start`) while placing the removed entry at `*start`, and `keys.swap_remove(*start)` fills `*start` from `keys.len() - 1` (an **already-visited** pre-wrap survivor, or pops `*start` itself when `*start == keys.len() - 1`). Both visited slices `[0..idx]` and `[*start..keys.len()]` remain strictly disjoint from `keys[idx..*start]`. - Added `native::textures::tests::probe_round_wraparound_removals_visit_each_live_entry_once`, testing both an explicit post-wrap removal trace (`cursor = 4` on `[0..6]`, removing entries at indices `0` and `2` after wraparound) and an exhaustive sweep of all `6` start cursors across all `3^6 = 729` combinations of `(already expired, expires after probe, stays hot)` (`4,374` schedules), asserting via `Tracked::skipped` that every live entry is visited **once and only once** and every expired entry is evicted with **0** probes.
Olima34 commented 2026-11-27 13:06:06 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T13:06:06Z.


Check on a2cd4625 merged onto main c5d7f4d0 (M5 Pro 48 GB, arm64, with the converter): clippy, policy and victory-shader pass. In victory-gpu, the new native::tests::texture_tests::hot_textures_cool_by_submissions_even_when_probe_round_is_capped failed once in three full runs of the lib tests, at texture_tests.rs:349 (uploads_of(refreshed) == 4, 'still-hot texture is probed on the next submission'). It passes alone, and the other two full runs passed (330 passed). That looks like the order-or-timing flake of #307: with a zero budget the round stops after MIN_SYNC_HOT probes, so whether refreshed is among them depends on the cursor and on other tests' hot textures. @Sheffy007, could you make that assertion independent of the cursor, e.g. by giving the last sync no deadline or by checking the textures probed? I'm holding the merge until then. I'm also including this PR in a game run of the GPU PRs and will post that result here.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7219057305 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/252#issuecomment-7219057305). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T13:06:06Z. --- Check on `a2cd4625` merged onto main `c5d7f4d0` (M5 Pro 48 GB, arm64, with the converter): clippy, policy and victory-shader pass. In victory-gpu, the new `native::tests::texture_tests::hot_textures_cool_by_submissions_even_when_probe_round_is_capped` failed once in three full runs of the lib tests, at `texture_tests.rs:349` (`uploads_of(refreshed) == 4`, 'still-hot texture is probed on the next submission'). It passes alone, and the other two full runs passed (330 passed). That looks like the order-or-timing flake of #307: with a zero budget the round stops after `MIN_SYNC_HOT` probes, so whether `refreshed` is among them depends on the cursor and on other tests' hot textures. @Sheffy007, could you make that assertion independent of the cursor, e.g. by giving the last sync no deadline or by checking the textures probed? I'm holding the merge until then. I'm also including this PR in a game run of the GPU PRs and will post that result here.
Sheffy007 commented 2026-11-27 13:43:38 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T13:43:38Z.


Rebased onto main (60baf130 -> 09448b26) and replaced the two post-loop Duration::ZERO checks in hot_textures_cool_by_submissions_even_when_probe_round_is_capped with sync_dirty_without_probe_deadline() (keeping the HOT loop steps capped at Duration::ZERO), so WATCH dirty entries from earlier tests can never stop probe_round at MIN_SYNC_HOT before reaching keys / refreshed. Verified across 3 consecutive full victory-gpu test runs + Windows clippy + policy. @Olima34

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7219868955 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/252#issuecomment-7219868955). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T13:43:38Z. --- Rebased onto `main` (`60baf130` -> `09448b26`) and replaced the two post-loop `Duration::ZERO` checks in `hot_textures_cool_by_submissions_even_when_probe_round_is_capped` with `sync_dirty_without_probe_deadline()` (keeping the `HOT` loop steps capped at `Duration::ZERO`), so `WATCH` dirty entries from earlier tests can never stop `probe_round` at `MIN_SYNC_HOT` before reaching `keys` / `refreshed`. Verified across 3 consecutive full `victory-gpu` test runs + Windows clippy + policy. @Olima34
Olima34 commented 2026-11-27 13:48:48 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T13:48:48Z.


@Sheffy007 thanks. The maintainer had already merged this (329d0f8f) with his own fix of the test's last syncs (c8bdf4dc), so I checked main b639c175 instead: cargo test -p victory-gpu --lib passes in 3 runs out of 3 (357 passed each), and Windows clippy and cargo xtask policy pass. The flake is gone. No game run: world runs currently hang the GPU on this Mac (#79).

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7219975041 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/252#issuecomment-7219975041). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T13:48:48Z. --- @Sheffy007 thanks. The maintainer had already merged this (329d0f8f) with his own fix of the test's last syncs (c8bdf4dc), so I checked main `b639c175` instead: `cargo test -p victory-gpu --lib` passes in 3 runs out of 3 (357 passed each), and Windows clippy and `cargo xtask policy` pass. The flake is gone. No game run: world runs currently hang the GPU on this Mac (#79).
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
LakshanSivakumar/Native-Victory-macOS!252
No description provided.