WIP: perf(nvn): a recorded command's room is checked inline and its header written once #2050

Draft
siantm wants to merge 1 commit from siantm/Longshot:perf/nvn-record-inline into main
Contributor

Summary

Every NVN command the game records goes through rec() or cmd_alloc(), then cmd_reserve_rec() (in
host/nvn_native.c): three calls a command. cmd_reserve_rec was one function holding both the test (does the record
fit in the segment) and the growth (a new segment from the pool, the old one's bytes copied over); cmd_alloc wrote a
header of op 0 that every caller then wrote over with its own (rec, BeginRecording, EndRecording, the push
constants, the uniform updates); rec zeroed its payload through memset with a length computed at the call.

With this, the test is an inline function and the growth is cmd_grow, out of line and cold, with the same body as
before; cmd_alloc is inline and leaves the header to its caller; rec is always inline, so for the constant payload
sizes of nearly every call the zeroing is a few stores. The result expected: less work for each recorded command on
the recording threads; the bytes recorded and the command memory counts are the same.

No switch: the same bytes either way, so a revert is enough.

From the local integration commit d1e33769 on exp/x86-asm-sync (there cmd_grow also carried HOS_NVN_SEG_PEAK
and the hand-off's segment prefix, which are not part of this). Prior work: a forge search (cmd_alloc, cmd_grow) finds
no pull request on it; #1812 (GummiNaomi) changed only where a first segment starts (size hints) and was closed
unmerged by its author with no change at 4 or 16 cores. This one leaves the sizes as they are and changes only the
calls. Related: #1954 (open) writes the bind records field by field instead of through rec(); this one makes
cmd_alloc and rec() inline for every record, the two merge without conflict, and #1954's bind_handles writes its
own header after cmd_alloc, as every caller must here.

Read first: cmd_grow, cmd_reserve_rec, cmd_alloc and rec in host/nvn_native.c, then
tests/test_nvn_record_inline.py.

Testing

Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head 6e07c3ea on main 75317e1f. Build jobs of this PC's job queue, each running tests/run_all.py --strict --exact with every check in a hidden console: batch AsmCut4-b1 (log t-AsmCut4-nvn-record-inline.log): tests/test_nvn_record_inline.py, tests/test_host_alloc_wait.py and every check --changed 75317e1f picks for the files this changes: 23 checks, 20 passed, 3 failed, 0 skipped; tests/test_nvn_record_inline.py passes, tests/test_host_alloc_wait.py passes; batch AsmCut3-b1b (log t-AsmCut3b-nvn-record-inline.log, at the earlier cut 163b8dd0): tests/test_host_alloc_wait.py and every check --changed 75317e1f picks for the files this changes: 23 checks, 19 passed, 4 failed, 0 skipped; tests/test_host_alloc_wait.py passes (there the own check, tests/test_nvn_record_inline.py, failed to build its fixture under clang's -Werror (a variable of the slot table the cut leaves unused, in the test), fixed in this head and passing in the run above). The other failures fail on the base too, run from a base worktree (batch AsmCut3-b1, log t-AsmCut3-base-main.log): tests/test_dev_packs.py (fails on main @ 75317e1f too), tests/test_host_guest_writes.py (fails on main @ 75317e1f too), tests/test_renderer_new_handler.py (fails on main @ 75317e1f too).

It has NOT run in the game on this PC yet.

Still to test:

  • Windows 11, i7-9700, RTX 4060 (this PC): Kokiri Forest, turning the camera, main @ 75317e1f against this PR, each
    warmed a minute, then at least three interleaved three-minute runs of each (AB AB AB), expecting the same picture, no
    FATAL, and fps and frame times of this PR at least main's; a difference beyond the runs' spread is a gain or a loss,
    one within it is no difference measured (by hand)
  • Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: Kokiri Forest, play three minutes on main and on this PR,
    warmed and interleaved, expecting the same picture and no FATAL (@Beanstalk linux+nvidia, linux+amd, windows+amd,
    windows+intel, steamdeck: Kokiri Forest three minutes on main and on this PR, warmed and interleaved, frame times and
    a screenshot)
  • Linux: build the host and run python3 -I -B tests/test_nvn_record_inline.py --cc gcc, expecting PASS (by hand)

Measurements

Verdict: not measured: no micro-benchmark of this port and no run in the game on this PC yet; the in-game
comparison is in Still to test. The branch's reason was its CPU profile (docs/cpu-profile.md on exp/x86-asm-sync,
Hyrule Field, taken of the branch's code, not of this port), where cmd_reserve_rec showed in its own instructions;
no number of it is quoted here.

Risks

What could break: a record whose header or payload came out wrong would show as wrong or missing draws in any scene.
Every caller of cmd_alloc writes its header right after it (the test checks each in the source), and the test
records 40,000 records over eight command buffers and compares each segment with the bytes written, across its
growths. A revert is enough. Host code: the runtime is rebuilt; nothing changes for players' saves, options or caches.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named).
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A check under tests/ that reads changed source is updated, not weakened.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.
## Summary Every NVN command the game records goes through `rec()` or `cmd_alloc()`, then `cmd_reserve_rec()` (in `host/nvn_native.c`): three calls a command. `cmd_reserve_rec` was one function holding both the test (does the record fit in the segment) and the growth (a new segment from the pool, the old one's bytes copied over); `cmd_alloc` wrote a header of op 0 that every caller then wrote over with its own (`rec`, BeginRecording, EndRecording, the push constants, the uniform updates); `rec` zeroed its payload through `memset` with a length computed at the call. With this, the test is an inline function and the growth is `cmd_grow`, out of line and cold, with the same body as before; `cmd_alloc` is inline and leaves the header to its caller; `rec` is always inline, so for the constant payload sizes of nearly every call the zeroing is a few stores. The result expected: less work for each recorded command on the recording threads; the bytes recorded and the command memory counts are the same. No switch: the same bytes either way, so a revert is enough. From the local integration commit d1e33769 on `exp/x86-asm-sync` (there `cmd_grow` also carried `HOS_NVN_SEG_PEAK` and the hand-off's segment prefix, which are not part of this). Prior work: a forge search (cmd_alloc, cmd_grow) finds no pull request on it; #1812 (GummiNaomi) changed only where a first segment starts (size hints) and was closed unmerged by its author with no change at 4 or 16 cores. This one leaves the sizes as they are and changes only the calls. Related: #1954 (open) writes the bind records field by field instead of through `rec()`; this one makes `cmd_alloc` and `rec()` inline for every record, the two merge without conflict, and #1954's `bind_handles` writes its own header after `cmd_alloc`, as every caller must here. Read first: `cmd_grow`, `cmd_reserve_rec`, `cmd_alloc` and `rec` in `host/nvn_native.c`, then `tests/test_nvn_record_inline.py`. ## Testing **Tested:** Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head `6e07c3ea` on main `75317e1f`. Build jobs of this PC's job queue, each running `tests/run_all.py --strict --exact` with every check in a hidden console: batch `AsmCut4-b1` (log `t-AsmCut4-nvn-record-inline.log`): `tests/test_nvn_record_inline.py`, `tests/test_host_alloc_wait.py` and every check `--changed 75317e1f` picks for the files this changes: 23 checks, 20 passed, 3 failed, 0 skipped; `tests/test_nvn_record_inline.py` passes, `tests/test_host_alloc_wait.py` passes; batch `AsmCut3-b1b` (log `t-AsmCut3b-nvn-record-inline.log`, at the earlier cut `163b8dd0`): `tests/test_host_alloc_wait.py` and every check `--changed 75317e1f` picks for the files this changes: 23 checks, 19 passed, 4 failed, 0 skipped; `tests/test_host_alloc_wait.py` passes (there the own check, `tests/test_nvn_record_inline.py`, failed to build its fixture under clang's -Werror (a variable of the slot table the cut leaves unused, in the test), fixed in this head and passing in the run above). The other failures fail on the base too, run from a base worktree (batch `AsmCut3-b1`, log `t-AsmCut3-base-main.log`): `tests/test_dev_packs.py` (fails on main @ 75317e1f too), `tests/test_host_guest_writes.py` (fails on main @ 75317e1f too), `tests/test_renderer_new_handler.py` (fails on main @ 75317e1f too). **It has NOT run in the game on this PC yet.** **Still to test:** - Windows 11, i7-9700, RTX 4060 (this PC): Kokiri Forest, turning the camera, main @ 75317e1f against this PR, each warmed a minute, then at least three interleaved three-minute runs of each (AB AB AB), expecting the same picture, no FATAL, and fps and frame times of this PR at least main's; a difference beyond the runs' spread is a gain or a loss, one within it is no difference measured (by hand) - Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: Kokiri Forest, play three minutes on main and on this PR, warmed and interleaved, expecting the same picture and no FATAL (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri Forest three minutes on main and on this PR, warmed and interleaved, frame times and a screenshot) - Linux: build the host and run `python3 -I -B tests/test_nvn_record_inline.py --cc gcc`, expecting PASS (by hand) ## Measurements **Verdict:** not measured: no micro-benchmark of this port and no run in the game on this PC yet; the in-game comparison is in Still to test. The branch's reason was its CPU profile (`docs/cpu-profile.md` on `exp/x86-asm-sync`, Hyrule Field, taken of the branch's code, not of this port), where `cmd_reserve_rec` showed in its own instructions; no number of it is quoted here. ## Risks What could break: a record whose header or payload came out wrong would show as wrong or missing draws in any scene. Every caller of `cmd_alloc` writes its header right after it (the test checks each in the source), and the test records 40,000 records over eight command buffers and compares each segment with the bytes written, across its growths. A revert is enough. Host code: the runtime is rebuilt; nothing changes for players' saves, options or caches. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [ ] Tested on my own PC, in the game for a change the game runs, as Testing says. - [ ] `mise run test` passes (any failure that also fails on `main` is named). - [ ] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] A check under `tests/` that reads changed source is updated, not weakened. - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold.
perf(nvn): a recorded command's room is checked inline and its header written once
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 14s
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's ๐Ÿ‘ on the request
6e07c3ea50
Every NVN command the game records goes through rec() or cmd_alloc(), then cmd_reserve_rec(): three calls a command.
cmd_reserve_rec was one function holding both the test (does the record fit in the segment) and the growth (a new
segment from the pool, the old one's bytes copied over); cmd_alloc wrote a header of op 0 that every caller then wrote
over with its own (rec, BeginRecording, EndRecording, the push constants, the uniform updates); rec zeroed its payload
through memset with a length it computed at the call.

Now the test is an inline function and the growth is cmd_grow, out of line and cold (the same body as before);
cmd_alloc is inline and leaves the header to its caller; rec is always inline, so for the constant payload sizes of
nearly every call the zeroing is a few stores. The bytes recorded and the command memory counts are the same, so there
is no switch: a revert is enough.

From the branch's local integration commit d1e33769 on exp/x86-asm-sync (its cmd_grow also carried HOS_NVN_SEG_PEAK
and the hand-off's segment prefix, which are not part of this). The branch's reason: docs/cpu-profile.md there (Hyrule
Field) has cmd_reserve_rec at 0.3 % of a core in its own instructions. Not measured on this port.

Test: tests/test_nvn_record_inline.py compiles the real slot table, put_hdr, cmd_grow, cmd_reserve_rec, cmd_alloc, rec
and its REC/RECN macros cut from the source and records 40,000 records over eight command buffers (constant and variable
payloads, callers writing their own headers, command buffers initialized anew): every segment holds exactly the records
written across its growths, the command memory counts and the segments' counts are right, cmd_alloc leaves the header
to the caller, every caller of cmd_alloc writes one, and the growth is out of line behind the inline test.
tests/test_host_alloc_wait.py now finds the pool allocation in cmd_grow.

Assisted-by: Claude Code
Author
Contributor

@Beanstalk This makes NVN command recording do less work for every recorded command on the recording threads (host/nvn_native.c), in every scene. No switch: a revert gives back main's code. It has not run in the game on these PCs yet. Please play Kokiri Forest and Hyrule Field, two minutes each, the PR against main on the same PC, with a picture in each compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture as main, no crash and no FATAL line; frame time p99 equal to main's or better, within the run-to-run spread.

@Beanstalk This makes NVN command recording do less work for every recorded command on the recording threads (host/nvn_native.c), in every scene. No switch: a revert gives back main's code. It has not run in the game on these PCs yet. Please play Kokiri Forest and Hyrule Field, two minutes each, the PR against main on the same PC, with a picture in each compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture as main, no crash and no FATAL line; frame time p99 equal to main's or better, within the run-to-run spread.
Member

@siantm only members of BigBeans can start the farm. One of them can approve this request by reacting ๐Ÿ‘ to it or to this comment.

@siantm only members of `BigBeans` can start the farm. One of them can approve this request by reacting ๐Ÿ‘ to it or to this comment.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 14s
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's ๐Ÿ‘ on the request
This pull request is marked as a work in progress.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'perf/nvn-record-inline:siantm-perf/nvn-record-inline'
git switch 'siantm-perf/nvn-record-inline'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!2050
No description provided.