From 0a28d840b77668a92208c1b353b66ee0c2b8c83b Mon Sep 17 00:00:00 2001 From: leemour Date: Thu, 26 Nov 2026 01:33:36 +0100 Subject: [PATCH] perf(mail): time cli-core's subscriptions read and the bots filter by what they send 100,000 synthetic emails in 1,600 broadcast groups: a warm page of 50 in 162 ms, 500 in 132 ms, no store version needed; bots filters under 60 ms. For cli-core#3379. Co-Authored-By: Claude Opus 5.5 --- performance/mail/README.md | 3 + performance/mail/subscriptions/README.md | 88 ++++++++++ performance/mail/subscriptions/bench.mjs | 199 +++++++++++++++++++++++ 3 files changed, 290 insertions(+) create mode 100644 performance/mail/subscriptions/README.md create mode 100644 performance/mail/subscriptions/bench.mjs diff --git a/performance/mail/README.md b/performance/mail/README.md index 16932d3..fed8ce1 100644 --- a/performance/mail/README.md +++ b/performance/mail/README.md @@ -20,3 +20,6 @@ Measurements of reading a mailbox into the shared store with memo. - [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). +- [subscriptions](subscriptions/README.md): cli-core's Subscriptions read, broadcast mail grouped by List-Id and paged + in SQL, and the bots filter by what they send — 100,000 emails in 1,600 groups: a warm page of 50 in 162 ms, 500 in + 132 ms, no store version needed; bots filters under 60 ms (2026-11-26). diff --git a/performance/mail/subscriptions/README.md b/performance/mail/subscriptions/README.md new file mode 100644 index 0000000..3211481 --- /dev/null +++ b/performance/mail/subscriptions/README.md @@ -0,0 +1,88 @@ +# Subscriptions: grouping broadcast mail by list in the store + +## Why it was run + +The owner ruled on 2026-11-25 that WireCat Desktop gets a Subscriptions view beside mail. It shows the mail sent to +many readers (reach `broadcast`), grouped by list (`List-Id`, else the sender), with the mail count, the newest mail +and an unsubscribe action, like HEY's Feed. The Bots tab also filters bots by what they send: alerts and codes, +receipts, newsletters, marketing and messenger bots. Desktop could not build either one on cli-core 1.1.0 +([wirecat-desktop#482](https://github.com/WireCatLabs/wirecat-desktop/pull/482), +[cli-core#3371](https://github.com/WireCatLabs/cli-core/issues/3371)). + +cli-core adds `store.mail.subscriptions` and `people.listBots({ sends })`. Desktop never filters a list in the +browser, so both page in SQL. The questions: + +1. On 100,000 emails, is a page of Subscriptions fast enough for a screen without a new index or a stored grouping + column? That decides whether the change needs a store version. +2. What does the first read cost, when the store has not typed its mail yet, on a writable and on a read-only open? +3. Do the bots filters stay fast? + +## Setup + +- Machine: AMD Ryzen AI 9 HX 370 (24 threads), Linux 7.0, Node 24.19.0. Other agents' runs kept the load average at + 18 to 48 during the runs. +- Code: cli-core branch `feat/subscriptions-read`, built with `pnpm build`. +- Data, all synthetic: 100,000 emails saved through `store.mail.saveThreads`, 100 per thread, the mix of + [types/search.mjs](../types/search.mjs) with more senders. 40% newsletters from 1,000 lists (half name a signed + unsubscribe target), 20% sales from 600 shops (target and one-click), 15% order receipts from the shops' no-reply + addresses, 5% sign-in alerts, 5% the owner's own mail to 2,000 people, 15% mail from those people. 40 messenger bots. + 60,000 emails are broadcast, in 1,600 groups. +- Each warm step ran 5 times. The tables give the median. + +```sh +node bench.mjs 100000 5 +``` + +## Results + +Final query, one run (load 18.7 to 21.8): + +| Step | Time | +| --- | ---: | +| read-only open of an untyped store: subscriptions, first 50 (types worked out in memory, nothing kept) | 0.91 s | +| writable open, first read: type all 100,000 emails, then subscriptions, first 50 | 20.5 s | +| warm subscriptions, first 50 | 162 ms | +| warm subscriptions, page 10 of 50 | 166 ms | +| warm subscriptions, first 500 | 132 ms | +| warm subscriptions, `types: [promotion]`, first 50 | 62 ms | +| warm subscriptions, last 7 days, first 50 | 45 ms | +| warm subscriptions, a page past the end (the total is counted again) | 350 ms | +| warm `mailSenders reach:[broadcast]`, first 50, for comparison (pages in JavaScript) | 398 ms | +| warm `listBots sends:[receipts]`, first 50 of 600 | 49 ms | +| warm `listBots sends:[marketing]`, first 50 of 600 | 52 ms | +| warm `listBots sends:[messenger]`, 40 bots | 3 ms | +| warm `listBots sends:[alerts, receipts, messenger]`, first 50 of 640 | 48 ms | +| warm `listBots`, no filter, first 50 | 4 ms | + +The first `listBots sends` after `people list` sorted 2,240 senders into bots took 22 s. Sorting a sender into a bot +clears the type of its mail, so that read typed most of the mail again. Later reads were warm. + +Two earlier versions of the query, on the same data: + +| Version | first 50 | first 500 | +| --- | ---: | ---: | +| window functions (`row_number` per group) for the newest mail and the newest target, on an earlier data set with 140 groups | 304 ms | 180 ms | +| bare newest row, target looked up per group, planner on `emails_by_list_id` for "no List-Id" | 489 ms | 3,453 ms | +| bare newest row, target per group through the sender's identities (shipped) | 162 ms | 132 ms | + +## What it showed + +- No store version is needed. The grouping is one scan of the broadcast mail. SQLite takes the newest row of each + group as the bare column beside `max(at)`, which is about 90 ms of the page. The unsubscribe target is then looked + up only for the page's groups, through `emails_by_list_id` or the sender's identities, at 0.03 to 5 ms a group. +- The per-group lookup has a trap. For "this sender's mail with no List-Id", SQLite chose `emails_by_list_id`, and + most mail has no List-Id. That cost 10 ms a group and 3.5 s for 500 groups. A unary `+` on the List-Id expression + keeps it on `emails_by_from_identity_id`. +- Window functions for the newest mail cost about 200 ms more than the bare-column form. +- The first read after an upgrade costs what typing costs ([types](../types/README.md)): about 20 s on a writable open, + under 1 s on a read-only one. +- The bots filters stay under 60 ms warm. They are `EXISTS` subqueries over the bot's identities and the stored types. + +## Not measured + +- Real mail. The synthetic data has no real List-Id names, mixed-signature lists or senders that change domains. + In this data no `security@` sender became a bot, so `sends:[alerts]` found none. The time is still the cost of the + filter. +- More than 100,000 emails, or more than 1,600 groups. +- Writes while the read runs. The read takes no write lock once the types are kept. +- The memo command around the read (`memo mail subscriptions list`). It adds the store open and the JSON output. diff --git a/performance/mail/subscriptions/bench.mjs b/performance/mail/subscriptions/bench.mjs new file mode 100644 index 0000000..41ed564 --- /dev/null +++ b/performance/mail/subscriptions/bench.mjs @@ -0,0 +1,199 @@ +// Times cli-core's subscriptions read (store.mail.subscriptions: broadcast mail grouped by List-Id, else by sender, +// paged in SQL) and the bots filter by what a bot sends (people.listBots sends), on synthetic mail. +// Usage: node bench.mjs [N] [runs] +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 RUNS = Number(process.argv[4] ?? 5) +const { openStore } = await import(join(repo, "dist/store/index.js")) +const { openCache } = await import(join(repo, "dist/store/open.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 once = async (work) => { + const start = performance.now() + const result = await work() + return { ms: performance.now() - start, result } +} + +const time = async (label, work) => { + const { ms, result } = await once(work) + say(`${label}: ${Math.round(ms)} ms`) + return result +} + +const median = (values) => { + const sorted = [...values].sort((a, b) => a - b) + return sorted[Math.floor(sorted.length / 2)] +} + +const warm = async (label, work) => { + const times = [] + let result + for (let run = 0; run < RUNS; run += 1) { + const done = await once(work) + times.push(done.ms) + result = done.result + } + say( + `${label}: median ${Math.round(median(times))} ms, min ${Math.round(Math.min(...times))}, max ${Math.round(Math.max(...times))}`, + ) + return result +} + +const target = (domain, n) => ({ + "list-unsubscribe": true, + "list-unsubscribe-https": `https://${domain}/u/${n}`, + "list-unsubscribe-mailto": `mailto:leave@${domain}`, +}) + +// The mix of search.mjs in ../types, most mail lists and shops and a fifth people, with more senders: 1,000 lists and +// 600 shops. Half the lists and every shop name a signed unsubscribe target; the sales add one-click. +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 = Math.floor(n / 20) % 1000 + const domain = `list${list}.example.com` + return { + ...base, + subject: `Issue ${n}`, + from: { address: `editor@${domain}`, name: `List ${list}` }, + to, + headers: { "list-id": domain, ...(list % 2 === 0 ? target(domain, n) : { "list-unsubscribe": true }) }, + } + } + if (slot < 12) { + const shop = Math.floor(n / 20) % 600 + const domain = `shop${shop}.example.com` + return { + ...base, + subject: `Winter sale ${n}: 30% off`, + from: { address: `shop@${domain}`, name: `Shop ${shop}` }, + to, + headers: { ...target(domain, n), "list-unsubscribe-post": true }, + } + } + if (slot < 15) { + const shop = Math.floor(n / 20) % 600 + return { + ...base, + subject: `Thank you for your order #${n}`, + from: { address: `noreply@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-subscriptions-")), "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) + } + await store.people.savePeople({ provider: "telegram", account: "1" }, [ + ...Array.from({ length: 40 }, (_, i) => ({ id: String(100 + i), name: `Helper ${i}`, isBot: true })), + ]) + return { store, path } +} + +say( + `node ${process.version}, N = ${N}, runs = ${RUNS}, load ${loadavg() + .map((x) => x.toFixed(1)) + .join(" ")}`, +) +const { store, path } = await time(`seed ${N} emails`, seeded) +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 emails before the first read: ${untyped()}`) + +{ + const readOnly = await openStore({ path, readOnly: true }) + const page = await time("read-only open, untyped: subscriptions, first 50", () => + readOnly.mail.subscriptions({ limit: 50 }), + ) + say(` ${page.total} groups, first: ${page.items[0]?.key} (${page.items[0]?.emails} emails)`) + await readOnly.close() +} +say(`untyped after the read-only read: ${untyped()}`) + +const writer = await openStore({ path }) +const first = await time(`writable open, first read: types for ${N} emails, then subscriptions, first 50`, () => + writer.mail.subscriptions({ limit: 50 }), +) +say(` untyped after: ${untyped()}, ${first.total} groups`) +const broadcast = Number(db.prepare("SELECT count(*) AS n FROM emails WHERE reach = 'broadcast'").get().n) +say(` broadcast emails: ${broadcast}`) +const withTarget = first.items.filter((item) => item.unsubscribe !== null).length +say( + ` first page: ${withTarget} of ${first.items.length} groups can unsubscribe, one-click: ${first.items.filter((item) => item.unsubscribe?.oneClick).length}`, +) + +await warm("warm subscriptions, first 50", () => writer.mail.subscriptions({ limit: 50 })) +await warm("warm subscriptions, page 10 of 50 (offset 450)", () => + writer.mail.subscriptions({ limit: 50, offset: 450 }), +) +await warm("warm subscriptions, past the end (offset 5000)", () => + writer.mail.subscriptions({ limit: 50, offset: 5000 }), +) +await warm("warm subscriptions, first 500", () => writer.mail.subscriptions({ limit: 500 })) +await warm("warm subscriptions, types [promotion], first 50", () => + writer.mail.subscriptions({ limit: 50, types: ["promotion"] }), +) +await warm("warm subscriptions, last 7 days, first 50", () => + writer.mail.subscriptions({ limit: 50, since: NOW - 7 * 86_400_000 }), +) +await warm("warm mailSenders reach [broadcast], first 50 (for comparison)", () => + writer.mail.mailSenders({ reaches: ["broadcast"], limit: 50 }), +) + +await time("people list: sorts senders into bots", () => writer.people.list({ limit: 0 })) +const bots = await writer.people.listBots({ limit: 1 }) +say(` bots: ${bots.total}`) +for (const sends of [["alerts"], ["receipts"], ["marketing"], ["messenger"], ["alerts", "receipts", "messenger"]]) { + const found = await warm(`warm listBots sends [${sends.join(", ")}], first 50`, () => + writer.people.listBots({ limit: 50, sends }), + ) + say(` ${found.total} bots`) +} +await warm("warm listBots, no filter, first 50", () => writer.people.listBots({ limit: 50 })) +await writer.close() +db.close()