Apex › Storage & databases

The rope — how editors edit huge text fast

Inserting a character in the middle of a flat string is O(n) — every later byte shifts. A rope stores the document as the leaves of a balanced tree, where each internal node caches the length of its left subtree, so index, split, insert, and delete are all O(log n). Edits rewrite only one root-to-leaf path, and because nodes are immutable and shared, every past version stays alive for free undo. Edit the text and watch the tree splice.

More in Storage & databases