Sub topic 2
What persistence means: versions and the four models
This chapter assumes you can read Python, know linked lists and binary search trees, understand Big-O notation, and know the difference between an object reference and a copy. It introduces the vocabulary used by the rest of the tutorial. Later chapters cover how the sharing works.
Persistence means old versions stay usable
A persistent data structure keeps every earlier version of itself available after an update. An update does not overwrite anything. It produces a new version, and the old one still answers queries.
This is a different meaning from "persistent" in databases, where it means data survives a restart. Here, persistence is about versions in memory.
An ordinary mutable structure is called ephemeral. Each update destroys the previous state. A Python list is ephemeral.
xs = [1, 2, 3]
ys = xs
ys.append(4)
print(xs) # [1, 2, 3, 4]: the old state is goneBecause xs and ys are the same object, there is no way to ask what xs looked like before the append.
Versions form a history
Think of each version as a node in a graph. An update takes one version and produces another. The shape of that graph defines the four models.
The chain v0 to v3 is a linear history. The branch from v1 to v4 needs updates on an old version. The merge into v5 combines two versions.
The four models
| Model | Can query old versions | Can update old versions | Can combine versions |
|---|---|---|---|
| Ephemeral | No | No | No |
| Partially persistent | Yes | No, only the newest | No |
| Fully persistent | Yes | Yes, any version | No |
| Confluently persistent | Yes | Yes | Yes |
- Ephemeral: one live version. Updates destroy the past.
- Partial persistence: you can read any past version, but you can only update the latest. The history is a single line, like the chain above.
- Full persistence: you can update any version, so the history becomes a tree. Branching is allowed.
- Confluent persistence: you can also merge two versions into a new one, so the history is a directed acyclic graph. A typical operation is concatenating two lists that may share structure.
Each model includes the abilities of the one before it. Stronger models are harder to build efficiently. Confluent persistence is the hardest, because merging can make a structure share parts with itself and its size can grow far faster than the work done.
Persistence from immutability
The simplest way to get full persistence is to make nodes immutable and never change them. An "update" builds a new node that points to old ones.
from typing import NamedTuple, Optional
class Node(NamedTuple):
head: int
tail: Optional["Node"]
v0 = Node(2, Node(3, None))
v1 = Node(1, v0) # branch A from v0
v2 = Node(0, v0) # branch B from v0, an update to an old version
assert v1.tail is v0
assert v2.tail is v0
print(v0.head) # 2: v0 is untouchedv2 was built from v0 after v1 existed. That is an update to a non-latest version, so this list is fully persistent. Immutability gives you this for free, because no update can interfere with another.
Common mistakes
- Treating "immutable" and "persistent" as the same. An immutable object cannot change. A persistent structure also provides cheap update operations that return new versions. A frozen tuple copied in full on every change is immutable but not efficiently persistent.
- Assuming persistence means a stored history. The structure does not keep a list of versions. A version stays alive only while something refers to it.
- Mixing up the database meaning with the data-structure meaning.
Exercise
A text editor keeps an undo history. The user undoes three steps, then types new text. Which is the minimum persistence model needed to represent the history as a tree of edits, and why?
Show answer
Full persistence. The new text is an update to an older version, not the latest, so updates must be allowed on any version. Partial persistence only allows updating the newest version. Confluent persistence is not needed because no two versions are merged.