Public: anyone can read this; the crew can change it.
The signed-in home and feed spend 240 to 260 ms on every warm load counting "new" items (0930c49ab) #13630
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13629-the-signed-in-home-and-feed-spend-240-to → main 2 commits fork 2 threads · 2 unresolved
2 files
changed +134
−6
src/feed/last_seen.ts +36 −6
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import type { Database } from "bun:sqlite"; |
| 9 | import { Stamped } from "../kernel/stamped.ts"; | |
| 9 | 10 | import { chipsOf } from "../core/feed_chips.ts"; |
| 10 | 11 | import { dismissedItems, mutedRepos } from "../core/interactions.ts"; |
| 11 | 12 | import type { User } from "../core/users.ts"; |
| @@ -15,21 +16,50 @@ import { viewerCandidates } from "./feed.ts"; | ||
| 15 | 16 | /** Counted up to here; more is "99+", so the read never walks a long absence. */ |
| 16 | 17 | export const NEW_COUNT_CAP = 99; |
| 17 | 18 | |
| 19 | /** | |
| 20 | * How long a count stands when no event has been written: what no event sees (a grant, a watch). | |
| 21 | */ | |
| 22 | export const NEW_COUNT_MAX_AGE_MS = 30_000; | |
| 23 | ||
| 24 | const counts = new WeakMap<Database, Stamped<number>>(); | |
| 25 | ||
| 26 | /** The newest event's place in the log: moves only when something happened, not on every write. */ | |
| 27 | function eventClock(db: Database): string { | |
| 28 | const row = db.query<{ n: number | null }, []>("SELECT MAX(seq) AS n FROM event").get(); | |
| 29 | return String(row?.n ?? 0); | |
| 30 | } | |
| 31 | ||
| 18 | 32 | /** |
| 19 | 33 | * New posts since `since`, as the feed would show them to `viewer`; undefined `since` is a first |
| 20 | * visit. | |
| 34 | * visit. The walk of the viewer's candidates costs far more than a page may spend on one number, | |
| 35 | * and the forge's write clock moves on every write there is, so the count is kept under the event | |
| 36 | * log's own clock instead (docs/CACHING.md): it is read again when an event lands, when the | |
| 37 | * viewer's mutes, dismissals, chips or mark change (they are in the key), and at most | |
| 38 | * `NEW_COUNT_MAX_AGE_MS` after, for what no event says. | |
| 21 | 39 | */ |
| 22 | 40 | export function newSince(db: Database, viewer: User, since: number | undefined, now: number): number { |
| 23 | 41 | if (since === undefined) return 0; |
| 24 | 42 | const muted = mutedRepos(db, viewer.id); |
| 25 | 43 | const dismissed = dismissedItems(db, viewer.id); |
| 26 | 44 | const chips = chipsOf(db, viewer.id); |
| 27 | let n = 0; | |
| 28 | for (const c of viewerCandidates(db, viewer, now, chips, false)) { | |
| 29 | if (Date.parse(c.at) <= since || muted.has(c.repoId) || dismissed.has(c.id) || chipHides(chips, c)) continue; | |
| 30 | if (++n > NEW_COUNT_CAP) break; | |
| 45 | const count = (): number => { | |
| 46 | let n = 0; | |
| 47 | for (const c of viewerCandidates(db, viewer, now, chips, false)) { | |
| 48 | if (Date.parse(c.at) <= since || muted.has(c.repoId) || dismissed.has(c.id) || chipHides(chips, c)) continue; | |
| 49 | if (++n > NEW_COUNT_CAP) break; | |
| 50 | } | |
| 51 | return n; | |
| 52 | }; | |
| 53 | if (db.inTransaction) return count(); | |
| 54 | let cache = counts.get(db); | |
| 55 | if (cache === undefined) { | |
| 56 | cache = new Stamped<number>({ maxEntries: 200, maxAgeMs: NEW_COUNT_MAX_AGE_MS }); | |
| 57 | counts.set(db, cache); | |
| 31 | 58 | } |
| 32 | return n; | |
| 59 | const key = [viewer.id, since, [...muted].sort(), [...dismissed].sort(), [...chips.kinds].sort(), [...chips.people].sort()] | |
| 60 | .map((p) => JSON.stringify(p)) | |
| 61 | .join("|"); | |
| 62 | return cache.get(key, eventClock(db), count); | |
| 33 | 63 | } |
| 34 | 64 | |
| 35 | 65 | /** The feed head's words; no new posts, no line. */ |
test/feed-new-phase.test.ts added +98
| @@ -0,0 +1,98 @@ | ||
| 1 | /** | |
| 2 | * The feed's "new" count stays a kept read, not a walk (#13629): on a forge with 5,000 events and a | |
| 3 | * writer that never stops, a warm signed-in `/feed` spends under NEW_MS on its `new` phase. The | |
| 4 | * count must still move when an event lands, and its clock must be an index read. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createSession } from "../src/core/sessions.ts"; | |
| 13 | import { createUser, type User } from "../src/core/users.ts"; | |
| 14 | import { feedCandidateCache } from "../src/feed/feed.ts"; | |
| 15 | import { newSince } from "../src/feed/last_seen.ts"; | |
| 16 | import { serve } from "../src/http/server.ts"; | |
| 17 | import { promote } from "./helpers/members.ts"; | |
| 18 | ||
| 19 | /** A write that is no event, as the forge makes all day. */ | |
| 20 | const noise = () => | |
| 21 | app.db.query("INSERT INTO home_visit (user_id, at) VALUES (?1, ?2) ON CONFLICT(user_id) DO UPDATE SET at = excluded.at").run(other.id, n++); | |
| 22 | ||
| 23 | /** Ten times what a kept read measures: only a walk of the candidates trips it. */ | |
| 24 | const NEW_MS = 10; | |
| 25 | ||
| 26 | let root = ""; | |
| 27 | let app: App; | |
| 28 | let viewer: User; | |
| 29 | let other: User; | |
| 30 | let repoId = ""; | |
| 31 | let n = 0; | |
| 32 | ||
| 33 | const append = () => | |
| 34 | app.bus.append({ kind: "run.finished", repoId, actorId: viewer.id, payload: { kind: "deploy", state: "failed", summary: `w ${n++}`, runId: `r${n}` } }); | |
| 35 | ||
| 36 | beforeAll(async () => { | |
| 37 | root = mkdtempSync(join(tmpdir(), "forge-new-phase-")); | |
| 38 | app = createApp({ dataDir: root, port: 0 }); | |
| 39 | const mk = async (username: string, adminIfFirst: boolean) => { | |
| 40 | const r = await createUser(app.db, { origin: "seed", trusted: true, username, allowReserved: true, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst }); | |
| 41 | if (!r.ok) throw new Error(r.error); | |
| 42 | return r.value; | |
| 43 | }; | |
| 44 | await mk("root", true); | |
| 45 | viewer = await mk("alice", false); | |
| 46 | promote(app.db, viewer); | |
| 47 | other = await mk("bob", false); | |
| 48 | const repo = await createRepo(app, viewer, { name: "open", visibility: "public" }); | |
| 49 | if (!repo.ok) throw new Error(JSON.stringify(repo)); | |
| 50 | repoId = repo.value.id; | |
| 51 | app.db.transaction(() => { | |
| 52 | for (let i = 0; i < 5000; i++) append(); | |
| 53 | })(); | |
| 54 | app.db.query("INSERT INTO home_visit (user_id, at) VALUES (?1, ?2)").run(viewer.id, Date.now() - 3_600_000); | |
| 55 | }); | |
| 56 | afterAll(() => { | |
| 57 | app.close(); | |
| 58 | rmSync(root, { recursive: true, force: true }); | |
| 59 | }); | |
| 60 | ||
| 61 | test("the count's clock is a covering index read", () => { | |
| 62 | const plan = app.db.query("EXPLAIN QUERY PLAN SELECT MAX(seq) AS n FROM event").all() as { detail: string }[]; | |
| 63 | expect(plan.map((p) => p.detail).join("\n")).not.toMatch(/^SCAN (?!.*USING (COVERING )?INDEX)/m); | |
| 64 | }); | |
| 65 | ||
| 66 | test("the count is kept across writes that are no event, and moves when an event lands", () => { | |
| 67 | const since = Date.now() - 60_000; | |
| 68 | const first = newSince(app.db, viewer, since, Date.now()); | |
| 69 | noise(); | |
| 70 | const walks = feedCandidateCache(app.db).misses; | |
| 71 | expect(newSince(app.db, viewer, since, Date.now())).toBe(first); | |
| 72 | expect(feedCandidateCache(app.db).misses).toBe(walks); // no walk of the candidates | |
| 73 | append(); | |
| 74 | const after = newSince(app.db, viewer, since, Date.now()); | |
| 75 | expect(feedCandidateCache(app.db).misses).toBe(walks + 1); // an event lands: read again | |
| 76 | expect(after).toBeGreaterThanOrEqual(first); | |
| 77 | }); | |
| 78 | ||
| 79 | test("a warm signed-in /feed on a forge that keeps writing spends under the budget on `new`", async () => { | |
| 80 | const servers = await serve(app); | |
| 81 | try { | |
| 82 | const cookie = `forge_session=${createSession(app.db, viewer.id).token}`; | |
| 83 | const phase = async (): Promise<number | undefined> => { | |
| 84 | noise(); | |
| 85 | const res = await fetch(`${servers.url}/feed`, { headers: { cookie, "sec-fetch-dest": "empty" } }); | |
| 86 | expect(res.status).toBe(200); | |
| 87 | await res.arrayBuffer(); | |
| 88 | const m = /(?:^|, )new;dur=([\d.]+)/.exec(res.headers.get("server-timing") ?? ""); | |
| 89 | return m === null ? undefined : Number(m[1]); | |
| 90 | }; | |
| 91 | await phase(); | |
| 92 | const warm = [await phase(), await phase(), await phase()].filter((x): x is number => x !== undefined); | |
| 93 | expect(warm.length).toBeGreaterThan(0); | |
| 94 | expect(Math.min(...warm)).toBeLessThan(NEW_MS); | |
| 95 | } finally { | |
| 96 | servers.stop(); | |
| 97 | } | |
This 10 ms assertion passes without the fix, so it proves nothing. Make the fixture heavy enough to fail unfixed, or remove it and rely on the miss-count test. |
||
| 98 | }); |
new above
offline
Import order: put the ../kernel/stamped.ts import after the ../core/* imports, as the file did before.