Avoid quadratic insertion-run offsets; cleared in review ask 52
of shared·codex-capacity-chair··Not checked
Version 175 · Files at version 176
2 files, 34 lines added, 6 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -380,6 +380,20 @@
380380 throw Error("stored atom text range missing"); 383+ // Sequential reads advance once per run without retaining a history-sized offset array. 386+ const insertionStart = (index: number): number => { 387+ if (index < offsetRun) { 391+ while (offsetRun < index) { 392+ offsetLine += readonlyStoredArray(storage, `atom-inserts:${atom.seq}:lines:${offsetRun}`).length; 383397 const streamed = (target: Atom): Atom => { 384398 registerJsonChunkValue(target, () => { 385399 const line = (raw: unknown, index: number): unknown => { @@ -397,9 +411,7 @@
397411 const lines = `${collection}:lines:${index}`; 398412 if (run.lines !== lines || typeof run.file !== "string" || typeof run.parent !== "string") 399413 throw Error("invalid stored insertion run"); 400− let start = 0;
401− for (let prior = 0; prior < index; prior++)
402− start += readonlyStoredArray(storage, `${collection}:lines:${prior}`).length;
414+ const start = insertionStart(index); 405417 parent: run.parent, @@ -440,9 +452,7 @@
440452 lines: readonlyStoredArray<string>(storage, lines, (rawLine, lineIndex) => { 441453 if (typeof rawLine === "string") return rawLine; 442454 const ref = rawLine as TextReference; 443− let atomLine = lineIndex;
444− for (let prior = 0; prior < index; prior++)
445− atomLine += readonlyStoredArray(storage, `${collection}:lines:${prior}`).length;
455+ const atomLine = insertionStart(index) + lineIndex; 446456 if (!ref || ref.text !== JSON.stringify([String(atom.seq), atomLine])) 447457 throw Error("invalid stored insertion text"); 448458 return atomText.read(ref.text); --- a/src/engine/test/json-string-chunks.test.ts
+++ b/src/engine/test/json-string-chunks.test.ts
@@ -6,6 +6,24 @@
66import { StoredText } from "../stored-text.ts"; 77import { SqlState } from "./storage-helper.ts"; 9+test("many stored insertion runs serialize with linear reads and support backwards access", (t) => { 10+ const storage = new SqlState(); 11+ t.after(() => storage.db.close()); 12+ const files = Object.fromEntries(Array.from({ length: 128 }, (_, i) => [`f${i}`, `${i}:${"x".repeat(17000)}\n`])); 13+ const graph = new Graph({ storage }); 14+ graph.add(buildBase(files, { author: "writer", message: "many runs" })); 15+ const stored = graph.atom("a0"); 16+ const expected = JSON.stringify(stored); 17+ const before = storage.reads; 18+ assert.equal([...jsonValueChunks(stored)].join(""), expected); 19+ assert.ok(storage.reads - before < 128 * 25, `serialization used ${storage.reads - before} reads`); 20+ for (const index of [127, 0, 64, 2, 126, 1]) { 21+ const run = stored.inserts[index]!; 22+ assert.equal(run.lines[0], files[run.file]!.trimEnd()); 24+ assert.equal([...jsonValueChunks(stored)].join(""), expected); 927test("chunk-backed JSON strings preserve escaping and split UTF-16 exactly", () => {