WIP: perf(nvn): AddControlMemory's release of old recordings waits for the next EndRecording's lock (HOS_NVN_COPY_LOCK_ONCE=1 to enable) #2025

Draft
siantm wants to merge 6 commits from siantm/Longshot:perf/nvn-copy-lock-once into main
Contributor

Summary

The game adds control memory to a command buffer before every recording (nvnCommandBufferAddControlMemory, as many
calls a frame as EndRecording), and AddControlMemory released the copies of the recordings made in that memory in an
exclusive copy_lock section of its own: two exclusive sections a recording, which every recording thread and every
CallCommands miss waits for.

With this, AddControlMemory keeps the range in the command buffer's host table with the number of the last recording
made by then (rec_born; each copy carries its recording's number), and the command buffer's next EndRecording
releases it in the copy_lock section it takes anyway (release_range, the dead copies freed first as
release_copies does). Only the recordings made up to that number go, so what another command buffer records in that
memory meanwhile stays, as with the release made at the call. A range still waiting when memory is added again, or when
the command buffer is finalized or initialized again (cmd_release), is released then. Old recordings live a little
longer than before, never shorter, and their free still waits for the replay to pass the submits made before their
release. The result expected: one exclusive copy_lock section a recording instead of two, when the switch is on.
With the review (BeanBot 64275): EndRecording reads the deferred release's ticket under the lock, the held count before
the pushed one, as release_copies does since #2022's fix, and rec_born is a plain increment by the lock holder
stored atomically (no locked read-modify-write a recording).

The switch: HOS_NVN_COPY_LOCK_ONCE (new, an attempt, bool, its row in gpu/vk/switches.def), off by default: it
has not run in the game, and it changes when every recording thread takes copy_lock, so it ships off until a run
measures it (the branch shipped it on). Read once, at the first AddControlMemory, and published atomically, so threads
that come to it first together keep one answer (=1 or any value but 0, off, false, no or empty is on).
Unset or off, AddControlMemory releases the copies at the call in a copy_lock section of its own: main's release
schedule, on top of #2022's flat buckets, not main's code; the comparison the switch gives is the deferred release
against the immediate one within this head.

Depends on #2022 (recorded copies in flat per-bucket arrays): this extends its bucket entries and walk. The branch is
on main @ fd7dc40d with #2022's three commits (ae69cdbb, 9f83a9a2, db31aa81) under its own; they show in this diff until
#2022 merges, and this is rebased then.

From d1e33769 on exp/x86-asm-sync. There the same switch also gave the dead copies back to the pool after the lock was
let go; that half is not here. Prior work: #1812 (GummiNaomi) freed the dead copies and bucket nodes outside
copy_lock and ran the sweep in slices outside EndRecording's hold; its author closed it unmerged, having measured no
change at 4 or 16 cores. This one does not move frees out of the lock: it folds AddControlMemory's own section into the
next EndRecording's.

Read first: release_range, copy_lock_once, AddControlMemory, EndRecording and cmd_release in
host/nvn_native.c, then tests/test_nvn_copy_lock_once.py.

Testing

Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head 762cac5a on #2022's head db31aa81 (on main fd7dc40d), with the Vulkan headers (RX_CPM). Build jobs fb-2022-2, fb-2025-3 and fb-2025-4 of this PC's job queue, each check in a hidden console: tests/test_nvn_copy_lock_once.py at the test commit ac1f4ad9 fails as expected (with HOS_NVN_COPY_LOCK_ONCE=1, a copy went back to the pool with 32 submits replayed when 34 were pushed or held as it was let go) and passes at the head, also with no --cc and no CC; tests/run_all.py --strict --exact with tests/test_nvn_copy_lock_once.py, tests/test_nvn_host_tables.py, tests/test_nvn_rec_release.py, tests/test_recording_log_handoff.py and tests/test_switch_table.py: 5 passed; tests/test_nvn_host_tables.py with no CC passes; host/nvn_native.c compiles (-fsyntax-only) with the game build's own command. tests/run_all.py --strict --changed db31aa81 (the checks for what this changes over #2022): 38 passed, 10 failed, 3 skipped, none for want of the Vulkan headers; the 9 other than tests/test_nvn_copy_lock_once.py (whose failure there was a fixture fixed since, passing at the head above) fail on main fd7dc40d too, run from a main worktree: tests/test_crash_dialog.py, tests/test_dev_ab.py, tests/test_dev_packs.py, tests/test_frame_overlap.py, tests/test_host_guest_writes.py, tests/test_linux_pressure.py, tests/test_present_queue_default.py, tests/test_target_reuse_memory.py, tests/test_vk_occlusion_queries.py. The full suite ran on #2022's head (its description).

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, walking into Link's house and out) and
    the Market (a heavy scene), main @ fd7dc40d with #2022 merged or #2022's head db31aa81, this PR unset (off) and this
    PR with HOS_NVN_COPY_LOCK_ONCE=1, each warmed a minute, then at least three interleaved three-minute runs of each
    (ABC ABC ABC), expecting no not a live recording line, no FATAL, the same picture, the log's live copies and the
    buckets' room about the same, and average fps, 1% lows, median and p99 frame times of =1 at least those of the
    other two; a difference beyond the runs' spread is a gain or a loss, one within it is no difference measured; with a
    Tracy build, the copy_lock waits (the "lock wait" zones) in EndRecording and outside it (by hand)
  • Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: Kokiri Forest, play three minutes on main, on this PR unset
    and on this PR with HOS_NVN_COPY_LOCK_ONCE=1, warmed and interleaved, expecting no not a live recording line, no
    FATAL and the same picture (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri
    Forest three minutes, main, HOS_NVN_COPY_LOCK_ONCE unset and =1, warmed and interleaved, the log, frame times and a
    screenshot)
  • Linux: build the host and run python3 -I -B tests/test_nvn_copy_lock_once.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 A/B is
in Still to test. The branch's reason was its CPU profile (docs/cpu-profile.md on exp/x86-asm-sync, taken of the
branch's code with the frees still inside the lock, not of this port), where n_nvnCommandBufferAddControlMemory showed
in every scene; no number of it is quoted here.

Risks

What could break: a copy released too early would show as a wrong or missing draw or a not a live recording stop in
any scene; released too late, as memory held a little longer. The test runs 12,000 adds, recordings and finalizes over
six command buffers whose control memory overlaps, with the switch on and off: every copy released with it off is
released with it on no earlier, and at the latest at that command buffer's next EndRecording, AddControlMemory or
Finalize, and none other, and with held submits and flushes between a ticket's reads, a copy goes back to the pool
only after the replay passed every submit pushed or held when it was let go. With the switch on, old recordings live
until the command buffer's next EndRecording, AddControlMemory, Finalize or Initialize: an idle command buffer keeps
them until then, and the blocks freed in EndRecording's section go back to the pool after that recording's copy was
taken. Off unless HOS_NVN_COPY_LOCK_ONCE is set; unset or =0 is the release at the call. 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 switch added or changed: it ships at the default the measurements chose, with its row in gpu/vk/switches.def.
  • 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 The game adds control memory to a command buffer before every recording (`nvnCommandBufferAddControlMemory`, as many calls a frame as `EndRecording`), and AddControlMemory released the copies of the recordings made in that memory in an exclusive `copy_lock` section of its own: two exclusive sections a recording, which every recording thread and every CallCommands miss waits for. With this, AddControlMemory keeps the range in the command buffer's host table with the number of the last recording made by then (`rec_born`; each copy carries its recording's number), and the command buffer's next EndRecording releases it in the `copy_lock` section it takes anyway (`release_range`, the dead copies freed first as `release_copies` does). Only the recordings made up to that number go, so what another command buffer records in that memory meanwhile stays, as with the release made at the call. A range still waiting when memory is added again, or when the command buffer is finalized or initialized again (`cmd_release`), is released then. Old recordings live a little longer than before, never shorter, and their free still waits for the replay to pass the submits made before their release. The result expected: one exclusive `copy_lock` section a recording instead of two, when the switch is on. With the review (BeanBot 64275): EndRecording reads the deferred release's ticket under the lock, the held count before the pushed one, as `release_copies` does since #2022's fix, and `rec_born` is a plain increment by the lock holder stored atomically (no locked read-modify-write a recording). The switch: `HOS_NVN_COPY_LOCK_ONCE` (new, an attempt, bool, its row in `gpu/vk/switches.def`), **off by default**: it has not run in the game, and it changes when every recording thread takes `copy_lock`, so it ships off until a run measures it (the branch shipped it on). Read once, at the first AddControlMemory, and published atomically, so threads that come to it first together keep one answer (`=1` or any value but `0`, `off`, `false`, `no` or empty is on). Unset or off, AddControlMemory releases the copies at the call in a `copy_lock` section of its own: main's release schedule, on top of #2022's flat buckets, not main's code; the comparison the switch gives is the deferred release against the immediate one within this head. Depends on #2022 (recorded copies in flat per-bucket arrays): this extends its bucket entries and walk. The branch is on main @ fd7dc40d with #2022's three commits (ae69cdbb, 9f83a9a2, db31aa81) under its own; they show in this diff until #2022 merges, and this is rebased then. From d1e33769 on `exp/x86-asm-sync`. There the same switch also gave the dead copies back to the pool after the lock was let go; that half is not here. Prior work: #1812 (GummiNaomi) freed the dead copies and bucket nodes outside `copy_lock` and ran the sweep in slices outside EndRecording's hold; its author closed it unmerged, having measured no change at 4 or 16 cores. This one does not move frees out of the lock: it folds AddControlMemory's own section into the next EndRecording's. Read first: `release_range`, `copy_lock_once`, `AddControlMemory`, `EndRecording` and `cmd_release` in `host/nvn_native.c`, then `tests/test_nvn_copy_lock_once.py`. ## Testing **Tested:** Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head `762cac5a` on #2022's head `db31aa81` (on main `fd7dc40d`), with the Vulkan headers (`RX_CPM`). Build jobs `fb-2022-2`, `fb-2025-3` and `fb-2025-4` of this PC's job queue, each check in a hidden console: `tests/test_nvn_copy_lock_once.py` at the test commit `ac1f4ad9` fails as expected (with `HOS_NVN_COPY_LOCK_ONCE=1`, a copy went back to the pool with 32 submits replayed when 34 were pushed or held as it was let go) and passes at the head, also with no `--cc` and no `CC`; `tests/run_all.py --strict --exact` with `tests/test_nvn_copy_lock_once.py`, `tests/test_nvn_host_tables.py`, `tests/test_nvn_rec_release.py`, `tests/test_recording_log_handoff.py` and `tests/test_switch_table.py`: 5 passed; `tests/test_nvn_host_tables.py` with no `CC` passes; `host/nvn_native.c` compiles (`-fsyntax-only`) with the game build's own command. `tests/run_all.py --strict --changed db31aa81` (the checks for what this changes over #2022): 38 passed, 10 failed, 3 skipped, none for want of the Vulkan headers; the 9 other than `tests/test_nvn_copy_lock_once.py` (whose failure there was a fixture fixed since, passing at the head above) fail on main `fd7dc40d` too, run from a main worktree: `tests/test_crash_dialog.py`, `tests/test_dev_ab.py`, `tests/test_dev_packs.py`, `tests/test_frame_overlap.py`, `tests/test_host_guest_writes.py`, `tests/test_linux_pressure.py`, `tests/test_present_queue_default.py`, `tests/test_target_reuse_memory.py`, `tests/test_vk_occlusion_queries.py`. The full suite ran on #2022's head (its description). **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, walking into Link's house and out) and the Market (a heavy scene), main @ fd7dc40d with #2022 merged or #2022's head db31aa81, this PR unset (off) and this PR with `HOS_NVN_COPY_LOCK_ONCE=1`, each warmed a minute, then at least three interleaved three-minute runs of each (ABC ABC ABC), expecting no `not a live recording` line, no FATAL, the same picture, the log's live copies and the buckets' room about the same, and average fps, 1% lows, median and p99 frame times of `=1` at least those of the other two; a difference beyond the runs' spread is a gain or a loss, one within it is no difference measured; with a Tracy build, the `copy_lock` waits (the "lock wait" zones) in EndRecording and outside it (by hand) - Linux NVIDIA and AMD, Windows AMD and Intel, Steam Deck: Kokiri Forest, play three minutes on main, on this PR unset and on this PR with `HOS_NVN_COPY_LOCK_ONCE=1`, warmed and interleaved, expecting no `not a live recording` line, no FATAL and the same picture (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri Forest three minutes, main, HOS_NVN_COPY_LOCK_ONCE unset and =1, warmed and interleaved, the log, frame times and a screenshot) - Linux: build the host and run `python3 -I -B tests/test_nvn_copy_lock_once.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 A/B is in Still to test. The branch's reason was its CPU profile (`docs/cpu-profile.md` on `exp/x86-asm-sync`, taken of the branch's code with the frees still inside the lock, not of this port), where `n_nvnCommandBufferAddControlMemory` showed in every scene; no number of it is quoted here. ## Risks What could break: a copy released too early would show as a wrong or missing draw or a `not a live recording` stop in any scene; released too late, as memory held a little longer. The test runs 12,000 adds, recordings and finalizes over six command buffers whose control memory overlaps, with the switch on and off: every copy released with it off is released with it on no earlier, and at the latest at that command buffer's next EndRecording, AddControlMemory or Finalize, and none other, and with held submits and flushes between a ticket's reads, a copy goes back to the pool only after the replay passed every submit pushed or held when it was let go. With the switch on, old recordings live until the command buffer's next EndRecording, AddControlMemory, Finalize or Initialize: an idle command buffer keeps them until then, and the blocks freed in EndRecording's section go back to the pool after that recording's copy was taken. Off unless `HOS_NVN_COPY_LOCK_ONCE` is set; unset or `=0` is the release at the call. 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 - [ ] A switch added or changed: it ships at the default the measurements chose, with its row in `gpu/vk/switches.def`. - [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): recorded copies in flat per-bucket arrays instead of malloc'd list nodes
