Cold reads (abso Capacity, issue 664): a packed layout reads only the text pages holding a line it shows; definition code stays in memory up to 4 MB; a stored array's element read skips its length read. Version 274 of shared· capacity· Oct 15 (2026-10-15 22:33 UTC) · Not checked
Layout: each line's file and place are kept and texts are read after the walk, a page at a time, only for shown lines. Every text a file and the files its moved-in lines came from ever had was read for every layout; abso's worker files, split out of index.ts, each read all of index.ts's history. Cold compare 612 to 1224 on abso read text pages 17,080 times, now 2,856. Definition code: at 64 KB every large file's parsed code went row by row into scratch and back (25,000 rows each for index.ts). The visible lines are already in memory, so 4 MB at most doubles a file's text for the call. withDefinitionCode takes the limit; its scratch test passes 64 KB. storedArray: set writes the row and the length together and nothing shrinks, so no row lies past the length; half of a cold layout's reads were the length. Side by side on abso's export, same machine, three rounds: cold collisions 10.5 s to 6.2 s, cold compare 12.6 s to 8.6 s. tsc clean; view, state-cache, state-source, graph, graph-long-text, graph-line-chunks, graph-move-write-membership, definition-code, definitions, changed-definition, collisions-kept-views and draft-reads-stored tests: 73 pass.
Version 273 · Files at version 274
5 files, 63 lines added, 13 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -691,6 +691,13 @@
691 691 *packedTexts(file: number): IterableIterator<string | null> { 692 692 yield* this.#fileTexts!.values(String(file)); 695 + * The texts at positions [page * 256, (page + 1) * 256) of packedTexts(file) (abso Capacity): a layout reads only the 696 + * pages holding a line it shows. 698 + packedTextPage(file: number, page: number): readonly (string | null)[] { 699 + return this.#fileTexts!.pageAt(String(file), page); 694 701 *packedDeletes(file: number): IterableIterator<PackedDelete> { 695 702 yield* this.#fileDeletes!.values(String(file)); --- a/src/engine/state-source.ts
+++ b/src/engine/state-source.ts
@@ -348,7 +348,9 @@
348 348 return new Proxy([] as T[], { 349 349 get(target, key, receiver) { 350 350 if (key === "length") return length(); 351 − if (indexKey(key)) return Number(key) < length() ? read(key) : undefined;
351 + // No length read first (abso Capacity): set writes the row and the length together and nothing shrinks, so no 352 + // row lies past the length. Half of a cold layout's reads were this length. 353 + if (indexKey(key)) return read(key); 352 354 return Reflect.get(target, key, receiver); 354 356 set(_target, key, value) { @@ -419,6 +421,14 @@
420 422 for (const [key] of storage.entries(`${name}:lengths`)) yield key; 424 + /** Values [index * PAGE, (index + 1) * PAGE) of `key`'s list: one page, not the whole list (abso Capacity). */ 425 + pageAt(key: string, index: number): readonly V[] { 426 + const count = length(key); 427 + const values = page(key, index); 428 + if (values.length !== Math.min(pageSize, count - index * pageSize)) 429 + throw new Error(`incomplete writer list page in ${name}`); 422 432 *values(key: string): IterableIterator<V> { 423 433 const count = length(key); 424 434 for (let start = 0; start < count; start += pageSize) { --- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -755,7 +755,11 @@
755 755 const relocs = this.#hasMoves ? this.#relocationsN() : undefined; 756 756 const files = new Set<number>([-1 - start, ...(this.#movedInFrom().get(path) ?? [])]); 757 757 const children = new Map<number, Child[]>(); 758 − const texts = new Map<number, string | null>();
758 + // Where each line's text is: its file and its place in that file's packed history. Texts are read after the walk, 759 + // only for the lines it shows (abso Capacity): every text a file and its moved-in sources ever had was read for 760 + // every layout, and abso's worker files, split out of index.ts, read all of index.ts's history each. 761 + const textFile = new Map<number, number>(); 762 + const textPlace = new Map<number, number>(); 759 763 const writers = new Map<number, number>(); 760 764 const places = new Map<number, number>(); 761 765 const deleters = new Map<number, number[]>(); @@ -770,9 +774,9 @@
772 776 for (const file of files) { 773 − const lineTexts = graph.packedTexts(file);
774 778 for (const [n, parent, seq, k, replaces] of graph.packedLines(file)) { 775 − const text = lineTexts.next().value as string | null;
776 780 if (!this.#has(seq)) continue; 777 781 const moved = relocs?.get(n); @@ -785,7 +789,8 @@
785 789 const list = children.get(at); 786 790 if (list) list.push(child); 787 791 else children.set(at, [child]); 788 − texts.set(n, text);
792 + textFile.set(n, file); 793 + textPlace.set(n, place); 789 794 writers.set(n, seq); @@ -796,7 +801,7 @@
796 801 else deleters.set(n, [deleter]); 798 803 // Runs written elsewhere under this file's lines follow them; the loop reads their files too. 799 − for (const [parent, into] of graph.packedCrossings(file)) if (texts.has(parent)) files.add(into);
804 + for (const [parent, into] of graph.packedCrossings(file)) if (writers.has(parent)) files.add(into); 801 806 const order = (a: Child, b: Child) => 802 807 b.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k; @@ -809,6 +814,18 @@
809 814 const seqWriter: number[] = []; 810 815 const seqK: number[] = []; 811 816 const deletedBy = new Map<number, readonly number[]>(); 817 + // The text pages read for this layout, by file and page: each at most once. 818 + const pages = new Map<number, Map<number, readonly (string | null)[]>>(); 819 + const textOf = (n: number): string | null => { 820 + const file = textFile.get(n)!; 821 + const place = textPlace.get(n)!; 822 + const page = Math.floor(place / 256); 823 + let byPage = pages.get(file); 824 + if (!byPage) pages.set(file, (byPage = new Map())); 825 + let texts = byPage.get(page); 826 + if (!texts) byPage.set(page, (texts = graph.packedTextPage(file, page))); 827 + return texts[place % 256] ?? null; 812 829 const stack: number[] = []; 813 830 const push = (code: number) => { 814 831 const list = children.get(code); @@ -824,7 +841,7 @@
824 841 if (complete) deletedBy.set(seq.length, by.length > 1 ? by.sort(priority) : by); 827 − visText.push(texts.get(n) ?? null);
844 + visText.push(textOf(n)); 828 845 visWriter.push(writers.get(n)!); 829 846 visK.push(places.get(n)!); --- a/worker/project/definition-code.ts
+++ b/worker/project/definition-code.ts
@@ -2,11 +2,19 @@
2 2 import { StoredText } from "../../src/engine/stored-text.ts"; 3 3 import { eachCodeLine } from "./names.ts"; 6 + * Characters of parsed code kept in memory before the rest goes to scratch (abso Capacity). The visible lines it is 7 + * parsed from are already in memory, so this at most doubles a file's text for the call; at 64 KB, every large file's 8 + * code went row by row into scratch and back, 25,000 rows each for abso's index.ts, a third of a cold collisions call. 10 + export const DEFINITION_CODE_IN_MEMORY = 4_000_000; 5 12 /** Parsed code is temporary derived data, discarded with the scratch transaction. */ 6 13 export function withDefinitionCode<T>( 8 15 visible: readonly VisibleLine[], 9 16 operation: (code: readonly string[]) => T, 17 + inMemory = DEFINITION_CODE_IN_MEMORY, 11 19 function* source() { 12 20 for (const line of visible) yield line.text; @@ -16,7 +24,7 @@
16 24 let prefix: string[] = []; 18 26 let pending = iterator.next(); 19 − while (!pending.done && size + pending.value.length + 1 <= 65536) {
27 + while (!pending.done && size + pending.value.length + 1 <= inMemory) { 20 28 prefix.push(pending.value); 21 29 size += pending.value.length + 1; 22 30 pending = iterator.next(); --- a/worker/project/test/definition-code.test.ts
+++ b/worker/project/test/definition-code.test.ts
@@ -39,18 +39,26 @@
39 39 const rows = () => storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all(); 40 40 const before = rows(); 41 41 let escaped: readonly string[] = []; 42 − withDefinitionCode(view, view.buildLines("f.ts"), (code) => {
43 − escaped = code;
44 + view.buildLines("f.ts"), 44 47 assert.deepEqual(code.slice(0, 4), expected.slice(0, 4)); 45 48 for (let i = code.length - 1; i >= 0; i--) assert.equal(code[i], expected[i]); 46 − for (let i = 0; i < code.length; i++) assert.equal(code[i], expected[i]);
47 − });
49 + for (let i = 0; i < code.length; i++) assert.equal(code[i], expected[i]); 48 53 assert.throws(() => escaped[0], /scope has ended/); 49 54 assert.deepEqual(rows(), before); 50 55 assert.deepEqual(definitionsIn(view), definitionsIn(new View(memory, ["a0"]))); 51 56 assert.deepEqual(definedTwice(view, "f.ts"), definedTwice(new View(memory, ["a0"]), "f.ts")); 52 57 assert.deepEqual(rows(), before); 53 58 storage.failWrite = storage.writes + 2; 54 − assert.throws(() => withDefinitionCode(view, view.buildLines("f.ts"), () => []), /injected storage failure/);
60 + () => withDefinitionCode(view, view.buildLines("f.ts"), () => [], 65536), 61 + /injected storage failure/, 55 63 assert.deepEqual(rows(), before);