Versions And Persistence
An ordinary structure is ephemeral: an update destroys the version before it. Insert a key into a mutable tree and the earlier tree exists nowhere. A persistent structure keeps it, so every update produces a new version and the old one stays queryable.
What varies is how much is allowed. Under partial persistence any version can be read but only the newest updated, so versions form a line. Under full persistence any version can be updated, so they form a tree. Under confluent persistence two versions can be merged, so they form a DAG. Copying the whole structure per update achieves all three at O(n) time and space, and every technique below exists to avoid that copy.
Path Copying
Path copying is what falls out of immutability. To change a node, copy it with the new contents, then copy each ancestor so it points at the copy. Every subtree off that path is reattached by pointer and never read.
insert k (Node l key r)
| k < key = Node (insert k l) key r -- rebuild here, share r
| k > key = Node l key (insert k r) -- rebuild here, share l
| otherwise = Node l k r
-- the caller's old root is still a complete, valid treeEvery returned Node is fresh and every subtree off the path is passed through untouched. In a balanced tree such as a red-black tree the path is O(log n) long, so an update allocates O(log n) nodes. The rope and the zipper both work this way. The constraint is structural: the rebuilt nodes must form a path, which requires one parent per node.
Fat Nodes
The alternative is to let a node hold every value it has ever had. A fat node stores, per field, the values written to it, each stamped with the version that wrote it. An update appends one entry, which is O(1) space whatever the shape of the structure.
void *read_field(FatNode *n, int f, version_t v) {
Slot *s = &n->field[f]; // entries sorted by version
int lo = 0, hi = s->count - 1, best = -1;
while (lo <= hi) { // latest entry at or before v
int mid = (lo + hi) / 2;
if (s->at[mid].version <= v) { best = mid; lo = mid + 1; }
else { hi = mid - 1; }
}
return best < 0 ? NULL : s->at[best].value;
}That binary search runs on every pointer the traversal follows, so reads carry an O(log m) factor in the number of versions. The scheme also needs versions totally ordered, which under full persistence means maintaining an order over a tree of them.
Node Copying
Node copying, from Driscoll, Sarnak, Sleator, and Tarjan in 1989, removes that read penalty. Each node carries its fields plus a fixed number of spare modification slots; a write takes a slot in O(1), and readers of older versions still see the original fields.
Node *write(Node *n, int f, void *val, version_t v) {
if (n->used < MAX_SLOTS) { // O(1): take a spare slot
n->slot[n->used++] = (Mod){ f, v, val };
return n;
}
Node *c = materialise(n, v); // values current at v
c->field[f] = val; // and empty slots again
for (Node *p = parents(n); p; p = p->next)
write(p, slot_of(n, p), c, v); // repoint; may cascade
return c;
}Each copy restores empty capacity, so the cascade terminates and the amortized cost is O(1) space per update with O(1) worst-case slowdown — for any structure whose nodes have bounded in-degree.
Degrees Of Persistence
Full persistence costs more than partial because the versions form a tree rather than a line. Node copying extends to it by maintaining a linear order over that tree, using a structure that answers whether one version precedes another in constant time and admits new versions between existing ones. With that order, the same slot-and-copy accounting applies and the bounds match: O(1) amortized space, O(1) slowdown.
Purely functional path copying gives full persistence with no version stamps at all, because nothing is mutated and every root is as updatable as any other. It pays O(log n) space per update rather than O(1) amortized, which is the price of having no bookkeeping to maintain.
Confluent Persistence
Confluent persistence adds an operation combining two versions into one, such as concatenating two versions of a sequence. The history becomes a DAG, and a node can then be reachable along exponentially many paths from a version's root.
Concatenating a version with itself doubles its logical length, so after k such operations the structure describes a sequence of length 2^k having performed only k updates. Any scheme naming positions by path from the root inherits that blowup. Fiat and Kaplan gave the general treatment, with costs depending on the depth of the version DAG rather than the update count. This is the setting the finger tree operates in whenever two versions are joined.
Amortization Under Persistence
Persistence breaks amortized analysis as usually practised. A queue built from two lists is the standard example: pops come off the front list, and when it empties the back list is reversed into it.
pop (Queue (x:front) back) = (x, Queue front back) -- O(1)
pop (Queue [] back) = pop (Queue (reverse back) []) -- O(n)
let q = Queue [] [1..n] -- one reversal away
in (pop q, pop q, pop q) -- three versions, three reversalsUsed ephemerally the reversal is O(1) amortized, because the pushes that filled back paid for it. Used persistently, q is popped three times from the same saved state, so the credit accumulated once is spent three times. This applies to any bound proved by the banker's or physicist's method.
Laziness As The Repair
Okasaki's answer is lazy evaluation with memoization. Suspend the expensive operation rather than running it, and let the first forcing pay the cost and cache the result.
data Queue a = Queue [a] [a] -- the front list is lazy
pop (Queue front back) = case force front of
(x:rest) -> (x, Queue rest back)
[] -> pop (Queue (reverse back) [])
-- force runs the reversal once; every version sharing
-- this suspension reads the memoised resultThat restores the accounting: the banker's method is restated to assign debits to suspensions, discharged by the operations performed before one is forced. Converting these amortized bounds to worst-case ones needs scheduling — forcing a fixed amount per operation so none inherits a large debt.
Copy-On-Write Storage
Storage engines reach path copying from another direction and call it copy-on-write. A B-tree is updated by writing a modified copy of the target page to free space, then copies of its ancestors, up to a root pointer switched in one atomic write. Before the switch the on-disk structure is the old version entirely; after it, the new one.
Rodeh's treatment of shadowing sets out the discipline, including the requirement that nodes be reachable by one path only, enforced by splitting and merging on the way down. ZFS and Btrfs build on that model, so a snapshot is a retained root. The cost is write amplification: updating one record rewrites every page on its path.
Reclaiming Old Versions
Keeping versions means keeping their nodes. A node is unreachable only when no retained root reaches it, which is a property of the whole version set, so reclamation needs reference counting or a tracing collector. Dropping a version frees only the nodes unique to it.
Memory therefore follows retained history rather than live data, and space grows even when the newest version is small. Systems that expose snapshots expose their lifetime too: a filesystem snapshot pins the blocks it references, and a long-lived LMDB read transaction pins the pages of its root. Reference counting is the usual choice because path copying never creates a cycle.
Engineering Trade-offs
Persistence gives a snapshot for the cost of an update, readers that never coordinate with writers because nothing is mutated in place, undo as a retained pointer, and versions that can cross threads without synchronisation. For a structure of O(log n) depth, path copying delivers all of it for O(log n) allocation per update.
The costs are allocation on every write, memory held by retained versions, and worse locality than a mutable array. The binding constraint is that persistence is a property of the whole structure: it rules out parent pointers, rules out sharing a node between two positions in one version, and invalidates amortized bounds proved for ephemeral use.
Applications
Clojure's core collections are persistent by default, so an update returns a new collection sharing structure with the old; its maps and sets are hash-array mapped tries paying the path-copying cost on every assoc. Git's object model is the same technique over a directory tree: a commit that changes one file writes a new blob plus new tree objects along the path to it, sharing every unchanged subtree by hash, so checking out an old commit is a read rather than a restore.
On the storage side, ZFS and Btrfs implement copy-on-write B-trees so a snapshot is a retained root, and LMDB uses one to give lock-free readers a stable view without a write-ahead log. CouchDB takes the append-only form, writing updates to the end of the file so old roots stay readable in place.
Summary
A persistent structure keeps old versions queryable after an update. Path copying rebuilds the path to the change and shares the rest, at O(log n) space per update. Fat nodes record every write with a version stamp for O(1) space and an O(log m) read slowdown. Node copying combines both for O(1) amortized space and O(1) slowdown on any bounded in-degree structure.
Full persistence requires ordering a tree of versions, and confluent persistence admits merges that make a version's logical size exponential in the update count. Amortized bounds proved for ephemeral use do not survive, and laziness with memoization is what restores them. An old version costs whatever later updates copied away from it, and nothing more.