Some checks failed
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 24s
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
969dcfa694
The recordings' copies are kept by the 64 KiB block of their control memory in 65,536 buckets. Each bucket was a
linked list of nodes malloc'd one a recording (EndRecording) and freed one a release, scattered through the heap: a
release or a sweep walks its buckets whole, the copies that stay included, so a walk of k copies was k dependent cache
misses, all of it with copy_lock held exclusively, which every recording thread waits for.

A bucket is now a flat array of {ctl, copy, marked} entries, oldest first, walked newest first as the head-inserted
lists were; the copies kept move back to the array's start in their order (rec_keep). So copies are released, wait
dead and are freed in exactly the order they were; EndRecording mallocs nothing (copy_track). An array grows by
doubling under copy_lock, as the dead list already does, and never shrinks. The three copies of "a copy is no handle
any more" (release_copies, the sweep) are one function, copy_retire. No switch: the same releases and frees in the
same order.

Micro-benchmark on the branch (d4eb931f, i7-9700, with 32-byte entries that also held a recording number for
HOS_NVN_COPY_LOCK_ONCE): one EndRecording copy_lock section with ~50,000 live copies (the game's logs: 46,000-51,000),
k copies a bucket: k=1 444 -> 349 ns, k=4 708 -> 385, k=16 1379 -> 410, k=64 2423 -> 572, k=256 6073 -> 1230; with a
submit's 155 dead copies waiting, k=1 650 -> 373 ns. Not measured in play.

Test: tests/test_nvn_rec_release.py runs the flat code cut from the source and the linked lists as they were
through the same 60,000 random operations (tracks, releases of chunks, blocks, unaligned and over-4-GiB ranges,
sweeps with marks kept, overwritten and outside the window, dead copies freed as the replay moves), in three layouts
(a few busy blocks, many, none); the handles leave the set and the blocks go back to the pool in the same order.
tests/test_recording_log_handoff.py's stand-ins follow EndRecording's publication (copy_track instead of the node).

#1812 (GummiNaomi) changed the same functions (release_copies, the sweep, EndRecording: frees outside the lock,
the ticket read at retirement) but kept the list nodes; its author closed it unmerged (no change at 4 or 16 cores).

