vkback: speed up slot_reads cache, packed SlotFlags, and write_slot #13

Manually merged
github merged 2 commits from github-bot/gh-13 into gh-pr/13/base 2026-11-30 20:46:41 +00:00 AGit
Owner

@⁠olafix52 on GitHub · 2026-11-30 17:45 UTC · original
olafix52:perf/slot-reads-hotpath → main · +217 −114 · 11 files · 2 commits · merged 2026-11-30 20:46 UTC

Summary

Optimizes the live: slot_reads at Execute and live: slot_reads in lists hot paths on the renderer thread.

Changes

  • slotcache.rs:
    • Replace the single-entry last page cache in Paged<T> with a 16-entry direct-mapped recent cache (accelerating both slot_cache and view_addr), inline insert/remove, and skip redundant zero-stores in remove.
    • Replace SlotBits with nibble-packed 4-bit SlotFlags (FLAG_HOOK, FLAG_BUFFER, FLAG_IMAGE, FLAG_RETRY; 1 MiB for 2^21 slots) so slot_read_at, slot_reset, write_null_slot, and slot_exit hit one byte/cache line instead of multiple maps or separate bitsets.
  • slotwrite.rs, lib.rs, bufview.rs, placed.rs, texback.rs, twin.rs, srclife.rs:
    • Cache primary_view_heap (largest shader-visible view heap) and move slot_index into slotwrite.rs using >> 5.
    • Move slot_read_at's cold hook path out-of-line (#[cold] slot_read_hook) guarded by FLAG_HOOK.
    • Guard twin_slots, texback, exact, slot_img, shown, and slot_retry work in slot_reset/write_null_slot/slot_exit/retry_dropped with SlotFlags, and skip twin_slots lookup on image SRVs.
  • live/slots.rs, lib.rs:
    • Eliminate resolved.clone() and the duplicate slot_cache.holds lookup on cache misses, add an early return to forget_slots, and batch pf_count_n("be: write_view (descriptor writes; count only)", written) once per slot_reads call.
  • notes.rs:
    • Switch Issues from BTreeMap to FxMap (sorting keys alphabetically only in window_lines).
  • greenfield/tools/policy/oversize.txt: lower greenfield/vkback/src/lib.rs count from 4378 to 4375.
  • Kept (AGENTS.md rule 24): cuts per-slot bookkeeping cleanly and improves both renderer CPU time and whole-frame throughput.

Invalidation & visual check (AGENTS.md rules 17, 18)

  • FLAG_IMAGE is set whenever either slot_set(index, Some(..)) or show_image(index, Some(..)) populates a slot, and cleared only in slot_reset(index) alongside slot_set(index, None) and show_image(index, None). write_null_slot keeps FLAG_IMAGE intact so supersede_placed/hold_lower/srclife nulling a slot before slot_reset does not drop image tracking.
  • Local visual check across full 0..140 world s runs passed with zero rendering differences or artifacts.

Measurements

  • Machine: AMD Ryzen 9 7900X (24T), 30 GiB RAM, AMD Radeon RX 9070 XT (RADV GFX1201, Mesa 26.3.1), Linux 7.3.6-3-cachyos, Proton Experimental (experimental-11.0-20261123b).
  • Scene: prison cell, steady state (0..140 world s, warm shader store, VKTORY_LOCK=1 MEASURE=1, VKB_LAT + greenfield/tools/frec/frames.py 0 140 --no-perf), interleaved A B B A (measured pre-rebase b112b59 vs main 7403bee; rebased cleanly onto 8a3cb80 with identical code diff):
