Skip to content

perf(sync): keep checksum only on remote graphs - #13476

Merged
tiensonqin merged 1 commit into
logseq:masterfrom
yuxi-liu-wired:perf/checksum-local-graphs
Sep 30, 2026
Merged

tiensonqin merged 1 commit into
logseq:masterfrom
yuxi-liu-wired:perf/checksum-local-graphs

Conversation

@yuxi-liu-wired

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

Copy link
Copy Markdown
Contributor

What changes

After every transaction, the db listener updates the sync checksum and stores it, with the tx it covers, in the graph's client-ops database (update-local-sync-checksum! in src/main/frontend/worker/sync.cljs, 2 synced sqlite commits through client-op/update-local-checksum). It does this on every graph, also on one that never syncs. On such a graph nothing uses the value:

  • Only sync compares it: verify-sync-checksum! in sync/handle_message.cljs (dev and test builds, on a sync connection). The rest of its uses keep it current: the next incremental update, and the check at open (reconcile-local-checksum!), which recomputes it when a write was lost.
  • A graph's first upload does not read it: upload-graph! in sync/upload.cljs recomputes the checksum in full, stores that, and sends it with the last snapshot batch.
  • The other readers display it: the db-sync-status thread API (the CLI's sync status) and the developer checksum diagnostics, which return a recomputed value.

With this change the stored checksum is kept only on a graph whose :logseq.kv/graph-remote? is true. Upload and download set that flag (set-graph-sync-metadata! in sync/upload.cljs and sync/download.cljs), nothing unsets it, and it is already how handle-local-tx! (asset sync) and the built-in sync repair in db_core.cljs tell a synced graph.

  • update-local-sync-checksum! returns before any read or write on a graph that is not remote after the transaction.
  • reconcile-local-checksum! returns on a graph that is not remote. Without this, a local graph that still holds a checksum from an earlier version would get a whole-graph recompute at every open, since its covered tx no longer follows the graph.
  • The transaction that makes a graph remote starts from a full recompute instead of the stored value, which may predate edits made while the graph was local. The upload's metadata transaction also sets :logseq.kv/graph-rtc-e2ee? for the first time, and update-checksum already recomputes in full when that flag changes, so an upload runs as many recomputes as before (1 in the listener, 1 in upload-graph!). In a download, the import batches before the one that carries the flag leave the checksum alone, and that batch starts from a full recompute.
  • On a graph that is remote before and after a transaction the code path is the same, plus 2 lookups of the flag.

Left as it is: db-sync-status on a graph that does not sync returns what was stored before (nil on a new graph). start! can connect sync to a graph without the flag when a remote graph has the same name (<resolve-start-graph-id); such a graph keeps no checksum now, which shows in the status output and in the dev-build compare log only, and asset sync already treats it as local.

Measured on the desktop app, release builds of master (16c4ed1a0) and this branch, a new graph without sync, 30 cycles per session, median (range), key to the next editor focused (delete) or to the editor gone (save):

  • 1-block delete: master 92.1 ms (74.5 - 156.6), this change 73.6 ms (47.7 - 135.9).
  • 1-block save: master 79.1 ms (68.5 - 135.4), this change 60.4 ms (49.7 - 96.6).
  • A second session per build on the same graphs: delete 91.9 ms (76.7 - 128.8) against 76.1 ms (57.2 - 121.8), save 82.6 ms (72.2 - 130.6) against 62.6 ms (44.9 - 101.9).
  • Disk syncs from the key to the end of the edit (an fsync logger preloaded into the app, the same count in 30 of 30 edits of each session): delete and save 5 on master, 3 with this change (the client-ops database 3 to 1), insert 4 and 2. Sync time per delete, median: 48.8 ms against 32.6 ms.

#13470 is complementary: it makes the checksum write 1 synced commit instead of 2 on graphs that sync, where this change keeps the write.

Tests

  • New in frontend.worker.db-sync-test:
    • local-graph-edit-writes-no-checksum-test: an edit on a local graph writes no checksum and no covered tx. On master it fails (3 failures: 1 write, both stored).
    • local-graph-open-does-not-recompute-checksum-test: the open-time check on a local graph with a lagging stored checksum recomputes nothing and leaves the stored values. On master it fails (3 failures: 1 recompute, both replaced).
    • graph-becoming-remote-starts-from-full-checksum-test: the transaction that marks a graph remote, without an E2EE flag change, stores a full recompute. On master it fails (1 failure: the old value stays).
    • remote-graph-edit-writes-checksum-test: an edit on a remote graph still writes once, a value equal to a full recompute, with the graph's :max-tx. Passes on master too.
    • upload-after-local-edits-stores-and-sends-full-checksum-test: upload-graph! with its network calls replaced, after a local edit that did not update the stored checksum, stores and sends a checksum equal to a full recompute. Passes on master too.
  • 12 tests in frontend.worker.db-sync-test and 5 in frontend.worker.db-sync-sim-test check the stored checksum along synced paths (rebase, reject, pull/ok, batch reports, undo and redo, the open-time check) on graphs built without the flag. They now build remote graphs (mark-graph-remote!, create-remote-conn), as those paths run on in the app. Without that they fail with this change (14 and 11 failures).
  • frontend.worker.db-sync-test 238 tests, 2238 assertions; frontend.worker.db-sync-sim-test 22 tests, 433 assertions; frontend.worker.db-listener-test 14 tests, 51 assertions; frontend.worker.sync.client-op-test 6 tests, 22 assertions; frontend.worker.sync.upload-test 10 tests, 33 assertions; logseq.db-sync.checksum-test (run from deps/db-sync) 28 tests, 78 assertions: 0 failures, 0 errors.
  • The whole unit suite at TZ=Etc/GMT+12 (2507 tests, 12513 assertions) and its time-zone-dependent namespaces at TZ=Pacific/Kiritimati (1474 tests, 9072 assertions): 0 failures, 0 errors.
  • clj-kondo on the 3 changed files: 0 errors, 0 warnings.

Allow edits from maintainers.


Devin Review

@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

After every transaction the db listener updates the sync checksum and
stores it, with the tx it covers, in the graph's client-ops database:
2 synced sqlite commits per edit, on every graph. That checksum is only
compared with the sync server's, and a graph's first upload recomputes
it in full before it sends the snapshot, so on a graph that does not
sync the work has no reader.

Keep the stored checksum only on a graph whose :logseq.kv/graph-remote?
is true (upload and download set it). On other graphs
update-local-sync-checksum! returns before it reads or writes, and
reconcile-local-checksum! does not recompute at open, which would
otherwise redo the whole checksum at every open of a local graph that
still holds an old stored value. The transaction that makes a graph
remote starts from a full recompute instead of the stored value, which
may predate edits made while the graph was local. On a remote graph
nothing changes.

The tests that check the stored checksum on a synced path now mark
their graph remote.
@tiensonqin
tiensonqin force-pushed the perf/checksum-local-graphs branch from c5a054e to b4993e7 Compare September 30, 2026 03:50
@tiensonqin
tiensonqin merged commit 8c3438d into logseq:master Sep 30, 2026
13 of 14 checks passed
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Sep 30, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Sep 30, 2026
@yuxi-liu-wired
yuxi-liu-wired deleted the perf/checksum-local-graphs branch September 30, 2026 20:02
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 1, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 4, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 8, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 11, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 13, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
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.

2 participants