Skip to content

perf(sync): recompute the checksum in 1 walk of :eavt - #13469

Closed
yuxi-liu-wired wants to merge 5 commits into
logseq:masterfrom
yuxi-liu-wired:perf/checksum-one-walk
Closed

yuxi-liu-wired wants to merge 5 commits into
logseq:masterfrom
yuxi-liu-wired:perf/checksum-one-walk

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

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-checksum runs 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 :eavt datoms, 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/uuid index, the recompute ran 5 d/datoms lookups 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 (-slice and its comparators), 419 ms in hashing and 381 ms in garbage collection.

The commits:

  1. test(sync): pin recompute-checksum to a reference: a reference recompute written from the definition in the test file, compared with recompute-checksum on 37 graphs (listed under Tests), and the value on 1 fixed graph pinned to the one master gives.
  2. test(sync): give the uuid churn test a real tx id: incremental-checksum-matches-recompute-with-duplicate-block-uuid-tx-churn-test gave its datoms tx 200, below datascript's tx0 (536870912). Such a datom is missed by (d/datoms db :eavt 3) and d/entity and returned by a walk of the whole :eavt index, so the test's outcome depended on which read each checksum path used. It now uses the next tx id, and passes on master.
  3. perf(sync): read each entity's datoms once: 1 :eavt read per entity answers the eligibility check and gives the tuples. The eligibility check of update-checksum reads the same way.
  4. perf(sync): read parent and page uuids from a map, filled by 1 walk of the :block/uuid index.
  5. 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/uuid datom, 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.

  • master (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).
  • commit 3: release cold 5.07 s (5.06 - 5.20), warm 2.66 s (2.19 - 3.16); dev cold 5.77 s (5.58 - 6.49), warm 2.65 s (2.58 - 4.24).
  • commit 4: release cold 4.56 s (4.56 - 5.09), warm 2.10 s (1.88 - 2.79); dev cold 4.98 s (4.39 - 5.29), warm 3.00 s (2.12 - 3.15).
  • the walk of commit 5 without the digest folding, an intermediate build that is not a commit here: release cold 3.50 s (3.00 - 4.16), warm 1.75 s (1.34 - 2.28); dev cold 3.97 s (3.74 - 4.21), warm 1.88 s (1.49 - 2.41).
  • commit 5, the head: release cold 2.81 s (1.91 - 3.02), warm 0.25 s (0.23 - 0.37); dev cold 2.83 s (1.96 - 2.94), warm 0.50 s (0.26 - 0.67).

Worker start on the graph as imported, which recomputes the checksum at start (fix/restore-max-tx removes 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-checksum takes 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 from deps/db-sync since it reads its fixtures from there: 29 tests, 116 assertions, 0 failures on master's checksum.cljs with this branch's tests, and on commits 3, 4 and 5.
  • The reference test compares on 37 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), a graph whose datoms bypass cardinality and uniqueness (2 titles, 2 uuids, 2 parents with equal uuids, a shared uuid), the sample graph, the 2 fixture payloads before and after, and 29 generated graphs, e2ee on and off. With the tuple-set path of commit 5 turned off, it fails on the repeated-attribute graph and on 1 fixture graph, and incremental-checksum-matches-recompute-on-rebased-retract-entity-log-repro-test fails as well (3 failures).
  • The churn test with its old tx 200 fails on this branch (1 failure: full bb99d466953b7166 against incremental d74eeb69c5a85c5b). With the next tx id it passes on master and on this branch.
  • frontend.worker.db-sync-test 233 tests, 2222 assertions; frontend.worker.sync.client-op-test 6 tests, 22 assertions: 0 failures.
  • The whole unit suite at TZ=Etc/GMT+12 (2502 tests, 12497 assertions) and its time-zone-dependent namespaces at TZ=Pacific/Kiritimati (1469 tests, 9056 assertions): 0 failures, 0 errors. Its runner leaves out logseq.db-sync.*, hence the separate run above.
  • clj-kondo on the 2 changed files, run from deps/db-sync: 0 errors, 0 warnings.

Allow edits from maintainers.


Devin Review

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.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Continued in #13545 together with #13470, #13466 and #13478: they share files, so they now merge once. The commits are the same, on current master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant