GPU: preparing a view heap's page table scans the change tracker a word at a time #3246

Open
borosf wants to merge 3 commits from upstream-pr/3248 into main
borosf commented 2026-11-26 14:29:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T14:29:09Z.


memory::prepare ran once per list binding a title-memory view heap, per submission, and asked watch::clean_bit once per copy the layout names.

The shape of it

A world submission carries tens of lists; each list's Step::Memory calls prepare, which loops over l.copies.iter().zip(&l.bits) — every copy the heap's page table names, about 4,700 in the world, nearly all clean — doing one acquire-load plus shift/test per copy. That is hundreds of thousands of single-bit loads per frame for a handful of answers.

What changes

The layout now stores its copies' tracker bits as (bit, copy_index) pairs ordered by bit, plus the first and last tracker word they span (built once in rewrite). dirty_indices walks the tracker a word at a time (dirty_word + trailing_zeros) over that span and returns only the changed copies' indices, merged against the sorted pairs.

  • Cost per list per submission: from ~4,700 loads to the ~74 words 4,700 contiguous bits span — ~19 µs → ~0.3 µs at 4 ns a load.
  • The set of copies handed to add is exactly what the per-copy loop found: same bits, same order. Bits belonging to other layouts that share a tracker word are skipped, which the new test pins down.

watch gains dirty_word (the word-level read of the same DIRTY array clean_bit reads); clean_bit itself is untouched.