Assisted-by: Claude Code
perf(nvn): AddControlMemory's release of old recordings waits for the next EndRecording's lock (HOS_NVN_COPY_LOCK_ONCE=0 to disable)
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 11s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
3b4edada27
Needs "perf(nvn): recorded copies in flat per-bucket arrays instead of malloc'd list nodes" first (it extends that
commit's bucket entries and walk).

The game adds control memory before every recording (as many AddControlMemory calls a frame as EndRecording), and
AddControlMemory released the copies of the recordings made in that memory in an exclusive copy_lock section of its
own: two exclusive sections a recording, which every recording thread and every CallCommands miss waits for.

Now AddControlMemory keeps the range in the command buffer's host table with the number of the last recording made by
then (rec_born; each copy carries its recording's number, rec_ent.born), and the command buffer's next EndRecording
releases it in the copy_lock section it takes anyway (release_range, with the dead copies freed first as
release_copies does). Only the recordings made up to that number go, so what another command buffer records in that
memory meanwhile stays, as with the release made at the call. A range still waiting when memory is added again, or
when the command buffer is finalized or initialized again (cmd_release), is released then. Old recordings live a
little longer than before, never shorter, and their free still waits for the replay to pass the submits made before
their release (that ticket is now read at the EndRecording, later than before).

HOS_NVN_COPY_LOCK_ONCE (new, kill switch, its row in gpu/vk/switches.def): on by default, as the branch shipped it
(d1e33769); =0 releases at AddControlMemory, as before. Read once, published atomically (threads that come first together keep one answer).

On the branch the same switch also gave the dead copies back to the pool after the lock was let go; that part is not
here: #1812 (GummiNaomi) freed outside copy_lock (frees / frees_run) and was closed unmerged by its author (no change at
4 or 16 cores). The branch's own estimate
(its docs/cpu-profile.md, i7-9700, Kokiri): AddControlMemory's section was 1.5 % of a core with the frees inside it.
Not measured, on the branch or here.

Test: tests/test_nvn_copy_lock_once.py runs the real AddControlMemory, BeginRecording, EndRecording, slot table and
buckets cut from the source over six command buffers whose control memory overlaps, 12,000 operations (adds,
recordings, finalizes), with the switch on and off: every copy released at AddControlMemory with it off is released
with it on no earlier, and at the latest at that command buffer's next EndRecording, AddControlMemory or Finalize;
none other is released; copy_lock is taken once a recording with it on, and by AddControlMemory or Finalize only when a
release was waiting. tests/test_nvn_rec_release.py (release_range, the entries' number), test_nvn_host_tables.py
(Finalize makes a waiting release, once) and test_recording_log_handoff.py (the release inside EndRecording's
section, with copy_lock held) follow the source.

The test sets the switch in each run's own process (unset, 0, off, No, 1, yes, 2, and 0 again) and sets it the
other way after its first read, which must not change the answer. The C compiler is --cc, $CC, LONGSHOT_MINGW's clang
on Windows, then clang, gcc or cc.

Assisted-by: Claude Code
BeanBot left a comment

Recommendation: Request changes. The deferred release can return a recording to the pool while a held submit still needs it. It also leaves retired control-memory addresses visible to the sweep, and the player-facing performance gain is unmeasured.

The patch folds the usual AddControlMemory release into EndRecording’s existing lock section. It saves one lock acquisition in the common sequence, while adding per-recording bookkeeping, extending copy lifetimes, and moving pool reclamation after the next recording’s allocation.

@fix will do (18)

  1. L1 host/nvn_native.c:1814: The new retirement path can undercount held submissions. (more on the line)
  2. L2 host/nvn_native.c:1686: Deferral leaves descriptor addresses in the sweep after AddControlMemory has returned. (more on the line)
  3. L3 host/nvn_native.c:1650: release_copies reads rec_buckets without synchronization while the first EndRecording writes it under copy_lock. (more on the line)
  4. L4 host/nvn_native.c:1609: The retirement helper carries forward the plain magic write that races with check_handle’s plain read. (more on the line)
  5. L5 host/nvn_native.c:1819: The new 1 MiB bucket directory uses unchecked calloc. (more on the line)
  6. L6 (minor) host/nvn_native.c:1618: Bucket capacity doubles in uint32_t. (more on the line)
  7. L7 (minor) host/nvn_native.c:1729: Both paths form the range end from an unchecked guest address and length. (more on the line)
  8. L8 (minor) host/nvn_native.c:1623: Every recording performs an atomic read-modify-write on rec_born while holding the sole writer lock, including with the switch off. (more on the line)
  9. L9 tests/test_nvn_copy_lock_once.py:121: The fixture discards recording pool returns and hardcodes held_count to zero. (more on the line)
  10. L10 (minor) tests/test_nvn_copy_lock_once.py:230: The test clears the new switch but inherits HOS_NVN_REC_SHIFT, which the extracted EndRecording reads. (more on the line)
  11. L11 (minor) tests/test_nvn_copy_lock_once.py:123: The fixture uses ordered pointer comparisons between a separately allocated segment and the copy arena, and forms the end with arena[NCOPY][0]. (more on the line)
  12. L12 (minor) tests/test_nvn_copy_lock_once.py:252: Both new compiler selectors pass CC as one executable name, so a setting such as CC='ccache gcc' fails before compilation. (more on the line)
  13. L13 (minor) tests/test_nvn_host_tables.py:28: The changed host-table and log-handoff checks still force --cc gcc through run_all. On a Windows checkout with only the bundled llvm-mingw clang, strict mode reports a missing compiler before either changed fixture runs. Let both use CC and the configured llvm-mingw fallback, like the new fixtures.
  14. L14 (minor) tests/test_nvn_rec_release.py:187: The stated outside-window sweep case is never marked. (more on the line)
  15. L15 (its part an edit can do) host/nvn_native.c:1800: Provide an eligible completed block before requesting a fresh allocation, or preserve the immediate-release path where pool reuse would otherwise be lost.
  16. L16 (its part an edit can do) host/nvn_native.c:1493: Report total bucket capacity and pending copy bytes separately from the live-copy count. Bound the lifetime of pending ranges and reclaim empty oversized buckets if the measurements show a cost.
  17. L17 (its part an edit can do): Add opt-in lock wait and hold counters and distinguish EndRecording from fallback AddControlMemory and Finalize releases.
  18. L18 (minor): The summary calls switch-off main’s code, but off still uses flat buckets, the enlarged slot table and the generation update. Describe it as an immediate-release comparison within this head. The checked current-main box should also reflect that #2022 is still in the diff pending merge and rebase.

For the author (3) (a run, a measurement or a decision)

  • L15 host/nvn_native.c:1800: The new sequence flattens the recording before reclaiming eligible dead copies. (more on the line)
  • L16 (minor) host/nvn_native.c:1493: Recordings kept in the buckets no longer tell you how much memory the table retains. (more on the line)
  • L17 (minor): The fixture counts entries into copy_lock and cannot show whether waits or frame times improve. The planned Kokiri run also leaves out a heavy scene. After #2022 lands, compare the rebased head with current main and with its switch off in warmed, interleaved runs in Kokiri and a heavy area. Report lock wait and hold time, average FPS, 1% lows, median and p99 frame time, and Tier 1 results before settling the default.

Checks: Still running: CI / Conventional Commit title (pull_request), CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request), CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request), CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request), CI / Workflow lint (actionlint) (pull_request), CI / Website build and checks (pull_request), Guide screenshots / Guide images and local UI source review (pull_request), Third-party parts / As their notes say (pull_request), Third-party parts / The same as upstream (pull_request).

