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

Open
github-bot wants to merge 2 commits from github-bot/gh-3248 into main AGit
Owner

@⁠borosf on GitHub · 2026-11-26 14:29 UTC · original
borosf:vibe/prepare-scans-words-842ca5 → main · +124 −10 · 2 files · 2 commits · open

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.

**@⁠borosf** on GitHub · 2026-11-26 14:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3248) `borosf:vibe/prepare-scans-words-842ca5` → `main` · +124 −10 · 2 files · 2 commits · open `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.
`memory::prepare` ran once per list that binds a title-memory view heap,
per submission, and asked `watch::clean_bit` once per copy the layout
names — about 4,700 in the world, nearly all clean — so a submission of
tens of lists paid hundreds of thousands of single-bit loads a frame.

The layout now keeps its copies' tracker bits as `(bit, index)` pairs
ordered by bit plus the words they span, and `dirty_indices` walks the
tracker a word at a time with `trailing_zeros`, asking only for the
changed bits. A layout costs the words its bits span (~74 for 4,700
contiguous bits) instead of the copies, and the set of copies handed to
`add` is exactly the one the per-copy loop found.

Arithmetic, not a title measurement: ~4,700 loads at ~4 ns becomes ~74,
i.e. ~19 µs to ~0.3 µs per list per submission; the frame line's
`StepMemoryUs` counter already reports the total, so a run with this merged
shows it directly.

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

Synthetic tests only; no game data.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:33:18 +00:00
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 14:42 UTC · original

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.

**@⁠cloph-dsp** on GitHub · 2026-11-26 14:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/3248#issuecomment-7195209986) 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.
Author
Owner

@⁠Bj0rnis on GitHub · 2026-11-26 14:44 UTC · original

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.

**@⁠Bj0rnis** on GitHub · 2026-11-26 14:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/3248#issuecomment-7195257807) 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.*
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 14:52 UTC · original · reviewed

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).

**@⁠Dukestar69** on GitHub · 2026-11-26 14:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/3248#pullrequestreview-6076207025) · **reviewed** 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).
Author
Owner

@⁠borosf on GitHub · 2026-11-26 14:55 UTC · original

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** on GitHub · 2026-11-26 14:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/3248#issuecomment-7195522681) 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.
Reviewer on #3248: copies with no tracker bit (untracked memory, or a full
bit pool) have `dirty_bit() == None`, and `clean_bit(None)` is false — never
clean — so the old per-copy loop asked for them every time. Building
`by_bit` with filter_map dropped them, which would have treated them as
permanently clean and stopped refreshing them.

`Layout` now keeps those indices in `untracked`, and `prepare` asks for them
unconditionally before the word-at-a-time scan of the tracked ones.
`dirty_indices` also returns before reading any word when `by_bit` is empty.

Synthetic tests only; no game data.
Author
Owner

@⁠borosf on GitHub · 2026-11-26 14:59 UTC · original

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.

**@⁠borosf** on GitHub · 2026-11-26 14:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/3248#issuecomment-7195615607) 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 can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3248/head:github-bot/gh-3248'
git switch 'github-bot/gh-3248'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3248'
git switch 'github-bot/gh-3248'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3248'
git switch 'github-bot/gh-3248'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3248'
git switch 'main'
git merge --squash 'github-bot/gh-3248'
git switch 'main'
git merge --ff-only 'github-bot/gh-3248'
git switch 'main'
git merge 'github-bot/gh-3248'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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!3248
No description provided.