runtime: cache ScratchBuffers device addresses and use best-fit largest-4 spare reuse (RT-VK-SCRATCH-BEST-FIT-AND-CACHE) #1898

Open
github-bot wants to merge 1 commit from github-bot/gh-1898 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 15:07 UTC · original
IlyassRS3:fix/rt-vk-scratch-best-fit-and-cache → main · +173 −11 · 2 files · 1 commits · open

Item ID: RT-VK-SCRATCH-BEST-FIT-AND-CACHE (claimed in #1608).

Summary

Optimizes leonida::ScratchBuffers (runtime/src/vulkan_scratch.h) for continuous per-frame indirect clamping, quad index expansion, and staged transfer workloads:

  1. Cache VkDeviceAddress once per chunk (make_chunk / claim):
    • Chunk now caches VkDeviceAddress address once when the chunk is created (when addressable_ is true), instead of calling vkGetBufferDeviceAddress through the Vulkan driver on every single get(..., address) suballocation.
    • Non-addressable pools still never invoke vkGetBufferDeviceAddress.
  2. Best-fit spare selection (claim):
    • Picks the smallest spare chunk with size >= bytes instead of first-fit, so small suballocations do not consume a 16+ MiB spare chunk and trigger fresh large VkDeviceMemory allocations later in the same frame.
  3. Retain the 4 largest reusable chunks (collect):
    • When spare_ is already at the 4-chunk cap, a completed chunk larger than the smallest spare replaces and destroys that smallest spare instead of being destroyed itself, preventing repeated large buffer/VRAM allocation churn across frames while keeping the cap strictly at 4.
  4. Clean failure state (make_chunk):
    • Resets out = {} on entry and when vkCreateBuffer fails so a non-conforming driver/mock writing a handle on error cannot leave stale state.

Verification

  • AI-assisted: yes.
  • GPUs tested: none (macOS host, CPU-only portable Vulkan-Headers mock harness; no game files used).
  • runtime/test/vulkan_scratch_test.cpp (extended with cached-address, best-fit, largest-4 eviction, and failing vkCreateBuffer tests; Clang 20, C++20, -Wall -Wextra -Werror):
    • ASan + UBSan: PASS
    • TSan: PASS
    • -O2 × 3: PASS
  • Downstream header syntax check (vulkan_indirect_clamp.h + vulkan_scratch.h): PASS.
  • Mutation test against main's vulkan_scratch.h: fails on main (vk.get_device_address_calls == 1), passes on this branch.
  • Hygiene: python3 -I scripts/check_no_game_data.py and python3 -I tests/test_text_write_line_endings.py passed.
**@⁠IlyassRS3** on GitHub · 2026-11-25 15:07 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1898) `IlyassRS3:fix/rt-vk-scratch-best-fit-and-cache` → `main` · +173 −11 · 2 files · 1 commits · open Item ID: `RT-VK-SCRATCH-BEST-FIT-AND-CACHE` (claimed in #1608). ### Summary Optimizes `leonida::ScratchBuffers` (`runtime/src/vulkan_scratch.h`) for continuous per-frame indirect clamping, quad index expansion, and staged transfer workloads: 1. **Cache `VkDeviceAddress` once per chunk (`make_chunk` / `claim`)**: - `Chunk` now caches `VkDeviceAddress address` once when the chunk is created (when `addressable_` is true), instead of calling `vkGetBufferDeviceAddress` through the Vulkan driver on every single `get(..., address)` suballocation. - Non-addressable pools still never invoke `vkGetBufferDeviceAddress`. 2. **Best-fit spare selection (`claim`)**: - Picks the smallest spare chunk with `size >= bytes` instead of first-fit, so small suballocations do not consume a 16+ MiB spare chunk and trigger fresh large `VkDeviceMemory` allocations later in the same frame. 3. **Retain the 4 largest reusable chunks (`collect`)**: - When `spare_` is already at the 4-chunk cap, a completed chunk larger than the smallest spare replaces and destroys that smallest spare instead of being destroyed itself, preventing repeated large buffer/VRAM allocation churn across frames while keeping the cap strictly at 4. 4. **Clean failure state (`make_chunk`)**: - Resets `out = {}` on entry and when `vkCreateBuffer` fails so a non-conforming driver/mock writing a handle on error cannot leave stale state. ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (macOS host, CPU-only portable Vulkan-Headers mock harness; no game files used). - `runtime/test/vulkan_scratch_test.cpp` (extended with cached-address, best-fit, largest-4 eviction, and failing `vkCreateBuffer` tests; Clang 20, C++20, `-Wall -Wextra -Werror`): - ASan + UBSan: PASS - TSan: PASS - `-O2` × 3: PASS - Downstream header syntax check (`vulkan_indirect_clamp.h` + `vulkan_scratch.h`): PASS. - Mutation test against `main`'s `vulkan_scratch.h`: fails on `main` (`vk.get_device_address_calls == 1`), passes on this branch. - Hygiene: `python3 -I scripts/check_no_game_data.py` and `python3 -I tests/test_text_write_line_endings.py` passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:10:33 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 15:07 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 15:07 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1898#pullrequestreview-6060162690) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
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/1898/head:github-bot/gh-1898'
git switch 'github-bot/gh-1898'

Merge

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