Read long insertion text lazily and stream files through Git commit construction
of shared·codex-capacity-chair··Not checked
Version 165 · Files at version 166
6 files, 99 lines added, 6 removed.
--- a/server/src/push.ts
+++ b/server/src/push.ts
@@ -170,10 +170,10 @@
170170 // Each file goes into the tree as a blob with its mode (#27): 100755 for exec, 120000 for a link, whose blob is 172172 const enc = new TextEncoder(); 173− for (const [path, text] of Object.entries(shared.files)) {
173+ for (const path of Object.keys(shared.files)) { 174174 const mode = shared.modes?.[path]; 175175 const link = typeof mode?.link === "string" ? mode.link : null; 176− const bytes = enc.encode(link ?? text);
176+ const bytes = enc.encode(link ?? shared.files[path]!); 177177 await put(path, bytes, link !== null ? "120000" : mode?.exec ? "100755" : "100644", await blobOid(bytes)); 179179 const agent = shared.last?.agent ?? "abso"; --- a/server/test/push.test.ts
+++ b/server/test/push.test.ts
@@ -22,6 +22,33 @@
2424describe("commitShared", () => { 25+ it("reads one lazy file at a time and does not read symlink placeholder text", async (t) => { 26+ const fs = new MemoryFS(); 28+ await git.init({ fs, dir, defaultBranch: "main" }); 30+ const writeBlob = git.writeBlob; 31+ t.mock.method(git, "writeBlob", async (options: Parameters<typeof writeBlob>[0]) => { 32+ const result = await writeBlob(options); 38+ assert.equal(writes, 0); 42+ assert.equal(writes, 1); 46+ throw Error("symlink placeholder text must not be read"); 49+ await commitShared(fs, dir, null, { version: 0, files, modes: { link: { link: "a" } } }, () => null); 50+ assert.equal(writes, 3); 2552 it("writes shared as a commit with its modes, then only what changed", async () => { 2653 const fs = new MemoryFS(); 2754 const dir = "/repo"; --- a/src/engine/experimental/projection-reader.ts
+++ b/src/engine/experimental/projection-reader.ts
@@ -1,6 +1,5 @@
11import { startNode } from "../keys.ts"; 22import { StableSet } from "../state-source.ts"; 3−import { joinLines } from "../text.ts";
43import type { FileAttrs, ReadView, SeqLine, VisibleLine } from "../view.ts"; 54import type { ExperimentalMarkerProjection, MarkerState } from "./projection-marker.ts"; @@ -74,7 +73,14 @@
7574 lineNumber(path: string, line: string): number { 77− return this.lines(path).findIndex((item) => item.id === line);
77+ for (const id of this.#adapter.lineIds(this.#state, path)) { 79+ if (id === line) return index; 7985 sameText(path: string, other: ReadView): boolean { @@ -193,7 +199,16 @@
194200 text(path: string): string { 195201 this.#assertLive(); 196− return joinLines([...this.visibleLines(path)].map((line) => line.text));
204+ for (const line of this.visibleLines(path)) { 205+ part += `${line.text}\n`; 206+ if (part.length >= 16384) { 198213 hasFile(path: string): boolean { 199214 this.#assertLive(); --- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -429,7 +429,12 @@
430430 const listCount = Object.keys(lists).length; 431431 const hasLists = listCount > 0; 432− if (atom.inserts.length > 256 || atom.inserts.some((run) => run.lines.length > 256)) {
432+ let insertionCharacters = 0; 434+ atom.inserts.length > 256 || 435+ atom.inserts.some((run) => run.lines.length > 256) || 436+ atom.inserts.some((run) => run.lines.some((line) => (insertionCharacters += line.length) > 65536)) 433438 const collection = `atom-inserts:${input.seq}`; 434439 const runs = storedArray<unknown>(storage, collection); --- a/src/engine/test/graph-long-text.test.ts
+++ b/src/engine/test/graph-long-text.test.ts
@@ -4,6 +4,28 @@
44import { Graph } from "../graph.ts"; 55import { SqlState } from "./storage-helper.ts"; 7+test("large insertion text stays lazy even when the run has few lines", (t) => { 8+ const storage = new SqlState(); 9+ t.after(() => storage.db.close()); 10+ const memory = new MemoryStore(); 11+ const line = "x".repeat(100000); 12+ new Project(memory).init({ "a.txt": `${line}\n${line}\n${line}\n` }, 0); 13+ const entry = memory.entries[0]!; 14+ if (entry.type !== "init") throw Error("expected init"); 15+ new Graph({ storage }).add(entry.atom); 16+ const read = storage.read.bind(storage); 18+ t.mock.method(storage, "read", (collection: string, key: string) => { 19+ if (collection === "atom-text:long:chunks") textReads++; 20+ return read(collection, key); 22+ const atom = new Graph({ storage }).atom("a0"); 23+ assert.equal(atom.inserts[0]!.lines.length, 3); 24+ assert.equal(textReads, 0, "reading line count must not decode the complete run"); 25+ assert.equal(atom.inserts[0]!.lines[1], line); 26+ assert.equal(textReads, Math.ceil(line.length / 16384), "an indexed read decodes only the selected line"); 729test("persistent graph stores long lines in chunks and reopens exact text", () => { 830 const storage = new SqlState(); --- a/src/engine/test/projection-reader.test.ts
+++ b/src/engine/test/projection-reader.test.ts
@@ -10,6 +10,30 @@
1010import { Repo } from "./helpers.ts"; 1111import { SqlState } from "./storage-helper.ts"; 13+test("projected text streams lines and line lookup does not read file text", (t) => { 14+ const storage = new SqlState(); 15+ t.after(() => storage.db.close()); 16+ const text = `${"x".repeat(40000)}\nsecond\nthird\n`; 17+ const repo = Repo.init({ "f.txt": text, "empty.txt": "" }); 18+ const graph = new Graph({ storage: new CachedStateStorage(storage) }); 19+ graph.add(repo.graph.atom("a0")); 20+ const adapter = new ExperimentalMarkerProjection(storage, graph, "f.txt", "streamed-text"); 21+ const state = adapter.apply(adapter.empty(), graph.atom("a0")); 22+ const reader = new ProjectionReader(adapter, state, () => {}); 23+ t.mock.method(reader, "lines", () => { 24+ throw Error("full line array forbidden"); 26+ assert.equal(reader.text("f.txt"), text); 27+ assert.equal(reader.text("empty.txt"), ""); 28+ assert.equal(reader.text("missing.txt"), ""); 29+ const ids = [...adapter.lineIds(state, "f.txt")]; 30+ t.mock.method(graph, "textOf", () => { 31+ throw Error("line lookup must not read text"); 33+ for (let i = 0; i < ids.length; i++) assert.equal(reader.lineNumber("f.txt", ids[i]!), i); 34+ assert.equal(reader.lineNumber("f.txt", "missing"), -1); 1337test("unchanged atom builder consumes the private reader for replacement and insertion edits", () => { 1438 const storage = new SqlState();