GPU: speed up unwatched texture refresh with fast 128-bit digest and remap-epoch caching (fix on fork) #916

Open
opened 2026-11-26 04:14:09 +00:00 by github-bot · 6 comments
Owner

@⁠alessiodeia on GitHub · 2026-11-24 14:05 UTC · original

Problem

In world frames (measured on Windows 11 + RTX 2070 SUPER, retail 1.0.2.1, diagnostics build), Staging::refresh_now() spends 308–416 ms/frame (average ~380 ms/frame) checking 150–156.5 MB/frame of unwatched textures in physical-page views where 0.0 MB/frame actually changed:

ext 3814.5 preparing: copies 3359.1 ms 173.0 MB, textures 383.2 ms 156.5 MB textures 0.0 MB watched and changed, 156.5 MB hashed, 0.0 MB of them changed

Three avoidable costs compound on every submission (~19 submissions/frame):

  1. Repeated failing GetWriteWatch calls: unlike Shadow, Staging did not cache NOT_WATCHED, so every submission acquired the global JOURNAL mutex and issued a failing GetWriteWatch syscall for every physical-page-view texture.
  2. Repeated VirtualQuery walks: committed() walked every 64 KiB mapped view region on every submission, even when no physical-page views had been mapped or unmapped since the previous check.
  3. Cryptographic hashing: blake3::hash(tiled) ran over ~156 MB/frame of tiled texture bytes (~2.24 GB/s) solely for change detection.

Fix

Branch: alessiodeia/VIctory@perf/fast-texture-content-hash (commit 8cb0222, cleanly based on main ffe310e)
https://github.com/alessiodeia/VIctory/tree/perf/fast-texture-content-hash

  1. crates/victory-gpu/src/watch.rs:
    • Added REMAP_EPOCH: AtomicU64 incremented in title_memory_remapped() and exposed via pub(crate) fn remap_epoch() -> u64.
  2. crates/victory-gpu/src/texture.rs:
    • Added watched: AtomicU8 (NOT_WATCHED) to Staging, only cached after committed_kind() confirms the range is committed (so initially-unmapped write-watched textures are never misclassified).
    • Added committed_kind() returning Committed::{No, Mapped, Other}. Only MEM_MAPPED ranges cache committed_epoch (since all physical-page view unmaps go through title_memory_remapped()); MEM_PRIVATE ranges continue to call VirtualQuery. Both committed_epoch and watched invalidate immediately when remap_epoch() advances.
    • Replaced blake3::hash with a zero-dependency 4-lane u64 128-bit non-cryptographic digest (as_chunks::<32>() + SplitMix64 finalizer). Every byte is still checked on every submission, preserving identical correctness for write_physical / DirectStorage writes.
  3. Tests & Benchmark:
    • texture::tests::content_hash_detects_single_bit_tail_and_order_changes
    • texture::tests::committed_kind_distinguishes_mapped_and_private_and_remap_advances_epoch
    • texture::tests::content_hash_vs_blake3_throughput (#[ignore = "benchmark"])

Verification (Windows 11)

  • Throughput benchmark (160 MiB, release):
    blake3 = 71.43 ms (2.24 GB/s) -> content_hash = 11.62 ms (13.77 GB/s) (6.1x faster on hashing alone, plus elimination of per-submission GetWriteWatch + VirtualQuery overhead on MEM_MAPPED views)
  • cargo test -p victory-gpu --locked --offline -- --test-threads=1: 52 passed, 0 failed (2 ignored benchmarks)
  • cargo clippy -p victory-gpu --all-targets -- -D warnings (default & --features diagnostics): 0 warnings
  • cargo --offline xtask policy: 154 files ok
**@⁠alessiodeia** on GitHub · 2026-11-24 14:05 UTC · [original](https://github.com/Marvisak/VIctory/issues/916) ### Problem In world frames (measured on Windows 11 + RTX 2070 SUPER, retail 1.0.2.1, diagnostics build), `Staging::refresh_now()` spends **308–416 ms/frame** (average **~380 ms/frame**) checking **150–156.5 MB/frame** of unwatched textures in physical-page views where **0.0 MB/frame** actually changed: ` ext 3814.5 preparing: copies 3359.1 ms 173.0 MB, textures 383.2 ms 156.5 MB textures 0.0 MB watched and changed, 156.5 MB hashed, 0.0 MB of them changed ` Three avoidable costs compound on every submission (~19 submissions/frame): 1. **Repeated failing `GetWriteWatch` calls:** unlike `Shadow`, `Staging` did not cache `NOT_WATCHED`, so every submission acquired the global `JOURNAL` mutex and issued a failing `GetWriteWatch` syscall for every physical-page-view texture. 2. **Repeated `VirtualQuery` walks:** `committed()` walked every 64 KiB mapped view region on every submission, even when no physical-page views had been mapped or unmapped since the previous check. 3. **Cryptographic hashing:** `blake3::hash(tiled)` ran over ~156 MB/frame of tiled texture bytes (~2.24 GB/s) solely for change detection. ### Fix Branch: `alessiodeia/VIctory@perf/fast-texture-content-hash` (commit `8cb0222`, cleanly based on `main` `ffe310e`) https://github.com/alessiodeia/VIctory/tree/perf/fast-texture-content-hash 1. **`crates/victory-gpu/src/watch.rs`:** - Added `REMAP_EPOCH: AtomicU64` incremented in `title_memory_remapped()` and exposed via `pub(crate) fn remap_epoch() -> u64`. 2. **`crates/victory-gpu/src/texture.rs`:** - Added `watched: AtomicU8` (`NOT_WATCHED`) to `Staging`, only cached after `committed_kind()` confirms the range is committed (so initially-unmapped write-watched textures are never misclassified). - Added `committed_kind()` returning `Committed::{No, Mapped, Other}`. Only `MEM_MAPPED` ranges cache `committed_epoch` (since all physical-page view unmaps go through `title_memory_remapped()`); `MEM_PRIVATE` ranges continue to call `VirtualQuery`. Both `committed_epoch` and `watched` invalidate immediately when `remap_epoch()` advances. - Replaced `blake3::hash` with a zero-dependency 4-lane `u64` 128-bit non-cryptographic digest (`as_chunks::<32>()` + SplitMix64 finalizer). Every byte is still checked on every submission, preserving identical correctness for `write_physical` / DirectStorage writes. 3. **Tests & Benchmark:** - `texture::tests::content_hash_detects_single_bit_tail_and_order_changes` - `texture::tests::committed_kind_distinguishes_mapped_and_private_and_remap_advances_epoch` - `texture::tests::content_hash_vs_blake3_throughput` (`#[ignore = "benchmark"]`) ### Verification (Windows 11) - **Throughput benchmark (160 MiB, release):** `blake3 = 71.43 ms (2.24 GB/s)` -> `content_hash = 11.62 ms (13.77 GB/s)` (**6.1x faster** on hashing alone, plus elimination of per-submission `GetWriteWatch` + `VirtualQuery` overhead on `MEM_MAPPED` views) - `cargo test -p victory-gpu --locked --offline -- --test-threads=1`: **52 passed, 0 failed** (2 ignored benchmarks) - `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default & `--features diagnostics`): **0 warnings** - `cargo --offline xtask policy`: **154 files ok**
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:09 +00:00
Author
Owner

@⁠pickles976 on GitHub · 2026-11-24 14:07 UTC · original

Two cross-project data points from the sibling runtime (ivall/xcl), where the same pipeline is open bugs and open benchmarks today — worth a look before this lands, because our #504 is precisely the trap this design can step on:\n\n1. The epoch/hash must not skip remaps. Our content-hash cache (#504, open) has three defects from the same instinct: it skips textures whose pages were remapped (data moves under a stable VA — a pure epoch cache keyed on the VAs content misses exactly this, since nobody wrote through the VA you are digesting), double-reads changed ones (hash pass + read pass over a multi-GB subresource), and caches failures (a failed upload never retries). If the "remap-epoch" is bumped only by your commit/remap paths, check the case where the title maps over a range your epoch did not observe.\n2. Digest speed is measurable and build-sensitive. Our thread (#175) has today: a release cross-build hashing at ~13 GB/s vs 2.65 GB/s on another box traced to a debug-profile build, and a same-speed repro on ntfs3 vs ntfs-3g storage — so before crediting the 128-bit digest, pin (a) release profile, (b) where the source pages live, (c) huge-page state. And a partial-detile design (#915) composes with yours: detile-per-changed-page only pays off if the changed-set is exact, which is the same correctness bar as (1) — false negatives there are corrupted textures, a much worse failure than slow loads.\n\nPosted per the shared-ledger practice; our measurements are in #175 if useful.

**@⁠pickles976** on GitHub · 2026-11-24 14:07 UTC · [original](https://github.com/Marvisak/VIctory/issues/916#issuecomment-7133881913) Two cross-project data points from the sibling runtime (ivall/xcl), where the same pipeline is open bugs and open benchmarks today — worth a look before this lands, because our #504 is precisely the trap this design can step on:\n\n1. **The epoch/hash must not skip remaps.** Our content-hash cache (#504, open) has three defects from the same instinct: it **skips textures whose pages were remapped** (data moves under a stable VA — a pure epoch cache keyed on the *VAs* content misses exactly this, since nobody wrote through the VA you are digesting), **double-reads changed ones** (hash pass + read pass over a multi-GB subresource), and **caches failures** (a failed upload never retries). If the "remap-epoch" is bumped only by your commit/remap paths, check the case where the *title* maps over a range your epoch did not observe.\n2. **Digest speed is measurable and build-sensitive.** Our thread (#175) has today: a release cross-build hashing at ~13 GB/s vs 2.65 GB/s on another box traced to a **debug-profile build**, and a same-speed repro on ntfs3 vs ntfs-3g storage — so before crediting the 128-bit digest, pin (a) release profile, (b) where the source pages live, (c) huge-page state. And a partial-detile design (#915) composes with yours: detile-per-changed-page only pays off if the changed-set is exact, which is the same correctness bar as (1) — false negatives there are corrupted textures, a much worse failure than slow loads.\n\nPosted per the shared-ledger practice; our measurements are in #175 if useful.
Author
Owner

@⁠alessiodeia on GitHub · 2026-11-24 14:23 UTC · original

Thanks @⁠pickles976, both points checked against 8cb0222:

1. Remaps under a stable VA. The epoch caches only two facts, never content: that a MEM_MAPPED range is committed, and that it is not write-watched. The digest itself still runs over the full texture range through the VA on every refresh that reaches the hash path, so pages remapped under an unchanged VA produce a new digest and a re-detile. On your three #504 defects:

  • Skipped remaps: not possible here, since nothing skips the digest based on the epoch. The epoch only lets us skip VirtualQuery and watch::writes. Every title mapping change goes through xmem::map/xmem::unmap, and both call title_memory_remapped, which bumps the epoch. Ranges that are not all MEM_MAPPED (Committed::Other) never cache their commit state and are re-queried on every refresh.
  • Double reads: unchanged from main. One digest pass, then a detile read only when the digest differs.
  • Cached failures: an uncommitted range returns early without touching committed_epoch or hash, so the next refresh retries.

2. Build sensitivity. Re-run pinned on Windows 11, RTX 2070 SUPER host, --release (opt-level 3), source: a 160 MiB private heap buffer (not huge pages, not a file view): blake3 87.5 ms (1.83 GB/s), new digest 11.4 ms (14.0 GB/s), 7.7×. The earlier 6.1× was the same benchmark under the default test profile. In the title, the gain is bounded by the 156.5 MB/frame we digest, so it is about 383 ms → ~50 ms/frame on this box.

Agreed that a false negative in the changed set would be far worse than slowness, which is why this change keeps the full-range digest instead of a page-granular skip. A per-page scheme like #915 would need the exact-changed-set guarantee you describe.

**@⁠alessiodeia** on GitHub · 2026-11-24 14:23 UTC · [original](https://github.com/Marvisak/VIctory/issues/916#issuecomment-7134301537) Thanks @⁠pickles976, both points checked against `8cb0222`: **1. Remaps under a stable VA.** The epoch caches only two facts, never content: that a `MEM_MAPPED` range is committed, and that it is not write-watched. The digest itself still runs over the full texture range through the VA on every refresh that reaches the hash path, so pages remapped under an unchanged VA produce a new digest and a re-detile. On your three #504 defects: - *Skipped remaps:* not possible here, since nothing skips the digest based on the epoch. The epoch only lets us skip `VirtualQuery` and `watch::writes`. Every title mapping change goes through `xmem::map`/`xmem::unmap`, and both call `title_memory_remapped`, which bumps the epoch. Ranges that are not all `MEM_MAPPED` (`Committed::Other`) never cache their commit state and are re-queried on every refresh. - *Double reads:* unchanged from `main`. One digest pass, then a detile read only when the digest differs. - *Cached failures:* an uncommitted range returns early without touching `committed_epoch` or `hash`, so the next refresh retries. **2. Build sensitivity.** Re-run pinned on Windows 11, RTX 2070 SUPER host, `--release` (opt-level 3), source: a 160 MiB private heap buffer (not huge pages, not a file view): `blake3` 87.5 ms (1.83 GB/s), new digest 11.4 ms (14.0 GB/s), **7.7×**. The earlier 6.1× was the same benchmark under the default test profile. In the title, the gain is bounded by the 156.5 MB/frame we digest, so it is about 383 ms → ~50 ms/frame on this box. Agreed that a false negative in the changed set would be far worse than slowness, which is why this change keeps the full-range digest instead of a page-granular skip. A per-page scheme like #915 would need the exact-changed-set guarantee you describe.
Author
Owner

@⁠DrDarkDK on GitHub · 2026-11-24 14:27 UTC · original

The stable-address replacement worked in my test on 8cb0222 too. The staging texel picked up the new value on both WARP and RTX 4070 Ti.

I found a separate issue with the cached mapping check. xmem::unmap advances the epoch before Windows removes the view. If a texture refresh runs between those steps, it can cache the still-mapped view under the new epoch. After the unmap, Windows reports the address as unmapped while staging still treats the cached mapping as current. I confirmed that mismatch on both adapters. In the control, a notification after the unmap made the next refresh skip the range.

AI-assisted. Synthetic Windows test only, with no world-frame measurements.

**@⁠DrDarkDK** on GitHub · 2026-11-24 14:27 UTC · [original](https://github.com/Marvisak/VIctory/issues/916#issuecomment-7134407302) The stable-address replacement worked in my test on `8cb0222` too. The staging texel picked up the new value on both WARP and RTX 4070 Ti. I found a separate issue with the cached mapping check. `xmem::unmap` advances the epoch before Windows removes the view. If a texture refresh runs between those steps, it can cache the still-mapped view under the new epoch. After the unmap, Windows reports the address as unmapped while staging still treats the cached mapping as current. I confirmed that mismatch on both adapters. In the control, a notification after the unmap made the next refresh skip the range. AI-assisted. Synthetic Windows test only, with no world-frame measurements.
Author
Owner

@⁠alessiodeia on GitHub · 2026-11-24 16:52 UTC · original

Thanks @⁠DrDarkDK, you're right, and thanks for the control run. xmem::unmap notifies before UnmapViewOfFile2, so a refresh in between caches the still-mapped view under the new epoch.

Fixed in alessiodeia/VIctory@e2fba46 on perf/fast-texture-content-hash. A new title_memory_unmapped advances the epoch once more after the view is gone, and xmem::unmap calls it right after UnmapViewOfFile2. The existing call before the unmap stays because it also clears guards. A mapping cached in that window no longer matches the epoch, so the next refresh asks VirtualQuery again and skips the range. committed_kind_distinguishes_mapped_and_private_and_remap_advances_epoch now walks your order: notify, still Mapped, unmap, No, notify again, epoch differs from the cached one.

One window remains, and it is not new: a refresh that has already passed the check while another thread unmaps. main's committed() followed by the read has the same gap. Closing it would take the unmap and the refresh under one lock, which seems out of scope here.

Verified on Windows 11 with an RTX 2070 SUPER: cargo test -p victory-gpu 52 passed, 2 ignored; cargo test -p victory-hle xmem 10 passed; workspace clippy -D warnings clean, default and --all-features; cargo xtask policy ok.

**@⁠alessiodeia** on GitHub · 2026-11-24 16:52 UTC · [original](https://github.com/Marvisak/VIctory/issues/916#issuecomment-7138345822) Thanks @⁠DrDarkDK, you're right, and thanks for the control run. `xmem::unmap` notifies before `UnmapViewOfFile2`, so a refresh in between caches the still-mapped view under the new epoch. Fixed in `alessiodeia/VIctory@e2fba46` on `perf/fast-texture-content-hash`. A new `title_memory_unmapped` advances the epoch once more after the view is gone, and `xmem::unmap` calls it right after `UnmapViewOfFile2`. The existing call before the unmap stays because it also clears guards. A mapping cached in that window no longer matches the epoch, so the next refresh asks `VirtualQuery` again and skips the range. `committed_kind_distinguishes_mapped_and_private_and_remap_advances_epoch` now walks your order: notify, still `Mapped`, unmap, `No`, notify again, epoch differs from the cached one. One window remains, and it is not new: a refresh that has already passed the check while another thread unmaps. `main`'s `committed()` followed by the read has the same gap. Closing it would take the unmap and the refresh under one lock, which seems out of scope here. Verified on Windows 11 with an RTX 2070 SUPER: `cargo test -p victory-gpu` 52 passed, 2 ignored; `cargo test -p victory-hle xmem` 10 passed; workspace clippy `-D warnings` clean, default and `--all-features`; `cargo xtask policy` ok.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:56 UTC · original · edited 2026-11-25 09:56 UTC

Automated triage for the maintainers.

The change is now open as PR #2283, carried onto current main (940d32f) with @⁠DrDarkDK's unmap fix (title_memory_unmapped advances the epoch after UnmapViewOfFile2) and a test for it. Review continues there.

On main, Staging::refresh_now in crates/victory-gpu/src/texture.rs still calls committed and hashes an untracked texture's bytes with blake3::hash on every refresh. So the faster digest and the per-epoch commit check from this issue still apply. Whether a non-cryptographic digest is acceptable for change detection is for the maintainers to decide.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:56 UTC · [original](https://github.com/Marvisak/VIctory/issues/916#issuecomment-7155325991) · edited 2026-11-25 09:56 UTC <!-- victory-issue-triage v1 verdict=tracked seen=2026-11-25T09:40:24Z at=2026-11-25T09:50:38Z --> Automated triage for the maintainers. The change is now open as PR #2283, carried onto current `main` (`940d32f`) with @⁠DrDarkDK's unmap fix (`title_memory_unmapped` advances the epoch after `UnmapViewOfFile2`) and a test for it. Review continues there. On `main`, `Staging::refresh_now` in `crates/victory-gpu/src/texture.rs` still calls `committed` and hashes an untracked texture's bytes with `blake3::hash` on every refresh. So the faster digest and the per-epoch commit check from this issue still apply. Whether a non-cryptographic digest is acceptable for change detection is for the maintainers to decide. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠alessiodeia on GitHub · 2026-11-25 09:40 UTC · original

Opened as #2283, carried onto current main (940d32f). It includes @⁠DrDarkDK's unmap fix (title_memory_unmapped after UnmapViewOfFile2) with a test that fails without it. On main the tracked/untracked choice is already made per texture, so only the digest and the per-epoch commit check are left from this issue.

**@⁠alessiodeia** on GitHub · 2026-11-25 09:40 UTC · [original](https://github.com/Marvisak/VIctory/issues/916#issuecomment-7158646934) Opened as #2283, carried onto current `main` (`940d32f`). It includes @⁠DrDarkDK's unmap fix (`title_memory_unmapped` after `UnmapViewOfFile2`) with a test that fails without it. On `main` the tracked/untracked choice is already made per texture, so only the digest and the per-epoch commit check are left from this issue.
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#916
No description provided.