Persistent data structure

On this page 7

A persistent data structure is a data structure that preserves its previous versions when it is modified: every update returns a new version, while all earlier versions remain valid and unchanged. In functional programming, persistent structures are the standard way to combine immutability with efficient updates, because they avoid copying the whole structure by sharing unchanged parts between versions.

The word persistent here has nothing to do with saving data to disk. It describes the survival of old versions in memory, a sense established in the 1986 work of Driscoll, Sarnak, Sleator and Tarjan. Languages such as Clojure, Haskell, Scala and Elm build their standard collections on this idea, and libraries bring it to JavaScript, Java and other mainstream languages.

How structural sharing makes persistence efficient

Persistence is made efficient by structural sharing: a new version reuses every node of the old version that the update did not touch. Only the nodes on the path from the root to the changed element are copied, a technique known as path copying.

Consider a balanced binary search tree holding one million keys. Inserting a key changes one leaf, so the new version copies roughly 20 nodes along the path and points to the remaining subtrees of the old tree. Both trees are complete and independent from the perspective of any reader, yet together they occupy only slightly more memory than one tree.

Because no node is ever mutated after construction, versions can be read concurrently from many threads without locks, and keeping a reference to an old root is enough to keep that whole version alive. Unreachable versions are reclaimed by the garbage collector, which is why persistent structures are most natural in managed runtimes.

Levels of persistence

Persistent data structures are classified by which versions can be read and which can be updated. The classical taxonomy distinguishes four levels, and most functional programming libraries provide full persistence as a free consequence of immutability.

The table below lists the levels and what each one permits.

LevelRead accessUpdate access
EphemeralCurrent version onlyCurrent version only
Partial persistenceAll versionsNewest version only
Full persistenceAll versionsAny version, creating a branch
Confluent persistenceAll versionsAny version, including merging two versions
Levels of persistence and permitted operations

Partial persistence is common in imperative settings such as geometric algorithms that sweep through time, where techniques like fat nodes store a history of values inside each node. Purely functional structures obtain full persistence automatically because old roots are never altered, so updating any version simply builds another branch of the version tree.

Common persistent structures and their costs

Several well-studied designs provide persistence with near-optimal performance. The simplest is the immutable singly linked list, where adding an element to the front shares the entire existing list in constant time. Richer structures rely on trees with high branching factors to keep paths short.

The following table compares widely used persistent structures by typical operation cost and characteristic use.

StructureTypical update costTypical use
Cons listO(1) at the headStacks, recursive processing
Balanced search treeO(log n)Sorted maps and sets
Hash array mapped trieO(log32 n), effectively constantHash maps and sets in Clojure and Scala
Persistent vectorO(log32 n), effectively constantIndexed sequences with fast append
Finger treeAmortized O(1) at both endsDeques, sequences with concatenation
Banker’s queueAmortized O(1)First-in, first-out queues
Typical persistent structures and their complexity

With a branching factor of 32, a persistent vector of one billion elements is at most six or seven levels deep, which is why its operations are often described as effectively constant. Okasaki’s work on purely functional data structures showed how lazy evaluation can preserve amortized bounds even when old versions are reused.

Persistent structures compared with alternatives

A persistent data structure differs both from a mutable structure and from a naive immutable copy. The comparison below shows why it occupies a useful middle ground between in-place mutation and defensive copying.

ApproachCost of one updateOld versionsThread safety
Mutable structureLowest, often O(1)LostRequires synchronization
Full copy on updateO(n) time and memoryKeptSafe for readers
Persistent structureO(log n) or effectively constantKept, sharing memorySafe for readers
Persistent structures versus mutable and copied structures

Persistent structures also relate closely to other functional concepts. They make referential transparency practical for collections, since a function that updates a map can return a new map without side effects, and they support folds and other higher-order functions that consume one version while another is being built.

Limitations and practical cautions

Persistent data structures trade some raw speed for safety and versioning. Developers adopting them should weigh the following costs and pitfalls.

  • Constant-factor overhead: tree-based lookups are typically several times slower than a mutable array because of pointer chasing and weaker cache locality.
  • Allocation pressure: every update allocates new nodes, which increases garbage collection work in update-heavy loops.
  • Memory retention: holding a reference to one old version keeps all of its nodes alive and can cause unexpected memory growth.
  • Transients as a remedy: Clojure and similar libraries offer temporary mutable builders that batch updates and then freeze into a persistent result.
  • Not the same as durability: persistence in this sense does not write anything to storage and offers no crash recovery.

Put simply

A persistent data structure works like a document with a perfect, cheap version history. Each time something changes, a new version appears, and every earlier version stays exactly as it was.

The trick is that the new version does not copy everything. It reuses all the parts that did not change and adds only the few new pieces. That is why keeping old versions costs very little, and why many parts of a program can safely read the same data at the same time without getting in each other’s way.

In practice

A front-end team builds a collaborative form editor whose application state, about 5,000 fields with validation rules, lives in a persistent map. Every user action produces a new state object, while the previous one is pushed onto an undo stack. Because consecutive versions share nearly all of their nodes, keeping 200 undo steps adds only a few megabytes of memory.

The rendering layer compares the old and new state by reference: any subtree whose reference did not change is skipped, which makes change detection nearly free. Undo and redo become simple pointer swaps, and a bug report can include the exact sequence of states. The outcome is reliable time-travel debugging and responsive editing without any custom copy logic.