vkback + frame: avoid View clones, Vec allocs, and double cache lookups in slot_reads #116

Manually merged
github merged 3 commits from github-bot/gh-116 into gh-pr/116/base 2026-12-01 16:12:03 +00:00 AGit
Owner

@⁠olafix52 on GitHub · 2026-12-01 15:54 UTC · original
olafix52:perf/slot-reads-zero-copy → main · +347 −140 · 7 files · 3 commits · merged 2026-12-01 16:09 UTC

What

Fixes part of docs/TASKS.md item "Renderer CPU: index slots" (pure performance / allocation cleanup; preserves exact console slot resolution and nulling semantics).

  • greenfield/frame/src/views.rs:
    • Return [u64; 2] instead of allocating Vec<u64> in View::resources().
    • Add borrowed ViewRef<'a>, ResolvedRef<'a>, and ViewTable::resolve_ref so live: slot_reads borrows table/shape slices directly without cloning View or copying 16-word Placed arrays; ViewTable::resolve delegates to resolve_ref(slot).to_owned() (single source of truth).
  • greenfield/vkback/src/slotcache.rs:
    • Add Paged::slot_mut so hits and warm-page cache updates/invalidations complete in a single lookup; drop the redundant null check in Paged::page (u64::MAX sentinel never matches addr / PAGE).
  • greenfield/vkback/src/live/slots.rs, greenfield/vkback/src/live.rs:
    • Use slot_mut, resolve_ref, slot_read_at, and ensure_obj (split borrow alongside &self.table) in slot_reads_inner.
  • greenfield/vkback/src/slotwrite.rs, greenfield/vkback/src/bufview.rs, greenfield/vkback/src/lib.rs:
    • Add VulkanBackend::write_slot_uncounted taking ResolvedRef<'_>, avoiding copies on the ViewRef::Placed path and parsing ResourceDesc once instead of three times; write_slot(&SlotView) delegates to it.
    • Guard slot_dbg.get_mut with !self.slot_dbg.is_empty().
  • Kept (AGENTS.md rule 24): removes per-miss heap allocations and large struct copies cleanly, cuts ~1.2-1.3 ms/frame renderer CPU in steady state, and scales with higher slot-read scenes and once GPU/title bottlenecks drop.

The reference

No counterpart: slot_reads_inner, ViewTable, and SlotCache are live-stream decoding/caching infrastructure specific to vkback (docs/AGENTS.md rule 12). All descriptor-resolution semantics, write_slot exit invariants, and ResourceDesc parsing remain unchanged.

Evidence

  • Machine: AMD Ryzen 9 7900X, RX 9070 XT (RADV Mesa 26.3.1), Linux.
  • Unit tests, clippy, policy (-j 6):
    • cargo test -p frame -p vkback --lib -j 6: frame 12 passed, vkback 155 passed (3 ignored), 0 failed (includes new resolve_ref_and_resources_match_owned_across_all_variants and paged_slot_mut_updates_existing_without_allocating_new_pages calling production paths).
    • cargo test -p vkback --test policy -j 6: 6 passed, 0 failed.
    • cargo clippy --workspace --all-targets -j 6 -- -D warnings: 0 warnings.
    • cargo xtask policy: 516 files ok.
  • Vulkan validation run (docs/AGENTS.md rule 30):
    • VK_LAYER_KHRONOS_validation 1.4.363 (VK_KHRONOS_VALIDATION_REPORT_FLAGS=error, VK_KHRONOS_VALIDATION_DUPLICATE_MESSAGE_LIMIT=3) on the available world save (153.1 s startup + 66.0 s in the world):
    • 0 new errors beyond docs/TASKS.md ("Vulkan validation errors"); 4 distinct VUIDs observed:
      • VUID-VkBufferCreateInfo-size-06409
      • VUID-VkComputePipelineCreateInfo-layout-01687
      • VUID-VkGraphicsPipelineCreateInfo-layout-01688
      • VUID-VkRayTracingPipelineCreateInfoKHR-layout-03428
  • Interleaved A/B/B/A measurement (VKB_LAT + greenfield/tools/frec/frames.py 0 140 --no-perf, main dab263d vs perf/slot-reads-zero-copy 49bcc44):
Metric A1 (main) B1 (new) B2 (new) A2 (main) A mean -> B mean
live: slot_reads at Execute 8.68 ms/f 7.80 ms/f 8.02 ms/f 8.61 ms/f 8.64 -> 7.91 ms/f (-8.5%)
live: slot_reads in lists 2.69 ms/f 2.59 ms/f 2.64 ms/f 2.69 ms/f 2.69 -> 2.62 ms/f (-2.6%)
Steady miss cost (ns/miss) 147.2 ns 142.0 ns 134.3 ns 151.9 ns 149.6 -> 138.1 ns (-7.7%)
renderer busy (whole run) 27.5 ms/f 25.8 ms/f 26.1 ms/f 27.0 ms/f 27.25 -> 25.95 ms/f (-1.30 ms/f)
renderer busy (last 240) 25.6 ms/f 24.8 ms/f 24.5 ms/f 25.8 ms/f 25.70 -> 24.65 ms/f (-1.05 ms/f)
Renderer thread CPU (vk.log) 25.43 ms/f 23.78 ms/f 24.19 ms/f 24.98 ms/f 25.20 -> 23.98 ms/f (-1.22 ms/f)
Whole-frame FPS (mean ms) 25.52 (39.2) 25.30 (39.5) 25.19 (39.7) 24.93 (40.1) 25.23 (39.65) -> 25.25 (39.60)

Whole-frame FPS stays within variance because GPU busy (~27 ms) and title wait absorb the slack; all renderer CPU metrics separate cleanly across every arm, and loud.txt shows 0 new rule32 errors across all runs.

Checklist (docs/AGENTS.md)

  • One topic (rule 31); docs/TASKS.md not edited, the fixed item named above (rule 29)
  • "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits)
  • No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9)
  • Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b)
  • Every switch is listed, read in one place, and a bad value stops the start (rule 2)
  • Queues and other externally synchronized Vulkan objects are used under their lock (rule 35)
  • Every unsafe block has a // SAFETY: comment; no raw pointer around the borrow checker (rule 36)
  • Clippy clean; tests call the production code; TRANSLATOR_VERSION bumped if the translator's output changes (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 (hard constraints)
**@⁠olafix52** on GitHub · 2026-12-01 15:54 UTC · [original](https://github.com/vktory6/VKtory/pull/116) `olafix52:perf/slot-reads-zero-copy` → `main` · +347 −140 · 7 files · 3 commits · merged 2026-12-01 16:09 UTC ## What Fixes part of `docs/TASKS.md` item **"Renderer CPU: index slots"** (pure performance / allocation cleanup; preserves exact console slot resolution and nulling semantics). - `greenfield/frame/src/views.rs`: - Return `[u64; 2]` instead of allocating `Vec<u64>` in `View::resources()`. - Add borrowed `ViewRef<'a>`, `ResolvedRef<'a>`, and `ViewTable::resolve_ref` so `live: slot_reads` borrows table/shape slices directly without cloning `View` or copying 16-word `Placed` arrays; `ViewTable::resolve` delegates to `resolve_ref(slot).to_owned()` (single source of truth). - `greenfield/vkback/src/slotcache.rs`: - Add `Paged::slot_mut` so hits and warm-page cache updates/invalidations complete in a single lookup; drop the redundant null check in `Paged::page` (`u64::MAX` sentinel never matches `addr / PAGE`). - `greenfield/vkback/src/live/slots.rs`, `greenfield/vkback/src/live.rs`: - Use `slot_mut`, `resolve_ref`, `slot_read_at`, and `ensure_obj` (split borrow alongside `&self.table`) in `slot_reads_inner`. - `greenfield/vkback/src/slotwrite.rs`, `greenfield/vkback/src/bufview.rs`, `greenfield/vkback/src/lib.rs`: - Add `VulkanBackend::write_slot_uncounted` taking `ResolvedRef<'_>`, avoiding copies on the `ViewRef::Placed` path and parsing `ResourceDesc` once instead of three times; `write_slot(&SlotView)` delegates to it. - Guard `slot_dbg.get_mut` with `!self.slot_dbg.is_empty()`. - Kept (`AGENTS.md` rule 24): removes per-miss heap allocations and large struct copies cleanly, cuts ~1.2-1.3 ms/frame renderer CPU in steady state, and scales with higher slot-read scenes and once GPU/title bottlenecks drop. ## The reference No counterpart: `slot_reads_inner`, `ViewTable`, and `SlotCache` are live-stream decoding/caching infrastructure specific to `vkback` (`docs/AGENTS.md` rule 12). All descriptor-resolution semantics, `write_slot` exit invariants, and `ResourceDesc` parsing remain unchanged. ## Evidence - **Machine:** AMD Ryzen 9 7900X, RX 9070 XT (RADV Mesa 26.3.1), Linux. - **Unit tests, clippy, policy (`-j 6`):** - `cargo test -p frame -p vkback --lib -j 6`: `frame` 12 passed, `vkback` 155 passed (3 ignored), 0 failed (includes new `resolve_ref_and_resources_match_owned_across_all_variants` and `paged_slot_mut_updates_existing_without_allocating_new_pages` calling production paths). - `cargo test -p vkback --test policy -j 6`: 6 passed, 0 failed. - `cargo clippy --workspace --all-targets -j 6 -- -D warnings`: 0 warnings. - `cargo xtask policy`: 516 files ok. - **Vulkan validation run (`docs/AGENTS.md` rule 30):** - `VK_LAYER_KHRONOS_validation` 1.4.363 (`VK_KHRONOS_VALIDATION_REPORT_FLAGS=error`, `VK_KHRONOS_VALIDATION_DUPLICATE_MESSAGE_LIMIT=3`) on the available world save (153.1 s startup + 66.0 s in the world): - **0 new errors** beyond `docs/TASKS.md` ("Vulkan validation errors"); **4 distinct VUIDs observed:** - `VUID-VkBufferCreateInfo-size-06409` - `VUID-VkComputePipelineCreateInfo-layout-01687` - `VUID-VkGraphicsPipelineCreateInfo-layout-01688` - `VUID-VkRayTracingPipelineCreateInfoKHR-layout-03428` - **Interleaved A/B/B/A measurement (`VKB_LAT` + `greenfield/tools/frec/frames.py 0 140 --no-perf`, `main` `dab263d` vs `perf/slot-reads-zero-copy` `49bcc44`):** | Metric | A1 (`main`) | B1 (`new`) | B2 (`new`) | A2 (`main`) | A mean -> B mean | |---|---:|---:|---:|---:|---| | `live: slot_reads at Execute` | 8.68 ms/f | 7.80 ms/f | 8.02 ms/f | 8.61 ms/f | **8.64 -> 7.91 ms/f (-8.5%)** | | `live: slot_reads in lists` | 2.69 ms/f | 2.59 ms/f | 2.64 ms/f | 2.69 ms/f | **2.69 -> 2.62 ms/f (-2.6%)** | | Steady miss cost (`ns/miss`) | 147.2 ns | 142.0 ns | 134.3 ns | 151.9 ns | **149.6 -> 138.1 ns (-7.7%)** | | `renderer busy` (whole run) | 27.5 ms/f | 25.8 ms/f | 26.1 ms/f | 27.0 ms/f | **27.25 -> 25.95 ms/f (-1.30 ms/f)** | | `renderer busy` (`last 240`) | 25.6 ms/f | 24.8 ms/f | 24.5 ms/f | 25.8 ms/f | **25.70 -> 24.65 ms/f (-1.05 ms/f)** | | Renderer thread CPU (`vk.log`) | 25.43 ms/f | 23.78 ms/f | 24.19 ms/f | 24.98 ms/f | **25.20 -> 23.98 ms/f (-1.22 ms/f)** | | Whole-frame FPS (mean ms) | 25.52 (39.2) | 25.30 (39.5) | 25.19 (39.7) | 24.93 (40.1) | 25.23 (39.65) -> 25.25 (39.60) | Whole-frame FPS stays within variance because GPU busy (~27 ms) and title wait absorb the slack; all renderer CPU metrics separate cleanly across every arm, and `loud.txt` shows 0 new `rule32` errors across all runs. ## Checklist (docs/AGENTS.md) - [x] One topic (rule 31); `docs/TASKS.md` not edited, the fixed item named above (rule 29) - [x] "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits) - [x] No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9) - [x] Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b) - [x] Every switch is listed, read in one place, and a bad value stops the start (rule 2) - [x] Queues and other externally synchronized Vulkan objects are used under their lock (rule 35) - [x] Every `unsafe` block has a `// SAFETY:` comment; no raw pointer around the borrow checker (rule 36) - [x] Clippy clean; tests call the production code; `TRANSLATOR_VERSION` bumped if the translator's output changes (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 (hard constraints)
Remove per-miss heap allocations, large `View` clones, and redundant
lookups on the renderer `live: slot_reads` hot path (`docs/TASKS.md`:
"Renderer CPU: index slots"):

- `greenfield/frame/src/views.rs`:
  - Return `[u64; 2]` instead of allocating `Vec<u64>` in `View::resources()`.
  - Add borrowed `ViewRef<'a>`, `ResolvedRef<'a>`, and `ViewTable::resolve_ref`
    so live slot reads borrow table/shape slices directly without cloning
    `View` or copying 16-word `Placed` arrays; `ViewTable::resolve` delegates
    to `resolve_ref(slot).to_owned()` (single source of truth).
- `greenfield/vkback/src/slotcache.rs`:
  - Add `Paged::slot_mut` so hits and warm-page cache updates/invalidations
    complete in a single page lookup, and drop the redundant null check in
    `Paged::page` (u64::MAX sentinel never matches `addr / PAGE`).
- `greenfield/vkback/src/live/slots.rs`, `live.rs`:
  - Use `slot_mut`, `resolve_ref`, `slot_read_at`, and `ensure_obj` (clean
    split borrow alongside `&self.table`) in `slot_reads_inner`.
- `greenfield/vkback/src/slotwrite.rs`, `bufview.rs`, `lib.rs`:
  - Add `VulkanBackend::write_slot_uncounted` taking `ResolvedRef<'_>`,
    avoiding copies on the `ViewRef::Placed` path and parsing
    `ResourceDesc` once instead of three times; keep `write_slot(&SlotView)`
    delegating to it.
  - Guard `slot_dbg.get_mut` with `!self.slot_dbg.is_empty()`.
- Kept (`AGENTS.md` rule 24): eliminates per-miss heap allocation and large
  struct copies cleanly, saves ~1.2-1.3 ms/f renderer CPU in steady state, and
  scales with higher slot-read scenes and once GPU/title bottlenecks drop.

Measured (reference scene, steady state, 0..140 world s, A/B/B/A vs `main`
`dab263d`, 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 (index slots)`: 8.68 / 8.61 ms/f (A mean 8.64) ->
  7.80 / 8.02 ms/f (B mean 7.91, -0.73 ms/f, -8.5%), cleanly separated.
- `live: slot_reads in lists`: 2.69 / 2.69 ms/f -> 2.59 / 2.64 ms/f (B mean 2.62).
- Steady miss cost: 147.2 / 151.9 ns/miss (A mean 149.6) -> 142.0 / 134.3 ns/miss
  (B mean 138.1, -7.7%), cleanly separated.
- Whole-run renderer busy: 27.5 / 27.0 ms/f (A mean 27.25) -> 25.8 / 26.1 ms/f
  (B mean 25.95, -1.30 ms/f); `last 240` renderer busy: 25.6 / 25.8 ms (25.70) ->
  24.8 / 24.5 ms (24.65, -1.05 ms/f).
- Renderer thread on CPU: 25.43 / 24.98 ms/f (25.20) -> 23.78 / 24.19 ms/f
  (23.98, -1.22 ms/f), cleanly separated.
- Whole-frame FPS stayed within variance (A: 25.52 / 24.93, mean 25.23 fps,
  39.65 ms vs B: 25.30 / 25.19, mean 25.25 fps, 39.60 ms) alongside ~27 ms GPU
  busy and title wait.

Verification:
- `cargo test -p frame -p vkback --lib -j 6`: frame 12 passed, vkback 155 passed
  (3 ignored), 0 failed.
- `cargo test -p vkback --test policy -j 6`: 6 passed, 0 failed.
- `cargo clippy --workspace --all-targets -j 6 -- -D warnings`: 0 warnings.
- `cargo xtask policy`: 516 files ok.
- Vulkan validation run (`VK_LAYER_KHRONOS_validation` 1.4.363, 153.1 s startup +
  66.0 s in the world): 4 distinct VUIDs (`VUID-VkBufferCreateInfo-size-06409`,
  `VUID-VkComputePipelineCreateInfo-layout-01687`,
  `VUID-VkGraphicsPipelineCreateInfo-layout-01688`,
  `VUID-VkRayTracingPipelineCreateInfoKHR-layout-03428`), 0 unlisted VUIDs,
  0 new loud (`rule32`) errors across all runs.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 15:55:53 +00:00
Paged::remove drops an odd address's entry; storing zeros through
slot_mut kept it, and zero words would then read as a hit.
github-bot changed target branch from main to gh-pr/116/base 2026-12-01 16:12:01 +00:00
github manually merged commit f32089c4fc into gh-pr/116/base 2026-12-01 16:12:03 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-12-01 16:09 UTC · original

Merged as 28ccd9f, thanks! Clean change, and the A/B/B/A table made it easy to judge.

I pushed one small commit first (90f3651): when a slot has to be tried again, the cache entry is now removed (Paged::remove) instead of set to zeros through slot_mut. For addresses off the 16-byte grid, zeros would later count as a hit for empty words. That never happens in practice, but now it behaves exactly as before. The validation run on the merged tree showed no new VUIDs.

**@⁠vktory6** on GitHub · 2026-12-01 16:09 UTC · [original](https://github.com/vktory6/VKtory/pull/116#issuecomment-7317129775) Merged as 28ccd9f, thanks! Clean change, and the A/B/B/A table made it easy to judge. I pushed one small commit first (90f3651): when a slot has to be tried again, the cache entry is now removed (`Paged::remove`) instead of set to zeros through `slot_mut`. For addresses off the 16-byte grid, zeros would later count as a hit for empty words. That never happens in practice, but now it behaves exactly as before. The validation run on the merged tree showed no new VUIDs.
This discussion has been locked. Commenting is limited to contributors.
No description provided.