Skip to content

Runtime: VulkanQueue keeps its PreparedList storage between executions (layer CPU cost per command halved) - #893

Merged
ItsNotRexed merged 1 commit into
ProjectLeonida:mainfrom
traeb0n:prepared-list-reuse
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ProjectLeonida:mainfrom
traeb0n:prepared-list-reuse

Conversation

@traeb0n

@traeb0n traeb0n commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Depends on: #878 (the benchmark; this branch carries its commit). Follow-up to my PreparedList finding (claim).

What

VulkanQueue::execute built a new std::vector<PreparedList> on every call. So every command's detail::Prepared entry (528 bytes) was made in fresh memory and freed at the end, roughly 11 MB per 21k-command frame. The queue now keeps that vector as a member, prepared_. It's guarded by the context mutex, which execute already holds, and resized to the list count each call. prepare already refills list, commands and unpredicated_copies, so entries are rewritten in place. The per-entry vectors also keep their capacity, which removes the allocations copies and UAV clears made in prepare. The change is the declaration line plus one member. The open PRs that touch execute (#306, #419, #856, #860) edit the prepare/emit call lines, not this one.

Memory: a queue now holds on to its largest execution's entries, about 528 bytes per command (11 MB for 21k commands), instead of allocating that much every frame.

Numbers

replay_cpu_bench from #878, i7-14700K, Windows 11, MSVC 14.44, RelWithDebInfo, three runs each:

ns per command before after
frame (8 lists × 128 passes, 21,504 commands) 195–290 97–99
one frame, layer only 4.2–6.2 ms 2.1 ms
transition pair 166–189 93
copy texture region 249–427 119
allocations per command, prepare (frame) 0.33 0.19

The benchmark now mirrors the kept storage, and prints both floors: entries refilled in kept storage, about 26 ns, and made fresh, about 110 ns. Full output after:

ns per command               commands |    record   prepare      emit     total | alloc:r      p      e |    vk
transition pair                  4096 |       6.4      34.5      51.7      92.7 |   0.00   0.00   1.00 |  1.00
buffer barrier (enhanced)        4096 |       6.4      33.0      47.7      87.1 |   0.00   0.00   0.00 |  1.00
clear RTV, whole view            4096 |      11.6      35.6      62.4     109.7 |   0.00   0.00   1.00 |  3.00
clear RTV, 2 rects               4096 |      65.9     103.2      75.1     244.2 |   2.00   2.00   1.00 |  5.00
clear DSV, whole view            4096 |      11.8      36.0      61.1     108.9 |   0.00   0.00   1.00 |  3.00
copy buffer region               4096 |       7.2      36.6       5.2      49.0 |   0.00   0.00   0.00 |  1.00
copy texture region              4096 |      20.4      59.2      39.5     119.0 |   0.00   0.00   1.00 |  2.00
clear UAV (buffer)               4096 |      20.2     147.0      32.2     199.4 |   0.00   0.00   0.00 |  3.00
occlusion Begin+End              4096 |       8.0      33.1       6.1      47.2 |   0.00   0.00   0.00 |  1.50
timestamp                        4096 |       7.0      37.5       5.7      50.2 |   0.00   0.00   0.00 |  2.00
resolve 16 queries               4096 |      11.6      36.5      12.0      60.1 |   0.00   0.00   0.00 |  1.00
predication on+off               4096 |      10.8      31.3       1.6      43.7 |   0.00   0.00   0.00 |  1.00
frame (8 lists x 128 passes)    21504 |      14.9      45.8      37.5      98.2 |   0.19   0.19   0.43 |  1.86
  entries, storage kept         21504 |                25.8                25.8 |
  entries, made fresh           21504 |               109.4               109.4 |

frame: 2111.6 us per ExecuteCommandLists of 21504 commands, layer only (no driver)
one PreparedList entry (detail::Prepared): 528 bytes; one recorded Command: 136 bytes

Tests

  • d3d12_vulkan_device_test gains a block that executes {B, A}, then {B} alone, on its own fence so the main fence's values are untouched. B clears blue and A red, so the target must end blue. Negative control: with storage that only grows, the stale A is replayed after B, and the block fails with got ff 00 00 ff, want 00 00 ff ff (checked, then reverted).
  • RTX 5070, driver 621.01, Windows 11, Vulkan SDK 1.4.363: ctest -E loader 29/29. loader_test is excluded because it hangs on this PC; that's unrelated and also happens on main. d3d12_vulkan_device_test and d3d12_com_test with LEONIDA_VK_VALIDATION=1 pass with 0 validation errors.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@quackottu quackottu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested e550911 on NVIDIA GeForce RTX 5070, driver 621.24, Windows 11 Pro (26300), VS 2022 Build Tools, Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1 and LEONIDA_D3D12=vulkan: pass

  • ctest -C RelWithDebInfo: 100% tests passed, 0 tests failed out of 33
  • d3d12_vulkan_device_test (changed here): exit=0 validation=0 | [rt] d3d12: upload heaps in VRAM (DEVICE_LOCAL | HOST_VISIBLE, Resizable BAR)
  • 32 test programs in the build pass with 0 validation errors
  • Not run (they take an input file, not covered here): vk_binary_occlusion_test
  • d3d12_clear_test fails (R8G8B8A8_UNORM/SNORM 0.5 ties and a D16 clear byte differ from WARP) exactly as on main 4a36bb1 here, so not from this PR; #521 addresses it

Implicit Vulkan layers (overlays) were off through an empty VK_IMPLICIT_LAYER_PATH.

@Parakeet-Wheat

Copy link
Copy Markdown
Contributor

Tested e550911 on an NVIDIA GeForce RTX 3070, driver 610.88, AMD Ryzen 5 5600X, Windows 11 (26300), MSVC 19.44 (VS generator, RelWithDebInfo), Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1: pass

  • Build passes. The only warnings are main's C4456 at vulkan_replay.h:1372. ctest: 27/27, including replay_cpu_bench_selftest (loader_test doesn't hang here).
  • d3d12_vulkan_device_test exits 0 with 0 validation and 0 synchronization-hazard messages, so the new {B, A} then {B} block reads back blue. 0 of 69 copy cases differ. vk_command_replay_test A to F and d3d12_com_test pass, with no validation messages.

