Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion performance/mail/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ Measurements of reading a mailbox into the shared store with memo.
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).
- [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).
read, and moving 500 senders to bots takes 4–6 s (2026-11-25). Round 2: an undo now puts 500 senders back without
the owner's mark; a loop of writes starved another writer until it paused 2 ms between writes; the commit, not the
number of senders, sets the lock time (2026-11-25).
66 changes: 63 additions & 3 deletions performance/mail/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,73 @@ senders in the same write as the rule; run 4 is the shipped code, which moves th
type again). On a free-mail domain with 30,000 emails it is about 1 s to add and 1.5 to 2.8 s on the next typed read.
Undo costs the same as the add.
- Moving senders is the slow part: each sender goes through `moveToBot`, about 8 to 12 ms each. In one write, 500
senders held the write lock for 4 to 6 s, past the 5 s another writer waits. So the shipped code moves 100 per
write: about 1 s of lock each, the same 5.6 s in total. A sender rule on a free-mail domain with 6,000 senders would
take about a minute in total by the same rate (an estimate, not measured).
senders held the write lock for 4 to 6 s, past the 5 s another writer waits. So the code moves 100 per write, 5.6 s
in total. A sender rule on a free-mail domain with 6,000 senders would take about a minute in total by the same rate
(an estimate, not measured).
- **Correction (round 2, below):** 100 per write alone did not protect another writer. A second writer measured
beside the add still waited the full 5 s and gave up, because the writes ran back to back.

## Round 2: undo and removal after the owner-mark fix

### Why

Three gaps in store version 77, fixed in cli-core branch `fix/mail-rules-owner`: the owner's rules must win across
levels; a removed or undone sender rule put its senders back with the owner as author, so the program never sorted
them again; and removal and undo moved senders back in one write. The owner asked for 100 senders per write, as the
add does, so the lock stays under about 1 s and other writers do not give up after 5 s. The question: what does the
undo of a 500-sender rule cost now, and does another writer get the lock meanwhile?

### Setup

