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

Home's fork line reads parents and forks once, and a fork's offers upstream by the fork: home renders in 72-97 ms, not 156 #17674

opened this

Merged 20h 25m ago, live in production

fork-flows-fast → main 0 commits 1 thread · 1 unresolved

  • Merged. fork-flows-fast was merged into main as 0baccb3.

Review

Sign in to review this change.
5 files changed +48 −7
src/core/fork_flows.ts +12 −4
@@ -11,7 +11,7 @@ import type { Database } from "bun:sqlite";
1111 import { keptStanding } from "./following.ts";
1212 import { forkOffers, sentOffers, type SentOfferState } from "./fork_offers.ts";
1313 import { lastTookAt } from "./fork_follow.ts";
14 import { canAdmin, forkParentFor, listReposWritableTo, type Repo } from "./repos.ts";
14 import { canAdmin, canRead, getReposByIds, listReposWritableTo, visibleForksByParent, type Repo } from "./repos.ts";
1515 import type { User } from "./users.ts";
1616
1717 /** Repositories read per call: what one person administers, bounded. */
@@ -86,17 +86,25 @@ export function forkFlows(forge: { db: Database; reposRoot: string }, viewer: Us
8686 if (viewer === undefined) return out;
8787 const { db } = forge;
8888 const mine = administered(db, viewer);
89 // Every parent and every fork of these repositories in one read each, not two reads a repository:
90 // home asked this for each of up to 100 repositories on every render (#17629). Read as
91 // `forkParentFor` and `visibleForks` read: only what the viewer may read is named.
92 const parents = getReposByIds(db, mine.flatMap((r) => (r.forkParentId === null ? [] : [r.forkParentId])));
93 const forksOf = visibleForksByParent(db, mine, viewer);
8994 for (const repo of mine) {
9095 const name = `${repo.ownerName}/${repo.name}`;
91 const parent = repo.forkParentId === null ? undefined : forkParentFor(db, repo, viewer);
96 const known = repo.forkParentId === null ? undefined : parents.get(repo.forkParentId);
97 const parent = known !== undefined && canRead(db, known, viewer) ? known : undefined;
9298 if (parent !== undefined) {
9399 const parentName = `${parent.ownerName}/${parent.name}`;
94100 const kept = keptStanding(forge, repo, parent);
95101 out.follows.push({ repo: name, parent: parentName, standing: standingWords(kept), behindBy: kept?.[1] ?? 0, tookAt: lastTookAt(db, repo.id) });
96 for (const o of sentOffers(db, repo, viewer))
102 for (const o of sentOffers(db, repo, viewer, parent))
97103 out.sent.push({ repo: name, parent: parentName, number: o.number, title: o.title, state: o.state, createdAt: o.createdAt, href: `/${parentName}/pulls/${o.number}` });
98104 }
99 for (const o of forkOffers(db, repo, viewer))
105 const forks = forksOf.get(repo.id) ?? [];
106 if (forks.length === 0) continue;
107 for (const o of forkOffers(db, repo, viewer, forks))
100108 out.offered.push({ repo: name, fork: o.fork, number: o.number, title: o.title, createdAt: o.createdAt, href: `/${name}/pulls/${o.number}` });
101109 }
102110 return out;
src/core/fork_offers.ts +5 −3
@@ -23,8 +23,9 @@ export function forkOffers(
2323 db: Database,
2424 repo: Repo,
2525 viewer: User | undefined,
26 /** The forks the viewer may see, when the caller read them already (`forkFlows`, in one read). */
27 forks: readonly Repo[] = visibleForks(db, repo.id, viewer),
2628 ): ForkOffer[] {
27 const forks = visibleForks(db, repo.id, viewer);
2829 if (forks.length === 0) return [];
2930 const names = new Map(forks.map((f) => [f.id, `${f.ownerName}/${f.name}`]));
3031 const ids = [...names.keys()];
@@ -69,8 +70,9 @@ export function sentOffers(
6970 db: Database,
7071 fork: Repo,
7172 viewer: User | undefined,
73 /** The parent the viewer may read, when the caller read it already (`forkFlows`). */
74 parent: Repo | undefined = forkParentFor(db, fork, viewer),
7275 ): SentOffer[] {
73 const parent = forkParentFor(db, fork, viewer);
7476 if (parent === undefined) return [];
7577 return db
7678 .query<
@@ -87,7 +89,7 @@ export function sentOffers(
8789 >(
8890 `SELECT issue.number, issue.title, issue.created_at, issue.state, pull.merged_at, issue.author_id,
8991 (SELECT withdrawn_at FROM fork_offer WHERE fork_offer.issue_id = issue.id) AS withdrawn_at
90 FROM pull JOIN issue ON issue.id = pull.issue_id
92 FROM pull CROSS JOIN issue ON issue.id = pull.issue_id
9193 WHERE issue.repo_id = ?1 AND pull.head_repo_id = ?2
9294 ORDER BY issue.created_at DESC, issue.number DESC LIMIT ${MAX_FORK_OFFERS}`,
9395 )
src/core/migration-pins/20261202213232-pull-head-repo-index.pin added +1
@@ -0,0 +1,1 @@
1 341918df304d19947c4c5c4f80ca5b703ab90390dc95423dd02115802677211f
src/core/migrations/20261202213232-pull-head-repo-index.ts added +8
@@ -0,0 +1,8 @@
1 // A fork's offers upstream are found by the fork (#17629). `sentOffers` asks for the pulls into a
2 // parent whose head lives in one fork; with no index on `pull.head_repo_id` it walked every issue
3 // of the parent (trevato/forge: 17,000) once for each fork, and home's fork line asked once for
4 // every fork its owner administers: 51% of home's render on the stage rig's copy. Additive: one
5 // index.
6 export default `
7 CREATE INDEX IF NOT EXISTS pull_head_repo ON pull(head_repo_id, issue_id);
8 `;
test/fork-flows.test.ts +22
@@ -24,6 +24,7 @@ import { serve, type Servers } from "../src/http/server.ts";
2424 import { loadMcp } from "../src/http/mcp/load.ts";
2525 import { forkFlowsScreen, flowRows } from "../src/tui/screens/forkflows.ts";
2626 import { promote } from "./helpers/members.ts";
27 import { statements } from "./helpers/statements.ts";
2728 import { mcpTool } from "./helpers/mcp.ts";
2829 import type { Repo } from "../src/core/repos.ts";
2930
@@ -190,6 +191,27 @@ describe("fork flows", () => {
190191 expect(draw("dave")).toContain("No fork family flows here yet.");
191192 });
192193
194 test("a repository with no parent and no forks costs the line no read of its own (#17629)", async () => {
195 // Home asks this on every render, for every repository its owner administers: the parents and
196 // the forks are one read each for all of them, not two a repository.
197 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "fran", email: "[email protected]", password: "a-good-passphrase" });
198 if (!made.ok) throw new Error(made.error);
199 const fran = made.value;
200 promote(app.db, fran);
201 const add = async (n: number) => {
202 for (let i = 0; i < n; i++) {
203 const r = await createRepo(app, fran, { name: `plain-${Math.random().toString(36).slice(2, 8)}`, visibility: "public" });
204 if (!r.ok) throw new Error(String(r.error));
205 }
206 };
207 await add(1);
208 const one = await statements(app.db, () => void forkFlows(app, fran));
209 await add(5);
210 const six = await statements(app.db, () => void forkFlows(app, fran));
211 // At most the admin check per repository; each also read its forks before.
212 expect(six - one).toBeLessThanOrEqual(5);

Non-blocking: <= 5 allows one extra statement per added repo. Tighten it to the measured cost and name the admin check in a comment.

213 });
214
193215 test("it is linked from /family and from a repository's forks page", async () => {
194216 const forks = await (await fetch(`${servers.url}/alice/lib/forks`)).text();
195217 expect(forks).toContain('href="/forks/flows"');

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs