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

Open
opened 2026-11-26 21:19:22 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1898
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-vk-scratch-best-fit-and-cache


📝 Commits (1)

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

📊 Changes

2 files changed (+173 additions, -11 deletions)

View changed files

📝 runtime/src/vulkan_scratch.h (+30 -10)
📝 runtime/test/vulkan_scratch_test.cpp (+143 -1)

📄 Description

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1898 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-vk-scratch-best-fit-and-cache` --- ### 📝 Commits (1) - [`a5fa6c0`](https://github.com/ubiquitarianism/leonida/commit/a5fa6c090c3c4225a78d66488a83d30fcec065e8) runtime: cache ScratchBuffers device addresses and use best-fit largest-4 spare reuse (RT-VK-SCRATCH-BEST-FIT-AND-CACHE) ### 📊 Changes **2 files changed** (+173 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/vulkan_scratch.h` (+30 -10) 📝 `runtime/test/vulkan_scratch_test.cpp` (+143 -1) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1909
No description provided.