Stream mover history when opening persisted views
of shared·codex-capacity-chair··Not checked
Version 122 · Files at version 123
2 files, 43 lines added, 11 removed.
--- a/src/engine/test/state-source.test.ts
+++ b/src/engine/test/state-source.test.ts
@@ -1,7 +1,7 @@
11import assert from "node:assert/strict"; 22import { test } from "node:test"; 33import { checkDraft, Graph, View } from "../index.ts"; 4−import { CachedStateStorage, storedLists, storedNumbers } from "../state-source.ts";
4+import { CachedStateStorage, StableOverlaySet, storedLists, storedNumbers } from "../state-source.ts"; 55import { lines, Repo } from "./helpers.ts"; 66import { SqlState } from "./storage-helper.ts"; @@ -44,6 +44,27 @@
47+test("stable views stream movers instead of allocating a filtered history array", (t) => { 48+ const oracle = Repo.init({ "a.txt": lines("one", "two"), "b.txt": "" }); 49+ oracle.edit({ move: { file: "a.txt", start: 0, end: 1, to: "b.txt", at: 0 } }); 50+ const expected = oracle.files(); 51+ const movers = oracle.graph.movers(); 57+ get(target, key, receiver) { 58+ if (key === "filter") throw new Error("whole mover history copied"); 59+ return Reflect.get(target, key, receiver); 63+ const view = new View(oracle.graph, new StableOverlaySet(oracle.shared)); 64+ assert.equal(view.hasMoves(), true); 65+ assert.deepEqual(view.files(), expected); 4768test("per-file list pages append and reopen without reading the full historical list", () => { 4869 const source = new SqlState(); --- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -104,7 +104,8 @@
104104 /** 1 for each atom (by seq) in the set. */ 106106 /** Atoms in the set that move lines. */ 107− #movers: AtomId[];
107+ #movers: AtomId[] | undefined; 108109 /** Each moved line's relocation that counts in this set; built on first use. */ 109110 #relocs: Map<LineId, RelocationInfo> | undefined; 110111 /** The same, by n, with the new parent's code. */ @@ -129,9 +130,14 @@
129130 throw new EngineError("not-closed", `set is not closed: ${id} depends on ${dependency}`); 132− this.#movers = graph.movers().filter((id) => set.has(id));
133− this.#relocs = this.#movers.length === 0 ? new Map() : undefined;
134− this.#relocsN = this.#movers.length === 0 ? new Map() : undefined;
133+ for (const id of graph.movers()) { 135+ this.#hasMoves = true; 139+ this.#relocs = this.#hasMoves ? undefined : new Map(); 140+ this.#relocsN = this.#hasMoves ? undefined : new Map(); 137143 const inSet = new Uint8Array(graph.size); @@ -152,6 +158,7 @@
152158 this.graph = graph; 154160 this.#movers = graph.movers().filter((id) => inSet[graph.atom(id).seq] === 1); 161+ this.#hasMoves = this.#movers.length > 0; 155162 this.#relocs = this.#movers.length === 0 ? new Map() : undefined; 156163 this.#relocsN = this.#movers.length === 0 ? new Map() : undefined; @@ -198,7 +205,7 @@
200207 const relocs = new Map<LineId, RelocationInfo>(); 201− for (const id of this.#movers) {
208+ for (const id of this.#eachMover()) { 202209 for (const m of graph.atom(id).moves!) { 203210 if (!relocs.has(m.line)) relocs.set(m.line, pick(m.line)!); @@ -252,7 +259,7 @@
253260 /** The node a line hangs off in this set: where it was written, or where a move put it. */ 254261 parentOf(line: LineId): NodeId { 255− const r = this.#movers.length === 0 ? undefined : this.#relocations().get(line);
262+ const r = this.#hasMoves ? this.#relocations().get(line) : undefined; 256263 if (r) return r.parent; 257264 const parent = this.graph.lineParent(line); 258265 if (parent === undefined) throw new EngineError("unknown-line", `no line ${line}`); @@ -261,8 +268,12 @@
262269 /** Whether any atom in the set moves lines. */ 263270 hasMoves(): boolean { 264− return this.#movers.length > 0;
271+ return this.#hasMoves; 273+ *#eachMover(): IterableIterator<AtomId> { 274+ if (this.#movers) yield* this.#movers; 275+ else for (const id of this.graph.movers()) if (this.#stable!.has(id)) yield id; 267278 /** The file a line is in, in this set: the one its chain of parents reaches. */ 268279 fileOf(line: LineId): string { @@ -273,7 +284,7 @@
274285 #fileOfN(n: number): string { 275286 const graph = this.graph; 276− if (this.#movers.length === 0) return graph.writtenIn(n);
287+ if (!this.#hasMoves) return graph.writtenIn(n); 277288 const relocs = this.#relocationsN(); 278289 const path: number[] = []; @@ -295,7 +306,7 @@
295306 /** The lines hanging off a node in this set, in layout order. */ 296307 #children(code: number): Child[] { 297308 const graph = this.graph; 298− const relocs = this.#movers.length === 0 ? undefined : this.#relocationsN();
309+ const relocs = this.#hasMoves ? this.#relocationsN() : undefined; 299310 const ns: number[] = []; 300311 graph.childrenN(code, ns); 301312 const out: Child[] = []; @@ -517,7 +528,7 @@
517528 weight(): number { 518529 let n = this.#in.byteLength; 519530 n += 2 * ARRAY_HEADER_BYTES + this.#membershipSeq.byteLength + this.#membershipValue.byteLength; 520− n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length;
531+ if (this.#movers) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length; 521532 n += MAP_HEADER_BYTES; // #files exists from construction. 522533 n += MAP_HEADER_BYTES; // #fileOf exists from construction. 523534 if (this.#stable) n += this.#stable.retainedBytes;