Arm Build FPS Mean / p50 / p90 / p99 / Max (ms) > 33.3 ms Renderer / Last-240 frame,rend (ms) slot_reads@Exec (ms/f) Cost/read / Cost/miss (ns)
A1 main (7403bee) 24.97 40.0 / 37.6 / 55.3 / 93.6 / 267.5 66.0% 29.8 / (38.5, 28.1) 10.44 59.5 / 177.3
B1 slot-reads-hotpath 25.26 39.6 / 37.4 / 53.7 / 91.8 / 225.1 67.4% 28.5 / (37.1, 26.5) 8.55 51.3 / 154.3
B2 slot-reads-hotpath 25.23 39.6 / 37.3 / 54.5 / 87.9 / 212.0 66.0% 28.6 / (37.3, 26.3) 8.53 50.9 / 152.9
A2 main (7403bee) 25.15 39.8 / 37.3 / 54.4 / 89.4 / 302.4 66.1% 29.9 / (38.0, 28.2) 10.21 59.2 / 177.3
  • Cleanly separates across every metric: slot_reads@Exec 10.33 -> 8.54 ms/f (-17.3%), unit cost 59.35 -> 51.10 ns/slot_read (-13.9%) & 177.3 -> 153.6 ns/miss (-13.4%), renderer busy 29.85 -> 28.55 ms/f (-1.30 ms/f; last 240: 28.15 -> 26.40 ms/f, -1.75 ms/f), settled last 240 frame 38.25 -> 37.20 ms (-1.05 ms), whole-frame throughput 25.06 -> 25.25 fps, and max spike 285.0 -> 218.6 ms.

Verification (rebased eaaaeab on 8a3cb80)

  • cargo test -p vkback --lib -j 6: 145 passed, 0 failed, 3 ignored.
  • cargo test -p vkback --test policy -j 6: 6 passed, 0 failed.
  • cargo xtask policy: asset policy: 533 files ok.
