memory: memo fast path for the builder (VICTORY_MEMO=0 turns it off) #533

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

@⁠cerealfordinner on GitHub · 2026-12-04 23:28 UTC · original
cerealfordinner:upstream-pr-memo-fastpath → main · +187 −1 · 2 files · 2 commits · open

Memo fast path for the hardware BVH builder: skips the builder's repeated work. On by default; VICTORY_MEMO=0 turns it off.

Repeated A/B benches: 1% lows +12% (10.2 -> 11.5), average unchanged.

cargo check -p victory-gpu -p victory-shader --lib --target x86_64-pc-windows-gnu: clean (one pre-existing dead-code warning).

Signed-off-by: cerealfordinner [email protected]

**@⁠cerealfordinner** on GitHub · 2026-12-04 23:28 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/533) `cerealfordinner:upstream-pr-memo-fastpath` → `main` · +187 −1 · 2 files · 2 commits · open Memo fast path for the hardware BVH builder: skips the builder's repeated work. On by default; `VICTORY_MEMO=0` turns it off. Repeated A/B benches: 1% lows +12% (10.2 -> 11.5), average unchanged. `cargo check -p victory-gpu -p victory-shader --lib --target x86_64-pc-windows-gnu`: clean (one pre-existing dead-code warning). Signed-off-by: cerealfordinner <[email protected]>
Skips the builder's repeated work; 1% lows +12% (10.2 -> 11.5) in
repeated A/B benches, avg unchanged.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 23:33:26 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 00:35 UTC · original

Checked 8e770e7 on native Windows with MSVC (one NVIDIA desktop card plus WARP, didn't run the game).

Builds clean. cargo test -p victory-gpu --lib gives the same failures on this branch with the memo on, with VICTORY_MEMO=0 and on main (only the known native ones). I also ran it with diagnostics and VICTORY_D3D12_DEBUG=1. On NVIDIA I only ran the branch, and it had the same known failures. On WARP the branch and main fail the same tests, including views_are_made_beside_the_heap_and_copied_in, which fails on main too. Clippy with and without diagnostics shows the same warning count as main. Nothing in the lib tests calls memory::ready, though, so equal results don't say much. I didn't measure the 1% lows.

For other reviewers, this isn't a cache of BVH results or of any data. It memoizes memory::ready per host list. What it hands back (directory address, faults address, heap base) is fixed for the life of Memory and the heap anyway. The memo is dropped on HostList::reset, so it lives for one recording and costs one small struct per list. The fast path skips the MEMORY lock and the rest of the body when every first_use key the body would set is already set and the global EPOCH hasn't moved. Main already skips the keyed work on repeat calls. Title writes recorded in the list clear the key set, and frees reach memory through LET_GO_PENDING, which memoed checks first (memory.rs:1904). So from reading I don't see a new path to stale contents or freed memory. Lock order doesn't change either. The slow path stores the memo while holding MEMORY and then the list lock, the same order first_use already uses, and the fast path only takes the list lock.

I wrote a throwaway test (not pushed) that calls ready on a real list and heap and counts the slow calls. With the memo on, a second call with the same arguments is answered from the memo with the same values. A pending LET_GO forces the slow path and gets drained, and after reset the next call is slow again. When I bump the frame counter between two calls, the memo still answers and Memory.frame stays on the old frame. With VICTORY_MEMO=0 the frame turn runs.

That frame turn is the real difference from main. The m.frame != frame block (memory.rs:1665) isn't part of the memo check. That block runs take_faults, plus look_again, which goes back over a sample of held pages each frame, and that's this code's per-frame check for title memory changing under it. With the memo it waits for the next slow call by any list. That's usually soon because new lists start with no keys, but it's still a change. Keeping the frame in Memo and comparing it with crate::frame::current() would close it cheaply.

Smaller things.

  • memoed copies every key formula from ready by hand (memory.rs:1893 on, plus a second READ const at 1886). If a key in ready changes and nobody mirrors it, the fast path silently skips the new work.
  • A memo hit also skips the spare page top-up (memory.rs:1690) and stats.uses, and with diagnostics trace.dispatch, so the memory line and traces undercount.
  • From reading only (not reproduced), if heap.dynamic(COPIES + 1)? fails at memory.rs:1806 the call returns before the EPOCH bump. Pending ranges it drained, or copies it added just before, then don't invalidate other lists' memos.
  • The docs say VICTORY_MEMO=1 (memory.rs:341, host.rs:2360, host.rs:6351), but the memo is on unless set to 0. It also covers Use::Reads draws, not only the builder.

VICTORY_MEMO=0 behaves the same as main apart from a few extra atomics. Because of the frame turn and the hand-copied keys I'd lean to off by default until it has been run in game with a frame check added, but that's the maintainers' call.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 00:35 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/533#issuecomment-7428412312) Checked 8e770e7 on native Windows with MSVC (one NVIDIA desktop card plus WARP, didn't run the game). Builds clean. `cargo test -p victory-gpu --lib` gives the same failures on this branch with the memo on, with `VICTORY_MEMO=0` and on main (only the known native ones). I also ran it with diagnostics and `VICTORY_D3D12_DEBUG=1`. On NVIDIA I only ran the branch, and it had the same known failures. On WARP the branch and main fail the same tests, including `views_are_made_beside_the_heap_and_copied_in`, which fails on main too. Clippy with and without diagnostics shows the same warning count as main. Nothing in the lib tests calls `memory::ready`, though, so equal results don't say much. I didn't measure the 1% lows. For other reviewers, this isn't a cache of BVH results or of any data. It memoizes `memory::ready` per host list. What it hands back (directory address, faults address, heap base) is fixed for the life of `Memory` and the heap anyway. The memo is dropped on `HostList::reset`, so it lives for one recording and costs one small struct per list. The fast path skips the `MEMORY` lock and the rest of the body when every `first_use` key the body would set is already set and the global `EPOCH` hasn't moved. Main already skips the keyed work on repeat calls. Title writes recorded in the list clear the key set, and frees reach memory through `LET_GO_PENDING`, which `memoed` checks first (memory.rs:1904). So from reading I don't see a new path to stale contents or freed memory. Lock order doesn't change either. The slow path stores the memo while holding `MEMORY` and then the list lock, the same order `first_use` already uses, and the fast path only takes the list lock. I wrote a throwaway test (not pushed) that calls `ready` on a real list and heap and counts the slow calls. With the memo on, a second call with the same arguments is answered from the memo with the same values. A pending `LET_GO` forces the slow path and gets drained, and after `reset` the next call is slow again. When I bump the frame counter between two calls, the memo still answers and `Memory.frame` stays on the old frame. With `VICTORY_MEMO=0` the frame turn runs. That frame turn is the real difference from main. The `m.frame != frame` block (memory.rs:1665) isn't part of the memo check. That block runs take_faults, plus look_again, which goes back over a sample of held pages each frame, and that's this code's per-frame check for title memory changing under it. With the memo it waits for the next slow call by any list. That's usually soon because new lists start with no keys, but it's still a change. Keeping the frame in `Memo` and comparing it with `crate::frame::current()` would close it cheaply. Smaller things. - `memoed` copies every key formula from `ready` by hand (memory.rs:1893 on, plus a second `READ` const at 1886). If a key in `ready` changes and nobody mirrors it, the fast path silently skips the new work. - A memo hit also skips the spare page top-up (memory.rs:1690) and `stats.uses`, and with diagnostics `trace.dispatch`, so the memory line and traces undercount. - From reading only (not reproduced), if `heap.dynamic(COPIES + 1)?` fails at memory.rs:1806 the call returns before the EPOCH bump. Pending ranges it drained, or copies it added just before, then don't invalidate other lists' memos. - The docs say `VICTORY_MEMO=1` (memory.rs:341, host.rs:2360, host.rs:6351), but the memo is on unless set to `0`. It also covers `Use::Reads` draws, not only the builder. `VICTORY_MEMO=0` behaves the same as main apart from a few extra atomics. Because of the frame turn and the hand-copied keys I'd lean to off by default until it has been run in game with a frame check added, but that's the maintainers' call. AI-assisted: yes
- Add frame to Memo and check it in memoed to close frame turn gap
- Change default to off (VICTORY_MEMO=1 enables)
- Update docs to reflect VICTORY_MEMO=1 enables memo
- Bump EPOCH before heap.dynamic to invalidate memos on failure
- Add comment warning about hand-copied key formulas in memoed
Author
Owner

