Public: anyone can read this; the crew can change it.
A pull's Files page answers a 40-file diff in under 100 ms on a repeat view #4045
scout/forge:scout/4043-a-pull-s-files-page-answers-a-40-file-di → main 1 commit fork
4 files
changed +196
−1
src/web/diff-cache.ts added +42
| @@ -0,0 +1,42 @@ | ||
| 1 | /** | |
| 2 | * What a pull's Files page remembers per head (#4043): the highlighted | |
| 3 | * tokens and each file's rendered HTML. Keys carry the repository directory, | |
| 4 | * the merge base and the head oid, so a moved head finds nothing of the old | |
| 5 | * one. Only what is the same for every viewer is kept here — a file with a | |
| 6 | * review comment or an open form is never remembered, and the links a | |
| 7 | * signed-in viewer sees are part of the key. Bounded, least recently used. | |
| 8 | */ | |
| 9 | export class Lru<V> { | |
| 10 | private readonly map = new Map<string, V>(); | |
| 11 | constructor(private readonly max: number) {} | |
| 12 | ||
| 13 | get(key: string): V | undefined { | |
| 14 | const hit = this.map.get(key); | |
| 15 | if (hit === undefined) return undefined; | |
| 16 | this.map.delete(key); | |
| 17 | this.map.set(key, hit); | |
| 18 | return hit; | |
| 19 | } | |
| 20 | ||
| 21 | set(key: string, value: V): void { | |
| 22 | this.map.delete(key); | |
| 23 | if (this.map.size >= this.max) { | |
| 24 | const oldest = this.map.keys().next(); | |
| 25 | if (!oldest.done) this.map.delete(oldest.value); | |
| 26 | } | |
| 27 | this.map.set(key, value); | |
| 28 | } | |
| 29 | ||
| 30 | get size(): number { | |
| 31 | return this.map.size; | |
| 32 | } | |
| 33 | ||
| 34 | clear(): void { | |
| 35 | this.map.clear(); | |
| 36 | } | |
| 37 | } | |
| 38 | ||
| 39 | /** A few heads' worth of highlighted diffs. */ | |
| 40 | export const syntaxMemo = new Lru<import("./diff-view.ts").SyntaxMap>(32); | |
| 41 | /** About ten 40-file heads of rendered files. */ | |
| 42 | export const fileMemo = new Lru<import("./html.ts").Html>(512); |
src/web/diff-view.ts +14
| @@ -23,6 +23,7 @@ import type { Token } from "./highlight.ts"; | ||
| 23 | 23 | import { escapeHtml, html, raw, when, type Html } from "./html.ts"; |
| 24 | 24 | import { icon, time, userLink } from "./ui.ts"; |
| 25 | 25 | import { renderMarkdown } from "./markdown.ts"; |
| 26 | import { fileMemo } from "./diff-cache.ts"; | |
| 26 | 27 | import { wordDiff, type Segment } from "./worddiff.ts"; |
| 27 | 28 | |
| 28 | 29 | /** Highlighted tokens per side for a file, keyed by path in a SyntaxMap. */ |
| @@ -71,6 +72,9 @@ export interface DiffViewOptions { | ||
| 71 | 72 | * `/owner/repo/blob/<oid>` — the file's path is appended. Omit to render |
| 72 | 73 | * the stub without a link (a live/origin diff has no local blob route). */ |
| 73 | 74 | fileLinkBase?: string; |
| 75 | /** Names the diff (directory, merge base, head). When set, a file with no | |
| 76 | * review on it is remembered under it; leave unset to remember nothing. */ | |
| 77 | memo?: string; | |
| 74 | 78 | } |
| 75 | 79 | |
| 76 | 80 | /** Per-file render context, resolved once per file. */ |
| @@ -517,6 +521,16 @@ function fileHeader(file: FileDiff): Html { | ||
| 517 | 521 | } |
| 518 | 522 | |
| 519 | 523 | function fileView(file: FileDiff, opts: DiffViewOptions): Html { |
| 524 | if (opts.memo === undefined || fileHasReview(file, opts)) return drawFile(file, opts); | |
| 525 | const key = [opts.memo, file.path, opts.reviewBase, opts.reviewPage, opts.fileLinkBase].join("\0"); | |
| 526 | const hit = fileMemo.get(key); | |
| 527 | if (hit) return hit; | |
| 528 | const made = drawFile(file, opts); | |
| 529 | fileMemo.set(key, made); | |
| 530 | return made; | |
| 531 | } | |
| 532 | ||
| 533 | function drawFile(file: FileDiff, opts: DiffViewOptions): Html { | |
| 520 | 534 | // Generated/lockfiles start collapsed; everything else is open. A file with |
| 521 | 535 | // an active comment or existing comments is forced open so it's visible. |
| 522 | 536 | const ctx: RowCtx = { |
src/web/pages/pull.ts +8 −1
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * it needs. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { syntaxMemo } from "../diff-cache.ts"; | |
| 11 | 12 | import { askCheck } from "../../checks/again.ts"; |
| 12 | 13 | import { addCommentThrough } from "../../actions/issues.ts"; |
| 13 | 14 | import type { AccessError } from "../../actions/access.ts"; |
| @@ -809,7 +810,12 @@ export async function pullFilesPage(ctx: Ctx): Promise<Response> { | ||
| 809 | 810 | db: ctx.app.db, |
| 810 | 811 | requireReview: repo.requireReview, |
| 811 | 812 | }); |
| 812 | const syntax = await diffSyntax(dir, diff, from, status.headOid); | |
| 813 | const memo = [dir, from, status.headOid].join("\0"); | |
| 814 | let syntax = syntaxMemo.get(memo); | |
| 815 | if (!syntax) { | |
| 816 | syntax = await diffSyntax(dir, diff, from, status.headOid); | |
| 817 | syntaxMemo.set(memo, syntax); | |
| 818 | } | |
| 813 | 819 | // A merged or closed pull is history; the wall is a question about landing. |
| 814 | 820 | const wall = |
| 815 | 821 | pull.mergedAt === null && pull.state === "open" |
| @@ -840,6 +846,7 @@ export async function pullFilesPage(ctx: Ctx): Promise<Response> { | ||
| 840 | 846 | : undefined; |
| 841 | 847 | const diffOptions: DiffViewOptions = { |
| 842 | 848 | syntax, |
| 849 | memo, | |
| 843 | 850 | comments: reviewComments, |
| 844 | 851 | threads: threadViews, |
| 845 | 852 | marks: marksByName(ctx.app.db, [ |
test/pull-files-cache.test.ts added +132
| @@ -0,0 +1,132 @@ | ||
| 1 | /** A pull's Files page answers a 40-file diff fast on a repeat view, from a cache keyed by head (#4043). */ | |
| 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 { openPull } from "../src/actions/pulls.ts"; | |
| 8 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 9 | import { createSession } from "../src/core/sessions.ts"; | |
| 10 | import { createUser } from "../src/core/users.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | let dataDir: string; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | let alice: { id: number }; | |
| 20 | let cookie = ""; | |
| 21 | let bobCookie = ""; | |
| 22 | let number = 0; | |
| 23 | let repoValue: Awaited<ReturnType<typeof createRepo>> extends { ok: true; value: infer V } ? V : never; | |
| 24 | const cells = process.env["FORGE_CELLS"]; | |
| 25 | ||
| 26 | const source = (i: number, tag: string) => | |
| 27 | Array.from( | |
| 28 | { length: 60 }, | |
| 29 | (_, n) => `export function f${i}_${n}_${tag}(a: number, b: string): string { return b + a + "${tag}"; } // line ${n}`, | |
| 30 | ).join("\n") + "\n"; | |
| 31 | ||
| 32 | const files = (tag: string) => | |
| 33 | Array.from({ length: 40 }, (_, i) => ({ path: `src/m${i}.ts`, content: source(i, tag) })); | |
| 34 | ||
| 35 | beforeAll(async () => { | |
| 36 | process.env["FORGE_CELLS"] = "off"; | |
| 37 | dataDir = await mkdtemp(join(tmpdir(), "forge-pull-files-cache-")); | |
| 38 | app = createApp({ dataDir, port: 0 }); | |
| 39 | servers = await serve(app); | |
| 40 | const mk = async (username: string) => { | |
| 41 | const made = await createUser(app.db, { | |
| 42 | origin: "seed", | |
| 43 | trusted: true, | |
| 44 | username, | |
| 45 | email: `${username}@forge.local`, | |
| 46 | password: "a-good-passphrase", | |
| 47 | adminIfFirst: true, | |
| 48 | }); | |
| 49 | if (!made.ok) throw new Error(String(made.error)); | |
| 50 | return made.value; | |
| 51 | }; | |
| 52 | const a = await mk("alice"); | |
| 53 | const bob = await mk("bob"); | |
| 54 | alice = a; | |
| 55 | cookie = `forge_session=${createSession(app.db, a.id).token}`; | |
| 56 | bobCookie = `forge_session=${createSession(app.db, bob.id).token}`; | |
| 57 | const repo = await createRepo(app, a, { name: "r", visibility: "public" }); | |
| 58 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 59 | repoValue = repo.value; | |
| 60 | const base = await commitToBranch(app, a, repo.value, { | |
| 61 | message: "base", | |
| 62 | files: [{ path: "forge.json", content: "{}" }], | |
| 63 | }); | |
| 64 | if (!base.ok) throw new Error(JSON.stringify(base.error)); | |
| 65 | const head = await commitToBranch(app, a, repo.value, { | |
| 66 | branch: "topic", | |
| 67 | message: "forty files", | |
| 68 | files: files("one"), | |
| 69 | }); | |
| 70 | if (!head.ok) throw new Error(JSON.stringify(head.error)); | |
| 71 | const pull = await openPull(app, a, repo.value, { | |
| 72 | title: "t", | |
| 73 | body: "", | |
| 74 | baseBranch: "main", | |
| 75 | headBranch: "topic", | |
| 76 | }); | |
| 77 | if (!pull.ok) throw new Error(JSON.stringify(pull.error)); | |
| 78 | number = pull.value.number; | |
| 79 | }); | |
| 80 | ||
| 81 | afterAll(async () => { | |
| 82 | await servers.stop(); | |
| 83 | app.close(); | |
| 84 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 85 | else process.env["FORGE_CELLS"] = cells; | |
| 86 | await rm(dataDir, { recursive: true, force: true }); | |
| 87 | }); | |
| 88 | ||
| 89 | const get = async (c: string) => { | |
| 90 | const started = performance.now(); | |
| 91 | const res = await fetch(`${servers.url}/alice/r/pulls/${number}/files`, { | |
| 92 | headers: c ? { cookie: c } : {}, | |
| 93 | }); | |
| 94 | const text = await res.text(); | |
| 95 | return { status: res.status, text, ms: performance.now() - started }; | |
| 96 | }; | |
| 97 | ||
| 98 | test("a repeat view of a 40-file diff answers under 100 ms", async () => { | |
| 99 | const cold = await get(cookie); | |
| 100 | expect(cold.status).toBe(200); | |
| 101 | expect(cold.text).toContain("src/m39.ts"); | |
| 102 | const times: number[] = []; | |
| 103 | for (let i = 0; i < 5; i++) times.push((await get(cookie)).ms); | |
| 104 | console.log("cold", cold.ms, "repeat", times); | |
| 105 | expect([...times].sort((a, b) => a - b)[2]!).toBeLessThan(100); | |
| 106 | // Defensible ceiling for the cold view on a loaded machine. | |
| 107 | expect(cold.ms).toBeLessThan(800); | |
| 108 | }); | |
| 109 | ||
| 110 | test("a second head does not see the first head's HTML", async () => { | |
| 111 | const first = await get(cookie); | |
| 112 | expect(first.text).toContain("f1_1_one"); | |
| 113 | const moved = await commitToBranch(app, alice as never, repoValue, { | |
| 114 | branch: "topic", | |
| 115 | message: "again", | |
| 116 | files: files("two"), | |
| 117 | }); | |
| 118 | if (!moved.ok) throw new Error(JSON.stringify(moved.error)); | |
| 119 | const second = await get(cookie); | |
| 120 | expect(second.text).toContain("f1_1_two"); | |
| 121 | expect(second.text).not.toContain("f1_1_one"); | |
| 122 | expect(second.text).not.toContain("f1_1_one"); | |
| 123 | }); | |
| 124 | ||
| 125 | test("nothing viewer-specific is shared: a signed-out view has no review links, and threads are not leaked", async () => { | |
| 126 | const signed = await get(cookie); | |
| 127 | expect(signed.text).toContain("?rc="); | |
| 128 | const anon = await get(""); | |
| 129 | expect(anon.text).not.toContain("?rc="); | |
| 130 | const bobView = await get(bobCookie); | |
| 131 | expect(bobView.text).toContain("?rc="); | |
| 132 | }); |
new above
offline