More: does it help, other systems, switches, what was read

Does it help: Unproven. The Windows fixture reportedly passed and checks two exclusive sections versus one in the common sequence. No game run or timing compares this head with its base. The commit’s estimate of 1.5% of one i7-9700 core comes from a different branch profile, and the flat-array microbenchmark comes from #2022’s prior work.

Other systems: Both submitting-thread and threaded Vulkan replay use these recording copies on Windows and Linux, across GPU vendors and Steam Deck. CallCommands, CopyCommands, held submits, and periodic sweeping share the lifetime changes. RADV batching can also make the added or shifted replay polls take the renderer’s present mutex under copy_lock. The diff changes no save, options, audio, input, shader, or renderer ABI code.

Switches: HOS_NVN_COPY_LOCK_ONCE is a new developer kill switch, on when unset. The OFF spellings use the shared boolean parser. Its sorted table row is present. Off restores immediate release scheduling on top of the flat buckets. The default has no hardware measurements behind it yet. No other switch changed.

Reviewed: 3b4edada27 against 75317e1f31: Read all four changed Python files, both commits, and the full diff for all six files. Read AGENTS.md, CONTRIBUTING.md, docs/dev/standards.md, and the Forgejo template in full. Read the changed host routines and their slot, pool, handle, submit, sweep, replay, and guest-memory dependencies. Skimmed unrelated host code and switch rows, the linked developer references, and other source-reading checks. The named run logs and branch profile were unavailable in the checkout. No local review-checklist file or sibling Tooling checkout was present.; nothing run (a code review).

Checked and fine:

  • Both commits have Assisted-by trailers. The diff contains no game-derived fixtures, generated files, third-party changes, shader changes, or public ABI changes.
  • The reverse compaction preserves survivor order and the old newest-first retirement order. The cutoff protects entries born after the AddControlMemory snapshot in the sequential fixture.
  • Finalize and reinitialize share cmd_release, and slot clearing removes the pending range. The log-handoff fixture still checks that logging runs after the recording lock and virtual core are released.
  • The switch cache publishes one scalar answer atomically, and the new test programs only compile and run temporary local fixtures.

Testing: Read-only review. The author reports 39 of 48 selected Windows checks passed, including the new recording checks, with nine failures also present on the named baselines. CI was still running in the supplied snapshot. The PR opened without the in-game run required by repository rules. The full suite, Linux GCC and host build, in-game correctness, and Tier 1 performance remain unverified.

Overlaps and merge order: #2022 is the flat-array prerequisite. Merge it first, rebase #2025, and retest and measure against the new main. #1812 is closed and unmerged.


Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.

**Recommendation: Request changes.** The deferred release can return a recording to the pool while a held submit still needs it. It also leaves retired control-memory addresses visible to the sweep, and the player-facing performance gain is unmeasured. The patch folds the usual AddControlMemory release into EndRecording’s existing lock section. It saves one lock acquisition in the common sequence, while adding per-recording bookkeeping, extending copy lifetimes, and moving pool reclamation after the next recording’s allocation. **`@fix` will do (18)** 1. **L1** `host/nvn_native.c:1814`: The new retirement path can undercount held submissions. (more on the line) 2. **L2** `host/nvn_native.c:1686`: Deferral leaves descriptor addresses in the sweep after AddControlMemory has returned. (more on the line) 3. **L3** `host/nvn_native.c:1650`: release_copies reads rec_buckets without synchronization while the first EndRecording writes it under copy_lock. (more on the line) 4. **L4** `host/nvn_native.c:1609`: The retirement helper carries forward the plain magic write that races with check_handle’s plain read. (more on the line) 5. **L5** `host/nvn_native.c:1819`: The new 1 MiB bucket directory uses unchecked calloc. (more on the line) 6. **L6** (minor) `host/nvn_native.c:1618`: Bucket capacity doubles in uint32_t. (more on the line) 7. **L7** (minor) `host/nvn_native.c:1729`: Both paths form the range end from an unchecked guest address and length. (more on the line) 8. **L8** (minor) `host/nvn_native.c:1623`: Every recording performs an atomic read-modify-write on rec_born while holding the sole writer lock, including with the switch off. (more on the line) 9. **L9** `tests/test_nvn_copy_lock_once.py:121`: The fixture discards recording pool returns and hardcodes held_count to zero. (more on the line) 10. **L10** (minor) `tests/test_nvn_copy_lock_once.py:230`: The test clears the new switch but inherits HOS_NVN_REC_SHIFT, which the extracted EndRecording reads. (more on the line) 11. **L11** (minor) `tests/test_nvn_copy_lock_once.py:123`: The fixture uses ordered pointer comparisons between a separately allocated segment and the copy arena, and forms the end with arena[NCOPY][0]. (more on the line) 12. **L12** (minor) `tests/test_nvn_copy_lock_once.py:252`: Both new compiler selectors pass CC as one executable name, so a setting such as CC='ccache gcc' fails before compilation. (more on the line) 13. **L13** (minor) `tests/test_nvn_host_tables.py:28`: The changed host-table and log-handoff checks still force --cc gcc through run_all. On a Windows checkout with only the bundled llvm-mingw clang, strict mode reports a missing compiler before either changed fixture runs. Let both use CC and the configured llvm-mingw fallback, like the new fixtures. 14. **L14** (minor) `tests/test_nvn_rec_release.py:187`: The stated outside-window sweep case is never marked. (more on the line) 15. **L15** (its part an edit can do) `host/nvn_native.c:1800`: Provide an eligible completed block before requesting a fresh allocation, or preserve the immediate-release path where pool reuse would otherwise be lost. 16. **L16** (its part an edit can do) `host/nvn_native.c:1493`: Report total bucket capacity and pending copy bytes separately from the live-copy count. Bound the lifetime of pending ranges and reclaim empty oversized buckets if the measurements show a cost. 17. **L17** (its part an edit can do): Add opt-in lock wait and hold counters and distinguish EndRecording from fallback AddControlMemory and Finalize releases. 18. **L18** (minor): The summary calls switch-off main’s code, but off still uses flat buckets, the enlarged slot table and the generation update. Describe it as an immediate-release comparison within this head. The checked current-main box should also reflect that #2022 is still in the diff pending merge and rebase. **For the author (3)** (a run, a measurement or a decision) - **L15** `host/nvn_native.c:1800`: The new sequence flattens the recording before reclaiming eligible dead copies. (more on the line) - **L16** (minor) `host/nvn_native.c:1493`: Recordings kept in the buckets no longer tell you how much memory the table retains. (more on the line) - **L17** (minor): The fixture counts entries into copy_lock and cannot show whether waits or frame times improve. The planned Kokiri run also leaves out a heavy scene. After #2022 lands, compare the rebased head with current main and with its switch off in warmed, interleaved runs in Kokiri and a heavy area. Report lock wait and hold time, average FPS, 1% lows, median and p99 frame time, and Tier 1 results before settling the default. **Checks:** Still running: `CI / Conventional Commit title (pull_request)`, `CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request)`, `CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request)`, `CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request)`, `CI / Workflow lint (actionlint) (pull_request)`, `CI / Website build and checks (pull_request)`, `Guide screenshots / Guide images and local UI source review (pull_request)`, `Third-party parts / As their notes say (pull_request)`, `Third-party parts / The same as upstream (pull_request)`. <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Unproven. The Windows fixture reportedly passed and checks two exclusive sections versus one in the common sequence. No game run or timing compares this head with its base. The commit’s estimate of 1.5% of one i7-9700 core comes from a different branch profile, and the flat-array microbenchmark comes from #2022’s prior work. **Other systems:** Both submitting-thread and threaded Vulkan replay use these recording copies on Windows and Linux, across GPU vendors and Steam Deck. CallCommands, CopyCommands, held submits, and periodic sweeping share the lifetime changes. RADV batching can also make the added or shifted replay polls take the renderer’s present mutex under copy_lock. The diff changes no save, options, audio, input, shader, or renderer ABI code. **Switches:** HOS_NVN_COPY_LOCK_ONCE is a new developer kill switch, on when unset. The OFF spellings use the shared boolean parser. Its sorted table row is present. Off restores immediate release scheduling on top of the flat buckets. The default has no hardware measurements behind it yet. No other switch changed. **Reviewed:** 3b4edada2780 against 75317e1f31ff: Read all four changed Python files, both commits, and the full diff for all six files. Read AGENTS.md, CONTRIBUTING.md, docs/dev/standards.md, and the Forgejo template in full. Read the changed host routines and their slot, pool, handle, submit, sweep, replay, and guest-memory dependencies. Skimmed unrelated host code and switch rows, the linked developer references, and other source-reading checks. The named run logs and branch profile were unavailable in the checkout. No local review-checklist file or sibling Tooling checkout was present.; nothing run (a code review). **Checked and fine:** - Both commits have Assisted-by trailers. The diff contains no game-derived fixtures, generated files, third-party changes, shader changes, or public ABI changes. - The reverse compaction preserves survivor order and the old newest-first retirement order. The cutoff protects entries born after the AddControlMemory snapshot in the sequential fixture. - Finalize and reinitialize share cmd_release, and slot clearing removes the pending range. The log-handoff fixture still checks that logging runs after the recording lock and virtual core are released. - The switch cache publishes one scalar answer atomically, and the new test programs only compile and run temporary local fixtures. **Testing:** Read-only review. The author reports 39 of 48 selected Windows checks passed, including the new recording checks, with nine failures also present on the named baselines. CI was still running in the supplied snapshot. The PR opened without the in-game run required by repository rules. The full suite, Linux GCC and host build, in-game correctness, and Tier 1 performance remain unverified. **Overlaps and merge order:** #2022 is the flat-array prerequisite. Merge it first, rebase #2025, and retest and measure against the new main. #1812 is closed and unmerged. </details> --- <sub>Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.</sub>
@ -1481,0 +1490,4 @@
the same releases and frees, in the same order (tests/test_nvn_rec_release.py) */
/* (born: the recording's number, rec_born as it was made: what a release put off to the next EndRecording goes by,
HOS_NVN_COPY_LOCK_ONCE) */
typedef struct { uint64_t ctl; uint8_t *copy; uint64_t born; int marked; } rec_ent;
Member

L16 (minor): Recordings kept in the buckets no longer tell you how much memory the table retains. The directory grows by 512 KiB on 64-bit hosts, bucket arrays keep their peak capacity, and this patch adds 8 bytes per capacity slot over #2022 plus 24 KiB per allocated command-buffer chunk. An idle command buffer with a pending range can also keep old copies live indefinitely. Measure peak and settled RAM after a scene transition and a long session.

**L16** (minor): Recordings kept in the buckets no longer tell you how much memory the table retains. The directory grows by 512 KiB on 64-bit hosts, bucket arrays keep their peak capacity, and this patch adds 8 bytes per capacity slot over #2022 plus 24 KiB per allocated command-buffer chunk. An idle command buffer with a pending range can also keep old copies live indefinitely. Measure peak and settled RAM after a scene transition and a long session.
@ -1739,3 +1798,3 @@
o->recording = 0;
const cmd_rec *r = cmd_host(o);
cmd_rec *r = cmd_host(o);
uint8_t *copy = flatten(r->seg, (size_t)(r->cur - r->seg));
Member

L15: The new sequence flattens the recording before reclaiming eligible dead copies. If the pool has no spare block in that size class but a dead block is ready, main returns the block before flatten and can reuse it. The new path can allocate a fresh block or enter the allocation retry while the reusable block is still held. Measure pool misses, peak commit and tail frame times through loads and steady play.