@⁠cerealfordinner on GitHub · 2026-12-05 03:14 UTC · original

Addressed the review items:

  1. Frame turn gap closed — added frame field to Memo struct, capture crate::frame::current() when creating the memo, and check it in memoed (memory.rs:340, 1874, 1918). The per-frame take_faults / look_again check now runs on every list's first call each frame.

  2. Default changed to off — memo now requires VICTORY_MEMO=1 to enable (was on unless VICTORY_MEMO=0). Updated the env var check and all three doc references (memory.rs:341, 1621; host.rs:2360, 6351).

  3. EPOCH bump on heap.dynamic failure — bump EPOCH before the heap.dynamic(COPIES + 1)? call so pending drains and copies added before the failure still invalidate other lists' memos (memory.rs:1810).

  4. Hand-copied keys warning — added a comment in memoed noting that key formulas are hand-copied from the slow path and must be mirrored if changed (memory.rs:1898).

Build verification will run in CI.

**@⁠cerealfordinner** on GitHub · 2026-12-05 03:14 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/533#issuecomment-7430902990) Addressed the review items: 1. **Frame turn gap closed** — added `frame` field to `Memo` struct, capture `crate::frame::current()` when creating the memo, and check it in `memoed` (memory.rs:340, 1874, 1918). The per-frame `take_faults` / `look_again` check now runs on every list's first call each frame. 2. **Default changed to off** — memo now requires `VICTORY_MEMO=1` to enable (was on unless `VICTORY_MEMO=0`). Updated the env var check and all three doc references (memory.rs:341, 1621; host.rs:2360, 6351). 3. **EPOCH bump on heap.dynamic failure** — bump `EPOCH` before the `heap.dynamic(COPIES + 1)?` call so pending drains and copies added before the failure still invalidate other lists' memos (memory.rs:1810). 4. **Hand-copied keys warning** — added a comment in `memoed` noting that key formulas are hand-copied from the slow path and must be mirrored if changed (memory.rs:1898). Build verification will run in CI.
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 04:30 UTC · original

Re-checked 73f6835 on native Windows (one NVIDIA desktop card plus WARP).

cargo test -p victory-gpu --lib gives the same 352 passed and 5 failed as main, both with VICTORY_MEMO unset and with VICTORY_MEMO=1. The 5 are the known native ones (two indirect host_list tests, two pipeline::host library loads, and the watch test). Clippy, with and without diagnostics, shows the same warning count as main, and none of the warnings are in the changed lines.

The frame check works. I rebuilt my throwaway test that calls ready on a real list and heap and counts slow calls, and this time it bumps the frame counter between calls. With the memo on, the second call is a memo hit. The first call after the bump takes the slow path and Memory.frame moves to the new frame, then the next call is a hit again. With it unset every call is slow, as on main. One small thing. The memo stores a fresh crate::frame::current() (memory.rs:1873) rather than the frame local the turn compared at 1665. If the frame moves on while a slow call is running, the memo is stamped with a frame whose turn never ran. That's from reading only and a narrow window, but storing the local would close it for free. The lib suite itself never calls ready, so the throwaway test is the real evidence here and the suite results are not.

Default off now really matches main. When the flag isn't set, memoed never runs and no memo is stored. What's left is the dirty flag, two sig tuples and a few EPOCH fetch_adds, including the one in the deferred land closure. That's no behaviour change, just a few atomics.

The EPOCH bump before heap.dynamic(COPIES + 1)? covers the failure case, and that ? is the only early exit between the pending drains and the end bump. It fires once per new heap even when nothing else changed. That costs an extra invalidation per heap, which is harmless. The success path still gets the end bump at 1867 when something changed. The hand-copied keys only got a comment, which is fine as long as it stays opt-in. A memo hit still skips the spare page top-up, stats.uses and the diagnostics trace, and the memo still covers Use::Reads too.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 04:30 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/533#issuecomment-7431884506) Re-checked 73f6835 on native Windows (one NVIDIA desktop card plus WARP). `cargo test -p victory-gpu --lib` gives the same 352 passed and 5 failed as main, both with `VICTORY_MEMO` unset and with `VICTORY_MEMO=1`. The 5 are the known native ones (two indirect host_list tests, two pipeline::host library loads, and the watch test). Clippy, with and without diagnostics, shows the same warning count as main, and none of the warnings are in the changed lines. The frame check works. I rebuilt my throwaway test that calls `ready` on a real list and heap and counts slow calls, and this time it bumps the frame counter between calls. With the memo on, the second call is a memo hit. The first call after the bump takes the slow path and `Memory.frame` moves to the new frame, then the next call is a hit again. With it unset every call is slow, as on main. One small thing. The memo stores a fresh `crate::frame::current()` (memory.rs:1873) rather than the `frame` local the turn compared at 1665. If the frame moves on while a slow call is running, the memo is stamped with a frame whose turn never ran. That's from reading only and a narrow window, but storing the local would close it for free. The lib suite itself never calls `ready`, so the throwaway test is the real evidence here and the suite results are not. Default off now really matches main. When the flag isn't set, `memoed` never runs and no memo is stored. What's left is the `dirty` flag, two `sig` tuples and a few EPOCH `fetch_add`s, including the one in the deferred land closure. That's no behaviour change, just a few atomics. The EPOCH bump before `heap.dynamic(COPIES + 1)?` covers the failure case, and that `?` is the only early exit between the pending drains and the end bump. It fires once per new heap even when nothing else changed. That costs an extra invalidation per heap, which is harmless. The success path still gets the end bump at 1867 when something changed. The hand-copied keys only got a comment, which is fine as long as it stays opt-in. A memo hit still skips the spare page top-up, `stats.uses` and the diagnostics trace, and the memo still covers `Use::Reads` too. AI-assisted: yes
Author
Owner

@⁠luisaugustz on GitHub · 2026-12-05 13:30 UTC · original

Measured on RX 9070 XT + Ryzen 7 5800X (RADV, 4K output, FSR 4 Performance, ray tracing on), on the downtown route from #554:

Avg fps Worst 5 s
Memo on 16.4 (10 runs) 10.3
VICTORY_MEMO=0 15.9 (2 runs) 8.2

So about +0.7 fps, and better lows.

Details are in #591.

**@⁠luisaugustz** on GitHub · 2026-12-05 13:30 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/533#issuecomment-7438791028) Measured on RX 9070 XT + Ryzen 7 5800X (RADV, 4K output, FSR 4 Performance, ray tracing on), on the `downtown` route from #554: | | Avg fps | Worst 5 s | |---|---|---| | Memo on | 16.4 (10 runs) | 10.3 | | `VICTORY_MEMO=0` | 15.9 (2 runs) | 8.2 | So about +0.7 fps, and better lows. Details are in #591.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/533/head:github-bot/gh-533'
git switch 'github-bot/gh-533'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-533'
git switch 'github-bot/gh-533'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-533'
git switch 'github-bot/gh-533'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-533'
git switch 'main'
git merge --squash 'github-bot/gh-533'
git switch 'main'
git merge --ff-only 'github-bot/gh-533'
git switch 'main'
git merge 'github-bot/gh-533'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.