An atom's long lists kept 256 values to a row (list format 2; disk-first 2; abso Capacity). Version 239 of shared· capacity· Oct 15 (2026-10-15 12:08 UTC) · Not checked
Collisions reads every key each draft atom writes. An atom's lists over 256 values were stored a row per value (readonlyStoredArray). On website, collisions read 75,000 of those rows (a draft deleting a big file has thousands of keys).
New atoms store such lists 256 values to a row (writePagedArray) and mark it listFormat 2. readonlyPagedArray reads them a page at a time, in order or by index. An atom stored before keeps listFormat 1 and its row per value, and reads as before. The format is per atom record, so old and new atoms sit side by side. Only atoms stored from this version on, or a fresh adoption, get pages. On website adopted fresh, cold, one opened writer: collisions 9.6 s to 4.7 s (79,000 reads to 3,900), matching replay (0.4 s). Adoption took 87 s instead of 119 s.
Tests: graph-atom-list-pages (new: a new atom's list is paged and reads whole; an atom rewritten as list format 1 reads whole). graph-atom-list-rollback now injects its failure on the list's second page. Every engine test (290 of 290), every worker/project test (341 of 341); tsc and biome clean.
Version 238 · Files at version 239
4 files, 158 lines added, 12 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -21,12 +21,14 @@
21 21 import { withProjectionScratch } from "./projection-scratch.ts"; 23 23 type GraphStateStorage, 24 25 readonlyStoredArray, 30 32 } from "./state-source.ts"; 31 33 import { StoredText } from "./stored-text.ts"; @@ -353,7 +355,7 @@
353 355 insertionFormat?: 1; 354 356 insertionRuns?: string; 356 − listFormat?: 1;
357 359 listCount?: number; 358 360 lists?: Partial<Record<(typeof listFields)[number], string>>; @@ -365,9 +367,9 @@
365 367 const encoded = value as unknown as Partial<EncodedAtom>; 366 368 if (encoded.textFormat !== undefined && encoded.textFormat !== 1) 367 369 throw Error("invalid stored atom text format"); 368 − if (encoded.listFormat !== undefined && encoded.listFormat !== 1)
370 + if (encoded.listFormat !== undefined && encoded.listFormat !== 1 && encoded.listFormat !== 2) 369 371 throw Error("invalid stored atom list format"); 370 − if (encoded.listFormat === 1) {
372 + if (encoded.listFormat === 1 || encoded.listFormat === 2) { 373 375 typeof encoded.lists !== "object" || @@ -382,7 +384,10 @@
382 384 if (encoded.insertionFormat !== undefined && encoded.insertionFormat !== 1) 383 385 throw Error("invalid stored insertion format"); 385 − encoded.textFormat === 1 || encoded.listFormat === 1 || encoded.insertionFormat === 1
387 + encoded.textFormat === 1 || 388 + encoded.listFormat === 1 || 389 + encoded.listFormat === 2 || 390 + encoded.insertionFormat === 1 388 393 if (encoded.insertionFormat === 1 && (!Array.isArray(atom.inserts) || atom.inserts.length !== 0)) @@ -462,7 +467,8 @@
462 467 (author) => author, 465 − if (encoded.listFormat !== 1 && encoded.insertionFormat !== 1) return streamed(frozen);
470 + if (encoded.listFormat !== 1 && encoded.listFormat !== 2 && encoded.insertionFormat !== 1) 471 + return streamed(frozen); 466 472 const result = { ...frozen }; 467 473 if (encoded.insertionFormat === 1) { 468 474 const collection = `atom-inserts:${atom.seq}`; @@ -492,7 +498,11 @@
492 498 if (collection === undefined) continue; 493 499 if (typeof collection !== "string" || collection !== `atom-list:${atom.seq}:${field}`) 494 500 throw Error("invalid stored atom list reference"); 495 − Object.defineProperty(result, field, { value: readonlyStoredArray(storage, collection), enumerable: true });
502 + encoded.listFormat === 2 503 + ? readonlyPagedArray(storage, collection) 504 + : readonlyStoredArray(storage, collection); 505 + Object.defineProperty(result, field, { value: list, enumerable: true }); 497 507 return streamed(Object.freeze(result)); @@ -502,9 +512,9 @@
502 512 for (const field of listFields) { 503 513 const values = input[field]; 504 514 if (!values || values.length <= 256) continue; 515 + // 256 to a row from list format 2 on (abso Capacity); an atom stored before keeps a row per value. 505 516 const collection = `atom-list:${input.seq}:${field}`; 506 − const saved = storedArray<unknown>(storage, collection);
507 − for (const value of values) saved.push(value);
517 + writePagedArray(storage, collection, values); 508 518 lists[field] = collection; 509 519 Object.defineProperty(atom, field, { value: [], enumerable: true }); @@ -535,12 +545,12 @@
536 546 insertionFormat: 1, 537 547 insertionRuns: collection, 538 − ...(hasLists ? { listFormat: 1, listCount, lists } : {}),
548 + ...(hasLists ? { listFormat: 2, listCount, lists } : {}), 539 549 atom: { ...atom, inserts: [] }, 542 552 if (!atom.inserts.some((run) => run.lines.some((line) => line.length > StoredText.chunkCharacters))) 543 − return hasLists ? { listFormat: 1, listCount, lists, atom } : atom;
553 + return hasLists ? { listFormat: 2, listCount, lists, atom } : atom; 545 555 const inserts = atom.inserts.map((run) => ({ @@ -553,7 +563,7 @@
556 − ...(hasLists ? { listFormat: 1, listCount, lists } : {}),
566 + ...(hasLists ? { listFormat: 2, listCount, lists } : {}), 557 567 atom: { ...atom, inserts }, --- a/src/engine/state-source.ts
+++ b/src/engine/state-source.ts
@@ -583,6 +583,70 @@
585 585 /** Read-only array access to individually stored values, without copying the list. */ 586 + /** How many values an atom's long list keeps to a row (readonlyPagedArray). */ 587 + export const LIST_PAGE = 256; 589 + /** Write a list as readonlyPagedArray reads it: its values LIST_PAGE to a row, and its length. */ 590 + export function writePagedArray(storage: GraphStateStorage, name: string, values: readonly unknown[]): void { 591 + for (let start = 0; start < values.length; start += LIST_PAGE) 592 + storage.write(`${name}:pages`, String(start / LIST_PAGE), values.slice(start, start + LIST_PAGE)); 593 + storage.write(`${name}:meta`, "length", values.length); 597 + * A stored list read a page of LIST_PAGE values at a time (abso Capacity): an atom's long lists, which 598 + * readonlyStoredArray reads a row per value. Collisions read every key a draft's atoms write; a draft deleting a big 599 + * file has thousands, and that was a row each. 601 + export function readonlyPagedArray<T>(storage: GraphStateStorage, name: string): readonly T[] { 602 + const length = () => { 603 + const count = storage.read(`${name}:meta`, "length"); 604 + if (!Number.isSafeInteger(count) || (count as number) < 0) throw Error("invalid stored list length"); 605 + return count as number; 609 + let page: readonly unknown[] = []; 610 + const at = (index: number): T => { 611 + const p = Math.floor(index / LIST_PAGE); 613 + const raw = storage.read(`${name}:pages`, String(p)); 614 + if (!Array.isArray(raw)) throw Error("missing stored list page"); 618 + const value = page[index % LIST_PAGE]; 619 + if (value === undefined) throw Error("missing stored list element"); 620 + return (value && typeof value === "object" ? Object.freeze(value) : value) as T; 622 + function* inOrder(): IterableIterator<T> { 623 + const count = length(); 624 + for (let index = 0; index < count; index++) yield at(index); 626 + return new Proxy([] as T[], { 627 + get(target, key, receiver) { 628 + if (key === "length") return length(); 629 + if (key === Symbol.iterator || key === "values") return inOrder; 630 + if (indexKey(key)) return Number(key) >= length() ? undefined : at(Number(key)); 631 + return Reflect.get(target, key, receiver); 634 + return indexKey(key) ? Number(key) < length() : Reflect.has(target, key); 637 + return [...Array.from({ length: length() }, (_, index) => String(index)), "length"]; 639 + getOwnPropertyDescriptor(target, key) { 640 + if (indexKey(key) && Number(key) < length()) 641 + return { configurable: true, enumerable: true, writable: false, value: at(Number(key)) }; 642 + return Reflect.getOwnPropertyDescriptor(target, key); 645 + throw Error("stored lists are read-only"); 586 650 export function readonlyStoredArray<T>( 587 651 storage: GraphStateStorage, --- /dev/null
+++ b/src/engine/test/graph-atom-list-pages.test.ts
@@ -0,0 +1,71 @@
1 + // An atom's long lists (abso Capacity, readonlyPagedArray): a new atom keeps them 256 values to a row (list format 2), 2 + // and an atom stored before keeps a row per value (list format 1). Both read back whole, in order and by index. 4 + import assert from "node:assert/strict"; 5 + import { test } from "node:test"; 6 + import { buildBase, Graph } from "../index.ts"; 7 + import { lineKey } from "../keys.ts"; 8 + import { storedArray } from "../state-source.ts"; 9 + import type { Atom } from "../types.ts"; 10 + import { SqlState } from "./storage-helper.ts"; 12 + function listAtom(keys: string[]): Atom { 31 + const keys = Array.from({ length: 600 }, () => lineKey("a0.0")); 33 + test("a new atom's long list is kept 256 values to a row and reads back whole", () => { 34 + const storage = new SqlState(); 36 + const graph = new Graph({ storage }); 37 + graph.add(buildBase({ "a.txt": "base" }, { author: "fixture", message: "base", time: 0 })); 38 + graph.add(listAtom(keys)); 39 + assert.equal((storage.read("atoms", "1") as { listFormat?: number }).listFormat, 2); 40 + assert.equal((storage.read("atom-list:1:writes:pages", "2") as unknown[]).length, 600 - 512); 41 + assert.equal(storage.read("atom-list:1:writes", "0"), undefined, "no row per value"); 42 + const writes = new Graph({ storage }).atom("a1").writes; 43 + assert.deepEqual([...writes], keys); 44 + assert.equal(writes.length, 600); 45 + assert.equal(writes[300], keys[300]); 46 + assert.equal(writes[600], undefined); 52 + test("an atom stored before pages, a row per value, still reads back whole", () => { 53 + const storage = new SqlState(); 55 + const graph = new Graph({ storage }); 56 + graph.add(buildBase({ "a.txt": "base" }, { author: "fixture", message: "base", time: 0 })); 57 + graph.add(listAtom(keys)); 58 + // Rewrite it as list format 1 kept it: a row per value and no pages. 59 + const record = storage.read("atoms", "1") as Record<string, unknown>; 60 + for (let page = 0; page < 3; page++) storage.remove("atom-list:1:writes:pages", String(page)); 61 + storage.remove("atom-list:1:writes:meta", "length"); 62 + const rows = storedArray<string>(storage, "atom-list:1:writes"); 63 + for (const key of keys) rows.push(key); 64 + storage.write("atoms", "1", { ...record, listFormat: 1 }); 65 + const writes = new Graph({ storage }).atom("a1").writes; 66 + assert.deepEqual([...writes], keys); 67 + assert.equal(writes[599], keys[599]); --- a/src/engine/test/graph-atom-list-rollback.test.ts
+++ b/src/engine/test/graph-atom-list-rollback.test.ts
@@ -31,7 +31,8 @@
31 31 const write = storage.write.bind(storage); 32 32 storage.write = (collection, key, value) => { 33 33 write(collection, key, value); 34 − if (collection === "atom-list:1:writes" && key === "128") throw Error("injected list persistence failure");
34 + // Partway through: the second of the list's two pages (256 values to a page). 35 + if (collection === "atom-list:1:writes:pages" && key === "1") throw Error("injected list persistence failure"); 36 37 assert.throws(() => graph.add(atom), /injected list persistence failure/); 37 38 assert.deepEqual(storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all(), before);