jonnify·bcs /bcs/elixir-core/the-champ-database/structural-sharing

B2.3.1 · dive 1 of 3 · module B2.3

Structural sharing.

A persistent map: a new version copies only the path to the change and shares every untouched subtree with the old. A snapshot is the old root kept, and it costs nothing.

Two roots sharing untouched subtrees; only the path to the change is copied. Select a part.
root v1kept = a snapshot root v2copied nodechanged leaf shared subtreespointed to by both
readout · Select a part. A CHAMP update returns a new version while the old root stays valid; only a short spine is copied; every untouched subtree is shared by reference.
§1

The idea

echo_data · champ_node.ex, branded_champ.ex

A CHAMP is a persistent map: updating it returns a new version while the old one stays valid and unchanged. It does this by sharing — a new version copies only the nodes on the path from the root to the change, and points at every untouched subtree the old version already held. A snapshot, then, is the old root kept; taking one costs nothing.

The node layout is what keeps the sharing tight. Each node carries two 32-bit bitmaps over one compact tuple — a datamap marking inline key-value pairs and a nodemap marking child nodes — with five-bit fragments of the hash driving 32-way branching down a handful of levels. A change rewrites a short spine of small nodes, not the map; the rest is reused by reference.

Codemojex wants this every time it shows or rewinds a game. A snapshot of a room for a spectator, a round preserved for replay, an undo of the last move — each is a cheap new root over a shared structure, not a copy of the board. The old state remains exactly itself, which is what makes versioning and broadcast affordable.

§4

The doors

the sibling courses
§2

References

sources · related

Sources

Related