Skip to content

Runtime: query commands prepare without a heap allocation each (Depends on #2928) - #2969

Open
traeb0n wants to merge 2 commits into
ItsNotRexed:mainfrom
traeb0n:prepared-query-reuse
Open

traeb0n wants to merge 2 commits into
ItsNotRexed:mainfrom
traeb0n:prepared-query-reuse

Conversation

@traeb0n

@traeb0n traeb0n commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Depends on: #2928 (this branch carries its commit; the last commit is this PR's own). Claim: #2891 (comment)

Before this PR, query commands were the only ones in replay_cpu_bench that allocated while being prepared: 1 per timestamp, 1 per ResolveQueryData, and 1 per occlusion Begin/End pair. There were two causes:

  • QueryReplay::pending_ made one std::map node per query a submission begins or ends. A QueryReplay lives for one submission and pending_ never erases, so its nodes now come from a std::pmr::monotonic_buffer_resource. That takes a few blocks per submission (4 KiB first, then growing) instead of one allocation per query, and none when the lists have no queries. QueryReplay is now non-copyable, since the map points into its arena.
  • prepare() refills PreparedList::queries from empty, so each ResolveQueryData's resolves vector was made again on every execution. Before the clear, the emptied vectors now go into PreparedList::spare_resolves, and the next resolves take them from there.

Numbers

replay_cpu_bench --reps 25, i7-14700K, base 8e1fae6 (#2928's head) → this branch:

kind prepare ns/cmd allocations per command (prepare)
occlusion Begin+End 80.9 → 70.6 0.50 → 0.00
timestamp 104.7 → 85.0 1.00 → 0.00
resolve 16 queries 161.8 → 127.0 1.00 → 0.00
frame (37,888 commands) within noise 0.04 → 0.00

The frame has few queries, so its time doesn't move beyond run-to-run noise on this machine. Preparing it now allocates nothing.

Tests

Tested 113d292 on an RTX 5070, driver 621.01, Windows 11, MSVC 14.44, Vulkan SDK 1.4.363, with LEONIDA_VK_VALIDATION=1:

  • ctest -E loader: 117/117 passed.
  • d3d12_vulkan_device_test: exit 0, 0 validation messages.
  • replay_cpu_bench --self-test: ok. It now fails any kind whose list takes more than one allocation to prepare (the arena's first block).
  • Negative control: that check on the old vulkan_replay.h (8e1fae6) fails as expected:
    • occlusion Begin+End: 9 allocations for 16 commands;
    • timestamp: 17 allocations;
    • resolve 16 queries: 18 allocations.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 113d292 on Windows 11, RTX 5080 (driver 616.92) and AMD Radeon iGPU gfx1036 (driver 26.10.1): ctest -R "d3d12_vulkan_device|predicated_copy|replay_|d3d12_state_map" (11 tests, includes replay_cpu_bench_selftest; MSVC, RelWithDebInfo, LEONIDA_VK_DEVICE set per run, device line confirmed, validation off) — pass on both, 11/11 each.

  • RTX 5080: 11/11 passed. replay_cpu_bench --self-test: "14 kinds and a 296-command frame recorded and replayed, ok".
  • AMD Radeon (gfx1036): 11/11 passed, same self-test line.
    Not run: the full ctest -E loader suite, and no timing numbers (the PR's benchmark figures are from another machine).

AI-assisted.

@FaKL-Code

Copy link
Copy Markdown
Contributor

Tested 113d292 (merged onto main 1628210) on NVIDIA GeForce RTX 4060 Ti, driver 610.88 (32.0.16.1088), Windows 11 26200, MSVC 14.51.36231 (VS 2026), Vulkan SDK 1.4.363.0: pass

  • Merges cleanly; no new compiler warnings against main.

  • ctest: 120 of 120 pass (same 8 skips as main). Loader test passes; validation run: 48 tests clean (2 skipped, as on main).

  • replay_cpu_bench --self-test: "14 kinds and a 296-command frame recorded and replayed, ok".

  • replay_cpu_bench --reps 25 on this machine. The baseline is main 1628210, not Runtime: a copy's prepared data lives beside the per-command entries #2928's head, so the ns columns include Runtime: a copy's prepared data lives beside the per-command entries #2928's own changes; the prepare allocation column is the one this PR is about:

    kind prepare ns/cmd, main → PR alloc per cmd (prepare), main → PR
    occlusion Begin+End 191.8 → 91.4 0.50 → 0.00
    timestamp 230.0 → 105.0 1.00 → 0.00
    resolve 16 queries 276.9 → 129.0 1.00 → 0.00
    frame 191.1 → 145.0 (21,504 → 37,888 commands) 0.07 → 0.00

AI-assisted (Claude).

@traeb0n

traeb0n commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto #2928's rebase on main f847748 (19353e1); no changes to this commit. Thanks @Vonsek (RTX 5080, AMD gfx1036) and @FaKL-Code (RTX 4060 Ti) for the runs on 113d292.

D3D-252's new statistics planner only runs for overlapping queries. The self-test's allocation check still passes for every kind on the rebased head.

Tested 19353e1: RTX 5070, driver 621.01, Windows 11, MSVC 14.44, with LEONIDA_VK_VALIDATION=1. ctest -E loader 128/128, d3d12_vulkan_device_test 0 validation messages. Prepare, main f847748 → this head:

  • occlusion Begin+End: 157.8 → 72.7 ns
  • timestamp: 180.1 → 88.1 ns
  • resolve 16 queries: 236.3 → 126.7 ns

All three are at 0 allocations, down from 0.5–1 per command. Most of the time drop comes from #2928's smaller entry, which main has grown to 1312 bytes.

AI-assisted (Claude Code).

traeb0n and others added 2 commits November 22, 2026 16:17
Copies, resolves and atomic copies keep their source, regions, depth24/rgb96
conversion and staging in detail::PreparedCopy, in PreparedList::copies,
indexed by Prepared::copy, the way ItsNotRexed#2123 moved the draws' data. An
ExecuteIndirect's count buffer uses the same slot. Each PreparedCopy is kept
between executions and reset() keeps its vectors' storage.

detail::Prepared: 872 -> 392 bytes (MSVC 14.44, measured with sizeof).

replay_cpu_bench --self-test checks that a buffer copy prepared where a
texture upload was before keeps none of its regions.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
QueryReplay::pending_ took one map node per query a submission begins or
ends (one allocation per timestamp, one per occlusion Begin/End pair). A
QueryReplay lives for one submission and never erases, so its nodes now come
from a monotonic arena that grows in a few blocks.

PreparedList::queries is refilled from empty on every execution, which made
each ResolveQueryData's resolve runs again. Their emptied storage now waits in
PreparedList::spare_resolves for the next execution's resolves.

replay_cpu_bench --self-test fails a kind whose list takes more than one
allocation (the arena's first block) to prepare.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@traeb0n
traeb0n force-pushed the prepared-query-reuse branch from 19353e1 to 2446c4a Compare November 22, 2026 22:23
@traeb0n

traeb0n commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto #2928's rebase on main aa2988c (2446c4a); no changes to this commit. Tested 2446c4a on RTX 5070, driver 621.01, Windows 11, MSVC 14.44, Vulkan SDK 1.4.363, LEONIDA_VK_VALIDATION=1: ctest -E loader 140/141 (run_tiny_game_dir failed once under -j 4 and passes 4 of 4 alone), device test 0 validation messages, replay_cpu_bench --self-test ok. Prepare time with no allocations: occlusion Begin+End 71.2 ns, timestamp 87.3 ns, resolve 16 queries 127.4 ns.

AI-assisted (Claude Code).

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Tested 2446c4ad on Windows 11 (26300), RTX 5060 Ti / 621.14, MSVC 14.51 (VS 2026), Vulkan SDK 1.4.363.0, RelWithDebInfo, clean build with -DLEONIDA_LLVM_MC (translated-shader D3D12 gate tests included), full LEONIDA_VK_VALIDATION=1 ctest -j4: 144/144 passed, 0 VUIDs, 0 build errors. AI-assisted (Claude Code).

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