replay_cpu_bench: stop overstating a draw 2.1x and a frame 1.9x (measure the layer, not the harness) #58

Open
github-bot wants to merge 1 commit from github-bot/gh-58 into main AGit
Owner

@⁠Andretuta on GitHub · 2026-11-24 02:09 UTC · original
ubiquitarianism:old-pr/9780 → main · +36 −1 · 1 files · 1 commits · draft

Carried over from old PR 9780 by pes7 in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9780, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
pes7: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

The layer's own CPU benchmark was overstating a draw by 2.1x and a frame by 1.9x, so anything optimized against it was optimized against the wrong number. This makes it measure the layer.

replay_cpu_bench's draw rows built a fresh DrawViews with make_shared inside the timed lambda — an allocation and a value-initialised block of 8 render-target views and 32 vertex-buffer views per draw. The layer does not do that: d3d12_com.cpp's draw_views() rebuilds the block only when the render targets or the vertex buffers change, and a steady-state draw copies the shared_ptr.

What changes

The steady-state kinds share one block; a state change keeps its own rows (draw, views rebuilt, draw indexed, views rebuilt), so the old case stays measured. The frame records the steady-state draw, which is what a frame does.

Measured, same build, same machine

ns per command before after
draw 389.4 record / 786.8 total, alloc 1.00 30.4 / 242.9, alloc 0.00
draw indexed 494.8 / 1036.4 31.2 / 248.3
frame (37,888 commands) 851.2 → 32,249.7 µs 446.0 → 16,898.4 µs

With the views cost out of the way the profile reads differently: prepare (138.6 ns) is the expensive part of a draw, record is 30.4 ns. Before, record looked like the hot spot because the benchmark's own make_shared sat in it.

This is the tool used to conclude that the remaining substantial lever is the size of one prepared entry (detail::Prepared is 1016 bytes, Command 296, PreparedDraw 312 — ~1.6 KB per draw per frame, measured in #8504 with the per-command cost rising 99 → 253 ns as the set outgrows cache), and to reject two changes that sounded good and measured flat.

replay_cpu_bench --self-test: 15 kinds and a 296-command frame, ok. No runtime/src change; no GPU, no game data.

cc @ItsNotRexed @KoertLichtendonk

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md. (Not applicable: no translation change.)
**@⁠Andretuta** on GitHub · 2026-11-24 02:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/58) `ubiquitarianism:old-pr/9780` → `main` · +36 −1 · 1 files · 1 commits · draft > Carried over from **old PR 9780** by `pes7` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9780`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `pes7`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): **The layer's own CPU benchmark was overstating a draw by 2.1x and a frame by 1.9x**, so anything optimized against it was optimized against the wrong number. This makes it measure the layer. `replay_cpu_bench`'s `draw` rows built a fresh `DrawViews` with `make_shared` **inside the timed lambda** — an allocation and a value-initialised block of 8 render-target views and 32 vertex-buffer views per draw. The layer does not do that: `d3d12_com.cpp`'s `draw_views()` rebuilds the block only when the render targets or the vertex buffers change, and a steady-state draw copies the `shared_ptr`. ### What changes The steady-state kinds share one block; a state change keeps its own rows (`draw, views rebuilt`, `draw indexed, views rebuilt`), so the old case stays measured. The frame records the steady-state draw, which is what a frame does. ### Measured, same build, same machine | ns per command | before | after | |---|---|---| | `draw` | 389.4 record / 786.8 total, alloc 1.00 | **30.4 / 242.9, alloc 0.00** | | `draw indexed` | 494.8 / 1036.4 | 31.2 / **248.3** | | frame (37,888 commands) | 851.2 → **32,249.7 µs** | 446.0 → **16,898.4 µs** | With the views cost out of the way the profile reads differently: `prepare` (138.6 ns) is the expensive part of a draw, `record` is 30.4 ns. Before, `record` looked like the hot spot because the benchmark's own `make_shared` sat in it. This is the tool used to conclude that the remaining substantial lever is the size of one prepared entry (`detail::Prepared` is 1016 bytes, `Command` 296, `PreparedDraw` 312 — ~1.6 KB per draw per frame, measured in `#8504` with the per-command cost rising 99 → 253 ns as the set outgrows cache), and to reject two changes that sounded good and measured flat. `replay_cpu_bench --self-test`: 15 kinds and a 296-command frame, ok. No `runtime/src` change; no GPU, no game data. cc `@ItsNotRexed` `@KoertLichtendonk` ### Checklist - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`. (Not applicable: no translation change.)
The draw rows built a fresh DrawViews with make_shared inside the timed lambda, so every draw in the table paid an
allocation and a value-initialised 40-field block. That is not what the layer does: d3d12_com.cpp:draw_views()
rebuilds the block only when the render targets or the vertex buffers change, and a steady-state draw copies the
shared_ptr. The table therefore reported 1.00 allocations per draw and about 400 ns of record time for something the
layer does not do, and the frame row carried the same cost once per draw.

The views block is now built once for the steady-state kinds, and the state change is measured as its own pair of
rows ("draw, views rebuilt", "draw indexed, views rebuilt"), which keeps the old case visible instead of losing it.
The frame keeps the steady-state draw, which is what a frame does.

Windows 11, Ryzen 9, RelWithDebInfo, this machine, same command:

    ns per command            before                     after
    draw                389.4 record, 786.8 total   30.4 record, 242.9 total   (alloc 1.00 -> 0.00)
    draw indexed        494.8 record, 1036.4 total  31.2 record, 248.3 total   (alloc 1.00 -> 0.00)
    frame (8 x 128)     851.2 ns, 32249.7 us        446.0 ns, 16898.4 us

So the layer's own per-draw cost was overstated by about 2.1x, and with the views cost out of the way prepare
(138.6 ns) is the part that dominates a draw, not record (30.4 ns).

--self-test still passes (17 kinds, 296-command frame recorded and replayed).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:42:40 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/58/head:github-bot/gh-58'
git switch 'github-bot/gh-58'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-58'
git switch 'github-bot/gh-58'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-58'
git switch 'github-bot/gh-58'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-58'
git switch 'main'
git merge --squash 'github-bot/gh-58'
git switch 'main'
git merge --ff-only 'github-bot/gh-58'
git switch 'main'
git merge 'github-bot/gh-58'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.