**@⁠olafix52** on GitHub · 2026-11-30 17:45 UTC · [original](https://github.com/vktory6/VKtory/pull/13) `olafix52:perf/slot-reads-hotpath` → `main` · +217 −114 · 11 files · 2 commits · merged 2026-11-30 20:46 UTC ### Summary Optimizes the `live: slot_reads at Execute` and `live: slot_reads in lists` hot paths on the renderer thread. ### Changes - **`slotcache.rs`**: - Replace the single-entry `last` page cache in `Paged<T>` with a 16-entry direct-mapped `recent` cache (accelerating both `slot_cache` and `view_addr`), inline `insert`/`remove`, and skip redundant zero-stores in `remove`. - Replace `SlotBits` with nibble-packed 4-bit `SlotFlags` (`FLAG_HOOK`, `FLAG_BUFFER`, `FLAG_IMAGE`, `FLAG_RETRY`; 1 MiB for `2^21` slots) so `slot_read_at`, `slot_reset`, `write_null_slot`, and `slot_exit` hit one byte/cache line instead of multiple maps or separate bitsets. - **`slotwrite.rs`, `lib.rs`, `bufview.rs`, `placed.rs`, `texback.rs`, `twin.rs`, `srclife.rs`**: - Cache `primary_view_heap` (largest shader-visible view heap) and move `slot_index` into `slotwrite.rs` using `>> 5`. - Move `slot_read_at`'s cold hook path out-of-line (`#[cold] slot_read_hook`) guarded by `FLAG_HOOK`. - Guard `twin_slots`, `texback`, `exact`, `slot_img`, `shown`, and `slot_retry` work in `slot_reset`/`write_null_slot`/`slot_exit`/`retry_dropped` with `SlotFlags`, and skip `twin_slots` lookup on image SRVs. - **`live/slots.rs`, `lib.rs`**: - Eliminate `resolved.clone()` and the duplicate `slot_cache.holds` lookup on cache misses, add an early return to `forget_slots`, and batch `pf_count_n("be: write_view (descriptor writes; count only)", written)` once per `slot_reads` call. - **`notes.rs`**: - Switch `Issues` from `BTreeMap` to `FxMap` (sorting keys alphabetically only in `window_lines`). - **`greenfield/tools/policy/oversize.txt`**: lower `greenfield/vkback/src/lib.rs` count from 4378 to 4375. - **Kept (`AGENTS.md` rule 24):** cuts per-slot bookkeeping cleanly and improves both renderer CPU time and whole-frame throughput. ### Invalidation & visual check (`AGENTS.md` rules 17, 18) - `FLAG_IMAGE` is set whenever either `slot_set(index, Some(..))` or `show_image(index, Some(..))` populates a slot, and cleared only in `slot_reset(index)` alongside `slot_set(index, None)` and `show_image(index, None)`. `write_null_slot` keeps `FLAG_IMAGE` intact so `supersede_placed`/`hold_lower`/`srclife` nulling a slot before `slot_reset` does not drop image tracking. - Local visual check across full `0..140` world s runs passed with zero rendering differences or artifacts. ### Measurements - Machine: AMD Ryzen 9 7900X (24T), 30 GiB RAM, AMD Radeon RX 9070 XT (RADV GFX1201, Mesa 26.3.1), Linux 7.3.6-3-cachyos, Proton Experimental (`experimental-11.0-20261123b`). - Scene: prison cell, steady state (`0..140` world s, warm shader store, `VKTORY_LOCK=1 MEASURE=1`, `VKB_LAT` + `greenfield/tools/frec/frames.py 0 140 --no-perf`), interleaved `A B B A` (measured pre-rebase `b112b59` vs `main` `7403bee`; rebased cleanly onto `8a3cb80` with identical code diff): | Arm | Build | FPS | Mean / p50 / p90 / p99 / Max (ms) | > 33.3 ms | Renderer / Last-240 frame,rend (ms) | `slot_reads@Exec` (ms/f) | Cost/read / Cost/miss (ns) | |---|---|---:|---|---:|---|---:|---:| | A1 | `main` (`7403bee`) | 24.97 | 40.0 / 37.6 / 55.3 / 93.6 / 267.5 | 66.0% | 29.8 / (38.5, 28.1) | 10.44 | 59.5 / 177.3 | | B1 | `slot-reads-hotpath` | **25.26** | **39.6** / 37.4 / 53.7 / 91.8 / **225.1** | 67.4% | **28.5** / (**37.1, 26.5**) | **8.55** | **51.3 / 154.3** | | B2 | `slot-reads-hotpath` | **25.23** | **39.6** / 37.3 / 54.5 / 87.9 / **212.0** | 66.0% | **28.6** / (**37.3, 26.3**) | **8.53** | **50.9 / 152.9** | | A2 | `main` (`7403bee`) | 25.15 | 39.8 / 37.3 / 54.4 / 89.4 / 302.4 | 66.1% | 29.9 / (38.0, 28.2) | 10.21 | 59.2 / 177.3 | - Cleanly separates across every metric: `slot_reads@Exec` **10.33 -> 8.54 ms/f (-17.3%)**, unit cost **59.35 -> 51.10 ns/slot_read (-13.9%)** & **177.3 -> 153.6 ns/miss (-13.4%)**, `renderer busy` **29.85 -> 28.55 ms/f (-1.30 ms/f; last 240: 28.15 -> 26.40 ms/f, -1.75 ms/f)**, settled `last 240` frame **38.25 -> 37.20 ms (-1.05 ms)**, whole-frame throughput **25.06 -> 25.25 fps**, and max spike **285.0 -> 218.6 ms**. ### Verification (rebased `eaaaeab` on `8a3cb80`) - `cargo test -p vkback --lib -j 6`: 145 passed, 0 failed, 3 ignored. - `cargo test -p vkback --test policy -j 6`: 6 passed, 0 failed. - `cargo xtask policy`: `asset policy: 533 files ok`.
Optimize the `live: slot_reads at Execute` and `live: slot_reads in lists`
hot paths on the renderer thread.

Changes:
- `slotcache.rs`: replace 1-entry `last` page cache in `Paged<T>` with a
  16-entry direct-mapped `recent` cache (accelerating both `slot_cache`
  and `view_addr`), inline `insert`/`remove`, skip redundant stores in
  `remove`, and replace `SlotBits` with packed 4-bit `SlotFlags`
  (`FLAG_HOOK`, `FLAG_BUFFER`, `FLAG_IMAGE`, `FLAG_RETRY`; 1 MiB for 2^21
  slots so `slot_read_at`, `slot_reset`, `write_null_slot`, and `slot_exit`
  hit one byte/cache line).
- `slotwrite.rs`, `lib.rs`, `bufview.rs`, `placed.rs`, `texback.rs`,
  `twin.rs`, `srclife.rs`: cache `primary_view_heap` (largest view heap)
  for inline `slot_index`, move `slot_read_at`'s cold hook path out of
  line, guard `twin_slots`/`texback`/`exact`/`slot_img`/`shown`/`slot_retry`
  lookups in `slot_reset`/`write_null_slot`/`slot_exit` with `SlotFlags`,
  and skip `twin_slots` lookup on image SRVs.
- `live/slots.rs`, `lib.rs`: remove `resolved.clone()` and redundant
  second `slot_cache.holds` on misses, fast-path empty `forget_slots`,
  and batch `pf_count_n("be: write_view (descriptor writes; count only)")`
  once per `slot_reads` call.
- `notes.rs`: switch `Issues` from `BTreeMap` to `FxMap` (sorting keys
  alphabetically only in `window_lines`).
- `oversize.txt`: lower `greenfield/vkback/src/lib.rs` from 4378 to 4375.
- Kept (`AGENTS.md` rule 24): reduces per-slot bookkeeping and improves both
  renderer CPU time and frame throughput cleanly.

Measured (prison cell, steady state, 0..140 world s, A/B/B/A vs `main`
7403bee, AMD Ryzen 9 7900X + RX 9070 XT RADV Mesa 26.3.1, `VKB_LAT` +
`frames.py 0 140 --no-perf`):
- `live: slot_reads at Execute`: 10.44 / 10.21 ms/f (A mean 10.33) ->
  8.55 / 8.53 ms/f (B mean 8.54, -1.79 ms/f, -17.3%), cleanly separated.
- Unit cost: 59.5 / 59.2 -> 51.3 / 50.9 ns/slot_read (-13.9%);
  177.3 / 177.3 -> 154.3 / 152.9 ns/miss (-13.4%).
- Renderer busy: 29.8 / 29.9 -> 28.5 / 28.6 ms/f (-1.30 ms/f; `last 240`:
  28.1 / 28.2 -> 26.5 / 26.3 ms/f, -1.75 ms/f), cleanly separated.
- Settled `last 240 frames` frame time: 38.5 / 38.0 -> 37.1 / 37.3 ms
  (-1.05 ms/f).
- Whole-frame throughput & tail: 24.97 / 25.15 (A mean 25.06 fps, mean
  39.90 ms, max 267.5 / 302.4 ms) -> 25.26 / 25.23 (B mean 25.25 fps, mean
  39.60 ms, max 225.1 / 212.0 ms), cleanly separated.
- Verified locally with zero visual artifacts across the full 0..140 s run.

Verification:
- `cargo test -p vkback --lib -j 6` + `cargo test -p vkback --test policy -j 6`
  + `cargo xtask policy`: all pass.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 17:49:43 +00:00
The comment still described the single-page cache this branch replaced
(AGENTS.md rule 3: comments say what is true now).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot changed target branch from main to gh-pr/13/base 2026-11-30 20:46:40 +00:00
github manually merged commit bbd2e0bb3a into gh-pr/13/base 2026-11-30 20:46:41 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-11-30 20:46 UTC · original

Thanks, nice work, and the A B B A measurements made it easy to judge. Merging.

I checked the flag gating against every place that creates per-slot state (twin slots, texture write-back, unaligned-view copies, shown images, buffer stride entries, retries): each sets its flag, and a flag left set only costs a cleanup, so nothing can go stale. I pushed one small commit onto your branch: the module comment of slotcache.rs still described the single-page cache (AGENTS.md rule 3: comments say what is true now).

Since today, changes to Vulkan code need a validation run with no new errors (AGENTS.md rule 30, roadmap "Vulkan validation errors"). I ran it for this PR (house scene, about 50 s in the world): nothing new.

#14 and #15 change the same files, so they need a rebase onto main after this one. Please also add the validation run to them (rule 30 explains how).

**@⁠vktory6** on GitHub · 2026-11-30 20:46 UTC · [original](https://github.com/vktory6/VKtory/pull/13#issuecomment-7291443106) Thanks, nice work, and the A B B A measurements made it easy to judge. Merging. I checked the flag gating against every place that creates per-slot state (twin slots, texture write-back, unaligned-view copies, shown images, buffer stride entries, retries): each sets its flag, and a flag left set only costs a cleanup, so nothing can go stale. I pushed one small commit onto your branch: the module comment of `slotcache.rs` still described the single-page cache (AGENTS.md rule 3: comments say what is true now). Since today, changes to Vulkan code need a validation run with no new errors (AGENTS.md rule 30, roadmap "Vulkan validation errors"). I ran it for this PR (house scene, about 50 s in the world): nothing new. #14 and #15 change the same files, so they need a rebase onto main after this one. Please also add the validation run to them (rule 30 explains how).
This discussion has been locked. Commenting is limited to contributors.
No description provided.