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
Merged 20h 25m ago, live in production
fork-flows-fast → main 0 commits 1 thread · 1 unresolved
5 files
changed +48
−7
src/core/fork_flows.ts +12 −4
| @@ -11,7 +11,7 @@ import type { Database } from "bun:sqlite"; | ||
| 11 | 11 | import { keptStanding } from "./following.ts"; |
| 12 | 12 | import { forkOffers, sentOffers, type SentOfferState } from "./fork_offers.ts"; |
| 13 | 13 | 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"; | |
| 15 | 15 | import type { User } from "./users.ts"; |
| 16 | 16 | |
| 17 | 17 | /** Repositories read per call: what one person administers, bounded. */ |
| @@ -86,17 +86,25 @@ export function forkFlows(forge: { db: Database; reposRoot: string }, viewer: Us | ||
| 86 | 86 | if (viewer === undefined) return out; |
| 87 | 87 | const { db } = forge; |
| 88 | 88 | 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); | |
| 89 | 94 | for (const repo of mine) { |
| 90 | 95 | 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; | |
| 92 | 98 | if (parent !== undefined) { |
| 93 | 99 | const parentName = `${parent.ownerName}/${parent.name}`; |
| 94 | 100 | const kept = keptStanding(forge, repo, parent); |
| 95 | 101 | 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)) | |
| 97 | 103 | out.sent.push({ repo: name, parent: parentName, number: o.number, title: o.title, state: o.state, createdAt: o.createdAt, href: `/${parentName}/pulls/${o.number}` }); |
| 98 | 104 | } |
| 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)) | |
| 100 | 108 | out.offered.push({ repo: name, fork: o.fork, number: o.number, title: o.title, createdAt: o.createdAt, href: `/${name}/pulls/${o.number}` }); |
| 101 | 109 | } |
| 102 | 110 | return out; |
src/core/fork_offers.ts +5 −3
| @@ -23,8 +23,9 @@ export function forkOffers( | ||
| 23 | 23 | db: Database, |
| 24 | 24 | repo: Repo, |
| 25 | 25 | 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), | |
| 26 | 28 | ): ForkOffer[] { |
| 27 | const forks = visibleForks(db, repo.id, viewer); | |
| 28 | 29 | if (forks.length === 0) return []; |
| 29 | 30 | const names = new Map(forks.map((f) => [f.id, `${f.ownerName}/${f.name}`])); |
| 30 | 31 | const ids = [...names.keys()]; |
| @@ -69,8 +70,9 @@ export function sentOffers( | ||
| 69 | 70 | db: Database, |
| 70 | 71 | fork: Repo, |
| 71 | 72 | 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), | |
| 72 | 75 | ): SentOffer[] { |
| 73 | const parent = forkParentFor(db, fork, viewer); | |
| 74 | 76 | if (parent === undefined) return []; |
| 75 | 77 | return db |
| 76 | 78 | .query< |
| @@ -87,7 +89,7 @@ export function sentOffers( | ||
| 87 | 89 | >( |
| 88 | 90 | `SELECT issue.number, issue.title, issue.created_at, issue.state, pull.merged_at, issue.author_id, |
| 89 | 91 | (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 | |
| 91 | 93 | WHERE issue.repo_id = ?1 AND pull.head_repo_id = ?2 |
| 92 | 94 | ORDER BY issue.created_at DESC, issue.number DESC LIMIT ${MAX_FORK_OFFERS}`, |
| 93 | 95 | ) |
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"; | ||
| 24 | 24 | import { loadMcp } from "../src/http/mcp/load.ts"; |
| 25 | 25 | import { forkFlowsScreen, flowRows } from "../src/tui/screens/forkflows.ts"; |
| 26 | 26 | import { promote } from "./helpers/members.ts"; |
| 27 | import { statements } from "./helpers/statements.ts"; | |
| 27 | 28 | import { mcpTool } from "./helpers/mcp.ts"; |
| 28 | 29 | import type { Repo } from "../src/core/repos.ts"; |
| 29 | 30 | |
| @@ -190,6 +191,27 @@ describe("fork flows", () => { | ||
| 190 | 191 | expect(draw("dave")).toContain("No fork family flows here yet."); |
| 191 | 192 | }); |
| 192 | 193 | |
| 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); | |
| 213 | }); | |
| 214 | ||
| 193 | 215 | test("it is linked from /family and from a repository's forks page", async () => { |
| 194 | 216 | const forks = await (await fetch(`${servers.url}/alice/lib/forks`)).text(); |
| 195 | 217 | expect(forks).toContain('href="/forks/flows"'); |
new above
offline
Non-blocking: <= 5 allows one extra statement per added repo. Tighten it to the measured cost and name the admin check in a comment.