From 724ddad1501ca401ca97bf42fb0b84d22ec05c17 Mon Sep 17 00:00:00 2001 From: leemour Date: Thu, 26 Nov 2026 00:15:11 +0100 Subject: [PATCH] perf(mail): time the type: and reach: fields of cli-core mail search 100,000 synthetic emails: the first type: search on a writable open types every email first (18.4 s at load 25), a read-only open works them out in memory (1.5-1.8 s, close to the 2 s search limit), warm searches take 33-105 ms against 40 ms for a plain word. For cli-core#3372. Co-Authored-By: Claude Opus 5.5 --- performance/mail/README.md | 3 +- performance/mail/types/README.md | 67 ++++++++++++++ performance/mail/types/search.mjs | 142 ++++++++++++++++++++++++++++++ 3 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 performance/mail/types/search.mjs diff --git a/performance/mail/README.md b/performance/mail/README.md index 4d0a4c2..16932d3 100644 --- a/performance/mail/README.md +++ b/performance/mail/README.md @@ -15,7 +15,8 @@ Measurements of reading a mailbox into the shared store with memo. - [agent-questions](agent-questions/README.md): six everyday questions an agent answers from an imported mailbox — run 4: 2 answered, 2 partly, 2 not found (before the imported years), and what memo made hard (2026-10-16). - [types](types/README.md): what typing every email costs cli-core's store (store version 76) — 100,000 emails typed - on the first filtered read in 1.7–11.6 s, one batch of 5,000 under the write lock in 0.1–0.6 s (2026-11-25). + on the first filtered read in 1.7–11.6 s, one batch of 5,000 under the write lock in 0.1–0.6 s; the first + `type:` search in 18 s on a writable open, 1.5–1.8 s on a read-only one, warm 33–105 ms (2026-11-26). - [rules](rules/README.md): what mail sorting rules cost cli-core's store (store version 77) — 50 rules add about 0.35 s per 100,000 emails typed, a `gmail.com` rule over 30,000 emails applies in about 1 s plus 1.5–2 s on the next typed read, and moving 500 senders to bots takes 4–6 s (2026-11-25). diff --git a/performance/mail/types/README.md b/performance/mail/types/README.md index 28d5301..525a018 100644 --- a/performance/mail/types/README.md +++ b/performance/mail/types/README.md @@ -70,3 +70,70 @@ the commit's sync to disk, which ranged from 18 to 680 ms per commit under the o counts kept in SQL. - Bodies and attachments: the synthetic mail had none, so attachment parts were never read. - What version 76's 14 new triggers add to a mail sync: there is no version 75 baseline run on the same data. + +## Search by type and reach + +### Why it was run + +memo searches mail only through cli-core's search (`memo search mail`). A filter by type that memo ran after the +search would return short pages, so cli-core's query language gained `type:` and `reach:`, which filter inside the +query's SQL ([cli-core#3372](https://github.com/WireCatLabs/cli-core/pull/3372)). Types are still kept lazily, so a +search can meet mail the store has not typed yet. The questions: + +1. What does the first `type:` search after the upgrade cost on 100,000 emails, on a writable open (every email typed + first, 5,000 per write) and on a read-only open (types worked out in memory, nothing kept)? +2. The search has a 2 s time limit. Does the read-only path stay inside it? +3. Do warm searches with `type:` or `reach:` cost more than a plain word search? + +The answers decided that the typing runs before the search, outside its time limit, and only when the query names +one of the two fields. + +### Setup + +- Machine: AMD Ryzen AI 9 HX 370 (24 threads), Linux 7.0, Node 24.19.0. Other agents' test runs kept the load average + at 22 to 28 during the run. +- Code: cli-core branch `feat/search-mail-type` (store version 77), built with `pnpm build`. +- Data: the same 100,000 synthetic emails as above, from the same generator, saved through `store.mail.saveThreads`. +- Search: `searchStore` with `kind: "mail"`, 20 hits per page, the call `memo search mail` makes. + +```sh +node search.mjs 100000 +``` + +### Results + +One run. + +| Step | Time | +| --- | ---: | +| read-only open of an untyped store: `type:order`, first 20 | 1.83 s | +| read-only open of an untyped store: `hello reach:direct`, first 20 | 1.49 s | +| first search on a writable open: type all 100,000 emails, then `type:order`, first 20 | 18.4 s | +| warm `type:order` | 33 ms | +| warm `type:order OR type:bill` | 35 ms | +| warm `reach:broadcast` | 72 ms | +| warm `hello type:personal` | 105 ms | +| warm `order -type:promotion` | 65 ms | +| warm `hello`, no type field (baseline) | 40 ms | + +Plain searches typed nothing: 100,000 emails were still untyped after a plain search and after both read-only +searches, and none after the first writable `type:` search. Every hit carried its type and reach, also on the read-only +open. + +The first plain search after seeding took 564 s. That is the store indexing the words of 100,000 new emails on the +first search after they were saved, not the type fields: the same happens with no `type:` in the query. + +### What it showed + +- The first writable `type:` search pays once for typing every email: 18 s here, 1.7–11.6 s in the runs above at a + lower load. The time is the commits' sync to disk, as above. Later searches never pay it again. +- A read-only open of an untyped store answers in 1.5–1.8 s, close to the 2 s limit. Under heavier load it may stop + with "out of time". The answer is to run one search on a writable open, which keeps the types. +- Warm, the type fields cost about as much as a word: 33–105 ms against 40 ms for a plain word search. + +### Not measured + +- More than one run: the load varied, so the first-search time has a wide range. +- Stores over 100,000 emails. The read-only path grows with the number of untyped emails and passes the 2 s limit + sooner on a larger store. +- Real mail: the timings depend on how many emails a type matches, and the synthetic mix is not a real inbox. diff --git a/performance/mail/types/search.mjs b/performance/mail/types/search.mjs new file mode 100644 index 0000000..97184b5 --- /dev/null +++ b/performance/mail/types/search.mjs @@ -0,0 +1,142 @@ +// Times the type: and reach: fields of cli-core's mail search on synthetic mail: a read-only open of a store never +// typed (types worked out in memory for that search), the first such search on a writable open (every email typed +// first, a batch of 5,000 per write), and warm searches next to a plain word search. +// Usage: node search.mjs [N] +import { mkdtempSync } from "node:fs" +import { loadavg, tmpdir } from "node:os" +import { join } from "node:path" + +const repo = process.argv[2] +const N = Number(process.argv[3] ?? 100000) +const { openStore } = await import(join(repo, "dist/store/index.js")) +const { openCache } = await import(join(repo, "dist/store/open.js")) +const { searchStore } = await import(join(repo, "dist/services/messages-archive-search.js")) +const ME = "owner@home.example.com" +const MAIL = { provider: "email", account: ME } +const NOW = Date.parse("2026-11-25T12:00:00Z") +const say = (line) => process.stdout.write(`${line}\n`) + +const time = async (label, work) => { + const start = performance.now() + const result = await work() + say(`${label}: ${Math.round(performance.now() - start)} ms`) + return result +} + +const ONE_CLICK = { "list-unsubscribe": true, "list-unsubscribe-post": true } + +// About the mix of a real inbox: most mail is lists and shops, a fifth is people. +const emailOf = (n) => { + const at = NOW - n * 60_000 + const base = { externalId: ``, sentAt: at, receivedAt: at, bodyText: null } + const to = [{ address: ME, name: null }] + const slot = n % 20 + if (slot < 8) { + const list = n % 200 + return { + ...base, + subject: `Issue ${n}`, + from: { address: `editor@list${list}.example.com`, name: `List ${list}` }, + to, + headers: { "list-id": `list${list}.example.com`, "list-unsubscribe": true }, + } + } + if (slot < 12) { + const shop = n % 300 + return { + ...base, + subject: `Winter sale ${n}: 30% off`, + from: { address: `shop@shop${shop}.example.com`, name: `Shop ${shop}` }, + to, + headers: ONE_CLICK, + } + } + if (slot < 15) { + const shop = n % 300 + return { + ...base, + subject: `Thank you for your order #${n}`, + from: { address: `shop@shop${shop}.example.com`, name: `Shop ${shop}` }, + to, + } + } + if (slot === 15) + return { + ...base, + subject: "New sign-in to your account", + from: { address: `security@bank${n % 50}.example.com`, name: "Bank" }, + to, + } + if (slot === 16) + return { + ...base, + subject: `Re: plan ${n}`, + from: { address: ME, name: "Me" }, + to: [{ address: `person${n % 2000}@firm${n % 300}.example.com`, name: null }], + outgoing: true, + } + return { + ...base, + subject: `Hello ${n}`, + from: { address: `person${n % 2000}@firm${n % 300}.example.com`, name: `Person ${n % 2000}` }, + to, + } +} + +const seeded = async () => { + const path = join(mkdtempSync(join(tmpdir(), "mail-search-types-")), "store.db") + const store = await openStore({ path }) + const accountId = await store.accounts.saveAccount(MAIL, { name: null }) + for (let at = 0; at < N; at += 2000) { + const threads = [] + for (let t = at; t < Math.min(N, at + 2000); t += 100) + threads.push({ + accountId, + externalId: `t${t}`, + now: NOW, + emails: Array.from({ length: Math.min(100, N - t) }, (_, i) => emailOf(t + i)), + }) + await store.mail.saveThreads(threads) + } + return { store, path } +} + +const search = (store, text) => searchStore(store, MAIL, { text, kind: "mail", limit: 20 }) +const shown = async (label, store, text) => { + const found = await time(label, () => search(store, text)) + say( + ` ${found.items.length} hits, first: ${found.items[0]?.email?.mailType ?? "-"}/${found.items[0]?.email?.reach ?? "-"}`, + ) +} + +say( + `node ${process.version}, N = ${N}, load ${loadavg() + .map((x) => x.toFixed(1)) + .join(" ")}`, +) +const { store, path } = await time(`seed ${N} emails`, seeded) +await shown("plain word search before any typing: hello, first 20", store, "hello") +await store.close() +const db = await openCache(path) +const untyped = () => Number(db.prepare("SELECT count(*) AS n FROM emails WHERE mail_type IS NULL").get().n) +say(`untyped before the first typed search: ${untyped()}`) + +{ + const readOnly = await openStore({ path, readOnly: true }) + await shown("read-only open, untyped: type:order, first 20", readOnly, "type:order") + await shown("read-only open, untyped: hello reach:direct, first 20", readOnly, "hello reach:direct") + await readOnly.close() +} +say(`untyped after the read-only searches: ${untyped()}`) + +const writer = await openStore({ path }) +await shown(`first search on a writable open: types for ${N} emails, then type:order, first 20`, writer, "type:order") +say(`untyped after: ${untyped()}`) +await shown("warm type:order, first 20", writer, "type:order") +await shown("warm type:order OR type:bill, first 20", writer, "type:order OR type:bill") +await shown("warm reach:broadcast, first 20", writer, "reach:broadcast") +await shown("warm hello type:personal, first 20", writer, "hello type:personal") +await shown("warm order -type:promotion, first 20", writer, "order -type:promotion") +await shown("warm hello, no type, first 20", writer, "hello") +await writer.close() +db.close()