Repository navigation
perf(sync): recompute the checksum in 1 walk of :eavt - #13469
Closed
yuxi-liu-wired wants to merge 5 commits into
Closed
yuxi-liu-wired wants to merge 5 commits into
yuxi-liu-wired wants to merge 5 commits into
Conversation
recompute-checksum-matches-reference-test compares recompute-checksum with a reference written from the definition, apart from the production code paths: for each :block/uuid datom whose entity is eligible, the entity's [uuid attr value] tuples, parent and page as their uuids, each digested as FNV-1a and djb2 and summed mod 2^32. The graphs: edge cases (tag-only pages, built-in true and false, a parent or page without a uuid, an entity with a uuid and nothing else), datoms that bypass cardinality and uniqueness (2 titles, 2 uuids, 2 parents with equal uuids, a shared uuid), the sample graph, 2 fixture payloads, and 29 generated graphs, e2ee on and off. The test also pins the value on 1 fixed graph.
The test's datoms carried tx 200, below datascript's tx0 (536870912). Such a datom is missed by (d/datoms db :eavt 3), by (d/datoms db :eavt 3 :block/uuid) and by d/entity, and returned by a walk of the whole :eavt index or of (d/datoms db :avet :block/uuid). The test passed only while the recompute and the incremental update read the uuid the same way. Use the next tx id.
A full recompute checked eligibility with 5 d/datoms lookups per entity, then read the entity's uuid and its datoms with 2 more. Read the entity's :eavt datoms once and answer the eligibility check and the checksum tuples from them. Parent and page uuids are still looked up per tuple.
A full recompute looked up the uuid of every block's parent and page, 2 index seeks per block. Fill an eid to uuid map from 1 walk of the :block/uuid index first and read them from it.
The recompute walked the :block/uuid index and sought each entity's :eavt datoms, then built a tuple set per entity and a [fnv djb] vector per tuple and per digest step. An entity's datoms are adjacent in :eavt, so walk the whole index once, read each entity's eligibility and checksum datoms as the walk passes them, and fold each tuple's digest into 2 running sums, with the entity uuid prefix folded once per entity. An entity still counts once per :block/uuid datom, and an entity with a repeated attribute still goes through the tuple set, which dedupes.
Contributor
Author
This was referenced Sep 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes
recompute-checksum(deps/db-sync/src/logseq/db_sync/checksum.cljs) computes the whole-graph sync checksum from scratch. The worker runs it when it opens a graph whose stored checksum does not cover the graph's:max-tx(#13415), before a snapshot upload, and for the checksum diagnostics;update-checksumruns it when the e2ee flag changes or the stored checksum is not valid. On a graph of 157,614 checksum-eligible entities (1,326,567:eavtdatoms, imported from a file graph), the release build took 3.39 s with the graph in memory and 5.83 s with its index nodes still in sqlite. This branch takes that to 0.25 s and 2.81 s, and the recompute returns the same value.On master, for each entity in the
:block/uuidindex, the recompute ran 5d/datomslookups for the eligibility check, 2 more for the entity's uuid and datoms, and 1 per parent and page tuple for their uuids. It then built a set of[uuid attr value]tuples and a[fnv djb]vector per tuple and per digest step. A CPU profile of the in-memory recompute on master (release build, 1 run, 1 ms sampling, 3,509 ms window) has 1,633 ms in index seeks (-sliceand its comparators), 419 ms in hashing and 381 ms in garbage collection.The commits:
test(sync): pin recompute-checksum to a reference: a reference recompute written from the definition in the test file, compared withrecompute-checksumon 37 graphs (listed under Tests), and the value on 1 fixed graph pinned to the one master gives.test(sync): give the uuid churn test a real tx id:incremental-checksum-matches-recompute-with-duplicate-block-uuid-tx-churn-testgave its datoms tx 200, below datascript's tx0 (536870912). Such a datom is missed by(d/datoms db :eavt 3)andd/entityand returned by a walk of the whole:eavtindex, so the test's outcome depended on which read each checksum path used. It now uses the next tx id, and passes on master.perf(sync): read each entity's datoms once: 1:eavtread per entity answers the eligibility check and gives the tuples. The eligibility check ofupdate-checksumreads the same way.perf(sync): read parent and page uuids from a map, filled by 1 walk of the:block/uuidindex.perf(sync): walk :eavt once for the full recompute: an entity's datoms are adjacent in:eavt, so 1 walk of the whole index reads each entity's eligibility and checksum datoms as it passes them, and folds each tuple's digest into 2 running sums, with the entity's uuid prefix folded once per entity. An entity still counts once per:block/uuiddatom, as when the recompute walked that index. An entity with a repeated attribute goes through the tuple set as before, since 2 of its datoms can give the same tuple (2 parents with equal uuids).Recompute time on that graph, standalone db worker, median (range) of 3 runs per build. Cold is the first recompute after the start, with every index node loaded from sqlite. Warm is the later recomputes of the same runs that loaded no index node, 4 to 6 values per build. Each build is the source tree of the commit named.
16c4ed1a0): release cold 5.83 s (5.42 - 6.59), warm 3.39 s (2.45 - 3.48); dev cold 6.70 s (6.56 - 7.66), warm 4.16 s (4.15 - 4.23).Worker start on the graph as imported, which recomputes the checksum at start (
fix/restore-max-txremoves that recompute), from the worker's first log line to its ready line, 3 runs per build: master release 5.75 s (5.61 - 7.22) and dev 7.90 s (7.00 - 8.24); this branch release 2.71 s (2.41 - 2.98) and dev 3.10 s (2.02 - 3.27). The cold recompute still loads the graph's 70,194 index nodes from sqlite, which this branch does not change.Per transaction,
update-checksumtakes about the same time as on master (release build, median (range) of 3 runs): 0.12 ms (0.12 - 0.13) against 0.14 ms (0.11 - 0.14) on master for a 1-block save, 11.0 ms (10.9 - 11.2) against 10.9 ms (10.5 - 11.0) for a 200-block insert.The value, on the same graph: every recompute of every build above gave
9b54a1fd6dc28b43, the value master gives. On the head's build that is 24 recomputes in 12 runs, dev and release. After 6 edits on that graph (save, insert, move and delete of 1 block, insert and delete of 200 blocks), the checksum the listener kept incrementally equals a full recompute in all 6 runs on the head's build.Tests
logseq.db-sync.checksum-test, run fromdeps/db-syncsince it reads its fixtures from there: 29 tests, 116 assertions, 0 failures on master'schecksum.cljswith this branch's tests, and on commits 3, 4 and 5.incremental-checksum-matches-recompute-on-rebased-retract-entity-log-repro-testfails as well (3 failures).bb99d466953b7166against incrementald74eeb69c5a85c5b). With the next tx id it passes on master and on this branch.frontend.worker.db-sync-test233 tests, 2222 assertions;frontend.worker.sync.client-op-test6 tests, 22 assertions: 0 failures.TZ=Etc/GMT+12(2502 tests, 12497 assertions) and its time-zone-dependent namespaces atTZ=Pacific/Kiritimati(1469 tests, 9056 assertions): 0 failures, 0 errors. Its runner leaves outlogseq.db-sync.*, hence the separate run above.deps/db-sync: 0 errors, 0 warnings.Allow edits from maintainers.