Read a path's history sixteen commits at a time, so why and a file's log answer in seconds
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- Live on dev-oscabriel (915): the first view of `why` took 64 s on click's src/click/exceptions.py and 81 s on changesets' packages/cli/src/index.ts, and `commits?path=` 81 s on the latter. A second view on another isolate paid it again (62 s). Both walks read one tree per commit per path segment, one after another: about 600 reads at tens of milliseconds each. - `READS_AT_ONCE` (16, in code/tree.ts): `whyView` reads the file's blob in each batch of 16 commits together, then walks the batch in order, so it still stops at the commit the file was born in; `logPage` with a path reads each batch's before/after entries together, and still stops where the page fills, so `next` is unchanged. 16 commits × 2 trees stays far under Artifacts' 2,000 requests per 10 s per repo. - Tests (red first by assertion, "expected 1 to be greater than or equal to 8"): code.test.ts "reads its commits' trees several at a time" and code-why.test.ts "reads the file's blob in each commit several at a time", over `overlappingTrees` (code-fixtures.ts): the memory store with each readTree taking 10 ms, the most reads under way at once kept. Live tests: TestClock cannot interleave the memory store's promise-based reads with its sleeps.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+192 added−42 removed
- tests:+116 added−2 removed
- other:+76 added−40 removed
Provenance
- Commit
73adbab- Tree
7b57a84- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent
353b924
6 files changed+192 added−42 removed
modified:packages/project/src/code/log.ts+48 added-28 removed
16 unmodified lines17 import type { LandingView } from "@repo/protocol/domain/landing";18 import { TreeSha } from "@repo/protocol/ids";19 import type { CommitSha, RepoName, RepoPath } from "@repo/protocol/ids";20 import type { UnknownRevision } from "@repo/protocol/refusal";21-import { DateTime, Effect, Equal, Option } from "effect";21+import { Array, DateTime, Effect, Equal, Option } from "effect";22 23 import type { CodeProject } from "../code.ts";24 import { CodeCache } from "./cache.ts";25 import { commitAt, resolveRev } from "./revision.ts";26-import { entryAt } from "./tree.ts";26+import { entryAt, READS_AT_ONCE } from "./tree.ts";27 28 /** The most commits one page holds. */29 export const LOG_LIMIT = 100;30 134 unmodified lines165 166 const scanned = scan.slice(0, PATH_SCAN);167 const touched: Commit[] = [];168 169+ /** Whether the commit at `at` in the scan changed `path` from its first parent. */170+ const changedAt = (at: number, commit: Commit) =>169- for (const [at, commit] of scanned.entries()) {170- const parent = Option.fromUndefinedOr(commit.parents[0]);171- const after = Option.fromUndefinedOr(scan[at + 1]);171+ Effect.gen(function* () {172+ const parent = Option.fromUndefinedOr(commit.parents[0]);173+ const after = Option.fromUndefinedOr(scan[at + 1]);174 173- // The log is first parents, so the next commit in it is this one's174- // parent; only past the scan is the parent read on its own.175- const parentCommit = yield* Option.match(parent, {176- onNone: () => Effect.succeedNone,177- onSome: (first) =>178- Option.exists(after, ({ sha: next }) => next === first)179- ? Effect.succeed(after)180- : commitAt(repo, first),181- }).pipe(Effect.orDie);175+ // The log is first parents, so the next commit in it is this one's176+ // parent; only past the scan is the parent read on its own.177+ const parentCommit = yield* Option.match(parent, {178+ onNone: () => Effect.succeedNone,179+ onSome: (first) =>180+ Option.exists(after, ({ sha: next }) => next === first)181+ ? Effect.succeed(after)182+ : commitAt(repo, first),183+ });184 183- const parentTree = Option.map(parentCommit, ({ tree }) => tree);185+ const parentTree = Option.map(parentCommit, ({ tree }) => tree);186 185- const now = yield* idAt(repo, commit.tree, path).pipe(Effect.orDie);187+ const [now, before] = yield* Effect.all(188+ [189+ idAt(repo, commit.tree, path),190+ Option.match(parentTree, {191+ onNone: () => Effect.succeedNone,192+ onSome: (tree) => idAt(repo, tree, path),193+ }),194+ ],195+ { concurrency: 2 }196+ );197 187- const before = yield* Option.match(parentTree, {188- onNone: () => Effect.succeedNone,189- onSome: (tree) => idAt(repo, tree, path),198+ return !Equal.equals(now, before);199 }).pipe(Effect.orDie);200 192- if (!Equal.equals(now, before)) {193- touched.push(commit);194- }201+ // Each batch is read together, then walked in order, so a page that202+ // fills stops the scan where its last commit is.203+ for (const batch of Array.chunksOf([...scanned.entries()], READS_AT_ONCE)) {204+ const changed = yield* Effect.forEach(205+ batch,206+ ([at, commit]) => changedAt(at, commit),207+ { concurrency: READS_AT_ONCE }208+ );209 196- if (touched.length === limit) {197- const commits = yield* commitViews(project, touched);210+ for (const [[at, commit], touches] of Array.zip(batch, changed)) {211+ if (touches) {212+ touched.push(commit);213+ }214 215+ if (touched.length === limit) {216+ const commits = yield* commitViews(project, touched);217+199- return pageWith(200- commits,201- at + 1 < scan.length ? Option.some(from + at + 1) : Option.none()202- );218+ return pageWith(219+ commits,220+ at + 1 < scan.length ? Option.some(from + at + 1) : Option.none()221+ );222+ }223 }224 }225 226 const commits = yield* commitViews(project, touched);6 unmodified linesmodified:packages/project/src/code/tree.ts+9 added
15 unmodified lines1617181920212223242526272829303132162 unmodified linesExpand all15 unmodified linesimport type { CodeProject } from "../code.ts";import { CodeCache } from "./cache.ts";import { commitAt, resolveRev } from "./revision.ts";
/** * How many commits a walk over history (a path's log, why each line is * there) reads at once. A live store answers each read in tens of * milliseconds, so one at a time a 200-commit walk took a minute on * dev-oscabriel; sixteen at once stays far under the store's 2,000 * requests per 10 s per repo. */export const READS_AT_ONCE = 16;
/** The entries of the tree `id`; a tree a commit or a tree names is always there. */export const entriesOf = ( repo: RepoName, id: string162 unmodified linesExpand allmodified:packages/project/src/code/why.ts+19 added-12 removed
25 unmodified lines26 import { CodeCache } from "./cache.ts";27 import { contentsOf, contentsOfBytes, unlessTooLarge } from "./file.ts";28 import { commitViews } from "./log.ts";29 import { commitAt, resolveRev } from "./revision.ts";30-import { entryAt } from "./tree.ts";30+import { entryAt, READS_AT_ONCE } from "./tree.ts";31 32 /** The most first-parent commits one view walks. */33 export const WHY_SCAN = 200;34 32 unmodified lines67 Effect.gen(function* () {68 const versions: Version[] = [];69 let current = newest;70 71- for (const commit of log.slice(1)) {72- const blob = yield* blobAt(repo, commit, path);71+ // Each batch's blobs are read together, then walked in order.72+ for (const batch of Array.chunksOf(log.slice(1), READS_AT_ONCE)) {73+ const blobs = yield* Effect.forEach(74+ batch,75+ (commit) => blobAt(repo, commit, path),76+ { concurrency: READS_AT_ONCE }77+ );78 79+ for (const [commit, blob] of Array.zip(batch, blobs)) {74- if (Option.contains(blob, current.blob)) {75- current = { blob: current.blob, commit };76- } else {77- versions.push(current);80+ if (Option.contains(blob, current.blob)) {81+ current = { blob: current.blob, commit };82+ } else {83+ versions.push(current);84 79- if (Option.isNone(blob)) {80- // The file was born in the commit after this one.81- return { stopped: false, versions };82- }85+ if (Option.isNone(blob)) {86+ // The file was born in the commit after this one.87+ return { stopped: false, versions };88+ }89 84- current = { blob: blob.value, commit };90+ current = { blob: blob.value, commit };91+ }92 }93 }94 95 versions.push(current);129 unmodified linesmodified:packages/project/test/code-fixtures.ts+49 added-1 removed
7 unmodified lines8 * one fork never come from the repo.9 */10 import { RefName, RepoHost } from "@repo/artifacts/repo-host";11 import type { RefUpdate } from "@repo/artifacts/repo-host";12+import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";13 import { memoryFs } from "@repo/artifacts/repo-host/memory-fs";14 import { Brand } from "@repo/brand";15 import type { ForkRef } from "@repo/protocol/domain/code";16 import type { LandingView } from "@repo/protocol/domain/landing";4 unmodified lines21 LandedSha,22 ProjectName,23 RepoName,24 } from "@repo/protocol/ids";24-import { Array, Data, DateTime, Effect, Option } from "effect";25 import {26+ Array,27+ Data,28+ DateTime,29+ Duration,30+ Effect,31+ Layer,32+ Option,33+ Ref,34+} from "effect";35+import {36 init,37 writeBlob,38 writeCommit,39 writeTag,362 unmodified lines402 yield* git.push(name, [["refs/heads/main", sha]]);403 404 return { code: recordOf(name, sha, []), sha };405 });406+407+/**408+ * The memory store, each `readTree` taking 10 ms once `reading` is set (a409+ * history is written fast, then read), with the most reads ever under way410+ * at once kept in `peak`: a live store answers a read in tens of411+ * milliseconds, so a walk that reads one tree at a time takes all its412+ * reads added up.413+ */414+export const overlappingTrees = (415+ reading: Ref.Ref<boolean>,416+ peak: Ref.Ref<number>417+) =>418+ Layer.effect(419+ RepoHost,420+ Effect.gen(function* () {421+ const host = yield* RepoHost;422+ const under = yield* Ref.make(0);423+424+ const timed = Effect.acquireUseRelease(425+ Ref.updateAndGet(under, (count) => count + 1).pipe(426+ Effect.tap((count) =>427+ Ref.update(peak, (most) => Math.max(most, count))428+ )429+ ),430+ () => Effect.sleep(Duration.millis(10)),431+ () => Ref.update(under, (count) => count - 1)432+ );433+434+ return RepoHost.of({435+ ...host,436+ readTree: (name, id) =>437+ Ref.get(reading).pipe(438+ Effect.flatMap((on) => (on ? timed : Effect.void)),439+ Effect.andThen(host.readTree(name, id))440+ ),441+ });442+ })443+ ).pipe(Layer.provide(RepoHostMemory));modified:packages/project/test/code-why.test.ts+36 added-1 removed
24 unmodified lines25 import { whyOf } from "../src/code.ts";26 import type { CodeProject } from "../src/code.ts";27 import { CodeCacheLive } from "../src/code/cache.ts";28 import { whyView } from "../src/code/why.ts";29-import { Content, png, writer } from "./code-fixtures.ts";29+import {30+ Content,31+ longHistory,32+ png,33+ overlappingTrees,34+ writer,35+} from "./code-fixtures.ts";36 37 const project = ProjectName.make("datekit");38 39 const repo = RepoName.make(project);217 unmodified lines257 );258 })259 );260 261+ it.live(262+ "reads the file's blob in each commit several at a time, not one after another",263+ () =>264+ Effect.gen(function* () {265+ const reading = yield* Ref.make(false);266+ const peak = yield* Ref.make(0);267+268+ yield* Effect.gen(function* () {269+ const { code, shas } = yield* longHistory(64);270+271+ yield* Ref.set(reading, true);272+273+ // `once.txt` never changes, so the walk reads its blob in all 64274+ // commits.275+ const { ranges } = yield* whyView(276+ code,277+ main,278+ RepoPath.make("once.txt")279+ );280+281+ expect(ranges).toStrictEqual([{ end: 1, sha: shas[0], start: 1 }]);282+ }).pipe(Effect.provide(overlappingTrees(reading, peak)));283+284+ expect(yield* Ref.get(peak)).toBeGreaterThanOrEqual(8);285+ expect(yield* Ref.get(peak)).toBeLessThanOrEqual(16);286+ }),287+ { timeout: 30_000 }288+ );289+290 it.effect("answers no ranges for a binary file, at its commit", () =>291 Effect.gen(function* () {292 const { code, week } = yield* history;293 26 unmodified linesmodified:packages/project/test/code.test.ts+31 added
34 unmodified lines35 datekit,36 fork,37 longHistory,38 project,39+ overlappingTrees,40 wide,41 } from "./code-fixtures.ts";42 43 const revision = (name: string) => Option.some(name);389 unmodified lines433 long434 );435 });436 437+describe("a path's log, read live", () => {438+ it.live(439+ "reads its commits' trees several at a time, not one after another",440+ () =>441+ Effect.gen(function* () {442+ const reading = yield* Ref.make(false);443+ const peak = yield* Ref.make(0);444+445+ yield* Effect.gen(function* () {446+ const { code, shas } = yield* longHistory(64);447+448+ yield* Ref.set(reading, true);449+450+ const { commits } = yield* logOf(code, {451+ ...page,452+ path: path("once.txt"),453+ });454+455+ expect(commits.map(({ sha }) => sha)).toStrictEqual([shas[0]]);456+ }).pipe(Effect.provide(overlappingTrees(reading, peak)));457+458+ // 64 commits, two trees each: one at a time a live store took a459+ // minute for 200; sixteen commits at once, each its two trees.460+ expect(yield* Ref.get(peak)).toBeGreaterThanOrEqual(8);461+ expect(yield* Ref.get(peak)).toBeLessThanOrEqual(32);462+ }),463+ long464+ );465+});466+467 describe("a commit", () => {468 it.effect("at the root adds every file", () =>469 Effect.gen(function* () {470 const { c1, code } = yield* datekit;170 unmodified lines