Skip to content

perf: 1 read for a push batch's answers, and link views read again once saves stop - #239

Merged
bketelsen merged 2 commits into
mainfrom
irulan/210-212-fewer-locks-and-reads
Nov 14, 2026
Merged

bketelsen merged 2 commits into
mainfrom
irulan/210-212-fewer-locks-and-reads

Conversation

@frostyard-machines

@frostyard-machines frostyard-machines commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two small performance fixes from a code review of main, each in its own commit.

Hub: 1 read for a push batch's earlier answers (Fixes #210). log.push read each item's earlier answer in a write transaction of its own (answered → DB.Tx → tx.PushAnswer), so a batch of 100 items took the database's write mutex 100 times and could hold up every vault writer on the hub. The new store.DB.PushAnswers reads the whole batch's answers in 1 query (push_id IN json_each(?)) outside DB.Tx. The per-vault log lock already keeps other pushes of the vault out, and only log.push records hub_pushes rows, so the read sees every answer a write transaction would. The answers the device gets stay the same; a refusal recorded for missing content is put in the batch's map, so a repeat of that push later in the same batch still gets it, as before.

Web app: link views read again once the saves stop (Fixes #212). The Backlinks and Outgoing links panes and the status bar's backlink count had the files map as their trigger, so each autosave cost 2 note.backlinks, a note.read, 1 or more note.search and a note.links, even when no link changed. useLinksVersion (app/src/app/vaultChanges.ts) is their new trigger: it goes up at once when the set of paths changes (pathsVersion: a file added, moved or removed, or a new listing), and otherwise LINKS_DELAY after the files last changed. LINKS_DELAY is 2 s, twice the editor's SAVE_DELAY: saves are at least 1 s apart, so a delay like COUNT_DELAY (150 ms) would merge none of the saves of a person typing. The panes now catch up about 2 s after the last save. docs/design/app.md says so.

Risk tier

Risk tier: 2 — a behavior change in code (when the hub reads push answers; when the web app's link views refresh). No path in .github/risk-paths.json is touched (internal/hub/log.go is not in internal/hub/{hub,pair,withdraw,relocate}.go).

Docs housekeeping

  • docs/design/app.md updated for the link views' refresh; no new doc, ADR or spec change (the sync protocol's answers are unchanged).

Verification

  • make ci green (host and nsl: verify, ui-check with 819 tests, desktop-build, race tests)
  • make ui-check green (nsl), for a change under app/
  • make sync-sim, for a change to sync, at head 41482c6: stepwise seeds 1–4 (10,000 steps each) passed, then SIM_MODE=concurrent SIM_SEEDS=3 make sync-sim passed (TestISimulation and TestRegressions). The full default run (10 seeds in each mode) was not run.
  • Revert test for a fix, in a scratch worktree with only internal/hub/log.go, app/src/app/status.ts and app/src/app/panes/links.tsx reverted: TestARepeatedBatchDoesNotWaitForTheWriteLock fails ("waited for the database's write lock") and the panes test fails (expected 8 to be 2); details in a comment.
  • Cross-review at head 41482c6: openai/gpt-6.1-sol, Ready, no findings (comment).

New targeted tests:

  • internal/hub: TestARepeatedBatchDoesNotWaitForTheWriteLock holds the write lock in a DB.Tx and pushes a batch of 3 repeated items; it fails if the batch waits for the lock, and checks that the answers equal the first ones.
  • internal/store: TestReplicasAndPushAnswers covers PushAnswers (another replica's answers left out, repeats, no pushes), read while a write transaction is open.
  • app/src/app/vaultChanges.test.ts: the hook goes up once after a burst of saves, and at once for an added, moved or removed file.
  • app/src/app/panes.test.tsx: "read a note's backlinks again once the vault's saves stop, not at each save": 3 saves cost no read until they stop, then 1 for the pane and 1 for the status bar.

… write lock

log.push read each item's earlier answer in a write transaction of its
own, so a batch of 100 items took the database's write mutex 100 times
and could hold up every vault writer on the hub. store.DB.PushAnswers
reads the whole batch's answers in 1 query outside DB.Tx; the per-vault
log lock already keeps other pushes of the vault out. A refusal recorded
for missing content stays visible to a repeat later in the same batch.

Fixes #210
The Backlinks and Outgoing links panes and the status bar's backlink
count read notesd again on every change to the files store, so each
autosave cost note.backlinks twice, note.read, note.search and
note.links, even when no link changed. useLinksVersion goes up at once
when the set of paths changes, and otherwise 2 s (twice the editor's
save delay) after the files last changed, so a burst of saves costs 1
read.

Fixes #212
@github-actions github-actions Bot added the level:L3 Skim label Nov 14, 2026
@frostyard-machines

Copy link
Copy Markdown
Contributor Author

Revert test at head 41482c6, in a scratch worktree (git worktree add --detach <tmp> HEAD), with only the fix reverted (git checkout origin/main -- internal/hub/log.go app/src/app/status.ts app/src/app/panes/links.tsx; store.DB.PushAnswers and useLinksVersion kept as the tests' dependencies):

  • go test ./internal/hub/ -run TestARepeatedBatchDoesNotWaitForTheWriteLock -count=1 fails: hub_test.go:482: a batch of repeated pushes waited for the database's write lock.
  • make ui-vitest F="src/app/panes.test.tsx src/app/vaultChanges.test.ts" (nsl) fails 1 test: the link panes > read a note's backlinks again once the vault's saves stop, not at each save, AssertionError: expected 8 to be 2 (8 note.backlinks reads during the 3 saves, where 2 were made before them).

At the head, both pass. The scratch worktree is removed.

@frostyard-machines

Copy link
Copy Markdown
Contributor Author

Cross-review (initial review, 1 of the budget): Ready, Blocking: none, no findings.

  • Reviewer: openai/gpt-6.1-sol; coder: anthropic/claude-opus-5-5.
  • Base bd37fdf395dfbd550930d04ba8867d3b69890099, head 41482c612fc435809a4560224dbcb0d264f5a2b8; all 10 changed files, none excluded (315 additions, 41 deletions).
  • Checked: PushAnswers keeps the recorded fields and isolates replicas; the only production RecordPush calls are in log.push, under the per-vault log lock, and a replica belongs to 1 vault, so the read outside DB.Tx is safe (WAL, _txlock=immediate, 8 connections); a repeated missing-content refusal in 1 batch keeps its answer; useLinksVersion cancels a pending content timer when the paths change, leaves no timer after StrictMode's effect replay, and goes up at both steps of loadFiles; the targeted tests assert the removed behavior.

@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 14, 2026
@bketelsen
bketelsen merged commit 6c33227 into main Nov 14, 2026
12 checks passed
@bketelsen
bketelsen deleted the irulan/210-212-fewer-locks-and-reads branch November 14, 2026 20:14
@frostyard-machines

frostyard-machines commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Full seeded sync simulation at this head: PASS.

  • Head: 41482c6
  • Command: make sync-sim with the defaults (no SIM_* variable set): go test -tags integration -run '^(TestISimulation|TestRegressions)$' -count=1 -v -timeout 4h ./internal/synctest
  • Seeds and steps: seeds 1 to 10, 10,000 steps each, a hub and 3 devices
  • Modes: stepwise and concurrent (20 runs in all)
  • Duration: 9,394.7 s (2 h 36 m) for the package, 2026-11-14 14:51 to 17:28 EST
    • stepwise: 733 s to 820 s per seed (make check ran beside seed 1)
    • concurrent: 164 s to 174 s per seed
  • Result: all 20 runs converge with no lost token and no conflict file. Each seed wrote 6,914 to 7,082 tokens; the hub's log ended at 8,644 to 8,916 revisions, over 702 to 769 files (stepwise) and 1,918 to 2,033 files (concurrent).
  • Regressions: TestRegressions passes both plans in internal/synctest/testdata/regressions.
  • make check at the same head: pass.

The seconds per seed are longer than plan 0004's record (7 to 10 min stepwise, about 80 to 90 s concurrent). This run was 1 process on a shared 8-core VM, and the record does not name its machine, so the times are not a like-for-like comparison.

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

Labels

level:L3 Skim ready-for-human Reviewed, CI green: ready for Brian to review and merge

Projects

None yet

2 participants