perf(editor-ops): copy-on-write structural sharing for editing - #7
Open
nathan-gage wants to merge 1 commit into
Open
perf(editor-ops): copy-on-write structural sharing for editing#7nathan-gage wants to merge 1 commit into
nathan-gage wants to merge 1 commit into
Conversation
Single-paragraph and single-cell editor operations deep-cloned the entire DocModel on every edit via cloneDocModel, making per-keystroke cost O(document size). Add copy-on-write helpers (cloneDocModelWithNode / cloneDocModelNodes) in doc-model and route the single-node and splice ops through them, so an edit deep-clones only the touched node and shares metadata + sibling nodes by reference. The edited node is produced by the same cloneDocNode, so content is byte-identical; document-wide replaceText keeps the full clone. Per-keystroke cost is now flat vs document size (~50-700x faster at 5000 paragraphs on an M4 Pro). Untouched nodes keep object identity, enabling downstream identity-based memoization. Adds an editing benchmark and structural-sharing identity tests, and relocates the perf tests to tests/benchmarks/. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
@nathan-gage is attempting to deploy a commit to the Extend Team on Vercel. A member of the Team first needs to authorize it. |
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.
Summary
Editing a loaded document deep-cloned the entire
DocModelon every keystroke. Each single-nodeeditor-opstransform (typing, run-style toggles, paragraph insert/remove/duplicate, paste, single-cell table edits) opened withcloneDocModel(model), which walks every paragraph/run/table plus all metadata (paragraph styles, numbering, header/footer/footnote trees). So per-edit cost scaled with total document size, not the edited region.This PR makes those ops copy-on-write: a single-node edit deep-clones only the touched node and shares
metadataand every untouched sibling node by reference.Approach
@extend-ai/react-docx-doc-model:cloneDocModelWithNode(model, index)— shares metadata + siblings by reference; deep-clones onlynodes[index]via the same authoritativecloneDocNode.cloneDocModelNodes(model)— shallow nodes-array copy for splice ops (insert/remove/paste).editor-opssingle-node and splice ops routed through them. The edited node is produced by the samecloneDocNode, so node content is byte-identical to before — only sibling/metadata sharing changes.cloneDocModelis unchanged and still used byreplaceText(which genuinely touches every node).Why it's safe
editor-opsidentity tests assert: untouched siblings +metadatakeep their identity across an edit, and the input model is never mutated.Benchmark
Per-keystroke editing ops on a realistic document (multi-run paragraphs + 30 paragraph styles + numbering definitions + header/footer sections). Median ms over 25 iterations, from
tests/benchmarks/editing-performance.test.ts.Hardware: Apple M4 Pro (14 cores), 48 GB RAM, macOS 26.5, Node 24.12.0.
Per-edit cost is now flat vs document size (was linear). The "after" figures sit near the measurement floor, so the exact multipliers are approximate — the point is the asymptotic change from O(document) to O(edited node). Numbers are single-machine medians and will vary by hardware.
Testing
pnpm test:unit: 231 passed / 18 failed. The 18 failures are pre-existing onmain(tests importing symbols not yet present in the source, e.g.resolveUiActiveTextRange,isLikelyFullPageCoverFloatingImage) and are unrelated to this change — 0 new failures.pnpm typecheck: clean across all packages + the playground.Notes
tests/benchmarks/(the existingperformance.test.tsmoved alongside the new editing benchmark) and added the directory to the vitest config.@extend-ai/react-docx).🤖 Generated with Claude Code