Add snapshot() for pre-packing directive subtrees#17
Open
Conversation
commit: |
df56324 to
8b51539
Compare
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 entireOp[]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[]): Opconstructor that pre-packs a directive array into its binary transfer encoding upfront. The returned value is an opaqueOpthat can appear anywhere in a directive array. Duringpack(), a snapshot's bytes are copied directly into the command buffer without re-encoding individual ops.snapshot()and its equivalence guarantee in Section 9.1ops.ts: NewSnapshotop type (not exported — opaque by design),packSize()for exact buffer allocation,snapshot()constructor, andpack()handler that memcpies pre-packed bytesAlternate Designs
Considered adding a
RenderInfomap toRenderResultwith 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).