replay_cpu_bench on the 5600X, three runs (ns per command):

run 1 run 2 run 3
frame (21,504 commands) 226.9 226.4 209.6
one frame, layer only 4.88 ms 4.87 ms 4.51 ms
entries, storage kept 44.3 99.7 43.6
entries, made fresh 103.1 168.4 96.2

So on Zen 3 the entry floor drops by about 55 ns per command (run 2 was noisy on both rows), about 1.2 ms of the 4.5–4.9 ms frame. Absolute numbers are roughly twice the 14700K's, but the kept/fresh ratio is about the same (2.2–2.3×). Allocations per command in the frame match yours (0.19 / 0.19 / 0.43).

Code read: the change looks right to me. execute holds the context mutex for its whole body, so the member is safe. prepare resets each entry with commands.assign(n, {}), which copy-assigns into the existing elements, so the inner vectors keep their capacity as described. A failed prepare leaves stale entries behind, but they're rewritten before the next emit, and your new block is the right negative control for that.

One small thing: the description's list of open PRs that touch execute misses #889 (binary occlusion). It inserts a ReplayFunctions replay = context_.replay_; copy right after the declaration this PR changes, so the two will probably conflict textually. The fix is trivial either way.

AI-assisted (Claude Code).

@UlteriorMotive00

Copy link
Copy Markdown
Contributor

Tested e550911 on AMD RDNA2 iGPU and RTX 5070: pass.

