Stream paged child history into layout filtering
of shared·codex-capacity-chair·
Version 129 · Files at version 130
3 files, 73 lines added, 23 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -324,7 +324,9 @@
324324 this.#resolversPages = storedLists<AtomId>(storage, "resolvers"); 325325 this.#togetherPages = storedLists<TogetherLink>(storage, "together"); 326326 this.#fileAtomPages = storedLists<AtomId>(storage, "file-atoms"); 327− this.#runStarts = storedMap(storage, "run-starts");
327+ this.#runStartPages = storedLists<number>(storage, "run-starts", (key) => 328+ JSON.stringify(["number", Number(key)]), 328330 this.#relocationPages = storedLists<RelocationInfo>(storage, "relocations"); 329331 this.#relocatedToPages = storedLists<RelocationInfo>(storage, "relocated-to"); 330332 this.#relocatedToCodePages = storedLists<RelocationInfo>(storage, "relocated-to-code", (key) => @@ -441,6 +443,7 @@
441443 #fileAtomPages: ReturnType<typeof storedLists<AtomId>> | undefined; 442444 /** First lines of runs (their n), by the node they were written after: a line's n, or -1 - a path's index. */ 443445 #runStarts: StateMap<number, number | number[]> = new Map(); 446+ #runStartPages: ReturnType<typeof storedLists<number>> | undefined; 444447 /** 1 for the first line of a run whose atom deleted the line it hangs off: the run replaces it. */ 445448 #replacing: NumberIndex = new Ints(true); 446449 #relocations: StateMap<LineId, RelocationInfo[]> = new Map(); @@ -612,18 +615,20 @@
614617 /** The lines (their n) written right after a node, as childrenOf, into `out`. */ 615619 childrenN(code: number, out: number[]): void { 620+ for (const n of this.eachChildN(code)) out.push(n); 622+ *eachChildN(code: number): IterableIterator<number> { 618625 code + 1 < this.#lineTotal && 619626 this.#lineAtom.at(code + 1) === this.#lineAtom.at(code) && 620627 this.#lineParent.at(code + 1) === code 622− out.push(code + 1);
623− const starts = this.#runStarts.get(code);
624− if (starts === undefined) return;
625− if (Array.isArray(starts)) out.push(...starts);
626− else out.push(starts);
630+ if (this.#runStartPages) yield* this.#runStartPages.values(String(code)); 631+ else yield* listOf(this.#runStarts.get(code)); 629634 replacesN(n: number): boolean { @@ -770,21 +775,10 @@
772777 /** Lines written right after a node (not counting moves), in seq order. */ 773779 childrenOf(node: NodeId): LineId[] { 774− const out: LineId[] = [];
775780 const code = this.#nodeCode(node); 776− if (code === undefined) return out;
777− // The next line of the same run hangs off this one.
778− if (
779− code >= 0 &&
780− code + 1 < this.#lineTotal &&
781− this.#lineAtom.at(code + 1) === this.#lineAtom.at(code) &&
782− this.#lineParent.at(code + 1) === code
783− ) {
784− out.push(this.#lineName(code + 1));
785− }
786− for (const n of listOf(this.#runStarts.get(code))) out.push(this.#lineName(n));
787− return out;
781+ return code === undefined ? [] : [...this.eachChildN(code)].map((n) => this.#lineName(n)); 790784 /** Whether a line starts a run whose atom deleted the line it hangs off. */ @@ -1007,7 +1001,8 @@
10071001 const file = this.#pathOf(run.file); 10081002 let parent = isStartNode(run.parent) ? -1 - file : this.#lineN(run.parent); 10091003 const first = this.#lineTotal; 1010− pushOne(this.#runStarts, parent, first);
1004+ if (this.#runStartPages) this.#runStartPages.append(String(parent), first); 1005+ else pushOne(this.#runStarts, parent, first); 10111006 for (let i = 0; i < run.lines.length; i++) { 10121007 const n = this.#lineTotal; 10131008 this.#lineAtom.push(seq); --- a/src/engine/test/state-source.test.ts
+++ b/src/engine/test/state-source.test.ts
@@ -362,3 +362,60 @@
362362 storage.db.close(); 366+test("child indexes stream bounded pages before filtering historical siblings", () => { 367+ class ObservedStorage extends SqlState { 370+ override read(collection: string, key: string): unknown { 371+ if (collection === "run-starts:pages") this.pageReads++; 372+ return super.read(collection, key); 374+ override write(collection: string, key: string, value: unknown): void { 375+ if (collection === "run-starts:pages") { 376+ assert.ok(Array.isArray(value)); 377+ this.largestPage = Math.max(this.largestPage, value.length); 379+ super.write(collection, key, value); 382+ const storage = new ObservedStorage(); 384+ const oracle = Repo.init({ "a.txt": "one\ntwo\n" }); 385+ const draft = oracle.draft({ edits: [{ file: "a.txt", old: "one", new: "ONE" }] }); 386+ const graph = new Graph({ storage }); 387+ graph.add(oracle.graph.atom("a0")); 388+ for (let seq = 1; seq <= 513; seq++) { 389+ const atom = { ...draft, id: `a${seq}`, seq }; 391+ oracle.graph.add(atom); 393+ const reopened = new Graph({ storage }); 394+ const parent = draft.inserts[0]!.parent; 395+ const code = reopened.codeOf(parent)!; 396+ storage.pageReads = 0; 397+ const iterator = reopened.eachChildN(code); 398+ const first = iterator.next().value!; 399+ assert.equal(first, reopened.lineN(oracle.graph.childrenOf(parent)[0]!)); 400+ assert.ok(storage.pageReads <= 1); 401+ if (storage.pageReads === 0) { 403+ assert.equal(storage.pageReads, 1, "the implicit child needs no page; the next sibling needs one"); 405+ assert.deepEqual(reopened.childrenOf(parent), oracle.graph.childrenOf(parent)); 407+ reopened.childrenOf("a0.0"), 408+ oracle.graph.childrenOf("a0.0"), 409+ "implicit same-run child order is retained", 411+ assert.equal(storage.largestPage, 256); 412+ const membership = new StableOverlaySet(["a0"]); 413+ assert.equal(new View(reopened, membership).text("a.txt"), "one\ntwo\n"); 415+ new View(reopened, membership).sequence("a.txt"), 416+ new View(oracle.graph, membership).sequence("a.txt"), --- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -319,10 +319,8 @@
319319 #children(code: number): Child[] { 320320 const graph = this.graph; 321321 const relocs = this.#hasMoves ? this.#relocationsN() : undefined; 322− const ns: number[] = [];
323− graph.childrenN(code, ns);
324322 const out: Child[] = []; 325− for (const n of ns) {
323+ for (const n of graph.eachChildN(code)) { 326324 const seq = graph.writerSeqOf(n); 327325 if (!this.#has(seq) || relocs?.has(n)) continue; 328326 out.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) });