project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | # Internal Diff Calculation |
| 2 | |
| 3 | Diff calculation produces the state patch between two versions. Checkout, |
| 4 | forking, 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 |
| 9 | base is a **critical version** in the Eg-walker sense (arXiv:2409.14252 §3.5): |
| 10 | a version that no concurrency crosses. When a concurrent branch invalidates |
| 11 | the candidate, the DAG retreats the base to the latest single-head critical |
| 12 | version, which can be older than the meet of the two versions. |
| 13 | |
| 14 | An 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 |
| 16 | version-vector difference and routes common history only to those calculators. |
| 17 | Otherwise each unchanged List/Text/MovableList can trigger its own full-history |
| 18 | tracker rebuild. |
| 19 | |
| 20 | The DAG walk follows explicit dependencies plus the implicit previous counter |
| 21 | of the same peer. An implicit path may be redundant when an explicit relay |
| 22 | dependency already contains that predecessor. The walk remembers the dependency |
| 23 | tip where an unmatched path split and performs a targeted ancestor lookup from |
| 24 | the 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 | |
| 31 | This lookup runs only for unmatched branch tips and prunes by visited DAG nodes |
| 32 | and Lamport time. It does not calculate a causal version for every new peer and |
| 33 | does 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 |
| 38 | may be concurrent with part of `from` (a stale unmerged head is the common |
| 39 | shape). The DAG alone can only demote such an import to `Checkout` and |
| 40 | retreat the base to a critical version, which replays the whole branch and |
| 41 | makes Text/List rebuild trackers from empty on every import. |
| 42 | |
| 43 | `OpLog::iter_from_replay_base_causally` therefore checks the concurrency at |
| 44 | container granularity before consulting the DAG: |
| 45 | |
| 46 | 1. `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. |
| 51 | 2. `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 | |
| 56 | Registers are harmless because their diffs need no positional context, but the |
| 57 | overlapping maps must still be resolved from the history cache (the calculator |
| 58 | is started in `Import` mode for them): persisted state drops the lamport |
| 59 | metadata of deleted roots and dead containers, so comparing lamports against |
| 60 | the state is not sound for concurrent ops. Containers with no concurrent old |
| 61 | ops keep the fast path; they are not marked `source_not_in_op_context`. |
| 62 | |
| 63 | On a shallow doc the history cache seeds a map's shallow-root entries only |
| 64 | when that map is first resolved (`ContainerHistoryCache::ensure_shallow_map_seeded`), |
| 65 | so the first concurrent map import costs O(that map), not O(every map in the |
| 66 | shallow root). Regression and perf test: `crates/loro/tests/shallow_lazy_map_checkout_index.rs`. |
| 67 | |
| 68 | Anything else (a text, list, movable list, tree or unknown container with ops |
| 69 | on both sides) falls back to the DAG's conservative answer exactly as before. |
| 70 | So does a shallow doc whose concurrent old history reaches below the shallow |
| 71 | root: later imports must causally follow the root |
| 72 | (`import_deps_before_shallow_root`), but the snapshot itself can retain |
| 73 | changes concurrent with the root (independent peer chains), and the trimmed |
| 74 | ops in `from` cannot be scanned for containers. The regression test is |
| 75 | `shallow_doc_accepts_cross_peer_op_whose_deps_include_boundary`. |
| 76 | |
| 77 | Map diffs in `Checkout`/`Import` mode only look up the keys written inside the |
| 78 | replayed span, and skip replayed ops that both versions already contain, so |
| 79 | their cost follows the update instead of the map size. |
| 80 | |
| 81 | ## Winner metadata, not just values |
| 82 | |
| 83 | Map and MovableList states store the winning op's lamport/peer (map entry, |
| 84 | movable-list `value_id`) next to the value. A checkout must move that metadata |
| 85 | even when both versions hold an equal value written by different ops (a value |
| 86 | rewritten after the target, e.g. by `revert_to`). |
| 87 | |
| 88 | `MapDiffCalculator` (Checkout/Import) compares the winners at `from` and `to` |
| 89 | by op id only (`MapHistoryCache::changed_winners_for_keys`): the same winner is |
| 90 | skipped 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 |
| 92 | diff is already at `from` (the isolated-import fast path diffs from the empty |
| 93 | version, 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 |
| 95 | equal-value entry is a silent metadata update and events still only report |
| 96 | value changes. `MovableListState` does the same for element values. This keeps |
| 97 | equal-value checkouts at the cost of `main` before the fix: the added state |
| 98 | write per key is paid for by the dropped `from` value lookup (loro-dev/loro#1124; |
| 99 | benchmark: `crates/loro/examples/map_equal_value_bench.rs`). |
| 100 | |
| 101 | Skipping those entries left the later op's metadata in the checked-out state. |
| 102 | Shallow and state-only exports build their root state through such a checkout, |
| 103 | so the root state carried a lamport/peer that is not in the root version. On |
| 104 | import, `ensure_shallow_map_seeded` inserted that entry into the map checkout |
| 105 | index, where it compared equal to the retained later op (the index is keyed by |
| 106 | container, key, lamport and peer) and was dropped, so `checkout(root)` lost the |
| 107 | key whenever its root-time writer was trimmed. `get_last_editor` after a |
| 108 | checkout 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 | |
| 122 | List, Text, and MovableList still rebuild their trackers from CRDT IDs when |
| 123 | retreating, when their source context is incomplete, or when shallow history |
| 124 | requires it. That fallback is a correctness requirement, not a replay-base |
| 125 | optimization. |