Skip to content

nvn/arena: release idle per-frame arena blocks, and let a submission go on in the block the one before it ended in - #2862

Closed
oliverames wants to merge 1 commit into
seedds:mainfrom
oliverames:nx18-arena-block-release
Closed

oliverames wants to merge 1 commit into
seedds:mainfrom
oliverames:nx18-arena-block-release

Conversation

@oliverames

Copy link
Copy Markdown
Contributor

Requested by Oliver · project thread

What and why

Credit: this is kencalx's work from the NX18 fork, ported onto current main. kencalx is a co-author on the commit, and every measurement below that is not marked as mine is theirs.

Since the submissions of a frame share a command allocator (PR #2090), the submit thread no longer waits for one of 8 allocators and up to POST_RING submissions can be in flight. Each took 4 MB arena blocks of its own and kept them for good, so the number of blocks followed the submissions in flight, not the bytes they wrote. kencalx's NX18 fork saw NieR:Automata make 26-28 blocks from about 105 s on (8 before the merge) and hold 100-150 MB more graphics memory. Releasing idle blocks alone gave nothing back: the blocks were in use.

  • arena_commit / arena_begin: the next submission continues in the block the last one ended in while it has room. The block's fence becomes the later submission's, so it is reused only when every submission that wrote it has completed. DM_NVN_ARENA_SHARE=0 restores a block per submission.
  • arena_new takes the smallest block that fits and, of those, the one used last, so blocks a peak made stay unused. arena_trim releases those above 4 (ARENA_KEEP_BLOCKS) once no submission used them for 600 submissions (ARENA_IDLE), never one whose fence is above g_done or one a submission is writing. DM_NVN_ARENA_TRIM=0 keeps them. The memory line gives the blocks alive and released.
  • encode_args_'s patch path read g_cur_arena->gpu after cbuf_slot and cbsize_table, which can move g_cur_arena to a new block: the copy was then bound at the new block and the old offset. Rare with a block per submission; NX18 saw it hang the GPU 1-4 times in 160 s once blocks were shared. The address is now read when the block is taken.
  • t_arena_trim (a Mac test): growth with the GPU held, idle release, shared blocks and the patched block whose size table goes to a new block.

Ported from NX18 commit 63c4e8d64 (kencalx/NX18@63c4e8d) without its memory-trim half, which depends on NX18's memory trim that this tree does not have. NX18's numbers are theirs, not measured here.

Checked

  • Tests: on a Mac (M2 Pro, macOS 27.3, Xcode 27.3 beta, GAME=0400486024DEC000): the dmhost build, and check_t_arena_trim passes in all four of its runs (17, 17, 14 and 17 checks). Not checked: a game, a device, or the memory figures above, which are NX18's.
  • Games run, with title ID and version: none for this PR; the game observations above are NX18's.
  • Devices and OS: M2 Pro, macOS 27.3, Xcode 27.3 beta. No iPhone or iPad run.
  • scripts/regress.py check --games: not run.

Docs

  • The finding is in its game note or its docs/reference/<subsystem>.md entry, and python3 dm/tools/docs_symptoms.py was run (the symptom index)
  • A chain tool changed: not applicable, no chain tool changed
  • No game files, keys or data extracted from a game in the change or the attachments

🤖 Generated with Claude Code

https://claude.ai/code/session_01RcRaRTbNVNsBJcfGqFidW1

…go on in the block the one before it ended in

Since the submissions of a frame share a command allocator (PR seedds#2090), the
submit thread no longer waits for one of 8 allocators and up to POST_RING
submissions can be in flight. Each took 4 MB arena blocks of its own and kept
them for good, so the number of blocks followed the submissions in flight, not
the bytes they wrote. kencalx's NX18 fork saw NieR:Automata make 26-28 blocks
from about 105 s on (8 before the merge) and hold 100-150 MB more graphics
memory. Releasing idle blocks alone gave nothing back: the blocks were in use.

- arena_commit / arena_begin: the next submission continues in the block the
  last one ended in while it has room. The block's fence becomes the later
  submission's, so it is reused only when every submission that wrote it has
  completed. DM_NVN_ARENA_SHARE=0 restores a block per submission.
- arena_new takes the smallest block that fits and, of those, the one used
  last, so blocks a peak made stay unused. arena_trim releases those above 4
  (ARENA_KEEP_BLOCKS) once no submission used them for 600 submissions
  (ARENA_IDLE), never one whose fence is above g_done or one a submission is
  writing. DM_NVN_ARENA_TRIM=0 keeps them. The memory line gives the blocks
  alive and released.
- encode_args_'s patch path read g_cur_arena->gpu after cbuf_slot and
  cbsize_table, which can move g_cur_arena to a new block: the copy was then
  bound at the new block and the old offset. Rare with a block per submission;
  NX18 saw it hang the GPU 1-4 times in 160 s once blocks were shared. The
  address is now read when the block is taken.
- t_arena_trim (a Mac test): growth with the GPU held, idle release, shared
  blocks and the patched block whose size table goes to a new block.

Ported from NX18 commit 63c4e8d64 (kencalx/NX18@63c4e8d)
without its memory-trim half, which depends on NX18's memory trim that this
tree does not have. NX18's numbers are theirs, not measured here.

Checked on a Mac (M2 Pro, macOS 27.3, Xcode 27.3 beta, GAME=0400486024DEC000): the dmhost build, and check_t_arena_trim passes in all four of its runs (17, 17, 14 and 17 checks). Not checked: a game, a device, or the memory figures above, which are NX18's.

Co-authored-by: kencalx <105067885+kencalx@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RcRaRTbNVNsBJcfGqFidW1
seedds added a commit that referenced this pull request Nov 17, 2026
Let consecutive submissions append to unused arena tails, retaining each
block through its latest writer's fence. Prefer the smallest completed
block and the most recently used equal-sized block so peak allocations
can age out. Retire ordinary blocks above four after 600 idle submissions,
while preserving the existing large-scratch retirement policy.

Shader argument caches borrow their buffers from the owning arena rather
than pinning scratch for shaders no longer drawn. Check the pass before
accessing a cached buffer. Count all idle releases and publish atomic
arena diagnostics. Preserve current main's arena_take address contract;
the duplicate PR address fix is already covered by 44d67fb/1073e98b.

On Apple M4 Pro, macOS 27.2.1, NieR 1.0.3's opening-flight route against
b04c42b: the unobscured baseline retains 24 blocks (96 MB), both candidates
two (8 MB). GPU allocated at frame 12300 falls 2401 -> 2313 MB; peak
footprint 2316 -> 2228/2225 MB, with 59.9 fps throughout the clean runs.
The full regression exits 0: CHANGED only for moving ships, below the
baseline screenshot variation. A covered baseline and an interrupted
overlapping measurement are documented separately from the memory claim.
Isaac was skipped at the user's request; no iPhone run.

Allocator GPU tests pass 25/25/22/20 checks; argument-generation tests pass
42 checks. Restoring strong shader ownership fails the physical-retirement
assertion in an isolated negative test. Docs and environment-switch checks
pass, and LLVM 23.1.5 reports no new lint findings. Final standards and spec
reviews found no remaining issues.

Co-authored-by: Oliver Ames <7366664+oliverames@users.noreply.github.com>
Co-authored-by: kenCalx <105067885+kencalx@users.noreply.github.com>
@seedds

seedds commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Landed on main in 3fe574df. Thanks Oliver and kenCalx; both are credited as co-authors for the retained code and design.

Integration: Yes. Shared arena tails and completed-idle retirement are integrated. The duplicate arena-address fix was omitted in favor of main's existing arena_take interface. Shader caches now borrow arena buffers, release totals include large blocks, and the GPU fixtures exercise pending/latest-writer protection and actual buffer lifetime. Documentation and the NieR backlink are updated.

Should it exist? Yes. Current main reproduced 24 retained blocks in the unobscured control; existing retirement covered only large blocks.

Verification: Pass. Allocator checks 25/25/22/20 and argument-buffer checks 42/42; these pass again after rebasing onto 015fed3. Docs/env checks pass; no new LLVM 23 findings. The rebase changed only documentation context in this patch. An isolated negative test restoring strong shader ownership fails the physical-retirement assertion.

NieR's full opening-flight regression exits 0, CHANGED solely for motion at 180 s (RGB 8.34 below baseline-repeat variation 10.67). No new red kind, crash or hang; all shaders unchanged and all 902 pipeline bases prebuild. Memory figures use the unobscured baseline and two unobscured candidates, excluding the covered control and interrupted overlapping run. Measurements and limitations are in the game note.

Confidence: High. Direct memory A/B and GPU boundary tests support the fix.

Binding: General, host now and host at its loosest correct binding. No game or build gate.

Root cause or workaround? Root-cause fix. Allocation and ownership policy are corrected directly.

From-scratch design? Yes. Existing arena, fence and residency mechanisms suffice.

Performance impact: Measured. NieR 1.0.3 on Apple M4 Pro/macOS 27.2.1 against b04c42b: blocks 24 (96 MB) to 2 (8 MB), GPU allocated at frame 12,300 2,401 to 2,313 MB, and peak footprint 2,316 to 2,228/2,225 MB. The clean runs remain at 59.9 fps; no idle trims were needed in the game because sharing prevented the peak, while the fixture verifies idle release.

Regression level: Moderate chance. General NVN scratch lifetime; NieR was selected because it reproduces the amplification, with targeted GPU fixtures covering lifetime hazards. Isaac was skipped at the user's request; no iPhone run.

Should we just leave it? No. The verified 88–91 MB reduction justifies the general fix.

Remaining: None in the implementation. Isaac and iPhone validation remain unrun as stated; no contributor retest is awaited. Closing as integrated.

@seedds seedds closed this Nov 17, 2026
dsi-hub pushed a commit to dsi-hub/nxnative that referenced this pull request Nov 17, 2026
)

Let consecutive submissions append to unused arena tails, retaining each
block through its latest writer's fence. Prefer the smallest completed
block and the most recently used equal-sized block so peak allocations
can age out. Retire ordinary blocks above four after 600 idle submissions,
while preserving the existing large-scratch retirement policy.

Shader argument caches borrow their buffers from the owning arena rather
than pinning scratch for shaders no longer drawn. Check the pass before
accessing a cached buffer. Count all idle releases and publish atomic
arena diagnostics. Preserve current main's arena_take address contract;
the duplicate PR address fix is already covered by 44d67fb/1073e98b.

On Apple M4 Pro, macOS 27.2.1, NieR 1.0.3's opening-flight route against
b04c42b: the unobscured baseline retains 24 blocks (96 MB), both candidates
two (8 MB). GPU allocated at frame 12300 falls 2401 -> 2313 MB; peak
footprint 2316 -> 2228/2225 MB, with 59.9 fps throughout the clean runs.
The full regression exits 0: CHANGED only for moving ships, below the
baseline screenshot variation. A covered baseline and an interrupted
overlapping measurement are documented separately from the memory claim.
Isaac was skipped at the user's request; no iPhone run.

Allocator GPU tests pass 25/25/22/20 checks; argument-generation tests pass
42 checks. Restoring strong shader ownership fails the physical-retirement
assertion in an isolated negative test. Docs and environment-switch checks
pass, and LLVM 23.1.5 reports no new lint findings. Final standards and spec
reviews found no remaining issues.

Co-authored-by: Oliver Ames <7366664+oliverames@users.noreply.github.com>
Co-authored-by: kenCalx <105067885+kencalx@users.noreply.github.com>

(cherry picked from commit 3fe574d, nxnative main; merged by hand onto HookLink X1, which lacks 44d67fb / 1073e98: encode_args_ keeps this tree's reuse test with the pass checked before the borrowed buffer, and t_arena_trim takes this tree's arena_alloc)
Melonboy22 added a commit to Melonboy22/greenhat-game that referenced this pull request Nov 17, 2026
… idle arena blocks go

Three upstream nxnative memory changes, adapted to oot-plus:

- Ghost table kept by use (034e91a). The ring of 32,768 finalized texture
  serials is replaced by a table with a least-recently-used list: past 512
  entries (DM_NVN_GHOST_N) an entry goes once idle for 120 frames
  (DM_NVN_GHOST_IDLE; upstream's 16) or 4,096 submissions, 32 a commit and
  two a put; textures the GPU wrote wait 600 frames. In a 2026-11-15 run on
  a 16 GB M5 the ring's own textures grew to 1.8 GB, 1.78 GB of them idle
  120 frames or more, until the watchdog's relief at its 13 GB cap.
  The relief now trims idle entries to 64. POOL_IDS_MAX 1024 -> 8192.
- A memory pool's Finalize ends every texture made on it (13c35e9): the
  serial entries of its live texture objects, its packaged textures and any
  live or ghost entry naming them go. Ocarina's 288 MB shadow-target pool
  stayed resident through Hyrule Field before.
- Per-frame arena blocks shared between submissions and those above four
  released once idle (3fe574d, PR seedds#2862); argument caches borrow their
  block. DM_NVN_ARENA_SHARE / DM_NVN_ARENA_TRIM turn them off.

The memory line reports the table's in-use and idle bytes, entries that went
with finalized pools, and arena blocks alive and released. Upstream's
release of drawn-over render targets (8653238) is not taken: the pool
heaps (seedds#1453) already hold Ocarina's render targets in 480 MB.

Tests: t_ghost_trim rewritten (115 + 2), t_pool_finalize new (9),
t_arena_trim from upstream (25/25/22/20), t_mem_watchdog, t_cpu_tex and
t_readback updated. Stale tests fixed for this branch's defaults (each
failed the same way before these changes):
- t_rt_recycle: the recycler is off here and pool heaps take targets
  first; its recycler runs now set DM_NVN_RT_RECYCLE=1 DM_NVN_POOL_HEAP=0.
- t_image_alias: a pool's own heap is not a family heap.
- t_pool_notes: the takeover purge (DM_NVN_GHOST_TAKEOVER) empties ID 4;
  a second run checks the purge-off behaviour.
- t_upgrade_alias: the host's rule for retired IDs, without the profile's
  pool_skip_retired (as t_pool_notes and t_pool_chunks do).
- t_perf_order: dispatches pass a game barrier by default
  (DM_NVN_CSH_BARRIER); a run with it off checks the stop.
docs/env_switches.tsv: the ghost and arena switches, and the recycler's
real default here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oliverames
oliverames deleted the nx18-arena-block-release branch November 28, 2026 01:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants