{"draft":null,"from":231,"to":318,"files":[{"path":"src/engine/view.ts","added":325,"removed":46,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -10,7 +10,7 @@\n // made when a caller asks for them, and the latest asked for is kept.\n \n import type { Graph, RelocationInfo } from \"./graph.ts\";\n-import { isStartNode, seqOf } from \"./keys.ts\";\n+import { atomId, isStartNode, lineId, seqOf } from \"./keys.ts\";\n import { resolveScratchMoves, scratchLayoutNodes } from \"./relocation-scratch.ts\";\n import { isClosedSet, StableSet } from \"./state-source.ts\";\n import { joinLines } from \"./text.ts\";\n@@ -49,12 +49,49 @@\n /** Structural read boundary shared by historical and transaction-bound projections. */\n export type ReadView = Omit<\n \tPick<View, keyof View>,\n-\t\"sameText\" | \"visibleCount\" | \"shareMoves\" | \"fileOfNumber\" | \"lineNumberOf\" | \"visibleWriters\"\n+\t| \"sameText\"\n+\t| \"visibleCount\"\n+\t| \"shareMoves\"\n+\t| \"shareFiles\"\n+\t| \"knowPaths\"\n+\t| \"laidOut\"\n+\t| \"forget\"\n+\t| \"placedDifferently\"\n+\t| \"fileOfNumber\"\n+\t| \"lineNumberOf\"\n+\t| \"visibleWriters\"\n > & {\n \tsameText(path: string, other: ReadView): boolean;\n };\n \n-const NO_ATTRS: FileAttrs = { exists: false, exec: false, link: null, blob: null };\n+const NO_ATTRS: FileAttrs = {\n+\texists: false,\n+\texec: false,\n+\tlink: null,\n+\tblob: null,\n+};\n+\n+/**\n+ * A file layout's bytes, less its lookup indexes, worked out once per layout (abso Capacity): a layout never changes\n+ * once made, and the view cache weighs every view it holds on every lookup, which summed every line's text again.\n+ */\n+const layoutSizes = new WeakMap<FileState, number>();\n+function layoutBytes(f: FileState): number {\n+\tlet n = layoutSizes.get(f);\n+\tif (n !== undefined) return n;\n+\t// FileState and FileAttrs are per laid-out file; typed-array headers are separate from their payload byte lengths.\n+\tn = OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES;\n+\tn += f.seq.byteLength + f.vis.byteLength;\n+\tif (f.visText) for (const text of f.visText) n += REFERENCE_BYTES + (text === null ? 0 : stringBytes(text));\n+\tif (f.visWriter) n += 4 * f.visWriter.length;\n+\tif (f.visK) n += 4 * f.visK.length;\n+\tif (f.seqWriter) n += 4 * f.seqWriter.length;\n+\tif (f.seqK) n += 4 * f.seqK.length;\n+\tn += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + ARRAY_HEADER_BYTES) * f.deletedBy.size;\n+\tfor (const deleters of f.deletedBy.values()) n += REFERENCE_BYTES * deleters.length;\n+\tlayoutSizes.set(f, n);\n+\treturn n;\n+}\n const NONE: readonly never[] = Object.freeze([]);\n \n interface FileState {\n@@ -71,6 +108,11 @@\n \treadonly visText?: readonly (string | null)[];\n \t/** Its visible lines' writers' seqs, when laid out from the file's packed history. */\n \treadonly visWriter?: Int32Array;\n+\t/** Its visible lines' places in their writers (a line id's k), when laid out from packed history. */\n+\treadonly visK?: Int32Array;\n+\t/** The same for every line of seq, when complete and laid out from packed history. */\n+\treadonly seqWriter?: Int32Array;\n+\treadonly seqK?: Int32Array;\n \t/** Positions by n, built the first time they're asked for. */\n \tseqIndex?: Map<number, number>;\n \tvisIndex?: Map<number, number>;\n@@ -86,8 +128,17 @@\n }\n \n const EMPTY_INTS = new Int32Array(0);\n-const NO_FILE: FileState = { attrs: NO_ATTRS, complete: true, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };\n+const NO_FILE: FileState = {\n+\tattrs: NO_ATTRS,\n+\tcomplete: true,\n+\tseq: EMPTY_INTS,\n+\tvis: EMPTY_INTS,\n+\tdeletedBy: new Map(),\n+};\n \n+/** A count of file uses shared by every view, so a cache can tell which layout across its views was used longest ago. */\n+let fileUses = 0;\n+\n // Approximate retained-container costs for View.weight(): reference slots,\n // object headers, and map/set entries. These are cache-budget units, not V8\n // heap measurements. Referenced Graph strings and records are not charged.\n@@ -97,7 +148,16 @@\n const MAP_ENTRY_BYTES = 40;\n const SET_ENTRY_BYTES = 32;\n const REFERENCE_BYTES = 8;\n+const STRING_HEADER_BYTES = 16;\n /**\n+ * What V8 holds for a string of its own (abso Capacity, measured on abso's layouts): a header, then one byte a\n+ * character when every character fits in Latin-1 and two otherwise, rounded up to 8.\n+ */\n+export function stringBytes(text: string): number {\n+\tconst chars = /[^\\u0000-\\u00ff]/.test(text) ? 2 * text.length : text.length;\n+\treturn (STRING_HEADER_BYTES + chars + 7) & ~7;\n+}\n+/**\n  * Moved lines a view resolves once and keeps (abso Capacity): a stored graph otherwise resolved every move again on\n  * disk for each file it laid out. Game's whole history has 290; past this many, the view goes back to that.\n  */\n@@ -124,7 +184,10 @@\n \treadonly #graph: Graph;\n \t#seen = 0;\n \t/** Movers in seq order, with the numbers of the lines each moves, in its moves' order. */\n-\treadonly movers: { readonly seq: number; readonly lines: readonly number[] }[] = [];\n+\treadonly movers: {\n+\t\treadonly seq: number;\n+\t\treadonly lines: readonly number[];\n+\t}[] = [];\n \t/** Every relocation of each moved line, by the line's number, in seq order. */\n \treadonly byLine = new Map<number, MoveEntry[]>();\n \treadonly #climbed = new Map<number, number>();\n@@ -133,9 +196,14 @@\n \t\tthis.#graph = graph;\n \t}\n \n+\t/** The graph's accepted count when movers were last taken in: nothing to read while it's the same. */\n+\t#stamp = -1;\n+\n \t/** Take in movers the graph gained since. */\n \tupdate(): this {\n \t\tconst graph = this.#graph;\n+\t\tif (this.#stamp === graph.accepted) return this;\n+\t\tthis.#stamp = graph.accepted;\n \t\tconst all = graph.movers();\n \t\tif (this.#seen === all.length) return this;\n \t\tfor (; this.#seen < all.length; this.#seen++) {\n@@ -143,7 +211,12 @@\n \t\t\tconst lines: number[] = [];\n \t\t\tgraph.atom(id).moves!.forEach((m, k) => {\n \t\t\t\tconst n = graph.lineN(m.line);\n-\t\t\t\tconst r: RelocationInfo = Object.freeze({ atom: id, line: m.line, parent: m.parent, k });\n+\t\t\t\tconst r: RelocationInfo = Object.freeze({\n+\t\t\t\t\tatom: id,\n+\t\t\t\t\tline: m.line,\n+\t\t\t\t\tparent: m.parent,\n+\t\t\t\t\tk,\n+\t\t\t\t});\n \t\t\t\tconst entry: MoveEntry = { n, r, parent: graph.codeOf(m.parent)! };\n \t\t\t\tconst list = this.byLine.get(n);\n \t\t\t\tif (list) list.push(entry);\n@@ -384,6 +457,38 @@\n \t\tthis.#movesFrom = from;\n \t}\n \n+\t/**\n+\t * Files laid out as another view lays them out (abso Capacity): a draft's view over its base's, for every file the\n+\t * draft doesn't touch. The caller knows what the draft is; `except` is asked once, when a file is first needed, for\n+\t * the files the draft touches, or undefined when nothing can be shared (a draft that moves lines). Without it, an\n+\t * agent's tree, files and blame laid out every file of the project again for each agent.\n+\t */\n+\tshareFiles(from: View, except: () => ReadonlySet<string> | undefined): void {\n+\t\tif (from.graph !== this.graph || from === this || this.#filesFrom !== undefined) return;\n+\t\tthis.#filesFrom = { from, except };\n+\t}\n+\t#filesFrom: { from: View; except: () => ReadonlySet<string> | undefined } | null | undefined;\n+\n+\t/**\n+\t * The files this view's set has, from what the project keeps for a published version (abso Capacity): hasFile\n+\t * answers from it, not a layout of the file. Collisions resolving an import tried up to seven names, and each one\n+\t * laid its file out. `paths` is asked once, when first needed; undefined lays files out as before.\n+\t */\n+\tknowPaths(paths: () => ReadonlySet<string> | undefined): void {\n+\t\tif (this.#pathsFrom === undefined) this.#pathsFrom = paths;\n+\t}\n+\t#pathsFrom: (() => ReadonlySet<string> | undefined) | undefined;\n+\t#knownPaths: ReadonlySet<string> | null | undefined;\n+\t#filesShared: { from: View; except: ReadonlySet<string> } | null | undefined;\n+\t#sharedFiles(): { from: View; except: ReadonlySet<string> } | null {\n+\t\tif (this.#filesShared === undefined) {\n+\t\t\tconst share = this.#filesFrom;\n+\t\t\tconst except = share ? share.except() : undefined;\n+\t\t\tthis.#filesShared = share && except ? { from: share.from, except } : null;\n+\t\t}\n+\t\treturn this.#filesShared;\n+\t}\n+\n \t#takeShared(): void {\n \t\tconst from = this.#movesFrom;\n \t\tif (!from) return;\n@@ -439,7 +544,10 @@\n \t\tif (this.#relocsN) return this.#relocsN;\n \t\tconst out = new Map<number, { readonly r: RelocationInfo; readonly parent: number }>();\n \t\tfor (const [line, r] of this.#relocations())\n-\t\t\tout.set(this.graph.lineN(line), { r, parent: this.graph.codeOf(r.parent)! });\n+\t\t\tout.set(this.graph.lineN(line), {\n+\t\t\t\tr,\n+\t\t\t\tparent: this.graph.codeOf(r.parent)!,\n+\t\t\t});\n \t\tthis.#relocsN = out;\n \t\treturn out;\n \t}\n@@ -458,6 +566,29 @@\n \t\treturn parent;\n \t}\n \n+\t/**\n+\t * The files where a moved line sits, in this view or in `other`, when the two place it differently: a different\n+\t * relocation wins, or a different parent (abso Capacity). Besides the files a draft's atoms name, these are the only\n+\t * ones a draft's moves can lay out differently from its base, since a line's descendants follow it. Undefined when\n+\t * either view resolves its moves in scratch, which keeps no map of them to compare.\n+\t */\n+\tplacedDifferently(other: View): Set<string> | undefined {\n+\t\tif (this.#scratchMoves() || other.#scratchMoves()) return undefined;\n+\t\tconst here = this.#hasMoves ? this.#relocationsN() : undefined;\n+\t\tconst there = other.#hasMoves ? other.#relocationsN() : undefined;\n+\t\tconst files = new Set<string>();\n+\t\tconst add = (n: number) => {\n+\t\t\tfiles.add(this.#fileOfN(n));\n+\t\t\tfiles.add(other.#fileOfN(n));\n+\t\t};\n+\t\tfor (const [n, x] of here ?? []) {\n+\t\t\tconst y = there?.get(n);\n+\t\t\tif (!y || y.parent !== x.parent || y.r.atom !== x.r.atom) add(n);\n+\t\t}\n+\t\tfor (const n of there?.keys() ?? []) if (!here?.has(n)) add(n);\n+\t\treturn files;\n+\t}\n+\n \t/** Whether any atom in the set moves lines. */\n \thasMoves(): boolean {\n \t\treturn this.#hasMoves;\n@@ -518,7 +649,13 @@\n \t\tfor (const n of graph.eachChildN(code)) {\n \t\t\tconst seq = graph.writerSeqOf(n);\n \t\t\tif (!this.#has(seq) || relocs?.has(n)) continue;\n-\t\t\tout.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) });\n+\t\t\tout.push({\n+\t\t\t\tn,\n+\t\t\t\treplaces: graph.replacesN(n) ? 1 : 0,\n+\t\t\t\tlamport: graph.lamportOfSeq(seq),\n+\t\t\t\tseq,\n+\t\t\t\tk: graph.kOf(n),\n+\t\t\t});\n \t\t}\n \t\tif (relocs) {\n \t\t\t// The lines this set's moves put under each node, from the moves it already resolved: no row read per node\n@@ -534,7 +671,13 @@\n \t\t\tfor (const n of this.#movedInto.get(code) ?? NO_LINES) {\n \t\t\t\tconst r = relocs.get(n)!.r;\n \t\t\t\tconst seq = graph.atom(r.atom).seq;\n-\t\t\t\tout.push({ n, replaces: 0, lamport: graph.lamportOfSeq(seq), seq, k: r.k });\n+\t\t\t\tout.push({\n+\t\t\t\t\tn,\n+\t\t\t\t\treplaces: 0,\n+\t\t\t\t\tlamport: graph.lamportOfSeq(seq),\n+\t\t\t\t\tseq,\n+\t\t\t\t\tk: r.k,\n+\t\t\t\t});\n \t\t\t}\n \t\t}\n \t\t// A line's replacements (runs of atoms that deleted it) first, then the\n@@ -642,12 +785,24 @@\n \t\tconst graph = this.graph;\n \t\tconst attrs = this.#attributes(path);\n \t\tconst start = graph.startCodeOf(path);\n-\t\tif (start === undefined) return { attrs, complete, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };\n+\t\tif (start === undefined)\n+\t\t\treturn {\n+\t\t\t\tattrs,\n+\t\t\t\tcomplete,\n+\t\t\t\tseq: EMPTY_INTS,\n+\t\t\t\tvis: EMPTY_INTS,\n+\t\t\t\tdeletedBy: new Map(),\n+\t\t\t};\n \t\tconst relocs = this.#hasMoves ? this.#relocationsN() : undefined;\n \t\tconst files = new Set<number>([-1 - start, ...(this.#movedInFrom().get(path) ?? [])]);\n \t\tconst children = new Map<number, Child[]>();\n-\t\tconst texts = new Map<number, string | null>();\n+\t\t// Where each line's text is: its file and its place in that file's packed history. Texts are read after the walk,\n+\t\t// only for the lines it shows (abso Capacity): every text a file and its moved-in sources ever had was read for\n+\t\t// every layout, and abso's worker files, split out of index.ts, read all of index.ts's history each.\n+\t\tconst textFile = new Map<number, number>();\n+\t\tconst textPlace = new Map<number, number>();\n \t\tconst writers = new Map<number, number>();\n+\t\tconst places = new Map<number, number>();\n \t\tconst deleters = new Map<number, number[]>();\n \t\t// Each atom's lamport once per layout: most lines share a few atoms, and a stored graph reads it from a page.\n \t\tconst lamports = new Map<number, number>();\n@@ -660,32 +815,50 @@\n \t\t\treturn lamport;\n \t\t};\n \t\tfor (const file of files) {\n-\t\t\tconst lineTexts = graph.packedTexts(file);\n-\t\t\tfor (const [n, parent, seq, k, replaces] of graph.packedLines(file)) {\n-\t\t\t\tconst text = lineTexts.next().value as string | null;\n+\t\t\t// The file's history decoded once and kept by the graph (abso Capacity): each view of it read every page again.\n+\t\t\tconst history = graph.packedHistory(file);\n+\t\t\tconst lines = history.lines;\n+\t\t\tfor (let i = 0, place = 0; i < lines.length; i += 5, place++) {\n+\t\t\t\tconst n = lines[i]!;\n+\t\t\t\tconst parent = lines[i + 1]!;\n+\t\t\t\tconst seq = lines[i + 2]!;\n+\t\t\t\tconst k = lines[i + 3]!;\n+\t\t\t\tconst replaces = lines[i + 4]!;\n \t\t\t\tif (!this.#has(seq)) continue;\n \t\t\t\tconst moved = relocs?.get(n);\n \t\t\t\tlet at = parent;\n \t\t\t\tlet child: Child = { n, replaces, lamport: lamportOf(seq), seq, k };\n \t\t\t\tif (moved) {\n \t\t\t\t\tconst mover = graph.atom(moved.r.atom).seq;\n-\t\t\t\t\tchild = { n, replaces: 0, lamport: lamportOf(mover), seq: mover, k: moved.r.k };\n+\t\t\t\t\tchild = {\n+\t\t\t\t\t\tn,\n+\t\t\t\t\t\treplaces: 0,\n+\t\t\t\t\t\tlamport: lamportOf(mover),\n+\t\t\t\t\t\tseq: mover,\n+\t\t\t\t\t\tk: moved.r.k,\n+\t\t\t\t\t};\n \t\t\t\t\tat = moved.parent;\n \t\t\t\t}\n \t\t\t\tconst list = children.get(at);\n \t\t\t\tif (list) list.push(child);\n \t\t\t\telse children.set(at, [child]);\n-\t\t\t\ttexts.set(n, text);\n+\t\t\t\ttextFile.set(n, file);\n+\t\t\t\ttextPlace.set(n, place);\n \t\t\t\twriters.set(n, seq);\n+\t\t\t\tplaces.set(n, k);\n \t\t\t}\n-\t\t\tfor (const [n, deleter] of graph.packedDeletes(file)) {\n+\t\t\tconst dels = history.deletes;\n+\t\t\tfor (let at = 0; at < dels.length; at += 2) {\n+\t\t\t\tconst n = dels[at]!;\n+\t\t\t\tconst deleter = dels[at + 1]!;\n \t\t\t\tif (!this.#has(deleter)) continue;\n \t\t\t\tconst by = deleters.get(n);\n \t\t\t\tif (by) by.push(deleter);\n \t\t\t\telse deleters.set(n, [deleter]);\n \t\t\t}\n \t\t\t// Runs written elsewhere under this file's lines follow them; the loop reads their files too.\n-\t\t\tfor (const [parent, into] of graph.packedCrossings(file)) if (texts.has(parent)) files.add(into);\n+\t\t\tconst crossings = history.crossings;\n+\t\t\tfor (let at = 0; at < crossings.length; at += 2) if (writers.has(crossings[at]!)) files.add(crossings[at + 1]!);\n \t\t}\n \t\tconst order = (a: Child, b: Child) =>\n \t\t\tb.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k;\n@@ -694,7 +867,22 @@\n \t\tconst vis: number[] = [];\n \t\tconst visText: (string | null)[] = [];\n \t\tconst visWriter: number[] = [];\n+\t\tconst visK: number[] = [];\n+\t\tconst seqWriter: number[] = [];\n+\t\tconst seqK: number[] = [];\n \t\tconst deletedBy = new Map<number, readonly number[]>();\n+\t\t// The text pages read for this layout, by file and page: each at most once.\n+\t\tconst pages = new Map<number, Map<number, readonly (string | null)[]>>();\n+\t\tconst textOf = (n: number): string | null => {\n+\t\t\tconst file = textFile.get(n)!;\n+\t\t\tconst place = textPlace.get(n)!;\n+\t\t\tconst page = Math.floor(place / 256);\n+\t\t\tlet byPage = pages.get(file);\n+\t\t\tif (!byPage) pages.set(file, (byPage = new Map()));\n+\t\t\tlet texts = byPage.get(page);\n+\t\t\tif (!texts) byPage.set(page, (texts = graph.packedTextPage(file, page)));\n+\t\t\treturn texts[place % 256] ?? null;\n+\t\t};\n \t\tconst stack: number[] = [];\n \t\tconst push = (code: number) => {\n \t\t\tconst list = children.get(code);\n@@ -710,10 +898,15 @@\n \t\t\t\tif (complete) deletedBy.set(seq.length, by.length > 1 ? by.sort(priority) : by);\n \t\t\t} else {\n \t\t\t\tvis.push(n);\n-\t\t\t\tvisText.push(texts.get(n) ?? null);\n+\t\t\t\tvisText.push(textOf(n));\n \t\t\t\tvisWriter.push(writers.get(n)!);\n+\t\t\t\tvisK.push(places.get(n)!);\n \t\t\t}\n-\t\t\tif (complete) seq.push(n);\n+\t\t\tif (complete) {\n+\t\t\t\tseq.push(n);\n+\t\t\t\tseqWriter.push(writers.get(n)!);\n+\t\t\t\tseqK.push(places.get(n)!);\n+\t\t\t}\n \t\t\tpush(n);\n \t\t}\n \t\treturn {\n@@ -724,6 +917,8 @@\n \t\t\tdeletedBy,\n \t\t\tvisText,\n \t\t\tvisWriter: Int32Array.from(visWriter),\n+\t\t\tvisK: Int32Array.from(visK),\n+\t\t\t...(complete ? { seqWriter: Int32Array.from(seqWriter), seqK: Int32Array.from(seqK) } : {}),\n \t\t};\n \t}\n \n@@ -787,7 +982,13 @@\n \t\t\tif (complete) seq.push(n);\n \t\t}\n \n-\t\treturn { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy };\n+\t\treturn {\n+\t\t\tattrs,\n+\t\t\tcomplete,\n+\t\t\tseq: Int32Array.from(seq),\n+\t\t\tvis: Int32Array.from(vis),\n+\t\t\tdeletedBy,\n+\t\t};\n \t}\n \n \tget #known(): string[] {\n@@ -806,11 +1007,26 @@\n \n \t/** Files with at least one visible line, sorted. */\n \tpaths(): string[] {\n+\t\t// Without walking every file the graph has ever had (abso Capacity): a draft's view over its base's has the base's\n+\t\t// files, with the ones its draft touches checked here; a published version's are kept (knowPaths).\n+\t\tconst shared = this.#filesFrom === undefined ? null : this.#sharedFiles();\n+\t\tif (shared) {\n+\t\t\tconst out = shared.from.paths().filter((p) => !shared.except.has(p));\n+\t\t\tfor (const p of shared.except) if (this.hasFile(p)) out.push(p);\n+\t\t\treturn out.sort();\n+\t\t}\n+\t\tif (this.#pathsFrom !== undefined) {\n+\t\t\tif (this.#knownPaths === undefined) this.#knownPaths = this.#pathsFrom() ?? null;\n+\t\t\tif (this.#knownPaths) return [...this.#knownPaths].sort();\n+\t\t}\n \t\tif (this.#stable) {\n \t\t\tconst paths: string[] = [];\n \t\t\tfor (const path of this.graph.knownFiles()) {\n \t\t\t\tif (this.hasFile(path)) paths.push(path);\n-\t\t\t\telse this.#files.delete(path);\n+\t\t\t\telse {\n+\t\t\t\t\tthis.#files.delete(path);\n+\t\t\t\t\tthis.#used.delete(path);\n+\t\t\t\t}\n \t\t\t}\n \t\t\treturn paths.sort();\n \t\t}\n@@ -834,6 +1050,15 @@\n \n \t/** Whether the file exists in this projection (has a visible line). */\n \thasFile(path: string): boolean {\n+\t\tif (!this.#files.has(path)) {\n+\t\t\t// A file the draft doesn't touch exists as it does in the base; a published version's files are kept.\n+\t\t\tconst shared = this.#filesFrom === undefined ? null : this.#sharedFiles();\n+\t\t\tif (shared && !shared.except.has(path)) return shared.from.hasFile(path);\n+\t\t\tif (this.#pathsFrom !== undefined) {\n+\t\t\t\tif (this.#knownPaths === undefined) this.#knownPaths = this.#pathsFrom() ?? null;\n+\t\t\t\tif (this.#knownPaths) return this.#knownPaths.has(path);\n+\t\t\t}\n+\t\t}\n \t\tconst attrs = this.attrs(path);\n \t\treturn attrs.exists || attrs.link !== null || attrs.blob !== null || this.#file(path).vis.length > 0;\n \t}\n@@ -870,32 +1095,43 @@\n \t}\n \n \t*lineChunks(path: string): IterableIterator<() => Iterable<string>> {\n-\t\tfor (const n of this.#file(path).vis) yield () => this.graph.textChunks(n);\n+\t\t// The texts the packed layout already holds, read a page at a time (abso Capacity, 714): a record's diff read each\n+\t\t// line's own row, 11,873 statements for abso's index.ts. Only a line too long to pack is read in chunks.\n+\t\tconst { vis, visText } = this.#file(path);\n+\t\tfor (let i = 0; i < vis.length; i++) {\n+\t\t\tconst packed = visText?.[i];\n+\t\t\tif (packed != null) yield () => [packed];\n+\t\t\telse yield () => this.graph.textChunks(vis[i]!);\n+\t\t}\n \t}\n \n \t/** Visible lines without retaining their text in the view cache. */\n \t*visibleLines(path: string): IterableIterator<VisibleLine> {\n \t\tconst graph = this.graph;\n \t\tfor (const n of this.#file(path).vis)\n-\t\t\tyield { id: graph.lineIdOf(n), text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };\n+\t\t\tyield {\n+\t\t\t\tid: graph.lineIdOf(n),\n+\t\t\t\ttext: graph.textOf(n),\n+\t\t\t\tatom: atomId(graph.writerSeqOf(n)),\n+\t\t\t};\n \t}\n \n \t/** Atom-building line handles; text is loaded only when requested. */\n \tbuildLines(path: string): readonly VisibleLine[] {\n \t\tconst graph = this.graph;\n \t\t// Texts and writers the packed layout already holds are not read again (abso Capacity).\n-\t\tconst { vis, visText, visWriter } = this.#file(path);\n+\t\tconst { vis, visText, visWriter, visK } = this.#file(path);\n \t\tconst lines: VisibleLine[] = new Array(vis.length);\n \t\tfor (let i = 0; i < vis.length; i++) {\n \t\t\tconst n = vis[i]!;\n \t\t\tconst packed = visText?.[i];\n \t\t\tlines[i] = {\n-\t\t\t\tid: graph.lineIdOf(n),\n+\t\t\t\tid: visWriter && visK ? lineId(atomId(visWriter[i]!), visK[i]!) : graph.lineIdOf(n),\n \t\t\t\ttextChunks: () => graph.textChunks(n),\n \t\t\t\tget text() {\n \t\t\t\t\treturn packed ?? graph.textOf(n);\n \t\t\t\t},\n-\t\t\t\tatom: graph.idOfSeq(visWriter ? visWriter[i]! : graph.writerSeqOf(n)),\n+\t\t\t\tatom: atomId(visWriter ? visWriter[i]! : graph.writerSeqOf(n)),\n \t\t\t};\n \t\t}\n \t\treturn lines;\n@@ -905,14 +1141,16 @@\n \tlines(path: string): readonly VisibleLine[] {\n \t\tif (this.#linesCache?.path === path) return this.#linesCache.lines;\n \t\tconst graph = this.graph;\n-\t\tconst { vis, visText, visWriter } = this.#file(path);\n+\t\tconst { vis, visText, visWriter, visK } = this.#file(path);\n \t\tconst lines: VisibleLine[] = new Array(vis.length);\n+\t\t// Writer seqs come from the graph's own layout, so they're named with atomId, not checked by idOfSeq (abso 738):\n+\t\t// on a stored graph its check read the atom count once a line, about 0.9 s of a warm draft-state on abso.\n \t\tfor (let i = 0; i < vis.length; i++) {\n \t\t\tconst n = vis[i]!;\n \t\t\tlines[i] = {\n-\t\t\t\tid: graph.lineIdOf(n),\n+\t\t\t\tid: visWriter && visK ? lineId(atomId(visWriter[i]!), visK[i]!) : graph.lineIdOf(n),\n \t\t\t\ttext: visText?.[i] ?? graph.textOf(n),\n-\t\t\t\tatom: graph.idOfSeq(visWriter ? visWriter[i]! : graph.writerSeqOf(n)),\n+\t\t\t\tatom: atomId(visWriter ? visWriter[i]! : graph.writerSeqOf(n)),\n \t\t\t};\n \t\t}\n \t\tthis.#linesCache = { path, lines };\n@@ -925,14 +1163,22 @@\n \t\tconst graph = this.graph;\n \t\tconst f = this.#file(path, true);\n \t\tconst out: SeqLine[] = new Array(f.seq.length);\n+\t\t// Ids and writers from the packed layout, and each text read only when asked (abso Capacity): callers walk a few\n+\t\t// lines of a file's history, and reading every line's text was a row each on a stored graph.\n+\t\tconst { seqWriter, seqK } = f;\n \t\tfor (let i = 0; i < f.seq.length; i++) {\n \t\t\tconst n = f.seq[i]!;\n \t\t\tconst by = f.deletedBy.get(i);\n+\t\t\tconst writer = seqWriter ? seqWriter[i]! : graph.writerSeqOf(n);\n+\t\t\tlet text: string | undefined;\n \t\t\tout[i] = {\n-\t\t\t\tid: graph.lineIdOf(n),\n-\t\t\t\ttext: graph.textOf(n),\n-\t\t\t\tatom: graph.idOfSeq(graph.writerSeqOf(n)),\n-\t\t\t\tdeletedBy: by ? by.map((s) => graph.idOfSeq(s)) : NONE,\n+\t\t\t\tid: seqWriter && seqK ? lineId(atomId(writer), seqK[i]!) : graph.lineIdOf(n),\n+\t\t\t\tget text() {\n+\t\t\t\t\ttext ??= graph.textOf(n);\n+\t\t\t\t\treturn text;\n+\t\t\t\t},\n+\t\t\t\tatom: atomId(writer),\n+\t\t\t\tdeletedBy: by ? by.map((s) => atomId(s)) : NONE,\n \t\t\t\tvisible: by === undefined,\n \t\t\t};\n \t\t}\n@@ -1017,9 +1263,12 @@\n \n \t/** Binary files, path -> the SHA-256 of their bytes, in path order. */\n \tblobs(): Record<string, string> {\n+\t\t// From attributes alone (abso Capacity): a file with a blob exists, so no file is laid out to find them. Going\n+\t\t// through paths() laid out every file in the set, and shared() asks for blobs on every call.\n \t\tconst out: Record<string, string> = {};\n-\t\tfor (const path of this.paths()) {\n-\t\t\tconst blob = this.#file(path).attrs.blob;\n+\t\tconst candidates = this.#stable ? [...this.graph.knownFiles()].sort() : this.#known;\n+\t\tfor (const path of candidates) {\n+\t\t\tconst blob = this.attrs(path).blob;\n \t\t\tif (blob !== null) out[path] = blob;\n \t\t}\n \t\treturn out;\n@@ -1033,7 +1282,7 @@\n \t * populate lazy caches, enumerate the graph, or count temporary traversal\n \t * containers and shared empty singleton payloads as retained view state.\n \t */\n-\tweight(): number {\n+\tweight(seen?: Set<object>): number {\n \t\tlet n = this.#in.byteLength;\n \t\tn += 2 * ARRAY_HEADER_BYTES + this.#membershipSeq.byteLength + this.#membershipValue.byteLength;\n \t\tif (this.#movers) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length;\n@@ -1044,17 +1293,13 @@\n \t\tif (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size;\n \t\tif (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size;\n \t\tn += MAP_ENTRY_BYTES * this.#picked.size;\n+\t\tn += MAP_ENTRY_BYTES * this.#used.size;\n \t\tfor (const f of this.#files.values()) {\n \t\t\tn += MAP_ENTRY_BYTES;\n \t\t\tif (f !== NO_FILE) {\n-\t\t\t\t// FileState and FileAttrs are per laid-out file; typed-array\n-\t\t\t\t// headers are separate from their payload byte lengths below.\n-\t\t\t\tn += OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES;\n-\t\t\t\tn += f.seq.byteLength + f.vis.byteLength;\n-\t\t\t\tif (f.visText) for (const text of f.visText) n += REFERENCE_BYTES + 2 * (text?.length ?? 0);\n-\t\t\t\tif (f.visWriter) n += 4 * f.visWriter.length;\n-\t\t\t\tn += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + ARRAY_HEADER_BYTES) * f.deletedBy.size;\n-\t\t\t\tfor (const deleters of f.deletedBy.values()) n += REFERENCE_BYTES * deleters.length;\n+\t\t\t\t// A layout another view counted already (`seen`, across a cache's views) isn't counted again.\n+\t\t\t\tif (!seen?.has(f)) n += layoutBytes(f);\n+\t\t\t\tseen?.add(f);\n \t\t\t\tif (f.seqIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size;\n \t\t\t\tif (f.visIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size;\n \t\t\t}\n@@ -1073,11 +1318,45 @@\n \t#file(path: string, complete = false): FileState {\n \t\tlet state = this.#files.get(path);\n \t\tif (!state || (complete && !state.complete)) {\n-\t\t\tstate = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE;\n+\t\t\tconst shared = this.#filesFrom === undefined ? null : this.#sharedFiles();\n+\t\t\tif (shared && !shared.except.has(path)) state = shared.from.#file(path, complete);\n+\t\t\telse state = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE;\n \t\t\tthis.#files.set(path, state);\n \t\t}\n+\t\tif (state !== NO_FILE) this.#used.set(path, ++fileUses);\n \t\treturn state;\n \t}\n+\n+\t/** When each laid-out file was last used (fileUses). */\n+\treadonly #used = new Map<string, number>();\n+\n+\t/**\n+\t * Each laid-out file: its layout, when it was last used (a count shared by every view) and what it holds, for a\n+\t * cache that trims the least recently used layouts across its views before whole views (views.ts, abso Capacity).\n+\t */\n+\t*laidOut(): IterableIterator<{\n+\t\tpath: string;\n+\t\tlayout: object;\n+\t\tused: number;\n+\t\tbytes: number;\n+\t}> {\n+\t\tfor (const [path, f] of this.#files) {\n+\t\t\tif (f === NO_FILE) continue;\n+\t\t\tlet bytes = 2 * MAP_ENTRY_BYTES + layoutBytes(f);\n+\t\t\tif (f.seqIndex) bytes += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size;\n+\t\t\tif (f.visIndex) bytes += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size;\n+\t\t\tyield { path, layout: f, used: this.#used.get(path) ?? 0, bytes };\n+\t\t}\n+\t}\n+\n+\t/** Let go of a file's layout: the next ask lays it out again, the same. */\n+\tforget(path: string): void {\n+\t\tthis.#files.delete(path);\n+\t\tthis.#used.delete(path);\n+\t\tif (this.#linesCache?.path === path) this.#linesCache = undefined;\n+\t\tif (this.#seqCache?.path === path) this.#seqCache = undefined;\n+\t\tif (this.#textCache?.path === path) this.#textCache = undefined;\n+\t}\n }\n \n /** The files of a closed set, path -> text. */"}],"unmatched":[]}