Skip to content

refactor: separate renderer initialization from resize - #164

Merged
cowboyd merged 1 commit into
mainfrom
refactor/native-renderer-lifecycle
Dec 1, 2026
Merged

cowboyd merged 1 commit into
mainfrom
refactor/native-renderer-lifecycle

Conversation

@cowboyd

@cowboyd cowboyd commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Why?

The context of this is that I spiked out a caching techique where as part of the ElementInfo returned for each element in a RenderResul it has not only the bounding box, but also a replay operation:

interface ElementInfo {
  bounds: BoundingBox;
  replay: Op;
}

This replay is an operation that is a drop in replacement for the list of ops that rendered the element in the first place. Internally, it is just a byte offset and length to operations in the last frame, so when tty sees it, it just copies those bytes directly from the last frame into the current one.

It performed 25% better than the snapshotting mechanism, and it means that every element that gets replayed takes up only 2 bytes in the transfer buffer.

But, as a result it requires that we not destroy that information on a resize which currently re-initializes the entire native renderer.

What

Split this into clayterm_init(), which initializes the instance once, and clayterm_resize(), which rebuilds cells, the output buffer, and Clay's arena while invalidating the old layout and diff state.

Capabilities and the emitted pointer shape now stay on the native instance across resizes. Remove the JavaScript save/restore bookkeeping and the unused native pointer-shape accessors. Regression coverage checks growth, shrink, and capability changes interleaved with resizing.

Based on main after #162 merged. This is an internal refactor preparing the renderer lifecycle for replay; no public API change or separate issue.

Type of change

  • Bug fix
  • Feature
  • Refactor (no behavior change)
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • All tests pass (deno task test: 29 suites, 387 steps)
  • Files are formatted (deno task fmt and deno task fmt:check)
  • I have added/updated tests for my changes
  • I have added a changeset (not applicable: internal refactor)

Also verified the WASM build, deno lint, deno task verify, and git diff --check.

AI-generated code disclosure

  • This PR includes AI-generated code

@bombshell-cooper

Copy link
Copy Markdown
Contributor

No changeset needed

This pull request is classified as no_release_impact: no published package

@pkg-pr-new

pkg-pr-new Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

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

commit: bfac0b7

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Size Reduced — -0.3 KB

132.1 KB unpacked

@cowboyd
cowboyd added this pull request to stack #165 October 23, 2026 14:26
Base automatically changed from cleanup/renderer-directive-constants to main October 23, 2026 19:16
@cowboyd
cowboyd force-pushed the refactor/native-renderer-lifecycle branch from 7460265 to e7f5b2b Compare October 23, 2026 19:25
@cowboyd cowboyd mentioned this pull request Oct 23, 2026
6 of 12 tasks

@dreyfus92 dreyfus92 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

awesome job charles, looks good to me.

but i got one question, resize still drops the last frame and resets clay's arena, and opsBuf can move when the state size changes, so i'd guess replay entries get invalidated on resize anyway. is the plan that replay only survives across normal frames, and this pr just keeps the instance itself (caps, pointer shape) from being recreated?

@natemoo-re natemoo-re left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@cowboyd

cowboyd commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

but i got one question, resize still drops the last frame and resets clay's arena, and opsBuf can move when the state size changes, so i'd guess replay entries get invalidated on resize anyway. is the plan that replay only survives across normal frames, and this pr just keeps the instance itself (caps, pointer shape) from being recreated?

It's a great question. The answer is that further up this PR stack, the unary state is split into two frames, last_frame and current_frame, and instead of one opsBuf, each frame holds onto its own runlist that contains the Clay directives for that layout pass. The frames survive resize, and so will also their runlists, so even though the layout context is changing, the actual layout operations from the different size can still be replayed from the old runlist.

@cowboyd
cowboyd force-pushed the refactor/native-renderer-lifecycle branch from e7f5b2b to bfac0b7 Compare December 1, 2026 02:23
@cowboyd
cowboyd merged commit 05c5ba2 into main Dec 1, 2026
13 checks passed
@cowboyd
cowboyd deleted the refactor/native-renderer-lifecycle branch December 1, 2026 02:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants