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

The next-up page runs 70 statements, over its 67 budget #15955

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15940-the-next-up-page-runs-70-statements-over → main 0 commits fork

  • Merged. scout/15940-the-next-up-page-runs-70-statements-over was merged into main as 1bc52e6.

Review

Sign in to review this change.
14 files changed +101 −19
src/actions/runs.ts +3 −1
@@ -109,9 +109,11 @@ export function mayReadRuns(
109109 app: App,
110110 actor: User | undefined,
111111 repo: Repo,
112 /** The repository's app when the caller has already read it (`undefined` inside: it has none). */
113 known?: { app: ReturnType<typeof getAppByRepo> },
112114 ): boolean {
113115 if (!requireReader(app, actor, repo).ok) return false;
114 const record = getAppByRepo(app.db, repo.id);
116 const record = known !== undefined ? known.app : getAppByRepo(app.db, repo.id);
115117 return (
116118 record === undefined ||
117119 canEnterApp(app.db, repo, record, actor, { named: false })
src/core/repos.ts +4 −2
@@ -969,6 +969,8 @@ export interface ListReposOptions {
969969 onlyArchived?: boolean;
970970 /** Only archived repositories. @default false */
971971 archivedOnly?: boolean;
972 /** Skip the open counts (one read per list) for a caller that never shows them. @default false */
973 withoutCounts?: boolean;
972974 /** Only this owner's repositories (a username, any case). */
973975 owner?: string;
974976 /** Only repositories an admin marked as a template for new forges (#12431). */
@@ -1036,7 +1038,7 @@ export function listReposVisibleTo(
10361038 )
10371039 .all(...params)
10381040 .map(toRepo);
1039 return withCounts(db, rows);
1041 return options.withoutCounts === true ? rows : withCounts(db, rows);
10401042 }
10411043
10421044 /**
@@ -1148,7 +1150,7 @@ export function listReposWritableTo(
11481150 )
11491151 .all(...params)
11501152 .map(toRepo);
1151 return withCounts(db, rows);
1153 return options.withoutCounts === true ? rows : withCounts(db, rows);
11521154 }
11531155
11541156 /** Repos owned by one user or org, visible to the viewer. */
src/http/archived_noindex.ts +3 −2
@@ -11,11 +11,12 @@
1111 import type { Database } from "bun:sqlite";
1212 import { canRead, getRepo } from "../core/repos.ts";
1313 import type { User } from "../core/users.ts";
14 import { repoOnRequest } from "./request_repo.ts";
1415
1516 /** Whether the route's owner/name is an archived repository the caller may read. */
16 export function archivedRepoAt(db: Database, params: Record<string, string>, user: User | undefined): boolean {
17 export function archivedRepoAt(db: Database, params: Record<string, string>, user: User | undefined, url?: URL): boolean {
1718 const { owner, name } = params;
1819 if (owner === undefined || name === undefined) return false;
19 const repo = getRepo(db, owner, name);
20 const repo = url === undefined ? getRepo(db, owner, name) : repoOnRequest(db, url, "GET", owner, name);
2021 return repo !== undefined && repo.archivedAt !== null && canRead(db, repo, user);
2122 }
src/http/request_repo.ts added +22
@@ -0,0 +1,22 @@
1 /**
2 * The repository a page's address names, read once per request. A repository page asks for it
3 * from the archived check, the loader, the palette and the head links; each read is a join with
4 * the origin. Only a GET is remembered: a mutation changes the row mid-request. The memo is keyed
5 * on the request's URL object, so it ends with the request.
6 */
7 import type { Database } from "bun:sqlite";
8 import { getRepo, type Repo } from "../core/repos.ts";
9
10 const seen = new WeakMap<URL, Map<string, Repo | undefined>>();
11
12 /**
13 * `getRepo`, remembered for the rest of this GET request; `url` is the request's own URL object.
14 */
15 export function repoOnRequest(db: Database, url: URL, method: string, owner: string, name: string): Repo | undefined {
16 if (method !== "GET" && method !== "HEAD") return getRepo(db, owner, name);
17 let byName = seen.get(url);
18 if (byName === undefined) seen.set(url, (byName = new Map()));
19 const key = `${owner}/${name}`;
20 if (!byName.has(key)) byName.set(key, getRepo(db, owner, name));
21 return byName.get(key);
22 }
src/http/server.ts +1 −1
@@ -3362,7 +3362,7 @@ async function answer(
33623362 if (clean !== undefined) return clean;
33633363 }
33643364 // A page under an archived repository is not indexed (`archived_noindex.ts`).
3365 const noindex = !isApiPath && routeMethod === "GET" && archivedRepoAt(app.db, matched.params, auth.user);
3365 const noindex = !isApiPath && routeMethod === "GET" && archivedRepoAt(app.db, matched.params, auth.user, url);
33663366 const invoke = (into: Request): Response | Promise<Response> =>
33673367 matched.handler({
33683368 app,
src/perf/page_budgets/next.ts +1 −1
@@ -1,3 +1,3 @@
11 import type { PageBudget } from "../page_budget.ts";
22
3 export const budget: PageBudget = { page: "next up", pattern: "/:owner/:name/next", medianMs: 50, statements: 72 };
3 export const budget: PageBudget = { page: "next up", pattern: "/:owner/:name/next", medianMs: 50, statements: 66 };
src/perf/page_budgets/space.ts +2 −2
@@ -1,5 +1,5 @@
11 import type { PageBudget } from "../page_budget.ts";
22
3 // The space (#12770), which the owner opens to watch growth. Statements: measured warm (15) plus
3 // The space (#12770), which the owner opens to watch growth. Statements: measured warm (12) plus
44 // two.
5 export const budget: PageBudget = { page: "space", pattern: "/:owner/:name/space", medianMs: 50, statements: 17 };
5 export const budget: PageBudget = { page: "space", pattern: "/:owner/:name/space", medianMs: 50, statements: 14 };
src/web/chrome.ts +4 −2
@@ -8,6 +8,7 @@
88 * `ui.ts` may not. That is the line between the two files.
99 */
1010
11 import { repoOnRequest } from "../http/request_repo.ts";
1112 import { hereAddress, returnTo, withReturn } from "./return_to.ts";
1213 import { MACHINE_PREFIX, typedMiss } from "../http/asset_miss.ts";
1314 import { audienceFacts, audienceOf } from "../core/audience.ts";
@@ -16,7 +17,6 @@ import {
1617 canAdmin,
1718 canRead,
1819 forkParentFor,
19 getRepo,
2020 } from "../core/repos.ts";
2121 import { nearRepos as nearTo } from "../core/near_repos.ts";
2222 import { type Repo, visibleForks } from "../core/repos.ts";
@@ -52,8 +52,10 @@ import { BIND, zeroAttr } from "./bind.ts";
5252 /** The repo, if the viewer may see it. Missing and hidden are the same
5353 * answer on purpose, so a private repo never leaks its existence. */
5454 export function loadRepo(ctx: Ctx): Repo | undefined {
55 const repo = getRepo(
55 const repo = repoOnRequest(
5656 ctx.app.db,
57 ctx.url,
58 ctx.request.method,
5759 ctx.params.owner ?? "",
5860 (ctx.params.name ?? "").replace(/\.git$/, ""),
5961 );
src/web/layout.ts +3 −2
@@ -7,6 +7,7 @@
77 * `/search`), and every dialog (a link to a real page).
88 */
99
10 import { repoOnRequest } from "../http/request_repo.ts";
1011 import { requestHolds } from "../http/conditional.ts";
1112 import { sshOn } from "../settings/ssh.ts";
1213 import { hereAddress, returnTo, withReturn } from "./return_to.ts";
@@ -456,7 +457,7 @@ function pageRepo(ctx: Ctx): Repo | undefined {
456457 if (ctx.auth.user === undefined) return undefined;
457458 const [owner, name] = ctx.url.pathname.split("/").filter((s) => s !== "");
458459 return owner !== undefined && name !== undefined && !NOT_AN_OWNER.has(owner)
459 ? getRepo(ctx.app.db, owner, name)
460 ? repoOnRequest(ctx.app.db, ctx.url, ctx.request.method, owner, name)
460461 : undefined;
461462 }
462463
@@ -490,7 +491,7 @@ function paletteIndex(ctx: Ctx, extra: PaletteItem[]): Html {
490491 // The viewer's own first, apps ahead of the rest in each half, thirty in
491492 // all; archived ones
492493 // are put away, found by typing the full name into search.
493 const wanted = { limit: 30, appsFirst: true, withoutArchived: true };
494 const wanted = { limit: 30, appsFirst: true, withoutArchived: true, withoutCounts: true };
494495 const mine = ctx.auth.user
495496 ? listReposByOwner(ctx.app.db, ctx.auth.user.username, ctx.auth.user, wanted)
496497 : [];
src/web/repo_steps.ts +1 −1
@@ -104,7 +104,7 @@ function work(ctx: Ctx, base: string, repo: Repo | undefined): Steps {
104104 ...(forks > 0 ? { count: forks } : {}),
105105 });
106106 // The runs, for who may read them (a private app's are for those who may enter it).
107 if (mayReadRuns(ctx.app, ctx.auth.user, repo)) {
107 if (mayReadRuns(ctx.app, ctx.auth.user, repo, { app: record })) {
108108 // The number of the list behind the tab, for this reader (`workingNowCount`).
109109 const running = workingNowCount(ctx.app, ctx.auth.user, repo);
110110 steps.push({
src/web/standards.ts +2 −1
@@ -29,6 +29,7 @@
2929 * FORGE_SECURITY_CONTACT.
3030 */
3131
32 import { repoOnRequest } from "../http/request_repo.ts";
3233 import { noneMatch } from "../http/conditional.ts";
3334 import { sshOn } from "../settings/ssh.ts";
3435 import { UPSTREAM_REPO } from "../core/upstream.ts";
@@ -721,7 +722,7 @@ export function pageLinks(ctx: Ctx): PageLink[] {
721722 name !== undefined &&
722723 ctx.url.pathname.startsWith(`/${owner}/${name}`)
723724 ) {
724 const repo = getRepo(ctx.app.db, owner, name.replace(/\.git$/, ""));
725 const repo = repoOnRequest(ctx.app.db, ctx.url, ctx.request.method, owner, name.replace(/\.git$/, ""));
725726 if (
726727 repo !== undefined &&
727728 canRead(ctx.app.db, repo, undefined) &&
test/did-you-mean.test.ts +3 −3
@@ -20,12 +20,12 @@ let servers: Awaited<ReturnType<typeof serve>>;
2020 // 41: the repository page reads the repository's wallet cap, one primary-key read, to show the
2121 // wallet line (#13309).
2222 // 42: whether reports are closed, one read per request from the row (#15053).
23 const FOUND_QUERIES = 42;
23 const FOUND_QUERIES = 37;
2424 const BARE_404_QUERIES = 0; // a 404 with no owner/name to suggest from
25 // The same 404 for a mistyped repository. One of the 7 is the noindex lookup that
25 // The same 404 for a mistyped repository (6 since #15940). One of them is the noindex lookup that
2626 // answerGet makes for every /:owner/:name route before its handler runs (1e71740ab, #14305,
2727 // src/http/archived_noindex.ts); on a missing repository it finds nothing.
28 const NEAR_404_QUERIES = 7;
28 const NEAR_404_QUERIES = 6;
2929 const cookies: Record<string, string> = {};
3030
3131 beforeAll(async () => {
test/pages-budget.test.ts +1 −1
@@ -56,7 +56,7 @@ const DEFAULT = { statements: 60, git: 8 };
5656 const HEAVIER: Record<string, { statements?: number; git?: number }> = {
5757 "/:owner/:name/issues/:number": { statements: 71 }, // +1: whether the repository is archived, so its pages say noindex (#14305)
5858 "/next": { statements: 90 }, // a new page: measured 90 warm over the fixture's repositories, one ranking per administered repository, at most GROUPS_MAX = 5 (#15870)
59 "/:owner/:name/next": { statements: 72 }, // to lower: #15940; +2: the waiting offers and the decision check beside the reports (#15887); +1: the size limits of held issues, one read of both (#15845); +2: the waiting outsider reports, the decider check and the list (#15840); +1: whether the repository is archived (#14305); +1: the wallet row, one read of the monthly cap (#13529); +1: the growth line, one read for the week, the open counts and the planner pause (#12290, #12867)
59 "/:owner/:name/next": { statements: 66 }, // -6 (#15940): the page's repository read once per request, the palette lists skip counts, the runs tab reuses the app read; +2: the waiting offers and the decision check beside the reports (#15887); +1: the size limits of held issues, one read of both (#15845); +2: the waiting outsider reports, the decider check and the list (#15840); +1: whether the repository is archived (#14305); +1: the wallet row, one read of the monthly cap (#13529); +1: the growth line, one read for the week, the open counts and the planner pause (#12290, #12867)
6060 "/:owner/:name/pulls/:number": { statements: 116 }, // +1: whether the reports door is closed, one primary-key read per request, never from memory (#15053); +1: whether the repository is archived (#14305); +1: the desk hold on the pull, one read of the notes that link it (#12536); +1: whether a repository is the forge's own copy, one read of the source setting for the tell box (#12037); +1: the paused sentence, one read of a queued paused run on the pull (#12010); +1: the header Talk count (#10327); +1: the landing line (#10860); +1: whether a reviewer is seated, read only when Require review is off and the pull would otherwise wait on one; the page loads no crew besides (#11666)
6161 "/api/v1/asks": { statements: 61 }, // +1: the stopped apps someone asked for, one read of the viewer's archived repositories (#12277)
6262 "/board": { statements: 72 }, // +1: the forge-copy read for the tell box (#12037); +1: the vitals read the database's growth (#12025); +6: the money line (#510) — one settings read, then the month's and the 30 days' meter, storage and ledger reads; constant in the number of apps
test/request-repo.test.ts added +51
@@ -0,0 +1,51 @@
1 import { afterAll, beforeAll, expect, test } from "bun:test";
2 import { mkdtempSync, rmSync } from "node:fs";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { createRepo } from "../src/actions/repos.ts";
6 import { createApp, type App } from "../src/app.ts";
7 import { createUser } from "../src/core/users.ts";
8 import { repoOnRequest } from "../src/http/request_repo.ts";
9 import { once } from "./helpers/statements.ts";
10
11 let root = "";
12 let app: App;
13 beforeAll(async () => {
14 root = mkdtempSync(join(tmpdir(), "forge-request-repo-"));
15 app = createApp({ dataDir: root, port: 0 });
16 const u = await createUser(app.db, {
17 origin: "seed", trusted: true, username: "alice", email: "[email protected]",
18 password: "a-good-passphrase", adminIfFirst: true,
19 });
20 if (!u.ok) throw new Error(u.error);
21 await createRepo(app, u.value, { name: "demo", visibility: "public" });
22 });
23 afterAll(() => {
24 app.close();
25 rmSync(root, { recursive: true, force: true });
26 });
27
28 test("a GET reads the repository once however many parts of the page ask", async () => {
29 const url = new URL("http://x/alice/demo/next");
30 const reads = await once(app.db, () => {
31 for (let i = 0; i < 5; i++) expect(repoOnRequest(app.db, url, "GET", "alice", "demo")?.name).toBe("demo");
32 });
33 expect(reads).toBe(1);
34 // a missing repository is remembered as missing too
35 const none = await once(app.db, () => {
36 for (let i = 0; i < 3; i++) expect(repoOnRequest(app.db, url, "GET", "alice", "nope")).toBeUndefined();
37 });
38 expect(none).toBe(1);
39 });
40
41 test("a mutation never reads a remembered row, and another request starts fresh", async () => {
42 const url = new URL("http://x/alice/demo/settings");
43 const reads = await once(app.db, () => {
44 for (let i = 0; i < 3; i++) repoOnRequest(app.db, url, "POST", "alice", "demo");
45 });
46 expect(reads).toBe(3);
47 const fresh = await once(app.db, () => {
48 repoOnRequest(app.db, new URL("http://x/alice/demo/next"), "GET", "alice", "demo");
49 });
50 expect(fresh).toBe(1);
51 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs