Rich text / foldkit-richtext

Performance

Transactions accumulate changes per block and copy each affected container once, so N edits in one paragraph run in O(N) instead of copying the array N times. Back-to-back benchmark means on one machine compare per-operation copying against accumulation:

WorkloadBeforeAfter
Paste 50k characters into one run0.021 ms0.023 ms
Type one character into a 20k-character run0.024 ms0.025 ms
Toggle a mark over a 400-run selection2.07 ms1.80 ms
Toggle a mark over a 2000-run selection16.99 ms9.59 ms
Toggle a mark over 200 runs / 200 blocks1.47 ms1.48 ms
Delete a range spanning 100 paragraphs3.06 ms3.27 ms
Split a block inside a 400-run paragraph1.14 ms1.35 ms
Paste one paragraph into a 200-block document1.13 ms1.22 ms

The large formatting case halves in time and scales linearly (5× the runs costs 5.3× the time instead of 8.2×). Differences under a few percent in the other rows reflect normal run-to-run noise.

apply caches validation per immutable block and builds each document's index once. Typing into a 4,000-paragraph document dropped from ~10 ms of whole-document decoding per edit to validating only the single replaced block, with identical schema enforcement.

The collaborative path types one character into the middle of an n-paragraph document (RichText.run, then Replicated.translate, applyOps, project, and resolve). Four optimizations—per-block validation, layered insert indexes in applyOps, entry reuse in project, and dirty-block shadow building in translate—cut latency across sizes (back-to-back means on one machine):

ParagraphsBeforeAfter
1001.08 ms0.17 ms
1,0008.70 ms1.91 ms
4,00049.2 ms9.61 ms

Remaining cost scales with document size because each new document builds its block, run, and node indexes, and project walks entries to reuse its run table. Structural deletes across B paragraphs batch contiguous joins into a single structural edit and single index rebuild (measured at 1.77 ms after batching vs. 3.06 ms before). Normalization still scans the dirty set including run IDs, leaving O(dirty) lookups in the merge pass.