search.query: frecent() costs a fixed ~1.6 ms on every Home with 64+ used notes #1053

Open
opened 2026-11-10 09:50:45 +00:00 by kayg · 0 comments
Owner

Problem

Once any note has been used, every search.query pays a fixed extra cost: crates/search/src/read.rs::frecent() returns at once when the frecency table is empty, but otherwise reads the 64 most recent used notes and re-checks their text against the query through FTS (SELECT ... FROM fts WHERE rowid IN (...), up to 64 rows with substr(body, 1, FRECENT_TEXT)). The cost saturates at 64 used notes, so any Home in normal use pays it on dev today. Found while measuring #1011.

Evidence (perf-01/02, 100k notes, 2026-11-10, same dev server code on both)

  • Cross-test on compute02 (server-side took_ms, same query set as the harness): the state with 593 frecency rows answered at ~7 ms p50, the state with 0 rows at ~3.4 ms, under either server binary. After swapping the rows between the two states the pattern inverted.
  • Perf VMs, ABAB x5 with both arms starting from a populated table: search.query p50 ~3.3 ms; before that fix the empty-table arm measured 1.5-1.7 ms. So a used Home costs about 1.6 ms (about 2x) more per query than an empty one.
  • Table layout, row counts and FTS segments were identical between the states; only the frecency row count differed.

Proposed fix (separate PR, not #1011)

Keep the top-64 used item ids (and their short text) in memory per user: load once when the index opens, update on Used changes in the writer (it already serialises them), drop on reset/rebuild. frecent() then matches the query against the cached text instead of running an FTS lookup for 64 rowids. Gate: search.query p50/p95 on a Home with 100+ used notes no worse than today's empty-table number within noise; no extra idle wakeups (the cache is only touched by writes and queries).

Alternative if the cache is too much: lower the cap from 64 to the number the ranker really uses, and skip frecent() for queries of 1-2 characters that the newest-matches pass already covers.

Refs #1011.

## Problem Once any note has been used, every `search.query` pays a fixed extra cost: `crates/search/src/read.rs::frecent()` returns at once when the `frecency` table is empty, but otherwise reads the 64 most recent used notes and re-checks their text against the query through FTS (`SELECT ... FROM fts WHERE rowid IN (...)`, up to 64 rows with `substr(body, 1, FRECENT_TEXT)`). The cost saturates at 64 used notes, so any Home in normal use pays it on dev today. Found while measuring #1011. ## Evidence (perf-01/02, 100k notes, 2026-11-10, same dev server code on both) - Cross-test on compute02 (server-side `took_ms`, same query set as the harness): the state with 593 `frecency` rows answered at ~7 ms p50, the state with 0 rows at ~3.4 ms, under **either** server binary. After swapping the rows between the two states the pattern inverted. - Perf VMs, ABAB x5 with both arms starting from a populated table: `search.query` p50 ~3.3 ms; before that fix the empty-table arm measured 1.5-1.7 ms. So a used Home costs about 1.6 ms (about 2x) more per query than an empty one. - Table layout, row counts and FTS segments were identical between the states; only the `frecency` row count differed. ## Proposed fix (separate PR, not #1011) Keep the top-64 used item ids (and their short text) in memory per user: load once when the index opens, update on `Used` changes in the writer (it already serialises them), drop on reset/rebuild. `frecent()` then matches the query against the cached text instead of running an FTS lookup for 64 rowids. Gate: `search.query` p50/p95 on a Home with 100+ used notes no worse than today's empty-table number within noise; no extra idle wakeups (the cache is only touched by writes and queries). Alternative if the cache is too much: lower the cap from 64 to the number the ranker really uses, and skip `frecent()` for queries of 1-2 characters that the newest-matches pass already covers. Refs #1011.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kayg/calternal#1053
No description provided.