Windows 11 Home 10.0.26300, MSVC 14.44.35207, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, Ninja RelWithDebInfo (scripts\build_runtime.ps1), LEONIDA_VK_VALIDATION=1, VK_LOADER_LAYERS_DISABLE=~implicit~. One PC, two GPUs, picked with VK_LOADER_DRIVERS_SELECT: AMD Radeon iGPU of a Ryzen 7 9800X3D (RDNA2, 1002:13C0, AMD 2026.04.17, subgroup 32..64) and NVIDIA GeForce RTX 5070 (Blackwell, 617.42, subgroup 32).

  • ctest: RTX 5070 33/33; AMD iGPU 32/33, every replay, copy, clear and barrier test passing with no validation errors. The AMD d3d12_vulkan_device failure is not from this PR: it is OPTIONS19.SupportedSampleCountsWithNoOutputs: ours 0xf not subset of host 0x1d, which this branch's base still has and main has since fixed (no-output sample counts without 2x).
  • I didn't measure CPU time, only correctness.

AI-assisted (Claude Code). No game files.

execute built a new std::vector<PreparedList> on every call, so every
command's detail::Prepared entry (1288 bytes on main now) was made in
fresh memory and freed again: replay_cpu_bench measures that at 274-292
ns per command on an i7-14700K, against 85 ns refilled in kept storage,
over half of the layer's whole CPU cost per command. The queue now keeps
the vector as a member (per queue, like its command buffers: one execute
at a time on a queue, as acquire() and submit() already assume) and
resizes it to the list count; prepare refills each list's entries in
place, where the per-entry vectors also keep their capacity.

replay_cpu_bench mirrors the change and reports both floors (entries in
kept storage and made fresh). Its frame goes from 425 to 194 ns per
command (a 21,504-command frame from 9.1 ms to 4.2 ms).

d3d12_vulkan_device_test executes {B, A}, then {B} alone, and checks
that only B ran the second time: with storage that only grows, a stale A
is replayed after B and the test fails (checked).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@traeb0n
traeb0n force-pushed the prepared-list-reuse branch from e550911 to 9df4eda Compare November 22, 2026 15:20
traeb0n added a commit to traeb0n/Leonida that referenced this pull request Nov 22, 2026
Three allocations replay_cpu_bench (ProjectLeonida#878) found on the hot path:

- detail::current returned a std::vector<LayoutPiece> for every
  transition, clear and copy in emit, though the answer is nearly always
  one piece (two with separate depth/stencil). LayoutPieces keeps two
  inline and spills to the heap beyond that, with the same interface
  (begin/end, size, push_back, clear), so `auto pieces = current(...)`
  callers compile as is. LayoutTracker::pieces takes either container.
- detail::clip built a new vector with push_back (two allocations for two
  rectangles); it now writes into the kept entry's p.rects.
- CommandList::append took const Command& and copied the temporary the
  recording calls build, so a rectangle clear's vector was allocated
  twice; an rvalue overload moves it.

replay_cpu_bench, i7-14700K, on top of ProjectLeonida#893: the frame goes from 194 to
173 ns per command, emit from 48 to 33 ns; emit makes no allocation per
command in the frame (0.43 before) and prepare 0.07 (0.26), and
recording makes one only for a rectangle clear's own rectangles.
d3d12_state_map_test checks LayoutPieces against the vector path,
including the spill at four pieces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@traeb0n

traeb0n commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 6a6704d (9df4eda); #878 is merged, so this is one commit now. Thanks @Parakeet-Wheat, @UlteriorMotive00 and @quackottu for the runs on e550911.

What changed in the rebase:

  • execute() on main no longer holds a context-wide mutex, so prepared_ is now documented as per-queue state like buffers_ (one execute at a time on a queue, as acquire()/submit() already assume). prepare() still resets every PreparedList field, including the ones added since (queries, segment counts, readback stages).
  • The device-test block waits through submit_and_wait() instead of its own fence.
  • The entry is now 1288 bytes, so the gain grew: frame 425 → 194 ns/cmd on an i7-14700K (9.1 → 4.2 ms per 21,504-command frame).

Tested 9df4eda on RTX 5070, driver 621.01, Windows 11, MSVC 14.44, Vulkan SDK 1.4.363: d3d12_vulkan_device_test with LEONIDA_VK_VALIDATION=1 passes, 0 validation errors; replay_cpu_bench --self-test passes. ctest -E loader: all pass except d3d12_compute and d3d12_draw, which fail identically on a clean main 6a6704d build (#1898/#1919 and #1966).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants