Look up only the changed ids on each JMAP pass (#171) #177

Merged
svc-agent-writer merged 1 commit from fix/171-source-flags-scan into main 2026-11-04 01:16:05 +00:00

Closes #171.

Summary

 _changes (once per Email/changes page)
   _refresh(created + updated)
-    store.source_flags(account)              # every stable-id row, walks past body_text
+    if no ids: return                         # an empty page reads nothing
+    store.source_flags(account, ids)          # one messages_source seek per id, 500 per chunk

 _backfill / _reconcile
-  set(store.source_flags(account))           # every row, to get the ids
+  store.source_ids(account)                  # covering read of messages_source, no table rows

source_flags holds the store lock per chunk, not across the whole read, so a waiting IMAP account gets the lock between chunks.

Evidence

  • Before: a delta pass with no changes called source_flags(account) and read every row. On a store of about 249,000 messages this cost about 1 GB of disk reads per pass, and the pass interval was 324 s at p50 (see #171).
    After: new tests in tests/test_jmap.py and tests/test_store.py:

    test_a_quiet_pass_reads_no_mirror_rows
      pass with no changes            -> no source_flags call, no source_ids call
      pass with M2 flagged, M4 added  -> source_flags called once with [M2, M4]
    test_source_ids_and_flags_read_only_what_they_name
      chunk size 2, duplicates, unknown ids, another account -> only the named rows
    test_source_lookups_never_scan_the_table
      source_ids   -> USING COVERING INDEX messages_source
      source_flags -> SEARCH messages USING INDEX messages_source, no SCAN
    

    make check: 1,315 passed and 8 skipped; mypy, ruff and pip-audit clean.

Not measured on a live mirror yet. That needs a release.

Merge Danger

Door: two-way

No schema change and no change to stored data. A revert restores the old full read.

Blast Radius: sync

Only the JMAP sync pass changes. A reconcile, which runs after a mailbox change or lost changes, still looks up every remote id, now as chunked index seeks instead of one scan. That can be a little slower on a cold cache. The reconcile also fetches every Email from the server, which costs more. Fix 3 in #171 (a covering index) would remove the cost and needs a schema change; it is not in this PR.

🤖 Generated with Claude Code

Closes #171. ## Summary ```diff _changes (once per Email/changes page) _refresh(created + updated) - store.source_flags(account) # every stable-id row, walks past body_text + if no ids: return # an empty page reads nothing + store.source_flags(account, ids) # one messages_source seek per id, 500 per chunk _backfill / _reconcile - set(store.source_flags(account)) # every row, to get the ids + store.source_ids(account) # covering read of messages_source, no table rows ``` `source_flags` holds the store lock per chunk, not across the whole read, so a waiting IMAP account gets the lock between chunks. ## Evidence - **Before:** a delta pass with no changes called `source_flags(account)` and read every row. On a store of about 249,000 messages this cost about 1 GB of disk reads per pass, and the pass interval was 324 s at p50 (see #171). **After:** new tests in `tests/test_jmap.py` and `tests/test_store.py`: ```text test_a_quiet_pass_reads_no_mirror_rows pass with no changes -> no source_flags call, no source_ids call pass with M2 flagged, M4 added -> source_flags called once with [M2, M4] test_source_ids_and_flags_read_only_what_they_name chunk size 2, duplicates, unknown ids, another account -> only the named rows test_source_lookups_never_scan_the_table source_ids -> USING COVERING INDEX messages_source source_flags -> SEARCH messages USING INDEX messages_source, no SCAN ``` `make check`: 1,315 passed and 8 skipped; mypy, ruff and pip-audit clean. Not measured on a live mirror yet. That needs a release. ## Merge Danger **Door:** two-way No schema change and no change to stored data. A revert restores the old full read. **Blast Radius:** sync Only the JMAP sync pass changes. A reconcile, which runs after a mailbox change or lost changes, still looks up every remote id, now as chunked index seeks instead of one scan. That can be a little slower on a cold cache. The reconcile also fetches every Email from the server, which costs more. Fix 3 in #171 (a covering index) would remove the cost and needs a schema change; it is not in this PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Look up only the changed ids on each JMAP pass (#171)
All checks were successful
ci/woodpecker/pr/validation Pipeline was successful
f0a24f287c
_refresh read flags and mailboxes for every stable-id row of the account
once per Email/changes page, even an empty one. mailboxes sits after
body_text, so on a large mirror each pass read close to the whole table.

- _refresh returns before any store read when the page names no ids.
- Store.source_flags takes the ids to look up and seeks messages_source
  per id, in chunks of 500, holding the store lock per chunk.
- New Store.source_ids lists the stable ids from the index alone; the
  backfill and reconcile id diffs use it.

A reconcile still looks up every remote id. It runs only on a mailbox
change or lost changes, and already fetches every Email from the server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
svc-agent-writer deleted branch fix/171-source-flags-scan 2026-11-04 01:16:05 +00:00
Sign in to join this conversation.
No description provided.