Public: anyone can read this; the crew can change it.

A pull's page answers under 50 ms: its render phase under 15 ms #3370

Parked opened this
Closed without merging

scout/forge:scout/3362-a-pull-s-page-answers-under-50-ms-its-re → main 2 commits fork 2 threads · 2 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +167 −0
src/web/markdown.ts +25
@@ -92,7 +92,32 @@ function cells(line: string, align: string[], tag: "th" | "td"): string {
9292 .join("");
9393 }
9494
95 /**
96 * Rendering is a pure function of the source, and a thread is drawn again on
97 * every visit and every live append, so the last few hundred results are kept.
98 * The value is the same `Html` the first render produced, so output is
99 * byte-identical; only the work is skipped.
100 */
101 const MEMO_MAX = 512;
102 const MEMO_MAX_SOURCE = 20_000;
103 const memo = new Map<string, Html>();

Worst case this holds 512 sources of 20 KB plus their HTML, roughly tens of MB. That is acceptable. A byte or character budget would make the bound explicit if threads get larger.

104
95105 export function renderMarkdown(source: string): Html {
106 if (source.length > MEMO_MAX_SOURCE) return renderUncached(source);
107 const hit = memo.get(source);
108 if (hit !== undefined) {
109 // Re-insert so the Map's order is recency: the oldest key is the first.
110 memo.delete(source);
111 memo.set(source, hit);
112 return hit;
113 }
114 const made = renderUncached(source);
115 if (memo.size >= MEMO_MAX) memo.delete(memo.keys().next().value as string);
116 memo.set(source, made);
117 return made;
118 }
119
120 function renderUncached(source: string): Html {
96121 const text = source.replaceAll("\r\n", "\n");
97122 // Escaping does not move a newline, so the two arrays stay in step: `lines`
98123 // is what prose is built from, `raws` is what the tokenizer needs.
test/pull-render-budget.test.ts added +142
@@ -0,0 +1,142 @@
1 /** A pull's thread remembers its rendered markdown, so a visit is cheap (#3362, pull #3370). */
outdated

The header names #3362 but this is pull #3370. Make sure the reference is the right issue.

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 { addComment } from "../src/actions/issues.ts";
8 import { openPull, submitReview } from "../src/actions/pulls.ts";
9 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
10 import { createRun } from "../src/core/runs.ts";
11 import { createSession } from "../src/core/sessions.ts";
12 import { createUser } from "../src/core/users.ts";
13 import { serve, type Servers } from "../src/http/server.ts";
14 import { quietLogs } from "./helpers/globals.ts";
15
16 quietLogs();
17
18 let dataDir: string;
19 let app: App;
20 let servers: Servers;
21 let cookie = "";
22 let number = 0;
23 const cells = process.env["FORGE_CELLS"];
24
25 const SECTION = [
26 "Looks **good** overall, with `inline code` and a [link](https://example.com/a?b=1&c=2).",
27 "",
28 "- first point <script>alert(1)</script>",
29 "- second point with *emphasis* and #12",
30 "",
31 "```ts",
32 "const x: number = 1;",
33 "```",
34 "",
35 "> quoted *text* with `code`",
36 "",
37 "| a | b |",
38 "| - | - |",
39 "| 1 | **2** |",
40 "",
41 "```js",
42 "function f(a, b) { return a + b; } // add",
43 "for (const k of Object.keys({})) console.log(k, \"s\", 12);",
44 "```",
45 "",
46 "1. one",
47 "2. two with [a](docs/x.md)",
48 ].join("\n");
49 // An agent's comment is several of these: long, with fences, lists and a table.
50 const BODY = Array.from({ length: 4 }, (_, i) => `## Part ${i}\n\n${SECTION}`).join("\n\n");
51
52 beforeAll(async () => {
53 process.env["FORGE_CELLS"] = "off";
54 dataDir = await mkdtemp(join(tmpdir(), "forge-pull-render-"));
55 app = createApp({ dataDir, port: 0 });
56 servers = await serve(app);
57 const mk = async (username: string) => {
58 const made = await createUser(app.db, {
59 origin: "seed",
60 trusted: true,
61 username,
62 email: `${username}@forge.local`,
63 password: "a-good-passphrase",
64 adminIfFirst: true,
65 });
66 if (!made.ok) throw new Error(String(made.error));
67 return made.value;
68 };
69 const alice = await mk("alice");
70 const bob = await mk("bob");
71 cookie = `forge_session=${createSession(app.db, alice.id).token}`;
72 const repo = await createRepo(app, alice, { name: "r", visibility: "public" });
73 if (!repo.ok) throw new Error(String(repo.error));
74 for (const [branch, file] of [[undefined, "forge.json"], ["topic", "t.txt"]] as const) {
75 const done = await commitToBranch(app, alice, repo.value, {
76 ...(branch === undefined ? {} : { branch }),
77 message: "change",
78 files: [{ path: file, content: file === "forge.json" ? "{}" : "x" }],
79 });
80 if (!done.ok) throw new Error(JSON.stringify(done.error));
81 }
82 const pull = await openPull(app, alice, repo.value, {
83 title: "t",
84 body: BODY,
85 baseBranch: "main",
86 headBranch: "topic",
87 });
88 if (!pull.ok) throw new Error(JSON.stringify(pull.error));
89 number = pull.value.number;
90 for (let i = 0; i < 30; i++) {
91 const c = addComment(app, i % 2 ? alice : bob, repo.value, pull.value, `#${i} ${BODY}`);
92 if (!c.ok) throw new Error(JSON.stringify(c.error));
93 }
94 for (const state of ["commented", "commented", "commented"] as const) {
95 const r = await submitReview(app, bob, repo.value, pull.value, { state, body: BODY });
96 if (!r.ok) throw new Error(JSON.stringify(r.error));
97 }
98 for (let i = 0; i < 3; i++) {
99 createRun(app.db, {
100 repoId: repo.value.id,
101 kind: "check",
102 issueId: pull.value.id,
103 env: `pull-${number}`,
104 commitOid: pull.value.headOid,
105 });
106 }
107 });
108
109 afterAll(async () => {
110 await servers.stop();
111 app.close();
112 if (cells === undefined) delete process.env["FORGE_CELLS"];
113 else process.env["FORGE_CELLS"] = cells;
114 await rm(dataDir, { recursive: true, force: true });
115 });
116
117 test("a pull with 30 comments, 3 reviews and 3 check runs renders, and times its phases", async () => {
118 const url = `${servers.url}/alice/r/pulls/${number}`;
119 // Wall-clock budgets flake on a loaded gate; the deterministic proof that a
120 // visit skips the work is the identity test below. Here: it serves, and the
121 // phases are named.
122 const res = await fetch(url, { headers: { cookie } });
123 expect(res.status).toBe(200);
124 await res.arrayBuffer();
125 expect(res.headers.get("server-timing") ?? "").toMatch(/render;dur=[\d.]+/);
126 });
127
128 test("a repeat render of the same source returns the identical Html object", async () => {
129 const { renderMarkdown } = await import("../src/web/markdown.ts");
130 const first = renderMarkdown(BODY);
131 expect(renderMarkdown(BODY)).toBe(first);
132 });
133
134 test("a remembered markdown render is the render, byte for byte, and still escapes", async () => {
135 const { renderMarkdown } = await import("../src/web/markdown.ts");
136 const hostile = "<img src=x onerror=alert(1)> [x](javascript:alert(1)) **b**\n\n```ts\n<b>\n```";
137 const first = String(renderMarkdown(hostile));
138 expect(String(renderMarkdown(hostile))).toBe(first);
139 expect(first).not.toContain("<img");
140 expect(first).not.toContain('href="javascript:');
141 expect(first).toContain("<strong>b</strong>");
142 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs