Repository navigation
Conversation
Replace full-document undo snapshots with reversible history entries and stop cloning the document on every commit. Retained history now tracks changed layers only, with 500-entry and 32 MiB budgets. - add documentHistory with entry model, apply/trim helpers - rewrite documentStore actions around entries; gate recorder clone - compare operation references in CanvasStage append detection - cache raster asset ids per operations array - add history, perf, and raster asset tests
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.
What this does
Every time you committed a stroke, the store was calling structuredClone on the entire drawing document and pushing a full copy onto the undo stack — capped at 500 full-document snapshots. On a document with 200 long strokes the undo stack was holding roughly 100× the raw stroke payload.
This PR replaces that with structural sharing: each history entry records only what changed (the new operation, the layer settings that were updated, the layer that was cleared, etc.), and untouched layers are reused by reference. The document object is still recreated on every commit — autosave and React reactivity are unchanged — but sibling layers, their operation arrays, and every individual operation object are shared across the version chain.
The numbers
Benchmark: 200 strokes × 500 points each, run in Vitest on the updated implementation.
What changed
New file — apps/web/src/state/documentHistory.ts
Pure helper module with no dependency on the store. Defines the HistoryEntry union (one variant per action kind), byte-weight estimators for budget tracking, �pplyHistoryEntry for applying or reversing any entry, and rimPast / rimFuture which enforce the 500-entry and 32 MiB budgets.
Rewritten — apps/web/src/state/documentStore.ts
past and future now hold HistoryEntry instead of DrawingDocument. A new commitEntry helper applies an entry forward and pushes it; undo/
edo apply it in reverse/forward and move it between stacks. structuredClone now appears in exactly two places:
eplaceDocument (an ownership boundary — the incoming document may be shared with import code) and the onCommand snapshot, which is now gated so normal drawing pays zero clone cost.
apps/web/src/components/CanvasStage.tsx
The append-detection logic used to build an operationIds: string[] array per layer per render and compare IDs. With immutable, structurally-shared operations, object identity is stable and meaningful, so we now compare references directly (operations[index] === operation). No allocation, and the comment that explained why IDs were needed has been updated to explain why they no longer are.
apps/web/src/services/rasterAssets.ts
esolveRasterImages was flat-mapping all operations in every layer on every document.layers change — including pure-vector commits that touch no raster data. Added a WeakMap<DrawingOperation[], string[]> cache keyed by the operations array. A commit changes only the active layer's array, so three of the four layers get cache hits and only the changed layer is scanned.
Tests
RecordingValidation.test.ts assertions stay green without weakening
What this doesn't change