Skip to content

perf(sync): write the checksum in 1 transaction - #13470

Closed
yuxi-liu-wired wants to merge 1 commit into
logseq:masterfrom
yuxi-liu-wired:perf/checksum-write-once
Closed

yuxi-liu-wired wants to merge 1 commit into
logseq:masterfrom
yuxi-liu-wired:perf/checksum-write-once

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 stores the new sync checksum and the tx it covers in the graph's client-ops database (update-local-checksum, src/main/frontend/worker/sync/client_op.cljs). The 2 keys went in 2 upserts, each its own sqlite autocommit. That database runs in WAL mode with synchronous FULL (read with PRAGMA synchronous on a graph's client-ops file), so each autocommit is a synced write. The 2 upserts now run in 1 transaction through sqlite-with-tx!, which the file already has, so each transaction pays 1 synced commit, and the 2 keys change together.

;; before
(sqlite-set-meta! store :db-sync/checksum checksum)
(sqlite-set-meta! store :db-sync/checksum-covered-tx covered-tx)
;; after
(sqlite-with-tx!
 store
 (fn [tx]
   (sqlite-set-meta! tx :db-sync/checksum checksum)
   (sqlite-set-meta! tx :db-sync/checksum-covered-tx covered-tx)))

In the node worker, sqlite-with-tx! calls the connection wrapper's transaction (wrap-node-sqlite-db in src/main/frontend/worker/platform/node.cljs), which runs the function between BEGIN and COMMIT.

Timings on a graph of 157,614 checksum-eligible entities (imported from a file graph), standalone db worker, median (range) of 3 runs per build:

  • The checksum write alone, 20 writes of changing values per run: master release 16.7 ms (14.0 - 17.0), dev 16.5 ms (15.5 - 17.2); with this change release 8.9 ms (8.8 - 10.5), dev 8.9 ms (8.5 - 11.2).
  • The listener's checksum stage per transaction (update-checksum plus the write), which the dev build logs, master against this change: 1-block insert 17.5 ms (17.0 - 17.8) against 10.0 ms (9.4 - 10.0), 1-block move 17.2 ms (16.4 - 25.0) against 9.2 ms (8.5 - 10.0), 1-block delete 17.2 ms (16.4 - 19.9) against 9.6 ms (9.2 - 10.3), 200-block insert 34.8 ms (33.7 - 36.6) against 28.2 ms (26.1 - 29.3), 200-block delete 33.1 ms (32.7 - 36.2) against 25.0 ms (24.6 - 27.4). The first transaction after the start, a 1-block save, takes 18.0 ms (17.2 - 24.0) on master and 17.7 ms (17.0 - 18.7) with this change.

The builds with this change also carried the commits of perf/checksum-one-walk and fix/restore-max-tx and 1 change to the sqlite read path. A build with the recompute changes and without this commit wrote in 16.8 ms (16.2 - 17.1) release and 17.2 ms (16.6 - 17.6) dev, 3 runs each, the same as master.

Tests

  • New checksum-and-covered-tx-written-in-1-transaction-test in frontend.worker.sync.client-op-test: counts the calls of the database's transaction during update-local-checksum and reads both keys back. On master it fails (1 failure: 0 calls). With this change it passes. The test's database is better-sqlite3 in memory, as in the rest of that namespace.
  • frontend.worker.sync.client-op-test 7 tests, 25 assertions; frontend.worker.db-sync-test 233 tests, 2222 assertions; logseq.db-sync.checksum-test (run from deps/db-sync) 28 tests, 78 assertions: 0 failures.
  • The whole unit suite at TZ=Etc/GMT+12 (2503 tests, 12500 assertions) and its time-zone-dependent namespaces at TZ=Pacific/Kiritimati (1469 tests, 9056 assertions): 0 failures, 0 errors.
  • clj-kondo on the 2 changed files: 0 errors, 0 warnings.

Allow edits from maintainers.


Devin Review

After every graph commit the checksum and the tx it covers were 2
sqlite autocommits on the client-ops database, each a synced write.
Write both in 1 transaction.

@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 #13469, #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