Skip to content
Myr

Read a file's older versions sixteen at a time when saying why each line is there

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Live on dev-oscabriel after 73adbab: why's first view fell to 19 s on click and 23 s on changesets, but took 40 s on ky's source/core/Ky.ts, a file most of whose 200 scanned commits changed: the blame read each older version's blob one after another. - whyView now reads the older versions in batches of `READS_AT_ONCE`, then traces each batch in order and stops once every line has its commit; at most 16 texts of at most 1 MiB are held at once. - Test (red first by assertion, "expected 1 to be greater than or equal to 8"): code-why.test.ts "reads the file's older versions several at a time", over `growingHistory(40)` (each commit appends a line, so every version is read) and `overlappingTrees(…, "blob")`.

authored by oscabriel · committed by oscabriel · parent 73adbab · Browse files · 3 files+103 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+103 added−18 removed

  • tests:+84 added−10 removed
  • other:+19 added−8 removed

3 files+103 added−18 removed

modified:packages/project/src/code/why.ts+19 added-8 removed
1/**2 * Why each line of a file is there (spec W9-D9): a first-parent blame from3 * a revision. The walk takes at most `WHY_SCAN` first-parent commits from4 * the log and the path's blob at each (trees kept by id through5 * `CodeCache`), keeps the commits where the blob changed, then reads those6 * blobs newest first, one at a time, only until every line has its commit7 * (`@repo/git/blame`). Each commit a line points at comes back as a reader5 * `CodeCache`, `READS_AT_ONCE` commits read together), keeps the commits6 * where the blob changed, then reads those blobs newest first, a batch at7 * a time, only until every line has its commit (`@repo/git/blame`). Each commit a line points at comes back as a reader8 * sees it, and the landings among them come from the project's record in9 * one batch, so an imported commit never reads as a landing. Lines older10 * than the walk reached point at the commit it stopped at, named11 * `stoppedAt`. A binary file, or one past 1 MiB, has no ranges. The whole
153 unmodified lines
165 sha: newest.commit.sha,166 text: contents.value.text.value,167 });168169 for (const version of older) {169 // Each batch of older versions is read together, then traced in170 // order, until every line has its commit.171 for (const batch of Array.chunksOf(older, READS_AT_ONCE)) {172 if (isSettled(tracing)) {173 break;174 }175174 tracing = traceBack(tracing, {175 sha: version.commit.sha,176 text: yield* textOf(repo, version.blob),177 });176 const texts = yield* Effect.forEach(177 batch,178 (version) => textOf(repo, version.blob),179 { concurrency: READS_AT_ONCE }180 );181182 for (const [version, text] of Array.zip(batch, texts)) {183 if (isSettled(tracing)) {184 break;185 }186187 tracing = traceBack(tracing, { sha: version.commit.sha, text });188 }189 }190191 const { ranges, reachedEnd } = settle(tracing);192 const named = new Set<string>(ranges.map(({ sha }) => sha));
43 unmodified lines
modified:packages/project/test/code-fixtures.ts+52 added-10 removed
384 unmodified lines
385386 return { code: recordOf(name, head, []), shas };387 });388389/**390 * A history of `count` commits, each appending one line to `grow.txt`, so391 * every line was brought by a commit of its own.392 */393export const growingHistory = (count: number) =>394 Effect.gen(function* () {395 const git = yield* writer;396 const name = RepoName.make("growkit");397 const shas: CommitSha[] = [];398399 for (let at = 1; at <= count; at += 1) {400 const lines = Array.makeBy(at, (line) => `line ${line + 1}\n`).join("");401402 const sha = yield* git.commit(403 `Commit ${at}`,404 [["grow.txt", text(lines)]],405 shas.slice(-1),406 at407 );408409 shas.push(sha);410 }411412 const head = shas.at(-1) ?? shas[0];413414 if (head === undefined) {415 return yield* Effect.die("no commits");416 }417418 yield* git.push(name, [["refs/heads/main", head]]);419420 return { code: recordOf(name, head, []), shas };421 });422423/** One root commit of `count` files. */424export const wide = (count: number) =>425 Effect.gen(function* () {426 const git = yield* writer;
11 unmodified lines
438 return { code: recordOf(name, sha, []), sha };439 });440441/**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.442 * The memory store, each `readTree` (or each `readBlob`, for `"blob"`)443 * taking 10 ms once `reading` is set (a history is written fast, then444 * read), with the most reads ever under way at once kept in `peak`: a live445 * store answers a read in tens of milliseconds, so a walk that reads one446 * object at a time takes all its reads added up.447 */448export const overlappingTrees = (449 reading: Ref.Ref<boolean>,416 peak: Ref.Ref<number>450 peak: Ref.Ref<number>,451 kind: "blob" | "tree" = "tree"452) =>453 Layer.effect(454 RepoHost,455 Effect.gen(function* () {
9 unmodified lines
465 () => Effect.sleep(Duration.millis(10)),466 () => Ref.update(under, (count) => count - 1)467 );468469 const slowed = Ref.get(reading).pipe(470 Effect.flatMap((on) => (on ? timed : Effect.void))471 );472473 return RepoHost.of({474 ...host,475 readBlob: (name, id) =>476 kind === "blob"477 ? Effect.andThen(slowed, host.readBlob(name, id))478 : host.readBlob(name, id),479 readTree: (name, id) =>437 Ref.get(reading).pipe(438 Effect.flatMap((on) => (on ? timed : Effect.void)),439 Effect.andThen(host.readTree(name, id))440 ),480 kind === "tree"481 ? Effect.andThen(slowed, host.readTree(name, id))482 : host.readTree(name, id),483 });484 })485 ).pipe(Layer.provide(RepoHostMemory));
modified:packages/project/test/code-why.test.ts+32 added
26 unmodified lines
27import { CodeCacheLive } from "../src/code/cache.ts";28import { whyView } from "../src/code/why.ts";29import {30 Content,31 growingHistory,32 longHistory,33 png,34 overlappingTrees,35 writer,
251 unmodified lines
287 }),288 { timeout: 30_000 }289 );290291 it.live(292 "reads the file's older versions several at a time, not one after another",293 () =>294 Effect.gen(function* () {295 const reading = yield* Ref.make(false);296 const peak = yield* Ref.make(0);297298 yield* Effect.gen(function* () {299 const { code, shas } = yield* growingHistory(40);300301 yield* Ref.set(reading, true);302303 // Each of the 40 lines came with a commit of its own, so the304 // blame reads every older version of `grow.txt`.305 const { ranges } = yield* whyView(306 code,307 main,308 RepoPath.make("grow.txt")309 );310311 expect(ranges).toStrictEqual(312 shas.map((sha, at) => ({ end: at + 1, sha, start: at + 1 }))313 );314 }).pipe(Effect.provide(overlappingTrees(reading, peak, "blob")));315316 expect(yield* Ref.get(peak)).toBeGreaterThanOrEqual(8);317 expect(yield* Ref.get(peak)).toBeLessThanOrEqual(16);318 }),319 { timeout: 30_000 }320 );321322 it.effect("answers no ranges for a binary file, at its commit", () =>323 Effect.gen(function* () {324 const { code, week } = yield* history;325
26 unmodified lines