forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

diff_calc.md125 lines, 6.8 KiBHistoryRawDownload
1# Internal Diff Calculation
2
3Diff calculation produces the state patch between two versions. Checkout,
4forking, import, and revert all use this path.
5
6## Replay base and changed containers
7
8`OpLog::iter_from_replay_base_causally` first chooses a replay base. The safe
9base is a **critical version** in the Eg-walker sense (arXiv:2409.14252 §3.5):
10a version that no concurrency crosses. When a concurrent branch invalidates
11the candidate, the DAG retreats the base to the latest single-head critical
12version, which can be older than the meet of the two versions.
13
14An old base is not evidence that every container in the replay range changed.
15`DiffCalculator::calc_diff_internal` derives the changed container set from the
16version-vector difference and routes common history only to those calculators.
17Otherwise each unchanged List/Text/MovableList can trigger its own full-history
18tracker rebuild.
19
20The DAG walk follows explicit dependencies plus the implicit previous counter
21of the same peer. An implicit path may be redundant when an explicit relay
22dependency already contains that predecessor. The walk remembers the dependency
23tip where an unmatched path split and performs a targeted ancestor lookup from
24the candidate common frontiers:
25
26- a covered tip is another route into already-common history, so `from` remains
27 the replay base;
28- an uncovered tip is a real concurrent branch, so the conservative base is
29 retained.
30
31This lookup runs only for unmatched branch tips and prunes by visited DAG nodes
32and Lamport time. It does not calculate a causal version for every new peer and
33does not compare each one with the complete `from` version vector.
34
35## Register-only concurrency
36
37`to ⊇ from` does not imply the `ImportGreaterUpdates` contract: the new ops
38may be concurrent with part of `from` (a stale unmerged head is the common
39shape). The DAG alone can only demote such an import to `Checkout` and
40retreat the base to a critical version, which replays the whole branch and
41makes Text/List rebuild trackers from empty on every import.
42
43`OpLog::iter_from_replay_base_causally` therefore checks the concurrency at
44container granularity before consulting the DAG:
45
461. `OpLog::uncovered_entry_parents` finds the entry changes of the new region
47 whose causal parents do not cover all of `from` (the version-vector form
48 of spec lemma L12; it scans only the new changes). Old history outside
49 `⋂ Events(parents)` may be concurrent with a new op; everything else is
50 causally before the whole new region.
512. `OpLog::register_only_concurrency` scans the ops in that concurrent old
52 history and in the new region. If every container present on both sides
53 is a register (Map, Counter), the import replays from `from` in
54 `ImportGreaterUpdates` mode.
55
56Registers are harmless because their diffs need no positional context, but the
57overlapping maps must still be resolved from the history cache (the calculator
58is started in `Import` mode for them): persisted state drops the lamport
59metadata of deleted roots and dead containers, so comparing lamports against
60the state is not sound for concurrent ops. Containers with no concurrent old
61ops keep the fast path; they are not marked `source_not_in_op_context`.
62
63On a shallow doc the history cache seeds a map's shallow-root entries only
64when that map is first resolved (`ContainerHistoryCache::ensure_shallow_map_seeded`),
65so the first concurrent map import costs O(that map), not O(every map in the
66shallow root). Regression and perf test: `crates/loro/tests/shallow_lazy_map_checkout_index.rs`.
67
68Anything else (a text, list, movable list, tree or unknown container with ops
69on both sides) falls back to the DAG's conservative answer exactly as before.
70So does a shallow doc whose concurrent old history reaches below the shallow
71root: later imports must causally follow the root
72(`import_deps_before_shallow_root`), but the snapshot itself can retain
73changes concurrent with the root (independent peer chains), and the trimmed
74ops in `from` cannot be scanned for containers. The regression test is
75`shallow_doc_accepts_cross_peer_op_whose_deps_include_boundary`.
76
77Map diffs in `Checkout`/`Import` mode only look up the keys written inside the
78replayed span, and skip replayed ops that both versions already contain, so
79their cost follows the update instead of the map size.
80
81## Winner metadata, not just values
82
83Map and MovableList states store the winning op's lamport/peer (map entry,
84movable-list `value_id`) next to the value. A checkout must move that metadata
85even when both versions hold an equal value written by different ops (a value
86rewritten after the target, e.g. by `revert_to`).
87
88`MapDiffCalculator` (Checkout/Import) compares the winners at `from` and `to`
89by op id only (`MapHistoryCache::changed_winners_for_keys`): the same winner is
90skipped without reading any value, and a different winner is emitted with the
91`to` value. The `from` value is never fetched, because the state applying the
92diff is already at `from` (the isolated-import fast path diffs from the empty
93version, where every winner is new). `MapState` writes each entry with one
94`insert` and reports a change only when the previous value differs, so an
95equal-value entry is a silent metadata update and events still only report
96value changes. `MovableListState` does the same for element values. This keeps
97equal-value checkouts at the cost of `main` before the fix: the added state
98write per key is paid for by the dropped `from` value lookup (loro-dev/loro#1124;
99benchmark: `crates/loro/examples/map_equal_value_bench.rs`).
100
101Skipping those entries left the later op's metadata in the checked-out state.
102Shallow and state-only exports build their root state through such a checkout,
103so the root state carried a lamport/peer that is not in the root version. On
104import, `ensure_shallow_map_seeded` inserted that entry into the map checkout
105index, where it compared equal to the retained later op (the index is keyed by
106container, key, lamport and peer) and was dropped, so `checkout(root)` lost the
107key whenever its root-time writer was trimmed. `get_last_editor` after a
108checkout was wrong for the same reason. Regression tests:
109`crates/loro/tests/shallow_checkout_equal_value.rs`.
110
111## Diff modes
112
113- `Checkout` is the general and slowest mode. It can move in either direction
114 and may use `ContainerHistoryCache`.
115- `Import` requires `to > from`, but some imported operations may be concurrent
116 with `from`.
117- `ImportGreaterUpdates` additionally guarantees that every imported operation
118 is causally after `from`, so the replay base is `from`.
119- `Linear` additionally guarantees that imported operations are ordered, so
120 diff calculation does not need to build CRDT trackers.
121
122List, Text, and MovableList still rebuild their trackers from CRDT IDs when
123retreating, when their source context is incomplete, or when shallow history
124requires it. That fallback is a correctness requirement, not a replay-base
125optimization.