Skip to content

Add snapshot() for pre-packing directive subtrees#17

Open
cowboyd wants to merge 1 commit intomainfrom
feat/snapshot
Open

Add snapshot() for pre-packing directive subtrees#17
cowboyd wants to merge 1 commit intomainfrom
feat/snapshot

Conversation

@cowboyd
Copy link
Copy Markdown
Member

@cowboyd cowboyd commented Apr 17, 2026

Motivation

Higher-level frameworks (like crankterm) need a way to skip re-packing unchanged component subtrees each frame. Currently, every render() call re-encodes the entire Op[] array into the transfer encoding, even for components whose inputs haven't changed. This adds unnecessary overhead proportional to tree size rather than change size.

Approach

Adds a new snapshot(ops: Op[]): Op constructor that pre-packs a directive array into its binary transfer encoding upfront. The returned value is an opaque Op that can appear anywhere in a directive array. During pack(), a snapshot's bytes are copied directly into the command buffer without re-encoding individual ops.

  • Spec: Added Section 8.3.4 defining snapshot() and its equivalence guarantee in Section 9.1
  • ops.ts: New Snapshot op type (not exported — opaque by design), packSize() for exact buffer allocation, snapshot() constructor, and pack() handler that memcpies pre-packed bytes
  • Tests: Verifies snapshot produces identical output to direct ops, and that a snapshot renders correctly as a child inside another element

Alternate Designs

Considered adding a RenderInfo map to RenderResult with per-element dimensions and packed byte slices. The snapshot-as-op approach is simpler and more composable — it lets frameworks own their dirty tracking without coupling to the renderer's return type.

Possible Drawbacks or Risks

A snapshot captures the transfer encoding at creation time. If the transfer encoding changes in a future version, stale snapshots would produce incorrect results. This is acceptable since snapshots are intended to be short-lived (cached between frames, not persisted).

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 17, 2026

Open in StackBlitz

npm i https://pkg.pr.new/clayterm@17

commit: c659dbe

@cowboyd cowboyd force-pushed the feat/snapshot branch 2 times, most recently from df56324 to 8b51539 Compare April 18, 2026 00:27
Introduces a new `snapshot(ops)` constructor that pre-packs a directive
array into its transfer encoding. The returned opaque `Op` can be
spliced into any directive array, and during packing its bytes are
copied directly into the command buffer without re-encoding.

This enables higher-level frameworks to implement dirty tracking:
unchanged component subtrees can reuse a cached snapshot, skipping
the per-frame packing cost entirely.
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.

1 participant