- Same machine and data as above. Load average 8.6 to 15.8; other agents' runs wrote to the same disk.
- A second writer, [lock-probe.mjs](lock-probe.mjs), runs in a worker thread beside steps 5 to 7. It writes through
cli-core's own `write` every 5 ms, as another WireCat command would. That `write` retries the lock every
millisecond for up to 5 s. The probe reports its longest wait and how often it gave up.
- Three builds: `main` at `acb6646` (#3366), the fix with 100 per write and no pause between writes, and the fix with
a 2 ms pause between writes (the shipped one).
- A first version of the probe took the lock with a raw `BEGIN IMMEDIATE` and SQLite's own busy handler, which polls
every 100 ms. WireCat's writers do not work that way, so those three runs are left out.

```sh
node bench.mjs <cli-core checkout> 100000 20000
```

### Results

| Step | `main` (#3366) | fix, no pause | fix, 2 ms pause |
| --- | ---: | ---: | ---: |
| load average at start | 15.8 | 10.5 | 8.6 |
| sender rule: 500 senders moved to bots, 100 per write | 9.5 s | 13.9 s | 6.6 s |
| second writer: longest wait, gave up | 5.0 s, 1 | 5.0 s, 2 | 2.0 s, 0 |
| undo of that rule: 500 senders back | 0.5 s (1 write) | 5.8 s (6 writes) | 3.7 s (6 writes) |
| second writer: longest wait, gave up | 0.2 s, 0 | 4.1 s, 0 | 1.9 s, 0 |
| senders left with the owner's mark after the undo | 500 | 0 | 0 |
| removal of the rule added again: 500 senders back | 0.8 s (1 write) | 1.6 s (6 writes) | 7.0 s (6 writes) |
| second writer: longest wait, gave up | 0.8 s, 0 | 1.3 s, 0 | 2.0 s, 0 |
| senders left with the owner's mark after the removal | 500 | 0 | 0 |

Where one write's time goes, timed once on 20,000 emails and 2,000 senders (load 5.8), for the writes of 100 senders
of an undo and a removal: the work 13 to 23 ms, the store's queued work (stems, mentions) 53 to 68 ms, the `COMMIT`
108 to 3,306 ms.

### What it showed

- **The owner's mark is gone.** On `main`, every one of the 500 senders an undo or a removal put back kept the
owner's mark, so the program would never sort them again. With the fix, none do.
- **Writes back to back starve another writer, however short each is.** The add's 100 per write still made the
second writer give up after 5 s, on `main` too. cli-core's `write` pauses after a commit only when it had to wait
itself, so a loop of writes never left a gap. With a 2 ms pause between writes (what `writeBatch` already does),
no run gave up.
- **The commit, not the number of senders, sets the lock time.** Putting a sender back takes about 0.2 ms; the
commit took 0.1 to 3.3 s on this busy disk. So 100 per write did not bring a write under 1 s here, and an undo in 6
writes took 3.7 to 5.8 s where `main` took 0.5 s in one. In one write, `main`'s undo held the lock 0.2 to 0.8 s.
- Timings that include a commit varied by a factor of 5 between runs (removal 1.6 s and 7.0 s with the same code
path), with the load of other runs.

## Not measured

- Real mail: how often the owner's rules disagree with the classifier on real data.
- A free-mail domain rule that moves its 6,000 senders.
- Mail sync with sender rules live: sync settles the senders of each saved batch, and its cost per batch was not timed.
- Stores over 100,000 emails, and more than 50 rules.
- Round 2: an idle disk, where the commit is short and the chunk size would matter more.
39 changes: 31 additions & 8 deletions performance/mail/rules/bench.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Times cli-core store version 77 mail sorting rules on synthetic mail: what the rule check adds to typing every
// email, and how long a domain rule takes to apply to past mail (a type rule on a company domain and on a free-mail
// domain, and a rule that moves a domain's senders to bots). Usage: node bench.mjs <cli-core checkout> [N] [senders]
// domain, and a rule that moves a domain's senders to bots, then its undo and its removal, with lock-probe.mjs as a
// second writer). Usage: node bench.mjs <cli-core checkout> [N] [senders]
import { mkdtempSync } from "node:fs"
import { loadavg, tmpdir } from "node:os"
import { join } from "node:path"
import { Worker } from "node:worker_threads"

const repo = process.argv[2]
const N = Number(process.argv[3] ?? 100000)
Expand Down Expand Up @@ -130,18 +132,39 @@ await time(" next typed read: retyped", () => store.mail.emails({ types: ["news
await time("undo the gmail.com rule", () => store.mail.sorting.undo("owner"))
await time(" next typed read", () => store.mail.emails({ types: ["newsletter"], limit: 50 }))

// 5. A rule that moves a domain's 500 senders to bots, each through moveToBot.
const bots = await time(`sender rule bigshop.example → bot: ${SHOP} senders moved`, () =>
store.mail.sorting.add({ scope: "domain", value: "bigshop.example", sender: "bot", applyTo: "all" }, "owner"),
// 5. A rule that moves a domain's 500 senders to bots, each through moveToBot, then its undo and its removal, each
// with a second writer beside it that times the longest wait for the write lock.
const probed = async (label, work) => {
const stop = new SharedArrayBuffer(4)
const probe = new Worker(new URL("./lock-probe.mjs", import.meta.url), { workerData: { repo, path, stop } })
const report = new Promise((resolve) => probe.once("message", resolve))
await new Promise((resolve) => setTimeout(resolve, 300))
const result = await time(label, work)
Atomics.store(new Int32Array(stop), 0, 1)
Atomics.notify(new Int32Array(stop), 0)
const { longest, tries, gaveUp } = await report
say(` second writer: longest wait ${Math.round(longest)} ms over ${tries} tries, gave up ${gaveUp}`)
return result
}
const bigshop = { scope: "domain", value: "bigshop.example", sender: "bot", applyTo: "all" }
const bots = await probed(`sender rule bigshop.example → bot: ${SHOP} senders moved`, () =>
store.mail.sorting.add(bigshop, "owner"),
)
say(` moved ${bots.preview.senders}, bots now ${count("SELECT count(*) AS n FROM sender_bots")}`)
await time(" next typed read (bot moves cleared their mail's types)", () =>
store.mail.emails({ types: ["order"], limit: 50 }),
)
await time(" remove it: senders moved back", () => store.mail.sorting.remove(bots.rule.id, "owner"))
say(
` bots holding an identity after the removal: ${count("SELECT count(DISTINCT bot_id) AS n FROM sender_bot_identities")}`,
)
const held = () => count("SELECT count(DISTINCT bot_id) AS n FROM sender_bot_identities")
const ownerMarked = () =>
count(
"SELECT count(*) AS n FROM sender_bot_moves m WHERE m.author = 'owner' AND m.to_bot_id IS NULL " +
"AND m.id = (SELECT max(id) FROM sender_bot_moves WHERE identity_id = m.identity_id)",
)
await probed(" undo it: senders moved back", () => store.mail.sorting.undo("owner"))
say(` bots holding an identity after the undo: ${held()}, senders left with the owner's mark: ${ownerMarked()}`)
const again = await time(" sender rule added again", () => store.mail.sorting.add(bigshop, "owner"))
await probed(" remove it: senders moved back", () => store.mail.sorting.remove(again.rule.id, "owner"))
say(` bots holding an identity after the removal: ${held()}, senders left with the owner's mark: ${ownerMarked()}`)

await store.close()
db.close()
27 changes: 27 additions & 0 deletions performance/mail/rules/lock-probe.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// A second writer beside bench.mjs: it writes through cli-core's own `write` again and again, as another WireCat
// command would (it retries the lock every millisecond, not on SQLite's 100 ms busy handler), and reports the longest
// it waited and how many times it gave up after the store's 5 s busy timeout.
import { join } from "node:path"
import { parentPort, workerData } from "node:worker_threads"

const { repo, path, stop } = workerData
const { openCache } = await import(join(repo, "dist/store/open.js"))
const { write } = await import(join(repo, "dist/store/sqlite/transaction.js"))
const db = await openCache(path)
const flag = new Int32Array(stop)
let longest = 0
let tries = 0
let gaveUp = 0
while (Atomics.load(flag, 0) === 0) {
const start = performance.now()
try {
write(db, () => db.prepare("SELECT 1").get())
} catch {
gaveUp += 1
}
longest = Math.max(longest, performance.now() - start)
tries += 1
Atomics.wait(flag, 0, 0, 5)
}
db.close()
parentPort.postMessage({ longest, tries, gaveUp })