Lamport numbers 256 to a row, and movers read once per graph (disk-first 2; abso Capacity). Version 228 of shared· capacity· Oct 15 (2026-10-15 08:39 UTC) · Not checked
After 226, where on typescript-rs still read about 12,000 lamport rows (one per atom, for ordering a file's lines) and the movers list (1,024 rows, one per atom that moves lines) nine times over.
A stored graph keeps its atoms' lamport numbers in pages of 256 (lamport-pages), from a fresh graph on. A graph that already keeps them a row each goes on doing so. The choice is made when a number is first needed, so opening still reads nothing. A stored graph reads its movers once and keeps them, checked against the stored list's length and last mover on each use so a rolled-back add can't leave one behind. On typescript-rs adopted fresh, cold: where 59,000 reads to 39,000. Its time locally is now mostly work, not reads (5.2 s). 26 of 27 answers match replay; collisions still runs past 60 s.
New test lamport-pages: a fresh graph keeps pages and reopens with them; a graph with a row each reads them and writes the next atom as a row. Named tests: every engine test (288 of 288), membership-pages, stored-closure, draft-reads-stored, version-files, packed-crossings, project-writer-storage, writer-adoption, moves, view-cache (36 of 36); tsc and biome clean.
Version 227 · Files at version 228
2 files, 82 lines added, 8 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -273,6 +273,18 @@
273 273 readonly #childCounts?: ChildCounts; 274 274 #atoms: Atom[] = []; 275 275 #lamport: number[] = []; 277 + * A stored graph's lamport numbers, 256 to a row (abso Capacity). A graph that already has them a row each (#lamport) 278 + * keeps that; the choice is made when first needed, so opening reads nothing. 280 + #lamportPaged: ReturnType<typeof storedNumbers> | undefined; 281 + #lamportPagesKnown: ReturnType<typeof storedNumbers> | null | undefined; 282 + get #lamportPages(): ReturnType<typeof storedNumbers> | null { 283 + if (this.#lamportPagesKnown === undefined) 284 + this.#lamportPagesKnown = 285 + this.#lamportPaged && this.#storage!.read("lamport:meta", "length") === undefined ? this.#lamportPaged : null; 286 + return this.#lamportPagesKnown; 276 288 // Lines, packed (#46): line n is the k-th line atom seq wrote, n = #lineBase[seq] + k. Each holds its 277 289 // atom's seq, the file it was written in (an index into #paths) and its parent (a line's n, or -1 - a 278 290 // path's index for a file's start). Its text stays in the atom. Ids and LineInfo objects are made only @@ -547,6 +559,7 @@
549 561 this.#lamport = storedArray(storage, "lamport"); 562 + this.#lamportPaged = storedNumbers(storage, "lamport-pages"); 550 563 this.#lineBase = storedArray(storage, "line-base"); 551 564 this.#paths = storedArray(storage, "paths"); 552 565 this.#movers = storedArray(storage, "movers"); @@ -793,6 +806,8 @@
793 806 #relocatedToCode: StateMap<number, RelocationInfo[]> = new Map(); 794 807 #relocatedToCodePages: ReturnType<typeof storedLists<RelocationInfo>> | undefined; 795 808 #movers: AtomId[] = []; 809 + /** A stored graph's movers, read once (abso Capacity): every view walks them, a row each. */ 810 + #moverList: AtomId[] | undefined; 796 811 #atomFiles: (readonly string[])[] = []; 797 812 /** Each atom's deleted lines by n, packed (#46), or undefined when it deletes none. */ 798 813 #atomDeletes: (readonly number[] | Int32Array | undefined)[] = []; @@ -830,7 +845,7 @@
830 845 /** lamport(a) = 1 + max lamport of its deps; 0 for an atom with no deps (such as a0). */ 831 846 lamport(id: AtomId): number { 832 847 if (!this.has(id)) throw new EngineError("unknown-atom", `no atom ${id}`); 833 − return this.#lamport[this.#seq(id)]!;
848 + return this.#lamportAt(this.#seq(id)); 836 851 /** Compare by priority (lamport, then seq). Negative when a comes first (is lower). */ @@ -969,8 +984,12 @@
969 984 return atomId(seq); 987 + #lamportAt(seq: number): number { 988 + return this.#lamportPages ? this.#lamportPages.at(seq) : this.#lamport[seq]!; 972 991 lamportOfSeq(seq: number): number { 973 − return this.#lamport[seq]!;
992 + return this.#lamportAt(seq); 976 995 /** The file line n was written in. */ @@ -1256,7 +1275,13 @@
1257 1276 /** Atoms that move lines, in seq order. */ 1258 1277 movers(): readonly AtomId[] { 1259 − return this.#movers;
1278 + if (!this.#storage) return this.#movers; 1279 + // Checked against the stored list's length and last mover, so a rolled-back add can't leave one behind. 1280 + const length = this.#movers.length; 1281 + const list = this.#moverList; 1282 + if (list && list.length === length && (length === 0 || list[length - 1] === this.#movers[length - 1])) return list; 1283 + this.#moverList = [...this.#movers]; 1284 + return this.#moverList; 1262 1287 /** Atoms that list this one directly in their deps. */ @@ -1330,7 +1355,7 @@
1330 1355 * the search never descends below the target's lamport. 1332 1357 #reaches(from: Iterable<AtomId>, target: AtomId): boolean { 1333 − const floor = this.#lamport[this.#seq(target)]!;
1358 + const floor = this.#lamportAt(this.#seq(target)); 1334 1359 if (this.#storage) { 1335 1360 return this.withProjectionScratch((scratch) => { @@ -1341,13 +1366,13 @@
1342 1367 for (const id of from) { 1343 1368 if (id === target) return true; 1344 − if (this.#lamport[this.#seq(id)]! > floor) push(id);
1369 + if (this.#lamportAt(this.#seq(id)) > floor) push(id); 1346 1371 while (length > 0) { 1347 1372 const key = String(--length); 1348 1373 const current = scratch.read("stack", key) as AtomId; 1349 1374 scratch.remove("stack", key); 1350 − if (this.#lamport[this.#seq(current)]! <= floor) continue;
1375 + if (this.#lamportAt(this.#seq(current)) <= floor) continue; 1351 1376 for (const dependency of this.atom(current).deps) { 1352 1377 if (dependency === target) return true; 1353 1378 push(dependency); @@ -1363,7 +1388,7 @@
1363 1388 if (cur === target) return true; 1364 1389 if (seen.has(cur)) continue; 1366 − if (this.#lamport[this.#seq(cur)]! <= floor) continue;
1391 + if (this.#lamportAt(this.#seq(cur)) <= floor) continue; 1367 1392 for (const d of this.atom(cur).deps) if (!seen.has(d)) stack.push(d); @@ -1507,7 +1532,8 @@
1507 1532 this.#atoms.push(this.#storage ? atom : frozen); 1509 1534 for (const d of deps) lam = Math.max(lam, this.lamport(d) + 1); 1510 − this.#lamport.push(lam);
1535 + if (this.#lamportPages) this.#lamportPages.push(lam); 1536 + else this.#lamport.push(lam); 1512 1538 // The run starts' parents and the deleted lines, as codes, before this atom's own lines exist. 1513 1539 // The atom as given: with lists kept elsewhere, the frozen one reads them back from the log. --- /dev/null
+++ b/src/engine/test/lamport-pages.test.ts
@@ -0,0 +1,48 @@
1 + // A stored graph's lamport numbers (abso Capacity, Graph.#lamportPages): a fresh graph keeps them 256 to a row, and a 2 + // graph that already keeps them a row each goes on doing so. Both answer every atom's lamport as the in-memory graph. 4 + import assert from "node:assert/strict"; 5 + import { test } from "node:test"; 6 + import { Graph } from "../index.ts"; 7 + import { storedArray } from "../state-source.ts"; 8 + import { lines, Repo } from "./helpers.ts"; 9 + import { SqlState } from "./storage-helper.ts"; 12 + const oracle = Repo.init({ "a.txt": lines("one", "two", "three"), "b.txt": "" }); 13 + oracle.edit({ edits: [{ file: "a.txt", old: "two", new: "TWO" }] }); 14 + oracle.edit({ move: { file: "a.txt", start: 1, end: 2, to: "b.txt", at: 0 } }); 15 + oracle.edit({ edits: [{ file: "a.txt", old: "three", new: "three\nfour" }] }); 19 + test("a fresh stored graph keeps lamport numbers in pages and reopens with them", () => { 20 + const oracle = history(); 21 + const storage = new SqlState(); 22 + const original = new Graph({ storage }); 23 + for (const atom of oracle.graph.atoms()) original.add(atom); 24 + assert.equal(storage.read("lamport:meta", "length"), undefined, "no row each"); 25 + assert.equal(storage.read("lamport-pages:meta", "length"), oracle.graph.size); 26 + const reopened = new Graph({ storage }); 27 + for (const atom of oracle.graph.atoms()) assert.equal(reopened.lamport(atom.id), oracle.graph.lamport(atom.id)); 28 + assert.deepEqual([...reopened.movers()], [...oracle.graph.movers()]); 31 + test("a graph that keeps lamport numbers a row each goes on reading and writing rows", () => { 32 + const oracle = history(); 33 + const atoms = [...oracle.graph.atoms()]; 34 + const storage = new SqlState(); 35 + const original = new Graph({ storage }); 36 + for (const atom of atoms.slice(0, -1)) original.add(atom); 37 + // As a graph adopted before pages: the same numbers a row each, and no pages. 38 + const rows = storedArray<number>(storage, "lamport"); 39 + for (let seq = 0; seq < atoms.length - 1; seq++) rows.push(original.lamportOfSeq(seq)); 40 + storage.remove("lamport-pages:meta", "length"); 41 + storage.remove("lamport-pages:meta", "format"); 42 + const reopened = new Graph({ storage }); 43 + for (const atom of atoms.slice(0, -1)) assert.equal(reopened.lamport(atom.id), oracle.graph.lamport(atom.id)); 44 + reopened.add(atoms.at(-1)!); 45 + assert.equal(storage.read("lamport:meta", "length"), atoms.length, "the new atom gets a row"); 46 + assert.equal(reopened.lamport(atoms.at(-1)!.id), oracle.graph.lamport(atoms.at(-1)!.id)); 47 + assert.equal(storage.read("lamport-pages:meta", "length"), undefined);