Verification

  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings — clean; --all-features too.
  • cargo test -p victory-gpu --lib --locked --no-fail-fast — 231 passed, 2 failed, both the known Intel baseline failures pristine main also fails (descriptor::host::tests::dynamic_slots_are_held_until_released, memory::tests::lists_share_a_table_and_copies_keep_their_views, #1744/#1829).
  • New test memory::tests::the_dirty_scan_finds_each_changed_copy_once_in_tracker_order: tracker order, an all-clean tracker, foreign bits in the same words, an empty layout.
  • rustfmt --edition 2024 --check hunk counts against main: memory.rs 30/30, watch.rs 39/39.

The frame line's StepMemoryUs counter already totals this step per frame, so a world run with this merged shows the delta directly; this PR claims the per-list arithmetic, not a frame-time number.

Synthetic tests only; no game data.

Part of the fifteen gaps claimed for this sweep on #1633. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5511847340 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3248). Originally posted by [borosf](https://github.com/borosf) on 2026-11-26T14:29:09Z. --- `memory::prepare` ran once per list binding a title-memory view heap, per submission, and asked `watch::clean_bit` once per copy the layout names. ## The shape of it A world submission carries tens of lists; each list's `Step::Memory` calls `prepare`, which loops over `l.copies.iter().zip(&l.bits)` — every copy the heap's page table names, about **4,700 in the world**, nearly all clean — doing one acquire-load plus shift/test per copy. That is hundreds of thousands of single-bit loads per frame for a handful of answers. ## What changes The layout now stores its copies' tracker bits as `(bit, copy_index)` pairs ordered by bit, plus the first and last tracker word they span (built once in `rewrite`). `dirty_indices` walks the tracker **a word at a time** (`dirty_word` + `trailing_zeros`) over that span and returns only the changed copies' indices, merged against the sorted pairs. - Cost per list per submission: from ~4,700 loads to the ~74 words 4,700 contiguous bits span — **~19 µs → ~0.3 µs** at 4 ns a load. - The set of copies handed to `add` is exactly what the per-copy loop found: same bits, same order. Bits belonging to other layouts that share a tracker word are skipped, which the new test pins down. `watch` gains `dirty_word` (the word-level read of the same `DIRTY` array `clean_bit` reads); `clean_bit` itself is untouched. ## Verification - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` — clean; `--all-features` too. - `cargo test -p victory-gpu --lib --locked --no-fail-fast` — 231 passed, 2 failed, both the known Intel baseline failures pristine `main` also fails (`descriptor::host::tests::dynamic_slots_are_held_until_released`, `memory::tests::lists_share_a_table_and_copies_keep_their_views`, #1744/#1829). - New test `memory::tests::the_dirty_scan_finds_each_changed_copy_once_in_tracker_order`: tracker order, an all-clean tracker, foreign bits in the same words, an empty layout. - `rustfmt --edition 2024 --check` hunk counts against `main`: `memory.rs` 30/30, `watch.rs` 39/39. The frame line's `StepMemoryUs` counter already totals this step per frame, so a world run with this merged shows the delta directly; this PR claims the per-list arithmetic, not a frame-time number. Synthetic tests only; no game data. Part of the fifteen gaps claimed for this sweep on #1633. AI-assisted.
Dukestar69 commented 2026-11-26 14:52:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T14:52:10Z.

Review: COMMENTED


Reviewed the diff and ran local verification on Windows 11 x64 (cargo test -p victory-gpu --lib memory::tests:: -- --test-threads=1 --skip lists_share_a_table_and_copies_keep_their_views: 8/8 passed; cargo clippy -p victory-gpu --all-targets -- -D warnings: clean; cargo xtask policy: 191 files ok).

The word-at-a-time bitset scan in dirty_indices is neat, but there is a correctness regression for untracked copies (c.dirty_bit() == None):

  • Previously, prepare() checked !crate::watch::clean_bit(*bit), and clean_bit(None) returns false ("none for memory that is not tracked: never clean" per Shadow::dirty_bit), so untracked copies (or copies created when the tracker's bit pool is full) were always passed to add(...).
  • With by_bit built via .filter_map(|(i, c)| c.dirty_bit().map(|b| (b, i as u32))), any copy with dirty_bit() == None is silently omitted from by_bit and will never be refreshed by memory::prepare() (effectively treated as permanently clean instead of never clean).

Keeping a separate untracked: Vec<u32> on Layout (for indices where c.dirty_bit().is_none()) and iterating it in prepare() preserves the original semantics while keeping the fast word-at-a-time scan for all tracked copies. (Also, an early if by_bit.is_empty() { return out; } in dirty_indices avoids reading word 0 when by_bit is empty.)

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6076207025 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3248#pullrequestreview-6076207025). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T14:52:10Z. **Review:** COMMENTED --- Reviewed the diff and ran local verification on Windows 11 x64 (`cargo test -p victory-gpu --lib memory::tests:: -- --test-threads=1 --skip lists_share_a_table_and_copies_keep_their_views`: 8/8 passed; `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean; `cargo xtask policy`: 191 files ok). The word-at-a-time bitset scan in `dirty_indices` is neat, but there is a correctness regression for **untracked copies (`c.dirty_bit() == None`)**: - Previously, `prepare()` checked `!crate::watch::clean_bit(*bit)`, and `clean_bit(None)` returns `false` ("none for memory that is not tracked: never clean" per `Shadow::dirty_bit`), so untracked copies (or copies created when the tracker's bit pool is full) were **always** passed to `add(...)`. - With `by_bit` built via `.filter_map(|(i, c)| c.dirty_bit().map(|b| (b, i as u32)))`, any copy with `dirty_bit() == None` is silently omitted from `by_bit` and will **never** be refreshed by `memory::prepare()` (effectively treated as permanently clean instead of never clean). Keeping a separate `untracked: Vec<u32>` on `Layout` (for indices where `c.dirty_bit().is_none()`) and iterating it in `prepare()` preserves the original semantics while keeping the fast word-at-a-time scan for all tracked copies. (Also, an early `if by_bit.is_empty() { return out; }` in `dirty_indices` avoids reading word `0` when `by_bit` is empty.) Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
cloph-dsp commented 2026-11-26 15:02:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T14:42:40Z.


Independent local verification of head c1c3302 on Windows 11, Rust 1.100.0/MSVC:

  • cargo test --locked -p victory-gpu --lib -- memory::tests::the_dirty_scan_finds_each_changed_copy_once_in_tracker_order --exact — 1 passed, 0 failed.
  • git diff --check against merge base — clean.

CPU-only synthetic tracker-order test; worktree-local CARGO_TARGET_DIR. AI-assisted (Hy4 flash). GPU/driver N/A. No game data, assets or DRM material used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7195209986 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3248#issuecomment-7195209986). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T14:42:40Z. --- Independent local verification of head `c1c3302` on Windows 11, Rust 1.100.0/MSVC: - `cargo test --locked -p victory-gpu --lib -- memory::tests::the_dirty_scan_finds_each_changed_copy_once_in_tracker_order --exact` — 1 passed, 0 failed. - `git diff --check` against merge base — clean. CPU-only synthetic tracker-order test; worktree-local `CARGO_TARGET_DIR`. AI-assisted (Hy4 flash). GPU/driver N/A. No game data, assets or DRM material used.
Bj0rnis commented 2026-11-26 15:02:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Bj0rnis on 2026-11-26T14:44:34Z.


Verified head c1c33027 on Windows 11 (x64, Rust 1.98.1, NVIDIA GeForce RTX 5070 Ti + WARP):

  • Merge check vs origin/main (63bcca43): clean
  • RTX 5070 Ti (cargo test -p victory-gpu --lib --features diagnostics): 245 passed, 2 failed, 1 ignored (2.96s) — the 2 failures are the pre-existing #1744 NVIDIA 1M view-heap cap failures on main (descriptor::host::tests::dynamic_slots_are_held_until_released, memory::tests::lists_share_a_table_and_copies_keep_their_views); no regressions and the new test passes on hardware
  • WARP (VICTORY_D3D12_WARP=1): 247 passed, 0 failed, 1 ignored (1.89s)
  • Clippy (-p victory-gpu --lib --tests --features diagnostics -- -D warnings): clean (0 warnings)
  • cargo xtask policy: 191 files ok

Not tested: live title workload / frame-prep performance under gameplay.

AI-assisted test run and summary.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7195257807 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3248#issuecomment-7195257807). Originally posted by [Bj0rnis](https://github.com/Bj0rnis) on 2026-11-26T14:44:34Z. --- Verified head `c1c33027` on Windows 11 (x64, Rust 1.98.1, NVIDIA GeForce RTX 5070 Ti + WARP): - **Merge check vs `origin/main` (`63bcca43`):** clean - **RTX 5070 Ti (`cargo test -p victory-gpu --lib --features diagnostics`):** **245 passed**, 2 failed, 1 ignored (2.96s) — the 2 failures are the pre-existing #1744 NVIDIA 1M view-heap cap failures on `main` (`descriptor::host::tests::dynamic_slots_are_held_until_released`, `memory::tests::lists_share_a_table_and_copies_keep_their_views`); no regressions and the new test passes on hardware - **WARP (`VICTORY_D3D12_WARP=1`):** **247 passed**, 0 failed, 1 ignored (1.89s) - **Clippy (`-p victory-gpu --lib --tests --features diagnostics -- -D warnings`):** clean (0 warnings) - **`cargo xtask policy`:** 191 files ok Not tested: live title workload / frame-prep performance under gameplay. *AI-assisted test run and summary.*
borosf commented 2026-11-26 15:02:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T14:55:11Z.


Correction to the arithmetic in this PR's body, from measuring it rather than estimating: the per-copy loop over 4,700 copies costs 2.7 µs per list, not ~19 µs (I had assumed ~4 ns per clean_bit load; measured it is 0.57 ns, hot in cache), and the word-scan costs 1.2 µs over the 74 words the bits span, not ~0.3 µs. So the real delta is ~2.3x per list, not ~60x — the win is smaller than the body claims, and the body's "~19 µs → ~0.3 µs" should be disregarded.

Measured standalone on this B580 (rustc -O, 4,700 copies / 74 words / 12 dirty bits, 20,000 rounds, both runs):

prepare scan over 4700 copies (74 words spanned): per-copy loop 2.8µs/round vs word-scan 1.235µs/round
prepare scan over 4700 copies (74 words spanned): per-copy loop 2.664µs/round vs word-scan 1.139µs/round

Same hits both ways (240,000 = 240,000), so the answer is identical; only the cost differs. At 200–900 lists a submission that is ~0.3–1.0 ms a frame, not the several ms the body implied. The change stays — it is the same answer for less work and the test pins the semantics — but it is a small win, and this comment supersedes the body's numbers. Synthetic benchmark only; no game data.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7195522681 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3248#issuecomment-7195522681). Originally posted by [borosf](https://github.com/borosf) on 2026-11-26T14:55:11Z. --- Correction to the arithmetic in this PR's body, from measuring it rather than estimating: the per-copy loop over 4,700 copies costs **2.7 µs per list**, not ~19 µs (I had assumed ~4 ns per `clean_bit` load; measured it is 0.57 ns, hot in cache), and the word-scan costs **1.2 µs** over the 74 words the bits span, not ~0.3 µs. So the real delta is **~2.3x per list**, not ~60x — the win is smaller than the body claims, and the body's "~19 µs → ~0.3 µs" should be disregarded. Measured standalone on this B580 (`rustc -O`, 4,700 copies / 74 words / 12 dirty bits, 20,000 rounds, both runs): ``` prepare scan over 4700 copies (74 words spanned): per-copy loop 2.8µs/round vs word-scan 1.235µs/round prepare scan over 4700 copies (74 words spanned): per-copy loop 2.664µs/round vs word-scan 1.139µs/round ``` Same hits both ways (240,000 = 240,000), so the answer is identical; only the cost differs. At 200–900 lists a submission that is ~0.3–1.0 ms a frame, not the several ms the body implied. The change stays — it is the same answer for less work and the test pins the semantics — but it is a small win, and this comment supersedes the body's numbers. Synthetic benchmark only; no game data.
borosf commented 2026-11-26 15:02:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T14:59:10Z.


Fixed at 1c18e7052f0cfc6ab746df210418a8847a3d1403: you're right — clean_bit(None) is false (never clean), so untracked copies were always passed to add, and my filter_map silently dropped them. Layout now keeps those indices in untracked and prepare asks for them unconditionally before the word-scan; dirty_indices also returns before reading any word when by_bit is empty. Test updated to pin the empty case. Clippy clean both feature sets; memory:: tests 8 passed / 1 failed (the known lists_share_a_table... B580 baseline); rustfmt memory.rs 30/30 vs main. Synthetic tests only; no game data.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7195615607 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3248#issuecomment-7195615607). Originally posted by [borosf](https://github.com/borosf) on 2026-11-26T14:59:10Z. --- Fixed at `1c18e7052f0cfc6ab746df210418a8847a3d1403`: you're right — `clean_bit(None)` is false (never clean), so untracked copies were always passed to `add`, and my `filter_map` silently dropped them. `Layout` now keeps those indices in `untracked` and `prepare` asks for them unconditionally before the word-scan; `dirty_indices` also returns before reading any word when `by_bit` is empty. Test updated to pin the empty case. Clippy clean both feature sets; memory:: tests 8 passed / 1 failed (the known `lists_share_a_table...` B580 baseline); rustfmt memory.rs 30/30 vs main. Synthetic tests only; no game data.
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/3248:upstream-pr/3248'
git switch 'upstream-pr/3248'

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 'main'
git merge --no-ff 'upstream-pr/3248'
git switch 'upstream-pr/3248'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3248'
git switch 'upstream-pr/3248'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3248'
git switch 'main'
git merge --squash 'upstream-pr/3248'
git switch 'main'
git merge --ff-only 'upstream-pr/3248'
git switch 'main'
git merge 'upstream-pr/3248'
git push origin 'main'
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
Marvisak/VIctory!3246
No description provided.