Public: anyone can read this; the crew can change it.
An issues list reads the pulls that close its rows in one query: `closing` under 3 ms #6262
scout/forge:scout/6204-an-issues-list-reads-the-pulls-that-clos → main 3 commits fork 1 thread · 1 unresolved
3 files
changed +117
−2
src/core/pulls.ts +35
| @@ -1119,6 +1119,41 @@ export function openPullsClosing( | ||
| 1119 | 1119 | return out; |
| 1120 | 1120 | } |
| 1121 | 1121 | |
| 1122 | /** | |
| 1123 | * `openPullsClosing` for a page: only the open pulls whose description | |
| 1124 | * mentions one of `numbers`, found in one query keyed by those numbers, so | |
| 1125 | * the read costs the page and not every open pull of the repository. Each | |
| 1126 | * number maps to its newest open pull, as in `openPullsClosing`. | |
| 1127 | */ | |
| 1128 | export function openPullsClosingAmong( | |
| 1129 | db: Database, | |
| 1130 | repoId: string, | |
| 1131 | numbers: readonly number[], | |
| 1132 | ): Map<number, Pull> { | |
| 1133 | const out = new Map<number, Pull>(); | |
| 1134 | if (numbers.length === 0) return out; | |
| 1135 | const ids = db | |
| 1136 | .query<{ id: string }, [string, string]>( | |
| 1137 | `SELECT issue.id AS id FROM issue | |
| 1138 | WHERE issue.repo_id = ?1 AND issue.kind = 'pull' AND issue.state = 'open' | |
| 1139 | AND EXISTS (SELECT 1 FROM json_each(?2) AS n | |
| 1140 | WHERE issue.body LIKE '%#' || n.value || '%') | |
| 1141 | ORDER BY issue.number DESC LIMIT 200`, | |
| 1142 | ) | |
| 1143 | .all(repoId, JSON.stringify(numbers)) | |
| 1144 | .map((r) => r.id); | |
| 1145 | const issues = getIssuesByIds(db, ids); | |
| 1146 | const wanted = new Set(numbers); | |
| 1147 | const ordered = ids.flatMap((id) => { | |
| 1148 | const issue = issues.get(id); | |
| 1149 | return issue === undefined ? [] : [issue]; | |
| 1150 | }); | |
| 1151 | for (const pull of joinMany(db, ordered)) | |
| 1152 | for (const n of closesFromBody(pull.body)) | |
| 1153 | if (wanted.has(n) && !out.has(n)) out.set(n, pull); | |
| 1154 | return out; | |
| 1155 | } | |
| 1156 | ||
| 1122 | 1157 | /** |
| 1123 | 1158 | * The person whose words a pull answers: the author of the issue it closes |
| 1124 | 1159 | * (or continues), when that is a person. Derived from the body and the issue |
src/web/studio/lists.ts +6 −2
| @@ -19,7 +19,7 @@ import { | ||
| 19 | 19 | type Issue, |
| 20 | 20 | type Label, |
| 21 | 21 | } from "../../core/issues.ts"; |
| 22 | import { listPulls, openPullsClosing } from "../../core/pulls.ts"; | |
| 22 | import { listPulls, openPullsClosingAmong } from "../../core/pulls.ts"; | |
| 23 | 23 | import { canRead, type Repo } from "../../core/repos.ts"; |
| 24 | 24 | import { waitingCount } from "../../actions/reports.ts"; |
| 25 | 25 | |
| @@ -348,7 +348,11 @@ export async function tellsList(ctx: Ctx): Promise<Response> { | ||
| 348 | 348 | ); |
| 349 | 349 | notePhase(ctx.request, "issues", performance.now() - at); |
| 350 | 350 | at = performance.now(); |
| 351 | const closingPulls = openPullsClosing(db, repo.id); | |
| 351 | const closingPulls = openPullsClosingAmong( | |
| 352 | db, | |
| 353 | repo.id, | |
| 354 | fetched.filter((i) => i.state !== "closed").map((i) => i.number), | |
| 355 | ); | |
| 352 | 356 | knowMergeClosings( |
| 353 | 357 | wordCtx, |
| 354 | 358 | repo.id, |
test/issues-list-closing.test.ts added +76
| @@ -0,0 +1,76 @@ | ||
| 1 | /** The issues list reads the pulls that close its rows in one query (#6204, #6262). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { createIssue } from "../src/core/issues.ts"; | |
| 9 | import { openPullsClosing, openPullsClosingAmong } from "../src/core/pulls.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 12 | import { statements } from "./helpers/statements.ts"; | |
| 13 | ||
| 14 | let dataDir = ""; | |
| 15 | let app: App, servers: Servers, repoId = ""; | |
| 16 | const numbers: number[] = []; | |
| 17 | ||
| 18 | beforeAll(async () => { | |
| 19 | dataDir = await mkdtemp(join(tmpdir(), "forge-closing-")); | |
| 20 | app = createApp({ dataDir, port: 0 }); | |
| 21 | servers = await serve(app); | |
| 22 | const admin = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" }); | |
| 23 | if (!admin.ok) throw new Error(admin.error); | |
| 24 | const made = await createRepo(app, admin.value, { name: "notes", visibility: "public", description: "" }); | |
| 25 | if (!made.ok) throw new Error(String(made.error)); | |
| 26 | repoId = made.value.id; | |
| 27 | const db = app.db; | |
| 28 | db.transaction(() => { | |
| 29 | for (let i = 0; i < 50; i++) { | |
| 30 | const issue = createIssue(db, { repoId, authorId: admin.value.id, title: `tell ${i}` }); | |
| 31 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 32 | numbers.push(issue.value.number); | |
| 33 | for (let k = 0; k <= i % 3; k++) { | |
| 34 | const pull = createIssue(db, { | |
| 35 | repoId, authorId: admin.value.id, title: `pull ${i}.${k}`, kind: "pull", | |
| 36 | body: `Closes #${issue.value.number}`, | |
| 37 | }); | |
| 38 | if (!pull.ok) throw new Error(String(pull.error)); | |
| 39 | db.query( | |
| 40 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id) | |
| 41 | VALUES (?1, 'main', ?2, 'a', 'b', ?3)`, | |
| 42 | ).run(pull.value.id, `b${i}-${k}`, repoId); | |
| 43 | } | |
| 44 | } | |
| 45 | })(); | |
| 46 | }); | |
| 47 | ||
| 48 | afterAll(async () => { | |
| 49 | servers.stop(); | |
| 50 | app.close(); | |
| 51 | await rm(dataDir, { recursive: true, force: true }); | |
| 52 | }); | |
| 53 | ||
| 54 | test("the page-keyed read names the same pull per issue as the whole-repository read", () => { | |
| 55 | const whole = openPullsClosing(app.db, repoId); | |
| 56 | const keyed = openPullsClosingAmong(app.db, repoId, numbers); | |
| 57 | expect(keyed.size).toBe(50); | |
| 58 | for (const n of numbers) expect(keyed.get(n)?.number).toBe(whole.get(n)?.number); | |
| 59 | expect(openPullsClosingAmong(app.db, repoId, []).size).toBe(0); | |
| 60 | expect(openPullsClosingAmong(app.db, repoId, [999999]).size).toBe(0); | |
| 61 | }); | |
| 62 | ||
| 63 | test("the keyed read costs a few statements, however many issues the page has", async () => { | |
| 64 | const few = await statements(app.db, () => void openPullsClosingAmong(app.db, repoId, numbers.slice(0, 5))); | |
| 65 | const many = await statements(app.db, () => void openPullsClosingAmong(app.db, repoId, numbers)); | |
| 66 | // Candidates, their issues, their pull rows: never a read per row. | |
| 67 | expect(many).toBeLessThanOrEqual(3); | |
| 68 | expect(many).toBe(few); | |
| 69 | }); | |
| 70 | ||
| 71 | test("the list page names `closing` in Server-Timing and still words every row", async () => { | |
| 72 | const res = await fetch(`${servers.url}/alice/notes/issues`); | |
| 73 | expect(res.status).toBe(200); | |
| 74 | expect(res.headers.get("server-timing") ?? "").toContain("closing"); | |
| 75 | expect(await res.text()).toContain("tell 49"); | |
| 76 | }); |
new above
offline
Wall-clock bound of 3 ms is flake-prone and may not fail on the old code; prove the old path fails or assert on work done.