Commit 9b6330d
perf: speed up B4 local editing ~2.4x and snapshot import ~45% (#1033)
* perf: speed up B4 local editing (~42%) and snapshot import (~45%)
Local text editing (applying the automerge-paper trace): ~112ms -> ~65ms.
- Compile the lock-order debug instrumentation out of release builds; it ran on
every per-op OpLog+DocState lock acquire/release (~30% of edit time). In
release `can_lock_in_this_thread` returns false, backed by the now-exact
cached visible op count.
- Bump `visible_op_count` incrementally for local ops instead of recomputing it
from the version vectors (which also heap-allocated an im::HashMap iterator)
on every op.
- Build the position-context error string in `checked_range_end` lazily (no
per-op alloc) and return entity ranges in a SmallVec (no per-delete Vec alloc).
- Route the per-insert event-index computation through the existing cursor cache
instead of a fresh `visit_previous_caches` walk every op.
Snapshot import (fast snapshot): B4 ~135us -> ~80us; B4x100 (22MB) ~8.15ms -> ~4.5ms.
- Skip the redundant per-block SSTable checksum on full import; the whole body
is already covered by the document checksum verified in parse_header_and_body.
Adds crates/examples/examples/b4_bench.rs (phase-timed B4 harness) plus
regression tests for the cached visible op count and the block-checksum skip.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* perf: avoid per-op heap alloc in DocState::is_deleted
`is_deleted` allocated a fresh `visited` Vec on every local op (the #1
allocation source after the earlier fixes: ~260k allocs on the B4 trace).
Parent chains are shallow (depth 1 for a root container), so use inline
SmallVec storage. apply 1x: ~65ms -> ~61ms.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* chore: vendor generic-btree into the workspace
Fork crates.io generic-btree 0.10.7 (which loro-dev maintains) into
crates/generic-btree and redirect all dependents via [patch.crates-io], so the
b-tree can evolve in-tree (e.g. deferred cache propagation). This is a verbatim
vendoring of 0.10.7 (build is transparent: B4 apply unchanged at ~62ms); only
the manifest is trimmed (benches dropped, dev-deps reduced to what the in-src
tests need).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* perf: fast path for plain-text local edits (B4 apply ~61ms -> ~46ms)
Add a specialized insert/delete path for style-free text on the attached,
non-wasm, unicode-index path (the common Rust text-editing case). When the
richtext has no style anchors, entity_index == event_index == unicode pos, so
the entire read phase -- cursor location, two `visit_previous_caches` coordinate
walks, and the styles lookup -- is unnecessary; `apply_local_op` then locates the
cursor exactly once. The delete path likewise skips the two `index_to_event_index`
walks. Falls back to the general path when styles are present, on wasm, or for
non-unicode position types, so results are unchanged (snapshot bytes identical;
loro, loro-internal lib, and mergeable tests all pass).
Also gate `apply_local_op`'s txn/doc context check (a per-op `Weak::upgrade`) to
debug builds, since the handler always passes its own doc.
Cumulative B4 apply: 112ms -> ~46ms (~2.4x), ~11.5 M op/s.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix: keep integrity checks on public APIs (review fixes)
Address two correctness regressions introduced by the B4 perf work:
1. The txn/doc context check in `Transaction::apply_local_op` was gated to
debug builds. `insert_with_txn`/`delete_with_txn` are public API, so a
caller can feed one document's transaction to another document's handler;
in release that silently stamped the target doc's state/oplog with the
wrong peer+counter instead of returning `UnmatchedContext`. Restore the
check for all builds using a cheap `Weak`-pointer comparison (no atomic
upgrade on the hot path; upgrade only to fill in the error on mismatch).
2. `MemKvStore::import_all` (re-exported publicly via loro-crdt) dropped
per-block checksums for all callers. Split the API: public `import_all`
(and `SsTable::import_all`) always verifies block checksums; a new
`import_all_unchecked` opts into the fast path and is used only by Loro's
snapshot decode (`ChangeStore::import_all`, `KvWrapper::import`), where the
document-level checksum from `parse_header_and_body` already guarantees
integrity over the whole body.
Adds regression tests: `cross_doc_txn_is_rejected` and the updated
`sstable_import_block_checksum_only_skipped_when_unchecked`.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* chore: make fuzz sanitizer platform-aware
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 3986805 commit 9b6330d
30 files changed
Lines changed: 8491 additions & 106 deletions
File tree
- .changeset
- crates
- examples/examples
- fuzz/fuzz
- generic-btree
- src
- generic_impl
- kv-store/src
- loro-internal/src
- container/richtext
- oplog
- state
- utils
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
19 | 25 | | |
20 | 26 | | |
21 | 27 | | |
| |||
31 | 37 | | |
32 | 38 | | |
33 | 39 | | |
34 | | - | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
0 commit comments