Sub topic 13
Beyond path copying: the fat node method
This chapter assumes you know path copying and structural sharing from the earlier chapters, and that you are comfortable with binary search and Big-O notation.
The problem with copying paths
Path copying creates new nodes for every update. Each update copies the route from the root to the change, so it costs O(log n) space in a balanced tree. In a structure with many incoming pointers, such as a graph or a doubly linked list, it breaks down. Changing one node means copying every node that points to it, then every node that points to those. The copying cascades through the whole structure.
The fat node method avoids copying. You keep the same node and record how its fields change over time.
The idea: fields with histories
A normal node stores one value per field. A fat node stores a list of (version, value) pairs for each field. An update appends a pair to the field's list. Nothing is overwritten and no other node needs to change.
To read at version v, you find the pair with the largest version number that is at most v. The pairs are sorted, so binary search finds it.
Reading version 2 of Node A gives value a, because the entry for version 3 is too new. Its next field gives B.
A partially persistent list
The code below implements a list where every change creates a new version. You can read any old version, but you can only change the newest one.
from bisect import bisect_right
class Field:
def __init__(self, version, value):
self.versions = [version]
self.values = [value]
def set(self, version, value):
if version == self.versions[-1]:
self.values[-1] = value
else:
self.versions.append(version)
self.values.append(value)
def get(self, version):
i = bisect_right(self.versions, version) - 1
if i < 0:
raise ValueError("field did not exist at that version")
return self.values[i]
class Node:
def __init__(self, version, value):
self.value = Field(version, value)
self.next = Field(version, None)
class PartialList:
def __init__(self):
self.version = 0
self.root = Field(0, None)
def _nodes(self, version):
node = self.root.get(version)
while node is not None:
yield node
node = node.next.get(version)
def append(self, value):
self.version += 1
new = Node(self.version, value)
last = None
for last in self._nodes(self.version - 1):
pass
if last is None:
self.root.set(self.version, new)
else:
last.next.set(self.version, new)
return self.version
def set_value(self, index, value):
for i, node in enumerate(self._nodes(self.version)):
if i == index:
self.version += 1
node.value.set(self.version, value)
return self.version
raise IndexError(index)
def to_list(self, version):
return [n.value.get(version) for n in self._nodes(version)]
lst = PartialList()
lst.append("a") # version 1
lst.append("b") # version 2
lst.set_value(0, "z") # version 3
assert lst.to_list(0) == []
assert lst.to_list(1) == ["a"]
assert lst.to_list(2) == ["a", "b"]
assert lst.to_list(3) == ["z", "b"]Appending changes the next field of an existing node. A mutable list would overwrite that field in place. Here the update adds a history entry, so versions 1 and 2 still read the old value.
Costs
Each update adds O(1) space per changed field. The time cost moves to reads, because every field access needs a binary search. If a field has m entries, one access takes O(log m) instead of O(1). A traversal of k nodes slows down by roughly that factor.
| Aspect | Path copying | Fat node |
|---|---|---|
| Space per update | Copies the path to the change | O(1) per changed field |
| Read cost | Unchanged | Extra O(log m) per field |
| Works for graphs and cyclic structures | Poorly | Yes |
| Persistence model | Full, with confluent merging possible | Partial as shown |
Limits
Version numbers in the example grow in one line, so only the newest version can be modified. That is partial persistence. Full persistence lets you update any old version, and versions then form a tree. Plain integers can no longer be compared to find the "latest entry not newer than v". You need a structure that orders versions along the tree, such as an order-maintenance list.
The node copying method of Driscoll, Sarnak, Sleator and Tarjan fixes the read slowdown. It limits each node to a fixed number of history entries. When a node is full, it is copied and the copy starts with a fresh history. For structures where each node has a bounded number of incoming pointers, this gives O(1) amortized overhead per access and per update.
Exercise
In the PartialList above, why does reading version 1 after the later updates still return ["a"]?
Show answer
The append at version 2 only added an entry for version 2 to node "a"'s next field. A read at version 1 picks the latest entry with a version of 1 or less, which is the original None. The list therefore ends after "a", and the later value change is likewise ignored.
Exercise
Why does the fat node method need a different version-ordering scheme to support full persistence?
Show answer
With full persistence, versions form a tree because any old version can be updated. A field's history entries then come from different branches, and integer comparison cannot say which entry applies to a given version. You need a way to test ancestry between versions.