Repository navigation
Runtime: keep the queue's prepared command lists across smaller submits instead of rebuilding them by copy (about 23% less per-draw submit work on a title-shaped frame) - #5384
Conversation
…ewer lists VulkanQueue::execute resized prepared_ to the number of lists of each call. A queue that alternates a one-list call with a many-list call (a title's pre pass, then its main pass) freed the main pass's PreparedLists on the short call, and the long call built every entry again by copy (the Prepared copy constructor, the vectors' storage, the draw pool). prepared_ now only grows, and an execution works on its first lists.size() entries. Lanterns stage 25 (6,880 draws a frame, RTX 5080, Wine 11), 3 alternating runs each: queue work 25.4 -> 20.5 ms a frame, 3.69 -> 2.98 us a draw. vk_queue_test: a direct queue runs two lists, then one, and the left-out list must not run again.
…drop their list The check for a queue that keeps prepared lists between executions now runs 2 lists, then 1, then 2 different lists, so the regrow puts another list at index 0. Each execution must run exactly its own lists with their own commands. The lists of one execution read only the upload buffer and write different buffers, so the check does not depend on a barrier between them. Entries past the execution's list count are never read, but they still point at their last list. Their list pointer is now cleared until prepare fills it again.
|
The queue can retain more memory than its largest submission needs. Each list position retains its own peak command capacity. One large list that moves between positions can therefore occupy the cache many times. I tested head
Both adapters gave these values with zero Vulkan validation errors. The measurements cover only the capacity of the prepared command vectors. At the last submission the head retains 32 times the storage needed by any single submission in this sequence. This is retained host memory for the queue lifetime. Please consider a byte budget for inactive entries. It can preserve reuse for the alternating one-list and six-list workload while limiting retention when the command distribution changes. To reproduce, add this public test accessor to std::size_t command_bytes_for_test() const {
std::size_t bytes = 0;
for (const auto& entry : prepared_)
bytes += entry.commands.capacity() * sizeof(detail::Prepared);
return bytes;
}Insert this block before the final {
VulkanQueue q(context, d.queue, d.family, Kind::direct);
CommandList empty, heavy;
EXPECT(empty.close());
for (unsigned i = 0; i < 1024; ++i)
EXPECT(heavy.copy_buffer({mid_id, i*4, upload_id, i*4, 4}));
EXPECT(heavy.close());
for (unsigned width = 32; width > 0; --width) {
std::vector<const CommandList*> batch(width, &empty);
batch.back() = &heavy;
EXPECT(q.execute(batch) == ReplayResult::success);
EXPECT(q.idle());
if (width == 32 || width == 16 || width == 1)
std::printf("width %u command bytes %zu\n", width, q.command_bytes_for_test());
}
}Build with AI-assisted. Synthetic command lists only. |
Each kept PreparedList position kept its own peak storage, so one large list that moved between positions was kept once per position: shdann measured 1,024 small copies in a batch narrowing from 32 lists to 1 keeping 30.9 MB, 32 times the largest submission (ItsNotRexed#5384). After each prepare, the entries past the execution's lists may keep at most twice the largest execution seen recently (a peak that loses 1/16 per execution), and at least 1 MiB; above that the largest are released first. The alternating one-list / six-list frame keeps its reuse: the main pass's lists are within the bound while the pre pass runs. vk_queue_test section 6 runs that narrowing batch and counts the heap the process holds: 1.3 MB at 32 lists, 3.3 MB at 1 (32.0 MB on 32eae8d, where the check fails). Reported by shdann.
|
Thanks @shdann, good catch, and thanks for the clean repro. Each kept position did hold its own peak, so a list that moved around was kept once per position.
AI-assisted. |
|
Tested 7f80464 natively on Windows 11 Pro 26200 (MSVC 14.44.35207, Vulkan SDK 1.4.363.0), merged onto
No game files. AI-assisted (Claude Code). |
Kam471
left a comment
There was a problem hiding this comment.
Reviewed head 7f80464da3.
Build: vcvars64 + Ninja + Vulkan 1.4.363, cmake --build build --target vk_queue_test.
.\build\vk_queue_test.exe-> OK on NVIDIA GeForce RTX 5070 Ti; new section prints "2, then 1, then 2 other lists: 0, 0 and 0 wrong bytes"; heavy list: 1312389 heap bytes held at 32 lists, 3294683 at 1 (bound is 4x).- Control with main's
runtime/src/vulkan_backend.h+ this test -> also OK, as expected for a perf change with no main-failure claim.
Notes: prepared_ only grows and the span limits every loop to lists.size(); idle entries have list cleared; trim_kept_lists terminates because idle > budget > 0 implies a non-empty idle entry. The test overrides global operator new but not the aligned form, so over-aligned allocations are uncounted. The claimed ~23% is not reproduced by a committed benchmark.
Verdict: LGTM.
Hiiva
left a comment
There was a problem hiding this comment.
Independent native Windows review of 7f80464. No blocking finding in the active-list replay and idle-entry retention scope tested.
Windows 11 x64 build 26200, MSVC 19.44.35228, Windows SDK 10.0.26100, Vulkan SDK/validation 1.4.363.0, RTX 4080 driver 610.62. Both runs used LEONIDA_VK_DEVICE=RTX 4080, LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=implicit.
Built and ran the unchanged final vk_queue_test with CMake/MSVC RelWithDebInfo. The copy-to-direct buffer/texture readbacks and direct-to-compute readback had zero bad bytes. The 2-list, 1-list, 2-different-list sequence had 0, 0 and 0 wrong bytes, including the untouched-buffer/range checks. All seven queue-picker layouts and the copy/compute rejection controls passed. Final result: exit 0, zero validation errors.
The narrowing-batch regression also distinguishes this revision from the pre-limit implementation:
| Production backend | Heap counter at 32 lists | Heap counter at 1 list | Result |
|---|---|---|---|
| Revised 7f80464 | 1,312,389 bytes | 3,294,683 bytes | Pass |
| Pre-limit 32eae8d | 1,312,389 bytes | 32,041,542 bytes | Fails the <= 4x retention assertion |
For the negative control, I used the byte-identical final test source, including its operator-new accounting and every byte assertion, with only vulkan_backend.h substituted from exact 32eae8d. All other headers remained at the reviewed head; CMake-generated MSVC optimization/runtime flags matched. The earlier implementation still passed the preceding byte checks and exited 1 at the retention assertion.
Source review also confirms that preparation, query planning and emission use the active span, and trimming touches only inactive PreparedList entries under the queue's existing state synchronization.
These are the test's process operator-new heap counters, not a measurement of total process memory or GPU memory. This checks the reported moving-heavy-list pattern and existing replay controls; it does not establish whole-frame performance or expand the documented bound to nested entry vectors.
No tracked changes, game files or game execution. AI-assisted: yes.
|
Tested 7f80464 on NVIDIA GeForce RTX 5080 Laptop GPU, driver 621.14, Windows 11 (build 26300), MSVC 14.44, RelWithDebInfo: pass. |
|
Tested 7f80464 on NVIDIA GeForce RTX 4080, driver 617.42: vk_queue_test exits 0 with "vk_queue_test: OK". It prints "2, then 1, then 2 other lists: 0, 0 and 0 wrong bytes" and "a heavy list moving from position 31 to 0: 1312389 heap bytes held at 32 lists, 3294683 at 1". The submit-time saving in the title was not measured. AI-assisted. |
|
Tested 7f80464 on Windows 11, AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32029.2010: pass (one unrelated, existing crash noted)
AI-assisted (Claude Code). |
main (7d71e83) already keeps PreparedList storage from a larger batch with a grow-only resize and runs only lists.size() entries. Keep that, and keep main's KHR instance stage pool next to this branch's trim_kept_lists. On top, this branch still adds what main lacks: the kept entries past lists.size() drop their dangling list pointer and are bounded by trim_kept_lists (twice the recent peak, at least 1 MiB). vk_queue_test: main's 3b (8 then 1 lists, no stale replay) replaces this branch's section 5, which checked the same thing; main's backlog test stays section 5; the heap budget check stays as section 6. Tested on an RTX 5080: vk_queue_test native, also with LEONIDA_VK_VALIDATION=1 (0 errors); without the trim_kept_lists call section 6 fails (34 MB kept at 1 list vs 1.4 MB at 32). clang-cl build under Wine: vk_queue, d3d12_com, d3d12_multithread, d3d12_queue_wait, d3d12_queue_handover (3 modes), d3d12_draw, d3d12_bundle pass. tests/test_docs_*.py pass.
|
Tested
Not covered: no frame-time or submit-cost measurement, so the submit-work saving in the title is not checked here. Only one GPU (no AMD or Intel), and no Wine run. |
|
Re-tested the merged head
The submit-time saving in the title was not measured. |
|
Re-tested the new head 4d86d07 (it merges
No game files. AI-assisted (Claude Code). |
#5256 (comment)
What and why
VulkanQueue::executekeeps itsPreparedLists between executions (prepared_), so thatpreparerefills each list's entries in the storage the last execution left. But it resizedprepared_to the number of lists of each call. A queue that alternates a one-list call with a many-list call frees the many-list call'sPreparedLists on the short call. Then the long call builds them again from nothing:commands.assigncopy-constructs everydetail::Preparedentry, the per-draw pool (draws) is allocated again, and the short call pays for freeing all of it.A title's direct queue does exactly this every frame: a pre pass in one list, then the main pass in several. Lanterns stage 25 has the same pattern:
Execute(pre)with 1 list, thenExecute(bind, 4 chunks, post)with 6 lists. In its profile, thePreparedcopy constructor was the layer's second-hottest function.This change:
prepared_only grows. An execution works on astd::spanof its firstlists.size()entries, so the loops overprepared(prepare, query planning, emit) see only this call's lists, as before.lists.size()keep their storage for the next larger call. Nothing reads them.PreparedListowns no Vulkan objects, so a kept entry holds nothing alive. Theirlistpointer is cleared untilpreparefills it again, so a stale entry can't point at a list the title has since released (a comment next to the span says this).Nothing else changes.
preparestill refills every entry it uses (out.commands.assign(n, {})). Over kept storage, that is now a copy-assignment that keeps the vectors' capacity, not a copy construction.Numbers
Lanterns stage 25 (
run_lanterns.py --stage 25, 60 frames, the guest's call-class timers over the 50 measured frames). 6,880 draws, 88 dispatches, 9 lists in 4ExecuteCommandListsa frame. RTX 5080, Linux, NVIDIA 580.65.06, Wine 11.0, clang-cl 21 + xwin cross-build. Three runs each, base and branch alternating. Base ismain3faac55 with the Lanterns stage 25 guest merged in. The branch adds this change.The direct queue's main-pass execution waits for copy and compute fences that are signalled later. So its work runs inside a later
Signalcall, and the queue work per frame is theSignalrow plus the twoExecuteCommandListsrows:Measured again after the second commit (which clears the kept entries' list pointer), in one later session. The pod was busier then (load about 11 to 40, just after a memory shortage), so every row is slower. Base, the first commit and the second commit ran one after another:
The second commit is as fast as the first. Both are 20 to 23% under base in the same session.
After the third commit (the bound on idle entries), branch and base were run alternately in the same session: branch 21.86 and 21.06 ms a frame (3.18 and 3.06 us a draw), base 25.97 ms (3.77 us). The frame-to-frame median was 45.3 ms for the branch and 49.2 ms for base. All three runs passed with the same hash.
The one-list calls (
ExecuteCommandLists (copy, compute, direct pre)) drop from about 1.1 ms to 0.04 ms a frame: they no longer free the main pass's lists.The guest's sampling profiler (
--sample-us 500, 300 frames,scale_report.py --samples), samples ingta6rt.exe:detail::Prepared::Prepared(const Prepared&)std::vector<VkImageCopy>copy constructor (inside it)detail::Prepared::operator=(const Prepared&)Other open pull requests
detail::Preparedand stop query preparation from allocating. Both changevulkan_replay.h. This change touches onlyvulkan_backend.handvk_queue_test.cpp, andgit merge-treeshows no conflict between this branch and either one beyond the conflict each already has withmaininvulkan_replay.h. They compose. This change keeps thePreparedListthat Runtime: a copy's prepared data lives beside the per-command entries #2928'scopiespool lives in, so that pool is also kept across a one-list call.note_slot, the other contended spot in the same profile (about 8% of the layer). That one is d3d12_xs: a proxied call writes no shared atomic after the first call of its slot (RT-295) #4500's, and this change does not touch it.vkCmdBeginRendering, thememcmp) and a draw's views. Those are theirs, and this branch merges cleanly with both.Tested
On an RTX 5080, Linux, NVIDIA 580.65.06, Wine 11.0, clang-cl 21 with the xwin SDK. Base and branch builds came from the same build folder.
vk_queue_test(native Linux, new check): one direct queue runs 2 lists, then 1, then 2 different lists, so the regrow puts another list at index 0. A part-buffer copy makes a stale entry show. Each execution must run exactly its own lists with their own data: every destination buffer has the list's bytes where that list copies and zeros everywhere else. The lists of one execution read only the upload buffer and write different buffers, so the check needs no barrier between them. Result: 0, 0 and 0 wrong bytes, and also 0 validation errors withLEONIDA_VK_VALIDATION=1.prepared_instead of this call's span, the test fails. With the clearedlistpointer it crashes (exit 139) as soon as a stale entry is emitted. Before that pointer was cleared, an earlier version of the check saw 65,280 bytes written by the left-out list.vk_queue_testsection 6 (the bound): @shdann's pattern, written as a test. One list of 1,024 four-byte copies sits among empty lists, last in batches that narrow from 32 lists to 1, with the queue idle after each one. The test counts the heap the process holds throughoperator newand requires the 1-list figure to be at most 4 times the 32-list figure. This branch: 1.30 MB at 32 lists, 3.28 MB at 1 (3.93 MB underLEONIDA_VK_VALIDATION=1, 0 validation errors). On 32eae8d, the head before the bound, the same test fails: 1.30 MB at 32 lists, 32.03 MB at 1.Lanterns stage 25: PASS in all 6 runs and both profiler runs, with the same frame hash (99f8df1bea376593) and every frame identical to frame 1.
The Wine ctest set and the GPU tests below ran on the first commit. The second commit only adds the pointer clear and the test, and its
gta6rt.exepassed stage 25 (3 runs, same hash).Windows ctest set under Wine (
scripts/run_wine_tests.py, 177 tests,LEONIDA_D3D12_SHIM_TESTS=ON): base 131 passed, 12 failed, 34 skipped; branch 132 passed, 10 failed, 35 skipped. The same tests fail in both runs, except two:device_proxy(base: timed out after 120 s; branch: skip, exit 77)loader_tls(base: fail; branch: pass)Neither binary was rebuilt by this change (both are older than the change), and three more runs of the same binaries gave pass/skip, pass/skip, then fail/timeout. They are flaky under Wine on this machine. The other 10 failures are the same in base and branch: vkd3d host-comparison mismatches and
/dev/dripermissions in the container.scripts/run_gpu_tests.py(Linux build): 170/217 passed, 47 skipped, for both base and branch. Every per-test line is identical except the output paths.Not covered
docs/lanterns.mdsays.preparestill refills each entry by copy-assignment from a freshPrepared. That is 40 profiler samples, about 2% of the layer. Resetting entries in place, field by field, would remove most of it, but the field list is what Runtime: a copy's prepared data lives beside the per-command entries #2928 rewrites, so that is left for after Runtime: a copy's prepared data lives beside the per-command entries #2928.commands,draws,queries), not the small vectors inside the entries, and not Runtime: a copy's prepared data lives beside the per-command entries #2928'scopiespool once that lands. Entries inside an execution are not bounded, exactly as onmain: a large list that moves between positions of equally wide batches is kept at each of them, as before this change.vk_queue_test.vk_queue_test(with validation) and stage 25 were run.No game files.
AI-assisted (Claude Code).