Skip to content

Runtime: no heap allocation per barrier, clear or copy in replay (Depends on #893) - #901

Merged
ItsNotRexed merged 2 commits into
ProjectLeonida:mainfrom
traeb0n:replay-cpu-allocs
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ProjectLeonida:mainfrom
traeb0n:replay-cpu-allocs

Conversation

@traeb0n

@traeb0n traeb0n commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Depends on: #893 (and through it #878; this branch carries both commits). Claim.

What

Three heap allocations that replay_cpu_bench found on the hot path, each removed without changing a caller's code:

  1. Layout pieces in emit. detail::current returned a std::vector<LayoutPiece> for every transition, clear and copy, even though the answer is nearly always one piece (two with separate depth and stencil). The new LayoutPieces keeps two inline and spills to the heap beyond that. Its interface is the same: begin/end, size, operator[], push_back, clear. So auto pieces = current(...) callers compile unchanged, including the ones Runtime: compute path and graphics draws on Vulkan (D3D-3, D3D-4, milestone M1) #291 and the PRs stacked on it add (Runtime: CreateHeap and CreatePlacedResource on the Vulkan layer (D3D-16) #286, Runtime: variable rate shading on Vulkan, RSSetShadingRate and the shading-rate image (D3D-32) #469, Runtime: primitive restart from the PSO's IBStripCutValue (D3D-57) #572, Runtime: SetSamplePositions and the centre sample pattern on VK_EXT_sample_locations #848, Runtime: viewport and scissor arrays (RSSetViewports, RSSetScissorRects) #873). LayoutTracker::pieces is now a template, so it still fills a std::vector for its existing test.
  2. Rectangle clipping in prepare. detail::clip built a new vector with push_back, which meant two allocations for two rectangles. It now writes into the entry's p.rects, which since Runtime: VulkanQueue keeps its PreparedList storage between executions (layer CPU cost per command halved) #893 keeps its storage between executions. It has one caller.
  3. Recording. CommandList::append(const Command&) copied the temporary that every recording call builds, so a rectangle clear's vector was allocated twice. An append(Command&&) overload moves it, and the recording calls pick it up as they are.

Numbers

replay_cpu_bench, i7-14700K, Windows 11, MSVC 14.44, RelWithDebInfo, three runs:

per command #893 this PR
frame total (21,504 commands) 97–99 ns 76–82 ns
frame emit 37.5 ns 22 ns
clear RTV, 2 rects 244 ns 132 ns
transition pair 93 ns 73 ns
allocations per command, frame: record / prepare / emit 0.19 / 0.19 / 0.43 0.10 / 0 / 0

Together with #893, a 21k-command frame goes from 4.2–6.2 ms to about 1.7 ms of layer CPU time. The one allocation left in the frame is a rectangle clear's own rects while recording.

ns per command               commands |    record   prepare      emit     total | alloc:r      p      e |    vk
transition pair                  4096 |       6.7      32.1      33.9      72.6 |   0.00   0.00   0.00 |  1.00
buffer barrier (enhanced)        4096 |      10.4      36.3      50.3      97.0 |   0.00   0.00   0.00 |  1.00
clear RTV, whole view            4096 |      14.5      43.5      41.7      99.7 |   0.00   0.00   0.00 |  3.00
clear RTV, 2 rects               4096 |      42.7      52.4      37.4     132.4 |   1.00   0.00   0.00 |  5.00
clear DSV, whole view            4096 |      10.0      33.8      34.1      77.9 |   0.00   0.00   0.00 |  3.00
copy buffer region               4096 |       6.8      36.6       5.4      48.7 |   0.00   0.00   0.00 |  1.00
copy texture region              4096 |      15.9      60.1      25.5     101.5 |   0.00   0.00   0.00 |  2.00
clear UAV (buffer)               4096 |      14.5      94.2      18.0     126.6 |   0.00   0.00   0.00 |  3.00
occlusion Begin+End              4096 |       9.7      35.5       5.5      50.7 |   0.00   0.00   0.00 |  1.50
timestamp                        4096 |      11.8      59.0       8.7      79.6 |   0.00   0.00   0.00 |  2.00
resolve 16 queries               4096 |       8.3      37.1      10.2      55.6 |   0.00   0.00   0.00 |  1.00
predication on+off               4096 |       8.5      31.4       1.9      41.8 |   0.00   0.00   0.00 |  1.00
frame (8 lists x 128 passes)    21504 |      15.9      42.2      22.1      80.1 |   0.10   0.00   0.00 |  1.86
  entries, storage kept         21504 |                25.4                25.4 |
  entries, made fresh           21504 |               112.4               112.4 |

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

Not changed: the UAV clear's prepare (about 94 ns) is mostly uav_clear::put writing one bit at a time. A frame has few UAV clears, and that file is where the clear-accuracy work happens, so I left it. Enhanced texture barriers still build a std::vector<VkImageMemoryBarrier2> each time.

Tests

  • d3d12_state_map_test: LayoutPieces gives the same pieces as the vector path for the four-piece separate-depth/stencil case, which takes the spill path. It also checks the one-piece case, and that the two inline pieces are kept, in order, when a third spills.
  • 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 (including Runtime: VulkanQueue keeps its PreparedList storage between executions (layer CPU cost per command halved) #893's multi-list block) and d3d12_com_test with LEONIDA_VK_VALIDATION=1 pass with 0 validation errors.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@Parakeet-Wheat

Copy link
Copy Markdown
Contributor

Tested 8f45677 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

replay_cpu_bench on the same 5600X, three runs each, against the #893 numbers I posted there:

per command #893 this PR
frame total (21,504 commands) 210–227 ns 131–145 ns
frame prepare / emit 97–106 / 83–97 ns 64–73 / 46–49 ns
one frame, layer only 4.5–4.9 ms 2.8–3.1 ms
transition pair 190–199 ns 144–154 ns
copy texture region 239–312 ns 182–189 ns
allocations per command, frame: record / prepare / emit 0.19 / 0.19 / 0.43 0.10 / 0 / 0

So about 35–40% off the frame on Zen 3, a little more than the 14700K's 20%. The allocation counts match yours exactly.

Code read: this looks right to me. LayoutPieces::push_back copies the two inline pieces to the heap before the third, clear() returns to inline storage, and current() clears before its fallback push, so a partial fill from pieces() can't leak through. clip() writing into p.rects is safe on a kept entry: #893's commands.assign(n, {}) empties it first, so the rects.empty() early return doesn't leave stale rectangles behind. I also grepped the open PRs that use these helpers (#291, #286, #469, #572, #848, #873, #856, #860 and #889): none uses a std::vector operation that LayoutPieces lacks, so "callers compile unchanged" holds.

One small thing, not blocking: the default move leaves a moved-from LayoutPieces with its old size_ but an empty heap_. Holding three or more pieces, data() would then fall back to the two-element inline_ with size_ of 3 or more. Nothing uses a moved-from one today (returns are NRVO or moved into a fresh object), but a move constructor and assignment that zero the source's size_ would close that off.

AI-assisted (Claude Code).

@UlteriorMotive00

Copy link
Copy Markdown
Contributor

Tested 8f45677 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.

traeb0n and others added 2 commits November 22, 2026 09:19
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>
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 #893's new head (9d47be3 on 9df4eda, on main 6a6704d); the commit itself applied unchanged. Thanks @Parakeet-Wheat and @UlteriorMotive00 for the runs on 8f45677.

On top of #893: frame 194 → 173 ns/cmd, emit 48 → 33 ns; emit allocations per command 0.43 → 0, prepare 0.26 → 0.07 (i7-14700K).

Tested 9d47be3 on RTX 5070, driver 621.01, Windows 11, MSVC 14.44, Vulkan SDK 1.4.363: d3d12_state_map_test and d3d12_vulkan_device_test (LEONIDA_VK_VALIDATION=1, 0 validation errors) pass; ctest -E loader: all pass except d3d12_compute and d3d12_draw, which fail the same way on a clean main build.

@ItsNotRexed
ItsNotRexed merged commit 729bee7 into ProjectLeonida:main Nov 22, 2026
4 checks passed
Vonsek pushed a commit to Vonsek/Leonida that referenced this pull request Nov 23, 2026
…byte elements

VOLUME_PATTERNS_4K rows 1, 2 and 4 and the matching tile extents in
tex_headers._VOLUME_4K_TILE matched no GFX10 pattern in Mesa addrlib. They
now follow GFX10_SW_4K_S3_PATINFO (the RbPlus table is identical, and so is
rpf9-explorer's VolumePatterns): 2-byte tiles are 8x16x16, 4-byte 8x16x8,
16-byte 4x8x8. In the 1.0.2.1 corpus this changes the one R16_FLOAT volume
(ttdb_graphics ProjectLeonida#901): adjacent-Y voxel differences fall from 0.357 to 0.067 of
the random-pair level, adjacent-Z from 0.029 to 0.007, and the check's 3D
Z-diff from 0.023623 to 0.004238 (29.3x below the 2D control instead of 5.3x).
The two 8-byte volumes are unchanged. New VolumePatternTest pins the table to
addrlib's rows and checks the extents follow from it; it fails on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

4 participants