Skip to content
Myr

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.

authored by oscabriel · committed by oscabriel · parent 353b924 · Browse files · 6 files+192 added−42 removed
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

6 files+192 added−42 removed

modified:packages/project/src/code/log.ts+48 added-28 removed
16 unmodified lines
17import type { LandingView } from "@repo/protocol/domain/landing";18import { TreeSha } from "@repo/protocol/ids";19import type { CommitSha, RepoName, RepoPath } from "@repo/protocol/ids";20import type { UnknownRevision } from "@repo/protocol/refusal";21import { DateTime, Effect, Equal, Option } from "effect";21import { Array, DateTime, Effect, Equal, Option } from "effect";2223import type { CodeProject } from "../code.ts";24import { CodeCache } from "./cache.ts";25import { commitAt, resolveRev } from "./revision.ts";26import { entryAt } from "./tree.ts";26import { entryAt, READS_AT_ONCE } from "./tree.ts";2728/** The most commits one page holds. */29export const LOG_LIMIT = 100;30
134 unmodified lines
165166 const scanned = scan.slice(0, PATH_SCAN);167 const touched: Commit[] = [];168169 /** 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]);174173 // 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 });184183 const parentTree = Option.map(parentCommit, ({ tree }) => tree);185 const parentTree = Option.map(parentCommit, ({ tree }) => tree);186185 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 );197187 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);200192 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 );209196 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 }214215 if (touched.length === limit) {216 const commits = yield* commitViews(project, touched);217199 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 }225226 const commits = yield* commitViews(project, touched);
6 unmodified lines
modified:packages/project/src/code/tree.ts+9 added
modified:packages/project/src/code/why.ts+19 added-12 removed
25 unmodified lines
26import { CodeCache } from "./cache.ts";27import { contentsOf, contentsOfBytes, unlessTooLarge } from "./file.ts";28import { commitViews } from "./log.ts";29import { commitAt, resolveRev } from "./revision.ts";30import { entryAt } from "./tree.ts";30import { entryAt, READS_AT_ONCE } from "./tree.ts";3132/** The most first-parent commits one view walks. */33export const WHY_SCAN = 200;34
32 unmodified lines
67 Effect.gen(function* () {68 const versions: Version[] = [];69 let current = newest;7071 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 );7879 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);8479 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 }8984 current = { blob: blob.value, commit };90 current = { blob: blob.value, commit };91 }92 }93 }9495 versions.push(current);
129 unmodified lines
modified:packages/project/test/code-fixtures.ts+49 added-1 removed
7 unmodified lines
8 * one fork never come from the repo.9 */10import { RefName, RepoHost } from "@repo/artifacts/repo-host";11import type { RefUpdate } from "@repo/artifacts/repo-host";12import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";13import { memoryFs } from "@repo/artifacts/repo-host/memory-fs";14import { Brand } from "@repo/brand";15import type { ForkRef } from "@repo/protocol/domain/code";16import type { LandingView } from "@repo/protocol/domain/landing";
4 unmodified lines
21 LandedSha,22 ProjectName,23 RepoName,24} from "@repo/protocol/ids";24import { Array, Data, DateTime, Effect, Option } from "effect";25import {26 Array,27 Data,28 DateTime,29 Duration,30 Effect,31 Layer,32 Option,33 Ref,34} from "effect";35import {36 init,37 writeBlob,38 writeCommit,39 writeTag,
362 unmodified lines
402 yield* git.push(name, [["refs/heads/main", sha]]);403404 return { code: recordOf(name, sha, []), sha };405 });406407/**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 */414export 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);423424 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 );433434 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 lines
25import { whyOf } from "../src/code.ts";26import type { CodeProject } from "../src/code.ts";27import { CodeCacheLive } from "../src/code/cache.ts";28import { whyView } from "../src/code/why.ts";29import { Content, png, writer } from "./code-fixtures.ts";29import {30 Content,31 longHistory,32 png,33 overlappingTrees,34 writer,35} from "./code-fixtures.ts";3637const project = ProjectName.make("datekit");3839const repo = RepoName.make(project);
217 unmodified lines
257 );258 })259 );260261 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);267268 yield* Effect.gen(function* () {269 const { code, shas } = yield* longHistory(64);270271 yield* Ref.set(reading, true);272273 // `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 );280281 expect(ranges).toStrictEqual([{ end: 1, sha: shas[0], start: 1 }]);282 }).pipe(Effect.provide(overlappingTrees(reading, peak)));283284 expect(yield* Ref.get(peak)).toBeGreaterThanOrEqual(8);285 expect(yield* Ref.get(peak)).toBeLessThanOrEqual(16);286 }),287 { timeout: 30_000 }288 );289290 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 lines
modified:packages/project/test/code.test.ts+31 added
34 unmodified lines
35 datekit,36 fork,37 longHistory,38 project,39 overlappingTrees,40 wide,41} from "./code-fixtures.ts";4243const revision = (name: string) => Option.some(name);
389 unmodified lines
433 long434 );435});436437describe("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);444445 yield* Effect.gen(function* () {446 const { code, shas } = yield* longHistory(64);447448 yield* Ref.set(reading, true);449450 const { commits } = yield* logOf(code, {451 ...page,452 path: path("once.txt"),453 });454455 expect(commits.map(({ sha }) => sha)).toStrictEqual([shas[0]]);456 }).pipe(Effect.provide(overlappingTrees(reading, peak)));457458 // 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});466467describe("a commit", () => {468 it.effect("at the root adds every file", () =>469 Effect.gen(function* () {470 const { c1, code } = yield* datekit;
170 unmodified lines