vkback: idle sweep walks an age queue per memory instead of sorting every unnamed texture #132

Manually merged
github merged 1 commit from github-bot/gh-132 into gh-pr/132/base 2026-12-01 20:09:16 +00:00 AGit
Owner

@⁠Flezzonkeys on GitHub · 2026-12-01 19:55 UTC · original
Flezzonkeys:idle-sweep-queue → main · +265 −35 · 7 files · 1 commits · merged 2026-12-01 20:07 UTC

What

let_go (residency.rs) ran at almost every present while the card was over its line. Each time it collected every unnamed texture (one clock read each), sorted the idle ones, and then skipped the ones in the other memory. So the VRAM sweep sorted and skipped all textures spilled to GTT, every frame, to let go about two textures. In one 6-minute session with ~35k textures in GTT this cost 17.2 s of renderer time (7,289 sweeps, 2.36 ms each, ~3.6 ms a present late in the run). The renderer waited only 4.3 s for remaps at presents in the same run. The cost grows with what sits in GTT, not with what is let go.

unnamed is now idle::Unnamed: the same id map, plus one queue per memory in entry order. Entries are entered at Instant::now(), so entry order is age order (asserted). Removing an entry or entering it again leaves a stale queue entry, which a walk drops when it reaches it. A queue is compacted once it holds more than twice the live entries plus 4,096. The sweep walks the oldest entries of its own memory that are idle long enough and stops once enough is freed. It still checks the image's memory at each visit, as before.

Every texture image now goes in through image_made, which files the entry under the image's memory. The three creation sites (ensure_image_or, the placed write in slotwrite.rs, object creation in lib.rs) can therefore not forget it.

Behaviour is unchanged: the same textures go, in the same order, under the same rules (idle time, 4 frames, evictable, not named, the image's memory). The keep rule, the lines and back_to_vram are untouched.

The reference

The D3D12 path's counterpart is let_go_idle in crates/victory-gpu/src/resource.rs (parity.md row "Residency line, idle let-go, target reserve"). I could not read it for this change: the Marvisak/VIctory repository answers HTTP 451 for me. This PR does not change what the sweep decides, only how it finds the candidates, so the parity row stays as it is. vkd3d-proton has no idle sweep of this kind.

Same pattern checked elsewhere (rule 22): I found no other per-present path in vkback that collects and sorts a large map. source_sweep walks a bounded part (PART) of tex_keys per present, and the rtsize/rtcheck/dump sorts are diagnostics.

Evidence

  • Unit tests (idle::tests, calling the production Unnamed): under 30k steps of enter, touch and remove churn, with compactions, the walk returns the same ids in the same order as a model of the old scan-and-sort, for both memories and several cuts. Further tests cover re-entry into the other memory, touch keeping the memory, removal during a walk, and the queue length staying bounded. cargo test -p umem -p frame -p vkback --lib, cargo test -p vkback --test policy, cargo test -p xtask and cargo clippy --workspace --all-targets -- -D warnings (greenfield) pass on this branch.
  • Outside the game (release build, standalone copy of the walk, Ryzen 7 9800X3D): 35k GTT + 15k VRAM entries, VRAM sweep: old scan 3.7 ms a sweep, new walk ~2 us. An enter+remove pair costs ~75 ns against ~50 ns with the plain map.
  • Player runs, RX 9070 XT 16 GB, Ryzen 7 9800X3D, 32 GB, Mesa 26.3.1, Proton Experimental, Series X, own save, free roam. Both builds are 31c20c7 plus local-only changes that do not touch residency (present sharpening, a resolution patch left off, trace fields):
    • Before (c94bfd0, 237 stats windows): tex: let go idle (sweep) 30,466 ms over 13,260 sweeps, ~128 ms per 60 presents.
    • After (5b32ce3, the same sweep code as this PR before the image_made refactor, 206 windows, EXIT 0): the label never appears among the profile line's 24 costliest (the 24th is at a median of 4 ms per 60 presents). 29,502 textures were still let go, and the memory line looked as before (VRAM ~11.5 of 14.5 GB, ~26-38k textures in GTT).
    • fps of the two runs is not compared: different routes, no A B B A.
  • Not run: no validation run (rule 30). The change makes no Vulkan call of its own and changes neither which images are retired nor when. If you want the validation run before merging, I can't do it on my side at the moment.

Checklist (docs/AGENTS.md)

  • One topic (rule 31); docs/TASKS.md not edited
  • "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen
  • No fallback or second path for a case that can't happen (rule 33); unexpected input fails loudly (rule 9): an entry out of time order stops with an assert
  • Constants come from the console, the hardware or the title's data (rule 9b): the compaction bound (twice the live entries plus 4,096) only bounds memory and does not change any decision
  • Every switch is listed, read in one place, and a bad value stops the start (rule 2): no new switches
  • Queues and other externally synchronized Vulkan objects are used under their lock (rule 35): not touched
  • Every unsafe block has a // SAFETY: comment (rule 36): no new unsafe
  • Clippy clean; tests call the production code; translator output unchanged (rule 27)
  • Comments say what is true now and name functions, not line numbers (rule 3)
  • Nothing from the title, and no pointers to where non-free data can be found

Learnings: new entry "The sweep's own cost" in docs/learnings/tex-letgo.md. oversize.txt: lib.rs 4254 -> 4250.

**@⁠Flezzonkeys** on GitHub · 2026-12-01 19:55 UTC · [original](https://github.com/vktory6/VKtory/pull/132) `Flezzonkeys:idle-sweep-queue` → `main` · +265 −35 · 7 files · 1 commits · merged 2026-12-01 20:07 UTC ## What `let_go` (residency.rs) ran at almost every present while the card was over its line. Each time it collected every unnamed texture (one clock read each), sorted the idle ones, and then skipped the ones in the other memory. So the VRAM sweep sorted and skipped all textures spilled to GTT, every frame, to let go about two textures. In one 6-minute session with ~35k textures in GTT this cost **17.2 s of renderer time** (7,289 sweeps, 2.36 ms each, ~3.6 ms a present late in the run). The renderer waited only 4.3 s for remaps at presents in the same run. The cost grows with what sits in GTT, not with what is let go. `unnamed` is now `idle::Unnamed`: the same id map, plus one queue per memory in entry order. Entries are entered at `Instant::now()`, so entry order is age order (asserted). Removing an entry or entering it again leaves a stale queue entry, which a walk drops when it reaches it. A queue is compacted once it holds more than twice the live entries plus 4,096. The sweep walks the oldest entries of its own memory that are idle long enough and stops once enough is freed. It still checks the image's memory at each visit, as before. Every texture image now goes in through `image_made`, which files the entry under the image's memory. The three creation sites (`ensure_image_or`, the placed write in slotwrite.rs, object creation in lib.rs) can therefore not forget it. Behaviour is unchanged: the same textures go, in the same order, under the same rules (idle time, 4 frames, evictable, not named, the image's memory). The keep rule, the lines and `back_to_vram` are untouched. ## The reference The D3D12 path's counterpart is `let_go_idle` in `crates/victory-gpu/src/resource.rs` (parity.md row "Residency line, idle let-go, target reserve"). I could not read it for this change: the Marvisak/VIctory repository answers HTTP 451 for me. This PR does not change what the sweep decides, only how it finds the candidates, so the parity row stays as it is. vkd3d-proton has no idle sweep of this kind. Same pattern checked elsewhere (rule 22): I found no other per-present path in vkback that collects and sorts a large map. `source_sweep` walks a bounded part (`PART`) of `tex_keys` per present, and the `rtsize`/`rtcheck`/`dump` sorts are diagnostics. ## Evidence - **Unit tests** (`idle::tests`, calling the production `Unnamed`): under 30k steps of enter, touch and remove churn, with compactions, the walk returns the same ids in the same order as a model of the old scan-and-sort, for both memories and several cuts. Further tests cover re-entry into the other memory, touch keeping the memory, removal during a walk, and the queue length staying bounded. `cargo test -p umem -p frame -p vkback --lib`, `cargo test -p vkback --test policy`, `cargo test -p xtask` and `cargo clippy --workspace --all-targets -- -D warnings` (greenfield) pass on this branch. - **Outside the game** (release build, standalone copy of the walk, Ryzen 7 9800X3D): 35k GTT + 15k VRAM entries, VRAM sweep: old scan 3.7 ms a sweep, new walk ~2 us. An enter+remove pair costs ~75 ns against ~50 ns with the plain map. - **Player runs**, RX 9070 XT 16 GB, Ryzen 7 9800X3D, 32 GB, Mesa 26.3.1, Proton Experimental, Series X, own save, free roam. Both builds are 31c20c7 plus local-only changes that do not touch residency (present sharpening, a resolution patch left off, trace fields): - Before (c94bfd0, 237 stats windows): `tex: let go idle (sweep)` 30,466 ms over 13,260 sweeps, ~128 ms per 60 presents. - After (5b32ce3, the same sweep code as this PR before the `image_made` refactor, 206 windows, EXIT 0): the label never appears among the profile line's 24 costliest (the 24th is at a median of 4 ms per 60 presents). 29,502 textures were still let go, and the memory line looked as before (VRAM ~11.5 of 14.5 GB, ~26-38k textures in GTT). - fps of the two runs is not compared: different routes, no A B B A. - **Not run:** no validation run (rule 30). The change makes no Vulkan call of its own and changes neither which images are retired nor when. If you want the validation run before merging, I can't do it on my side at the moment. ## Checklist (docs/AGENTS.md) - [x] One topic (rule 31); `docs/TASKS.md` not edited - [x] "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen - [x] No fallback or second path for a case that can't happen (rule 33); unexpected input fails loudly (rule 9): an entry out of time order stops with an assert - [x] Constants come from the console, the hardware or the title's data (rule 9b): the compaction bound (twice the live entries plus 4,096) only bounds memory and does not change any decision - [x] Every switch is listed, read in one place, and a bad value stops the start (rule 2): no new switches - [x] Queues and other externally synchronized Vulkan objects are used under their lock (rule 35): not touched - [x] Every `unsafe` block has a `// SAFETY:` comment (rule 36): no new `unsafe` - [x] Clippy clean; tests call the production code; translator output unchanged (rule 27) - [x] Comments say what is true now and name functions, not line numbers (rule 3) - [x] Nothing from the title, and no pointers to where non-free data can be found Learnings: new entry "The sweep's own cost" in `docs/learnings/tex-letgo.md`. `oversize.txt`: lib.rs 4254 -> 4250.
let_go collected every unnamed texture, read the clock for each, sorted the idle ones and then
skipped those in the other memory, at each present while the card was over its line. With ~35k
textures spilled to GTT that cost 17.2 s of renderer time over 7,289 sweeps in a 6 min session
(RX 9070 XT, 32 GB; 2.36 ms a sweep, ~3.6 ms a present late in the run) for ~17k textures let go,
more than the renderer's remap waits in the same run (4.3 s).

`unnamed` is now idle::Unnamed: the id map as before plus, per memory, a queue in entry order
(entries are entered at Instant::now(), so entry order is age order; asserted). Removing or
re-entering leaves a stale queue entry that the walk drops when it reaches it; a queue is
compacted past twice the live entries plus 4,096. The sweep walks the oldest entries of its memory
idle long enough and stops once enough is freed; the image's memory is still checked at each visit.
Every texture image now goes in through image_made, which files the entry under the image's memory,
so the three creation sites cannot forget it.

Same ids in the same order as the old sort (unit test against a model of the old scan under
enter/touch/remove churn and compaction). Outside the game, 35k GTT + 15k VRAM entries: 3.7 ms a
sweep before, ~2 us after; an enter+remove pair ~75 ns against ~50 ns. Learning:
docs/learnings/tex-letgo.md "The sweep's own cost". In game: the sweep left the profile's top 24
(~128 ms per 60 presents before, under ~4 after), textures still let go.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 19:58:38 +00:00
github-bot changed target branch from main to gh-pr/132/base 2026-12-01 20:09:14 +00:00
github manually merged commit f920573f18 into gh-pr/132/base 2026-12-01 20:09:16 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-12-01 20:07 UTC · original

Merged, thanks. This is a clean find. The sweep cost grew with what sits in GTT rather than with what it frees, which also fits the "frames get slower the longer the session runs" creep we see in the house scene. The model test against the old sort is exactly the evidence a "same decisions, cheaper" change needs.

Two things I checked, for the record: the age assert holds because every caller is on the renderer thread and passes Instant::now(); and ties at the same instant now go by entry order instead of by id, which changes nothing in practice. No validation run was needed: no Vulkan call changed.

**@⁠vktory6** on GitHub · 2026-12-01 20:07 UTC · [original](https://github.com/vktory6/VKtory/pull/132#issuecomment-7324097352) Merged, thanks. This is a clean find. The sweep cost grew with what sits in GTT rather than with what it frees, which also fits the "frames get slower the longer the session runs" creep we see in the house scene. The model test against the old sort is exactly the evidence a "same decisions, cheaper" change needs. Two things I checked, for the record: the age assert holds because every caller is on the renderer thread and passes `Instant::now()`; and ties at the same instant now go by entry order instead of by id, which changes nothing in practice. No validation run was needed: no Vulkan call changed.
This discussion has been locked. Commenting is limited to contributors.
No description provided.