Skip to content

ref(opt): scale Clay's arena with the terminal grid - #120

Merged
natemoo-re merged 2 commits into
mainfrom
ref/grid-scaled-arena
Sep 1, 2026
Merged

ref(opt): scale Clay's arena with the terminal grid#120
natemoo-re merged 2 commits into
mainfrom
ref/grid-scaled-arena

Conversation

@natemoo-re

@natemoo-re natemoo-re commented Sep 1, 2026

Copy link
Copy Markdown
Member

Clay reserves a fixed arena sized for 8192 elements regardless of terminal size, so an 80x24 pane holds 4.85 MB before drawing anything.

In this PR, we adjust the arena size to match an element cap derived from the grid size: 2x cells, clamped to [2048, 8192]. Every leaf takes at least one cell and internal nodes with 2+ children at most double the leaf count, so 2x cells bounds realistic trees with headroom for floating and wrapper chains. The ceiling preserves today's capacity on large grids; the floor protects tiny panes. 80x24 drops to 2.39 MB (-51%); 200x50 is unchanged; render benches are flat.

The cap is set at the top of both clayterm_size() and init() so the size the host queries always matches what init consumes. That's what lets this compose with #113 — a resize re-runs both and grows the arena correctly in either direction.

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

Size Increased — +0.1 KB

100.2 KB unpacked

@pkg-pr-new

pkg-pr-new Bot commented Sep 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@bomb.sh/tty@120

commit: 07b986b

@natemoo-re
natemoo-re marked this pull request as ready for review September 1, 2026 19:11
@natemoo-re
natemoo-re requested a review from cowboyd September 1, 2026 19:11

@cowboyd cowboyd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍🏻

Clay preallocates its arena from a fixed default of 8192 max elements,
so an 80x24 terminal reserved 4.85 MB of linear memory before rendering
anything. Element capacity drives nearly all of Clay_MinMemorySize
(~600 B/element on wasm32); the measure-text word cache is second-order.

Derive the cap from the grid instead: 2x cells, clamped to [2048, 8192].
Leaf elements occupy at least one cell and internal nodes with branching
>= 2 can at most double the leaf count, so 2x cells bounds realistic
trees while leaving headroom for floating elements and wrapper chains.
The ceiling keeps large grids at exactly today's capacity; the floor
protects tiny panes.

Arena for 80x24 drops 4.85 MB -> 2.39 MB (-51%); 20x5 bottoms out at
1.23 MB; 200x50 is unchanged. Render benches are flat.

The capacity setters run at the top of both clayterm_size() and init()
so the size the host queries always matches what init consumes, for the
first init and for in-place re-init alike: Clay_MinMemorySize prefers
the live context's caps and Clay_Initialize inherits them from the old
context, so the update() resize flow (#113) sizes correctly in both
directions. Verified against a local merge of nm/feat/resize: full
suite passes, and renders stay error-free across 80x24 -> 200x50 ->
20x5 -> 132x43 in-place resizes.

Exceeding the cap degrades the same way it always has, just at the new
boundary: Clay stops opening elements and the frame surfaces an error
through RenderResult.errors.
@natemoo-re
natemoo-re force-pushed the ref/grid-scaled-arena branch from 998cef7 to 3205fbd Compare September 1, 2026 20:47
@natemoo-re
natemoo-re merged commit b371922 into main Sep 1, 2026
12 checks passed
@natemoo-re
natemoo-re deleted the ref/grid-scaled-arena branch September 1, 2026 20:59
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