**L15**: The new sequence flattens the recording before reclaiming eligible dead copies. If the pool has no spare block in that size class but a dead block is ready, main returns the block before flatten and can reuse it. The new path can allocate a fresh block or enter the allocation retry while the reusable block is still held. Measure pool misses, peak commit and tail frame times through loads and steady play.
@ -1594,0 +1606,4 @@
until the replay passed `after` */
static void copy_retire(uint8_t *copy, uint64_t after) {
copy_prefix *p = (copy_prefix *)copy - 1;
p->magic = 0; /* (no submit names it any more) */
Member

L4: The retirement helper carries forward the plain magic write that races with check_handle’s plain read. A handle-set hit also provides no lifetime protection during inline_recording’s later memcpy or QueueSubmitCommands’ handoff. A reader that passes the check just before retirement can still use the copy after another cleanup returns it to the pool. Make validation and use share a lifetime guarantee, such as a pin spanning the copy or submit handoff. An atomic magic field alone leaves the lifetime race.

**L4**: The retirement helper carries forward the plain magic write that races with check_handle’s plain read. A handle-set hit also provides no lifetime protection during inline_recording’s later memcpy or QueueSubmitCommands’ handoff. A reader that passes the check just before retirement can still use the copy after another cleanup returns it to the pool. Make validation and use share a lifetime guarantee, such as a pin spanning the copy or submit handoff. An atomic magic field alone leaves the lifetime race.
@ -1594,0 +1615,4 @@
static void copy_track(uint64_t ctl, uint8_t *copy, int marked) {
rec_list *l = &rec_buckets[rec_bucket(ctl)];
if (l->n == l->cap) {
l->cap = l->cap ? l->cap * 2 : 4;
Member

L6 (minor): Bucket capacity doubles in uint32_t. If a bucket reaches 2^31 entries, the next growth passes zero bytes to realloc and the subsequent append writes outside a valid allocation. This requires an unusually large process, but entries without control memory all use the same bucket. Check growth before doubling and check the byte count before reallocating.

**L6** (minor): Bucket capacity doubles in uint32_t. If a bucket reaches 2^31 entries, the next growth passes zero bytes to realloc and the subsequent append writes outside a valid allocation. This requires an unusually large process, but entries without control memory all use the same bucket. Check growth before doubling and check the byte count before reallocating.
@ -1594,0 +1620,4 @@
}
rec_ent *e = &l->e[l->n++];
e->ctl = ctl, e->copy = copy, e->marked = marked;
e->born = __atomic_add_fetch(&rec_born, 1, __ATOMIC_RELAXED);
Member

L8 (minor): Every recording performs an atomic read-modify-write on rec_born while holding the sole writer lock, including with the switch off. Reuse the recording serial already incremented under copy_lock and publish it with an atomic store. That avoids an extra locked instruction on the hot path and gives the off comparison less bookkeeping.

**L8** (minor): Every recording performs an atomic read-modify-write on rec_born while holding the sole writer lock, including with the switch off. Reuse the recording serial already incremented under copy_lock and publish it with an atomic store. That avoids an extra locked instruction on the hot path and gives the off comparison less bookkeeping.
@ -1613,1 +1647,4 @@
}
}
static void release_copies(uint64_t lo, uint64_t hi, uint64_t born) {
if (hi <= lo || !rec_buckets) return;
Member

L3: release_copies reads rec_buckets without synchronization while the first EndRecording writes it under copy_lock. The new pending-release path can hit this when a command buffer is finalized as another thread publishes the first bucket table. The off path retains the same race from main. Put the check under copy_lock or publish a readiness flag atomically and recheck under the lock.

**L3**: release_copies reads rec_buckets without synchronization while the first EndRecording writes it under copy_lock. The new pending-release path can hit this when a command buffer is finalized as another thread publishes the first bucket table. The off path retains the same race from main. Put the check under copy_lock or publish a readiness flag atomically and recheck under the lock.
@ -1642,3 +1685,2 @@
uint64_t mark[2];
if (!n->marked) {
pn = &n->next;
void *at = e->marked ? hos_guest_range(e->ctl, sizeof mark) : NULL;
Member

L2: Deferral leaves descriptor addresses in the sweep after AddControlMemory has returned. If an added range is abandoned and its pages are returned before the next EndRecording, another recorder’s sweep can touch an old mark. On Windows the window check accepts a decommitted page, and the fault handler can recommit 64 KiB under copy_lock or fail under commit pressure. Immediate release had already removed that entry. Prevent the sweep from probing pending retirements, and use a guest-memory read that is safe against decommit for the marks it still probes.

**L2**: Deferral leaves descriptor addresses in the sweep after AddControlMemory has returned. If an added range is abandoned and its pages are returned before the next EndRecording, another recorder’s sweep can touch an old mark. On Windows the window check accepts a decommitted page, and the fault handler can recommit 64 KiB under copy_lock or fail under commit pressure. Immediate release had already removed that entry. Prevent the sweep from probing pending retirements, and use a guest-memory read that is safe against decommit for the marks it still probes.
@ -1675,0 +1726,4 @@
released then. The old recordings live a little longer than before, never shorter */
cmd_rec *r = cmd_host(o);
if (r->rel_hi > r->rel_lo) release_copies(r->rel_lo, r->rel_hi, r->rel_born);
r->rel_lo = U64(1), r->rel_hi = U64(1) + U64(2);
Member

L7 (minor): Both paths form the range end from an unchecked guest address and length. An overflowing length makes hi fall below lo, silently skipping retirement while still installing the new control-memory arguments. Validate the span once before changing the object and use the checked endpoints in both modes.

**L7** (minor): Both paths form the range end from an unchecked guest address and length. An overflowing length makes hi fall below lo, silently skipping retirement while still installing the new control-memory arguments. Validate the span once before changing the object and use the checked endpoints in both modes.
@ -1756,3 +1817,3 @@
const char *e = getenv("HOS_NVN_REC_SHIFT");
if (e && atoi(e) >= 12 && atoi(e) <= 24) rec_shift = (unsigned)atoi(e);
rec_buckets = (rec_node **)calloc(REC_BUCKETS, sizeof *rec_buckets);
rec_buckets = (rec_list *)calloc(REC_BUCKETS, sizeof *rec_buckets);
Member

L5: The new 1 MiB bucket directory uses unchecked calloc. If it fails on first recording, release_range or copy_track accesses a null table. Use hos_xcalloc so the allocation gets the host’s retry and out-of-memory handling.

**L5**: The new 1 MiB bucket directory uses unchecked calloc. If it fails on first recording, release_range or copy_track accesses a null table. Use hos_xcalloc so the allocation gets the host’s retry and out-of-memory handling.
@ -1754,0 +1811,4 @@
/* (longshot, HOS_NVN_COPY_LOCK_ONCE) the control memory added before this recording lets its old recordings go in
this section (nvnCommandBufferAddControlMemory), before the new one goes in */
const int waiting = r->rel_hi > r->rel_lo;
const uint64_t after = waiting ? rx_nvn_pushed() + held_count() : 0; /* (held submits name them too) */
Member

L1: The new retirement path can undercount held submissions. With HOS_NVN_HOLD=1, a submit can already be held when AddControlMemory returns. If a flush transfers it to Vulkan between the pushed-count read and the held-count read at EndRecording, the sum can miss it and the next cleanup can recycle its copy before replay. The volatile nheld read also races with the flush. Take a consistent snapshot at retirement, using the held queue’s synchronization or a submission sequence that covers both states. Moving the two reads under copy_lock alone cannot synchronize the flush.

**L1**: The new retirement path can undercount held submissions. With HOS_NVN_HOLD=1, a submit can already be held when AddControlMemory returns. If a flush transfers it to Vulkan between the pushed-count read and the held-count read at EndRecording, the sum can miss it and the next cleanup can recycle its copy before replay. The volatile nheld read also races with the flush. Take a consistent snapshot at retirement, using the held queue’s synchronization or a submission sequence that covers both states. Moving the two reads under copy_lock alone cannot synchronize the flush.
@ -0,0 +118,4 @@
if (!p) die(what);
return p;
}
static void pool_put(void *b, unsigned cls) {
Member

L9: The fixture discards recording pool returns and hardcodes held_count to zero. Changing EndRecording to retire every deferred copy with ticket zero can still pass because the assertions only observe handle removal and lock counts. Add a deterministic held-to-pushed handoff and record pool returns. Assert the copy stays out of the pool until replay reaches the transferred submit.

**L9**: The fixture discards recording pool returns and hardcodes held_count to zero. Changing EndRecording to retire every deferred copy with ticket zero can still pass because the assertions only observe handle removal and lock counts. Add a deterministic held-to-pushed handoff and record pool returns. Assert the copy stays out of the pool until replay reaches the transferred submit.
@ -0,0 +120,4 @@
}
static void pool_put(void *b, unsigned cls) {
(void)cls;
if ((uint8_t *)b >= &arena[0][0] && (uint8_t *)b < &arena[NCOPY][0]) return;
Member

L11 (minor): The fixture uses ordered pointer comparisons between a separately allocated segment and the copy arena, and forms the end with arena[NCOPY][0]. Both new fixtures also use declared byte arrays as copy_prefix objects without guaranteeing alignment or compatible storage. Use typed or suitably aligned allocated storage, valid one-past bounds, and integer addresses or explicit allocation tags to distinguish segments from copies.

**L11** (minor): The fixture uses ordered pointer comparisons between a separately allocated segment and the copy arena, and forms the end with arena[NCOPY][0]. Both new fixtures also use declared byte arrays as copy_prefix objects without guaranteeing alignment or compatible storage. Use typed or suitably aligned allocated storage, valid one-past bounds, and integer addresses or explicit allocation tags to distinguish segments from copies.
@ -0,0 +227,4 @@
def run(exe, value, once):
env = {k: v for k, v in os.environ.items() if k != SWITCH}
Member

L10 (minor): The test clears the new switch but inherits HOS_NVN_REC_SHIFT, which the extracted EndRecording reads. A developer with shift 24 exercises a different bucket layout from CI. Clear it for the default case and explicitly set it in any alternate-layout cases.

**L10** (minor): The test clears the new switch but inherits HOS_NVN_REC_SHIFT, which the extracted EndRecording reads. A developer with shift 24 exercises a different bucket layout from CI. Clear it for the default case and explicitly set it in any alternate-layout cases.
@ -0,0 +249,4 @@
def compiler(given):
"""--cc, else $CC, else the configured llvm-mingw's clang (LONGSHOT_MINGW, Windows: run_all.py sets it to the
clone's folder), else clang, gcc or cc on PATH"""
for c in (given, os.environ.get("CC")):
Member

L12 (minor): Both new compiler selectors pass CC as one executable name, so a setting such as CC='ccache gcc' fails before compilation. Accept a compiler command as an argument vector while preserving explicit executable paths that contain spaces.

**L12** (minor): Both new compiler selectors pass CC as one executable name, so a setting such as CC='ccache gcc' fails before compilation. Accept a compiler command as an argument vector while preserving explicit executable paths that contain spaces.
@ -0,0 +184,4 @@
const uint64_t ctl = pick_ctl(shape);
uint8_t *copy = new_copy();
int marked = 0;
if (ctl < WINDOW - 16 && rnd() % 8) {
Member

L14 (minor): The stated outside-window sweep case is never marked. Every address outside WINDOW enters the sweep with marked=0, so removing the range guard from marked entries would escape this case. Add a marked entry at or crossing the window boundary and assert the sweep handles it without reading the mark.

**L14** (minor): The stated outside-window sweep case is never marked. Every address outside WINDOW enters the sweep with marked=0, so removing the range guard from marked entries would escape this case. Add a marked entry at or crossing the window boundary and assert the sweep handles it without reading the mark.
siantm force-pushed perf/nvn-copy-lock-once from 3b4edada27
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 11s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
to 762cac5a63
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
Website / Website on Cloudflare (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 6s
beanstalk/approval waiting for a BigBean's 👍 on the request
Website / Website on Cloudflare (pull_request_target) Has been skipped
2026-11-21 08:18:40 +00:00
Compare
siantm changed title from WIP: perf(nvn): AddControlMemory's release of old recordings waits for the next EndRecording's lock (HOS_NVN_COPY_LOCK_ONCE=0 to disable) to WIP: perf(nvn): AddControlMemory's release of old recordings waits for the next EndRecording's lock (HOS_NVN_COPY_LOCK_ONCE=1 to enable) 2026-11-21 08:19:06 +00:00
Author
Contributor

Thanks for the review. The branch is rebased on #2022's new head db31aa81 (main @ fd7dc40d; "depends on #2022" still holds), with a test commit ac1f4ad9 and a fix commit 762cac5a on top; head 762cac5a. The PR's own commit bdba502b changed in the rebase only where #2022's fix forced it (git range-diff): EndRecording calls rec_buckets_make instead of making the table under the lock, AddControlMemory checks its range first, release_copies keeps #2022's locked test and ticket, the walk keeps #2022's in-place keep, and tests/test_nvn_copy_lock_once.py cuts rec_buckets_make, has a hos_guest_range stand-in and counts the first recording's two copy_lock sections (the table's publication). HOS_NVN_COPY_LOCK_ONCE is now off by default (762cac5a): the deferred release has not run in the game, so it ships off until a run measures it; =1 turns it on. The checks below ran in build job fb-2022-2 and fb-2025-3 of this PC's job queue (Windows 11, llvm-mingw clang 23): tests/test_nvn_copy_lock_once.py fails at the test commit ac1f4ad9 (with =1, a copy back in the pool with 32 submits replayed when 34 were pushed or held as it was let go) and passes at the head; the five checks it touches or inherits pass under tests/run_all.py --strict --exact; nvn_native.c compiles with the game build's command; of the checks --changed db31aa81 picks, the 9 failures other than this fixture's fail on main fd7dc40d too (Tested lists them). Nothing has run in the game yet.

  • L1 (#issuecomment-64255), the deferred release's ticket: fixed in 762cac5a, on #2022's db31aa81. EndRecording reads it inside its copy_lock section, the held count first, then the pushed one, and the flushes empty the held list with a release store only after pushing it (#2022), so a flush between the reads is counted twice, never missed. The fixture now holds and pushes submits, flushes the held ones right after a read of the pushed count, moves the replay one submit at a time and logs each copy's return to the pool; a copy may go back only once the replay passed every submit pushed or held when it was let go. At the test commit it failed so with the switch on (32 submits replayed, 34 pushed or held); it passes at the head.
  • L2 (#issuecomment-64257), pending ranges' marks in the sweep: no code change. Copies whose control memory the game freed without adding it again stay in the sweep on main too; that is the case the sweep is for, and a deferral only leaves ranges that were added again there until the next EndRecording. The read of a decommitted page is main's, answered in #2022 (#issuecomment-64213) and left for a separate change. With the switch now off by default, nothing is deferred unless it is set.
  • L3 (#issuecomment-64259), rec_buckets without synchronization: fixed in #2022's db31aa81 (tested in 9f83a9a2): release_copies, which Finalize's pending release calls too, tests the pointer under copy_lock.
  • L4 (#issuecomment-64261), magic and the copy's lifetime: the same as #2022's L3 (#issuecomment-64212): pre-existing on main, not changed here, left for a separate change that pins the copy from the check to its use.
  • L5 (#issuecomment-64263), the unchecked calloc: fixed in #2022's db31aa81: rec_buckets_make uses hos_xcalloc before the lock and publishes under it.
  • L6 (#issuecomment-64264), the doubling: fixed in #2022's db31aa81: stops past 2^31 entries, bytes in size_t, cap set after the allocation.
  • L7 (#issuecomment-64265), the range end: fixed in #2022's db31aa81: AddControlMemory checks the range is guest memory before either mode, so both take endpoints that do not wrap.
  • L8 (#issuecomment-64266), the atomic add on rec_born: fixed in 762cac5a. copy_track, its only writer, under copy_lock, stores rec_born + 1 with a relaxed atomic store for AddControlMemory's lockless read: no locked instruction a recording. It is a counter of its own beside EndRecording's made (one each a recording), which stays a function-local count for the log line.
  • L9 (#issuecomment-64267), pool returns and held submits in the fixture: fixed in ac1f4ad9, as in L1; at the end, with everything replayed, every copy let go must be back in the pool, once.
  • L10 (#issuecomment-64268), HOS_NVN_REC_SHIFT: fixed in ac1f4ad9: removed from every run's environment.
  • L11 (#issuecomment-64269), pointer comparisons and storage: fixed in ac1f4ad9 (the copies' arena is allocated storage, aligned for any object, a row a copy; a copy is told from a segment by its address as an integer against the arena's range, and its number comes from the same) and in #2022's 9f83a9a2 for test_nvn_rec_release.py.
  • L12 (#issuecomment-64270), CC as one name: fixed: both fixtures take their compiler from tests/cccmd.py (#2022's 9f83a9a2): --cc or CC naming an existing file is one program (spaces kept), any other value is split as a shell would (CC='ccache gcc').
  • L13 (item 13 of the review), --cc gcc through run_all: fixed: test_nvn_host_tables.py in ac1f4ad9 and test_recording_log_handoff.py in #2022's 9f83a9a2 no longer declare RUN_ALL_ARGS and use tests/cccmd.py. Run with no CC, both pass at the head.
  • L14 (#issuecomment-64271), marked entries out of the window: fixed in #2022's 9f83a9a2 (copies marked past the window and across its end, in the run and in a directed sweep).
  • L15 (#issuecomment-64272), pool reuse after flatten: no code change. By the code, with the switch on, the dead blocks EndRecording's section frees go back to the pool after that recording's copy was taken, and are there for the next one: the pool's demand is one copy a recording thread ahead, not growing. That is reading, not a measurement; pool misses, peak commit and tail frame times through loads and play are pending, and the switch is off by default until then.
  • L16 (#issuecomment-64273), memory reporting: the 50,000-recording line now gives the buckets' room (entries and MB) beside the live copies (#2022's db31aa81); copies waiting in a pending range are counted live until released. A pending range lasts until the command buffer's next EndRecording, AddControlMemory, Finalize or Initialize; Risks says an idle command buffer keeps its old copies until then. No reclaiming is added without a measurement; the RAM measurement after a scene change and a long session is pending.
  • L17 (item 17 of the review), lock counters by site: no code change. copy_lock waits are Tracy "lock wait" zones (HT_WRAP_LOCKS in nvn_native.c), inside EndRecording's own zone when they are EndRecording's; Still to test asks for them with a Tracy build in Kokiri and the Market, with average FPS, 1% lows, median and p99 frame times, main against this PR unset and =1. Pending for the author.
  • L18 (item 18 of the review), "=0 gives main's code": fixed in the description: unset or 0 (now the default) is the release at the call on top of #2022's flat buckets, a comparison within this head, not main's code; the description says the branch is on main @ fd7dc40d with #2022's commits in the diff until it merges.
Thanks for the review. The branch is rebased on #2022's new head db31aa81 (main @ fd7dc40d; "depends on #2022" still holds), with a test commit ac1f4ad9 and a fix commit 762cac5a on top; head 762cac5a. The PR's own commit bdba502b changed in the rebase only where #2022's fix forced it (`git range-diff`): EndRecording calls `rec_buckets_make` instead of making the table under the lock, AddControlMemory checks its range first, `release_copies` keeps #2022's locked test and ticket, the walk keeps #2022's in-place keep, and `tests/test_nvn_copy_lock_once.py` cuts `rec_buckets_make`, has a `hos_guest_range` stand-in and counts the first recording's two `copy_lock` sections (the table's publication). **`HOS_NVN_COPY_LOCK_ONCE` is now off by default** (762cac5a): the deferred release has not run in the game, so it ships off until a run measures it; `=1` turns it on. The checks below ran in build job `fb-2022-2` and `fb-2025-3` of this PC's job queue (Windows 11, llvm-mingw clang 23): `tests/test_nvn_copy_lock_once.py` fails at the test commit ac1f4ad9 (with `=1`, a copy back in the pool with 32 submits replayed when 34 were pushed or held as it was let go) and passes at the head; the five checks it touches or inherits pass under `tests/run_all.py --strict --exact`; `nvn_native.c` compiles with the game build's command; of the checks `--changed db31aa81` picks, the 9 failures other than this fixture's fail on main fd7dc40d too (Tested lists them). Nothing has run in the game yet. - **L1** ([#issuecomment-64255](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64255)), the deferred release's ticket: fixed in 762cac5a, on #2022's db31aa81. EndRecording reads it inside its `copy_lock` section, the held count first, then the pushed one, and the flushes empty the held list with a release store only after pushing it (#2022), so a flush between the reads is counted twice, never missed. The fixture now holds and pushes submits, flushes the held ones right after a read of the pushed count, moves the replay one submit at a time and logs each copy's return to the pool; a copy may go back only once the replay passed every submit pushed or held when it was let go. At the test commit it failed so with the switch on (32 submits replayed, 34 pushed or held); it passes at the head. - **L2** ([#issuecomment-64257](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64257)), pending ranges' marks in the sweep: no code change. Copies whose control memory the game freed without adding it again stay in the sweep on main too; that is the case the sweep is for, and a deferral only leaves ranges that were added again there until the next EndRecording. The read of a decommitted page is main's, answered in #2022 ([#issuecomment-64213](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2022#issuecomment-64213)) and left for a separate change. With the switch now off by default, nothing is deferred unless it is set. - **L3** ([#issuecomment-64259](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64259)), `rec_buckets` without synchronization: fixed in #2022's db31aa81 (tested in 9f83a9a2): `release_copies`, which Finalize's pending release calls too, tests the pointer under `copy_lock`. - **L4** ([#issuecomment-64261](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64261)), magic and the copy's lifetime: the same as #2022's L3 ([#issuecomment-64212](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2022#issuecomment-64212)): pre-existing on main, not changed here, left for a separate change that pins the copy from the check to its use. - **L5** ([#issuecomment-64263](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64263)), the unchecked `calloc`: fixed in #2022's db31aa81: `rec_buckets_make` uses `hos_xcalloc` before the lock and publishes under it. - **L6** ([#issuecomment-64264](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64264)), the doubling: fixed in #2022's db31aa81: stops past 2^31 entries, bytes in `size_t`, `cap` set after the allocation. - **L7** ([#issuecomment-64265](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64265)), the range end: fixed in #2022's db31aa81: AddControlMemory checks the range is guest memory before either mode, so both take endpoints that do not wrap. - **L8** ([#issuecomment-64266](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64266)), the atomic add on `rec_born`: fixed in 762cac5a. `copy_track`, its only writer, under `copy_lock`, stores `rec_born + 1` with a relaxed atomic store for AddControlMemory's lockless read: no locked instruction a recording. It is a counter of its own beside EndRecording's `made` (one each a recording), which stays a function-local count for the log line. - **L9** ([#issuecomment-64267](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64267)), pool returns and held submits in the fixture: fixed in ac1f4ad9, as in L1; at the end, with everything replayed, every copy let go must be back in the pool, once. - **L10** ([#issuecomment-64268](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64268)), `HOS_NVN_REC_SHIFT`: fixed in ac1f4ad9: removed from every run's environment. - **L11** ([#issuecomment-64269](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64269)), pointer comparisons and storage: fixed in ac1f4ad9 (the copies' arena is allocated storage, aligned for any object, a row a copy; a copy is told from a segment by its address as an integer against the arena's range, and its number comes from the same) and in #2022's 9f83a9a2 for `test_nvn_rec_release.py`. - **L12** ([#issuecomment-64270](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64270)), `CC` as one name: fixed: both fixtures take their compiler from `tests/cccmd.py` (#2022's 9f83a9a2): `--cc` or `CC` naming an existing file is one program (spaces kept), any other value is split as a shell would (`CC='ccache gcc'`). - **L13** (item 13 of the review), `--cc gcc` through run_all: fixed: `test_nvn_host_tables.py` in ac1f4ad9 and `test_recording_log_handoff.py` in #2022's 9f83a9a2 no longer declare `RUN_ALL_ARGS` and use `tests/cccmd.py`. Run with no `CC`, both pass at the head. - **L14** ([#issuecomment-64271](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64271)), marked entries out of the window: fixed in #2022's 9f83a9a2 (copies marked past the window and across its end, in the run and in a directed sweep). - **L15** ([#issuecomment-64272](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64272)), pool reuse after flatten: no code change. By the code, with the switch on, the dead blocks EndRecording's section frees go back to the pool after that recording's copy was taken, and are there for the next one: the pool's demand is one copy a recording thread ahead, not growing. That is reading, not a measurement; pool misses, peak commit and tail frame times through loads and play are pending, and the switch is off by default until then. - **L16** ([#issuecomment-64273](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2025#issuecomment-64273)), memory reporting: the 50,000-recording line now gives the buckets' room (entries and MB) beside the live copies (#2022's db31aa81); copies waiting in a pending range are counted live until released. A pending range lasts until the command buffer's next EndRecording, AddControlMemory, Finalize or Initialize; Risks says an idle command buffer keeps its old copies until then. No reclaiming is added without a measurement; the RAM measurement after a scene change and a long session is pending. - **L17** (item 17 of the review), lock counters by site: no code change. `copy_lock` waits are Tracy "lock wait" zones (`HT_WRAP_LOCKS` in `nvn_native.c`), inside EndRecording's own zone when they are EndRecording's; Still to test asks for them with a Tracy build in Kokiri and the Market, with average FPS, 1% lows, median and p99 frame times, main against this PR unset and `=1`. Pending for the author. - **L18** (item 18 of the review), "=0 gives main's code": fixed in the description: unset or `0` (now the default) is the release at the call on top of #2022's flat buckets, a comparison within this head, not main's code; the description says the branch is on main @ fd7dc40d with #2022's commits in the diff until it merges.
Author
Contributor

@Beanstalk This adds HOS_NVN_COPY_LOCK_ONCE, off by default: with HOS_NVN_COPY_LOCK_ONCE=1, AddControlMemory releases old recordings under the copy lock taken once (host/nvn_native.c); unset, they are released at the call as on main. It is on #2022, whose commit is in its diff. It has not run in the game on these PCs yet. Please play Hyrule Field for three minutes, the PR against main on the same PC, on the PR's side with HOS_NVN_COPY_LOCK_ONCE=1 and with it unset, with a picture compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture, no crash, no FATAL line and no "not a live recording" line; set, frame time p99 equal to unset or better; unset, as main's or better.

@Beanstalk This adds HOS_NVN_COPY_LOCK_ONCE, off by default: with HOS_NVN_COPY_LOCK_ONCE=1, AddControlMemory releases old recordings under the copy lock taken once (host/nvn_native.c); unset, they are released at the call as on main. It is on #2022, whose commit is in its diff. It has not run in the game on these PCs yet. Please play Hyrule Field for three minutes, the PR against main on the same PC, on the PR's side with HOS_NVN_COPY_LOCK_ONCE=1 and with it unset, with a picture compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture, no crash, no FATAL line and no "not a live recording" line; set, frame time p99 equal to unset or better; unset, as main's or better.
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
Website / Website on Cloudflare (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 6s
beanstalk/approval waiting for a BigBean's 👍 on the request
Website / Website on Cloudflare (pull_request_target) Has been skipped
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-copy-lock-once:siantm-perf/nvn-copy-lock-once'
git switch 'siantm-perf/nvn-copy-lock-once'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 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!2025
No description provided.