{"draft":null,"from":201,"to":318,"files":[{"path":"src/engine/view.ts","added":732,"removed":106,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -10,9 +10,9 @@\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 } from \"./keys.ts\";\n+import { atomId, isStartNode, lineId, seqOf } from \"./keys.ts\";\n import { resolveScratchMoves, scratchLayoutNodes } from \"./relocation-scratch.ts\";\n-import { StableSet } from \"./state-source.ts\";\n+import { isClosedSet, StableSet } from \"./state-source.ts\";\n import { joinLines } from \"./text.ts\";\n import { type AtomId, type AtomSet, EngineError, type LineId, type NodeId } from \"./types.ts\";\n \n@@ -47,11 +47,51 @@\n }\n \n /** Structural read boundary shared by historical and transaction-bound projections. */\n-export type ReadView = Omit<Pick<View, keyof View>, \"sameText\"> & {\n+export type ReadView = Omit<\n+\tPick<View, keyof View>,\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@@ -64,6 +104,15 @@\n \treadonly vis: Int32Array;\n \t/** For each deleted position in seq: the seqs of the atoms in the set that delete it, by priority. */\n \treadonly deletedBy: ReadonlyMap<number, readonly number[]>;\n+\t/** Its visible lines' text, when laid out from the file's packed history: null for a line too long to pack. */\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@@ -79,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@@ -90,9 +148,115 @@\n const MAP_ENTRY_BYTES = 40;\n const SET_ENTRY_BYTES = 32;\n const REFERENCE_BYTES = 8;\n-const FILE_LOCATION_MEMO_ENTRIES = 256;\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+export const MOVED_LINES_KEPT = 20_000;\n+class TooManyMoves extends Error {}\n+const NO_LINES: readonly number[] = Object.freeze([]);\n+/** Nodes whose nearest moved line above a graph remembers, at most: about 40 bytes each. */\n+const CLIMB_MEMO = 200_000;\n \n+/** One relocation of a moved line, with its line's number and its new parent's code. */\n+interface MoveEntry {\n+\treadonly n: number;\n+\treadonly r: RelocationInfo;\n+\treadonly parent: number;\n+}\n+\n /**\n+ * A graph's moves, whatever the set (abso Capacity): each mover's moved lines, each moved line's relocations in seq\n+ * order, and for any node the nearest node at or above it, by where lines were written, that a move ever relocated,\n+ * or its file's start. Worked out once per graph and extended as it grows. A view's moves then resolve over the moved\n+ * lines alone: before, every view walked each moved line's file up to its start, a read per line.\n+ */\n+class MoveSkeleton {\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: {\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+\n+\tconstructor(graph: Graph) {\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+\t\t\tconst id = all[this.#seen]!;\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({\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+\t\t\t\telse this.byLine.set(n, [entry]);\n+\t\t\t\tlines.push(n);\n+\t\t\t});\n+\t\t\tthis.movers.push({ seq: seqOf(id), lines });\n+\t\t}\n+\t\t// A new moved line can sit between a node and the one it climbed to.\n+\t\tthis.#climbed.clear();\n+\t\treturn this;\n+\t}\n+\n+\t/** The nearest node at or above this one, by where lines were written, that a move ever relocated; or its file's start. */\n+\tclimb(code: number): number {\n+\t\tconst walked: number[] = [];\n+\t\tlet c = code;\n+\t\twhile (c >= 0 && !this.byLine.has(c)) {\n+\t\t\tconst known = this.#climbed.get(c);\n+\t\t\tif (known !== undefined) {\n+\t\t\t\tc = known;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\twalked.push(c);\n+\t\t\tc = this.#graph.parentCodeOf(c);\n+\t\t}\n+\t\tif (this.#climbed.size + walked.length > CLIMB_MEMO) this.#climbed.clear();\n+\t\tfor (const w of walked) this.#climbed.set(w, c);\n+\t\treturn c;\n+\t}\n+}\n+\n+const skeletons = new WeakMap<Graph, MoveSkeleton>();\n+function skeletonOf(graph: Graph): MoveSkeleton {\n+\tlet skeleton = skeletons.get(graph);\n+\tif (!skeleton) skeletons.set(graph, (skeleton = new MoveSkeleton(graph)));\n+\treturn skeleton.update();\n+}\n+\n+/**\n  * The projection of a closed set. Construct once, then ask for files, lines\n  * and sequences. Throws `not-closed` if some atom's dependency is missing.\n  * Each file is laid out the first time it is asked for, so a view of a large\n@@ -118,8 +282,10 @@\n \t#relocs: Map<LineId, RelocationInfo> | undefined;\n \t/** The same, by n, with the new parent's code. */\n \t#relocsN: Map<number, { readonly r: RelocationInfo; readonly parent: number }> | undefined;\n-\t/** Bounded memo: ancestry can span the entire history even for one visible line. */\n-\t#fileOf = new Map<number, string>();\n+\t/** The moved lines under each node, by its code, made from the relocations on first use. */\n+\t#movedInto: Map<number, number[]> | undefined;\n+\t/** Whether this view's moves are too many to keep, so they're resolved on disk per layout; undefined until asked. */\n+\t#movesOnDisk: boolean | undefined;\n \t/** The latest records asked for: one file's lines, one file's sequence, one file's text. */\n \t#linesCache: { path: string; lines: readonly VisibleLine[] } | undefined;\n \t#seqCache: { path: string; seq: readonly SeqLine[] } | undefined;\n@@ -131,7 +297,8 @@\n \t\t\tthis.#stable = set;\n \t\t\tthis.#set = set;\n \t\t\tthis.#in = new Uint8Array(0);\n-\t\t\tfor (const id of set) {\n+\t\t\t// A set kept closed when it was written needs no proof here (abso Capacity).\n+\t\t\tfor (const id of isClosedSet(set) ? [] : set) {\n \t\t\t\tconst atom = graph.atom(id);\n \t\t\t\tfor (const dependency of atom.deps) {\n \t\t\t\t\tgraph.atom(dependency);\n@@ -150,23 +317,24 @@\n \t\t\treturn;\n \t\t}\n \t\tconst inSet = new Uint8Array(graph.size);\n-\t\tfor (const id of set) {\n-\t\t\tconst atom = graph.atom(id);\n-\t\t\tinSet[atom.seq] = 1;\n-\t\t}\n+\t\t// An atom's number is in its id (abso Capacity): reading the atom for it read the whole history on a stored graph.\n+\t\tconst seqIn = (id: AtomId): number => {\n+\t\t\tconst seq = seqOf(id);\n+\t\t\treturn seq >= 0 && seq < inSet.length && graph.idOfSeq(seq) === id ? seq : graph.atom(id).seq;\n+\t\t};\n+\t\tfor (const id of set) inSet[seqIn(id)] = 1;\n \t\t// Validate one atom at a time. Keeping all decoded atoms here defeats\n \t\t// storage-backed Graph reads even though the view only needs membership.\n \t\tfor (let seq = 0; seq < inSet.length; seq++) {\n \t\t\tif (inSet[seq] !== 1) continue;\n \t\t\tconst atom = graph.atom(graph.idOfSeq(seq));\n \t\t\tfor (const d of atom.deps) {\n-\t\t\t\tif (inSet[graph.atom(d).seq] !== 1)\n-\t\t\t\t\tthrow new EngineError(\"not-closed\", `set is not closed: ${atom.id} depends on ${d}`);\n+\t\t\t\tif (inSet[seqIn(d)] !== 1) throw new EngineError(\"not-closed\", `set is not closed: ${atom.id} depends on ${d}`);\n \t\t\t}\n \t\t}\n \t\tthis.graph = graph;\n \t\tthis.#in = inSet;\n-\t\tthis.#movers = graph.movers().filter((id) => inSet[graph.atom(id).seq] === 1);\n+\t\tthis.#movers = graph.movers().filter((id) => inSet[seqOf(id)] === 1);\n \t\tthis.#hasMoves = this.#movers.length > 0;\n \t\tthis.#relocs = this.#movers.length === 0 ? new Map() : undefined;\n \t\tthis.#relocsN = this.#movers.length === 0 ? new Map() : undefined;\n@@ -203,34 +371,49 @@\n \t */\n \t#relocations(): Map<LineId, RelocationInfo> {\n \t\tif (this.#relocs) return this.#relocs;\n+\t\tthis.#takeShared();\n+\t\tif (this.#relocs) return this.#relocs;\n \t\tconst graph = this.graph;\n+\t\tconst skeleton = skeletonOf(graph);\n \t\tconst stronger = (a: RelocationInfo, b: RelocationInfo): boolean => graph.comparePriority(a.atom, b.atom) > 0;\n-\t\t// Persistent reads decode new objects. Cycle suppression must identify the\n-\t\t// logical move, not one particular decoded JavaScript object.\n \t\tconst identity = (r: RelocationInfo): string => JSON.stringify([r.atom, r.k]);\n \t\tconst ignored = new Set<string>();\n-\t\tconst pick = (line: LineId): RelocationInfo | undefined => {\n-\t\t\tlet best: RelocationInfo | undefined;\n-\t\t\tfor (const r of graph.eachRelocationOf(line)) {\n-\t\t\t\tif (this.#has(graph.atom(r.atom).seq) && !ignored.has(identity(r)) && (!best || stronger(r, best))) best = r;\n+\t\tconst pick = (n: number): MoveEntry | undefined => {\n+\t\t\tlet best: MoveEntry | undefined;\n+\t\t\tfor (const e of skeleton.byLine.get(n) ?? []) {\n+\t\t\t\tif (this.#has(seqOf(e.r.atom)) && !ignored.has(identity(e.r)) && (!best || stronger(e.r, best.r))) best = e;\n \t\t\t}\n \t\t\treturn best;\n \t\t};\n-\t\tconst relocs = new Map<LineId, RelocationInfo>();\n-\t\tfor (const id of this.#eachMover()) {\n-\t\t\tfor (const m of graph.atom(id).moves!) {\n-\t\t\t\tif (!relocs.has(m.line)) relocs.set(m.line, pick(m.line)!);\n+\t\t// By the moved line's number, in the order its movers come (seq order, then each mover's moves).\n+\t\tconst relocs = new Map<number, MoveEntry>();\n+\t\tfor (const mover of skeleton.movers) {\n+\t\t\tif (!this.#has(mover.seq)) continue;\n+\t\t\tfor (const n of mover.lines) {\n+\t\t\t\tif (!relocs.has(n)) relocs.set(n, pick(n)!);\n+\t\t\t\tif (graph.hasOrderedChildren && relocs.size > MOVED_LINES_KEPT) throw new TooManyMoves();\n \t\t\t}\n \t\t}\n-\t\tconst parentOf = (line: LineId): NodeId => relocs.get(line)?.parent ?? graph.lineParent(line)!;\n-\t\tconst reaches = new Set<LineId>();\n+\t\t// From a node, the next node that matters for loops: a line moved in this set, or a file's start.\n+\t\tconst hop = (code: number): number => {\n+\t\t\tlet c = skeleton.climb(code);\n+\t\t\twhile (c >= 0 && !relocs.has(c)) c = skeleton.climb(graph.parentCodeOf(c));\n+\t\t\treturn c;\n+\t\t};\n+\t\tconst step = (n: number): number => {\n+\t\t\tconst e = relocs.get(n);\n+\t\t\treturn hop(e ? e.parent : graph.parentCodeOf(n));\n+\t\t};\n+\t\t// Two moves made apart can loop lines into each other, so no start node reaches them; then the oldest\n+\t\t// relocation on the loop is ignored, and the next newest of that line counts, until none loop.\n+\t\tconst reaches = new Set<number>();\n \t\tfor (const line of [...relocs.keys()]) {\n \t\t\tfor (;;) {\n-\t\t\t\tconst path: LineId[] = [];\n-\t\t\t\tconst onPath = new Map<LineId, number>();\n-\t\t\t\tlet cur: NodeId = line;\n+\t\t\t\tconst path: number[] = [];\n+\t\t\t\tconst onPath = new Map<number, number>();\n+\t\t\t\tlet cur = line;\n \t\t\t\tlet loopAt = -1;\n-\t\t\t\twhile (!isStartNode(cur) && !reaches.has(cur)) {\n+\t\t\t\twhile (cur >= 0 && !reaches.has(cur)) {\n \t\t\t\t\tconst at = onPath.get(cur);\n \t\t\t\t\tif (at !== undefined) {\n \t\t\t\t\t\tloopAt = at;\n@@ -238,33 +421,133 @@\n \t\t\t\t\t}\n \t\t\t\t\tonPath.set(cur, path.length);\n \t\t\t\t\tpath.push(cur);\n-\t\t\t\t\tcur = parentOf(cur);\n+\t\t\t\t\tcur = step(cur);\n \t\t\t\t}\n \t\t\t\tif (loopAt < 0) {\n \t\t\t\t\tfor (const l of path) reaches.add(l);\n \t\t\t\t\tbreak;\n \t\t\t\t}\n-\t\t\t\tlet weakest: RelocationInfo | undefined;\n+\t\t\t\tlet weakest: MoveEntry | undefined;\n \t\t\t\tfor (const l of path.slice(loopAt)) {\n-\t\t\t\t\tconst r = relocs.get(l);\n-\t\t\t\t\tif (r && (!weakest || stronger(weakest, r))) weakest = r;\n+\t\t\t\t\tconst e = relocs.get(l);\n+\t\t\t\t\tif (e && (!weakest || stronger(weakest.r, e.r))) weakest = e;\n \t\t\t\t}\n-\t\t\t\tignored.add(identity(weakest!));\n-\t\t\t\tconst next = pick(weakest!.line);\n-\t\t\t\tif (next) relocs.set(weakest!.line, next);\n-\t\t\t\telse relocs.delete(weakest!.line);\n+\t\t\t\tignored.add(identity(weakest!.r));\n+\t\t\t\tconst next = pick(weakest!.n);\n+\t\t\t\tif (next) relocs.set(weakest!.n, next);\n+\t\t\t\telse relocs.delete(weakest!.n);\n \t\t\t}\n \t\t}\n-\t\tthis.#relocs = relocs;\n-\t\treturn relocs;\n+\t\tthis.#relocsN = new Map([...relocs].map(([n, e]) => [n, { r: e.r, parent: e.parent }]));\n+\t\tthis.#relocs = new Map([...relocs.values()].map((e) => [e.r.line, e.r]));\n+\t\treturn this.#relocs;\n \t}\n \n+\t/** The view whose moves this one may take, until it needs them all; then the view it took them from. */\n+\t#movesFrom: View | undefined;\n+\t#movesTaken: View | undefined;\n+\n+\t/**\n+\t * Take another view's resolved moves (abso Capacity), when both sets hold the very same atoms that move lines: the\n+\t * moves then resolve the same, and every line sits where it sits there. Checked when this view first needs all its\n+\t * moves, so any caller may ask, and a view that only finds a few lines' files never resolves them.\n+\t */\n+\tshareMoves(from: View): void {\n+\t\tif (from.graph !== this.graph || from.#hasMoves !== this.#hasMoves || !this.#hasMoves || this.#relocs) return;\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+\t\tthis.#movesFrom = undefined;\n+\t\tif (from.#scratchMoves()) return;\n+\t\tfor (const id of this.graph.movers()) {\n+\t\t\tconst seq = seqOf(id);\n+\t\t\tif (this.#has(seq) !== from.#has(seq)) return;\n+\t\t}\n+\t\tthis.#relocs = from.#relocations();\n+\t\tthis.#relocsN = from.#relocationsN();\n+\t\tthis.#movesOnDisk = false;\n+\t\tthis.#movesTaken = from;\n+\t}\n+\n+\t/** Moved lines' relocations picked one at a time, before the view resolves them all (null: not moved here). */\n+\treadonly #picked = new Map<number, MoveEntry | null>();\n+\n+\t/**\n+\t * The relocation of moved line n that counts here, as #relocations picks it before any loop: right whenever the\n+\t * walk that reached n goes on to a file's start, since only lines on a loop have their relocation set aside.\n+\t */\n+\t#pickOne(n: number): MoveEntry | undefined {\n+\t\tlet picked = this.#picked.get(n);\n+\t\tif (picked === undefined) {\n+\t\t\tconst graph = this.graph;\n+\t\t\tlet best: MoveEntry | undefined;\n+\t\t\tfor (const e of skeletonOf(graph).byLine.get(n) ?? []) {\n+\t\t\t\tif (this.#has(seqOf(e.r.atom)) && (!best || graph.comparePriority(e.r.atom, best.r.atom) > 0)) best = e;\n+\t\t\t}\n+\t\t\tthis.#picked.set(n, (picked = best ?? null));\n+\t\t}\n+\t\treturn picked ?? undefined;\n+\t}\n+\n+\t/** Whether moves are resolved on disk for each layout: only a stored graph's view with more than MOVED_LINES_KEPT. */\n+\t#scratchMoves(): boolean {\n+\t\tif (!this.#hasMoves || !this.graph.hasOrderedChildren) return false;\n+\t\tif (this.#movesOnDisk === undefined) {\n+\t\t\ttry {\n+\t\t\t\tthis.#relocations();\n+\t\t\t\tthis.#movesOnDisk = false;\n+\t\t\t} catch (error) {\n+\t\t\t\tif (!(error instanceof TooManyMoves)) throw error;\n+\t\t\t\tthis.#movesOnDisk = true;\n+\t\t\t}\n+\t\t}\n+\t\treturn this.#movesOnDisk;\n+\t}\n+\n \t/** The relocations by n, with each new parent's code. */\n \t#relocationsN(): Map<number, { readonly r: RelocationInfo; readonly parent: number }> {\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@@ -272,7 +555,7 @@\n \t/** The node a line hangs off in this set: where it was written, or where a move put it. */\n \tparentOf(line: LineId): NodeId {\n \t\tif (this.graph.lineParent(line) === undefined) throw new EngineError(\"unknown-line\", `no line ${line}`);\n-\t\tif (this.#hasMoves && this.graph.hasOrderedChildren)\n+\t\tif (this.#scratchMoves())\n \t\t\treturn this.graph.withProjectionScratch((scratch) =>\n \t\t\t\tresolveScratchMoves(this.graph, (id) => this.#has(this.graph.atom(id).seq), scratch).parent(line),\n \t\t\t);\n@@ -283,14 +566,33 @@\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 \t}\n-\t*#eachMover(): IterableIterator<AtomId> {\n-\t\tif (this.#movers) yield* this.#movers;\n-\t\telse for (const id of this.graph.movers()) if (this.#stable!.has(id)) yield id;\n-\t}\n \n \t/** The file a line is in, in this set: the one its chain of parents reaches. */\n \tfileOf(line: LineId): string {\n@@ -302,36 +604,41 @@\n \t#fileOfN(n: number): string {\n \t\tconst graph = this.graph;\n \t\tif (!this.#hasMoves) return graph.writtenIn(n);\n-\t\tif (graph.hasOrderedChildren)\n+\t\tif (this.#scratchMoves())\n \t\t\treturn graph.withProjectionScratch((scratch) => {\n \t\t\t\tconst moves = resolveScratchMoves(graph, (id) => this.#has(graph.atom(id).seq), scratch);\n \t\t\t\tlet current = graph.nodeOf(n);\n \t\t\t\twhile (!isStartNode(current)) current = moves.parent(current);\n \t\t\t\treturn current.slice(1);\n \t\t\t});\n-\t\tconst relocs = this.#relocationsN();\n-\t\t// Keep only a fixed tail of the walk for path compression. The selected\n-\t\t// relocation graph is acyclic, so traversal needs no history-sized stack.\n-\t\tconst path: number[] = [];\n-\t\tlet visited = 0;\n-\t\tlet cur = n;\n-\t\tlet file: string | undefined;\n-\t\twhile (file === undefined) {\n-\t\t\tif (cur < 0) file = graph.nodeOf(cur).slice(1);\n-\t\t\telse {\n-\t\t\t\tfile = this.#fileOf.get(cur);\n-\t\t\t\tif (file === undefined) {\n-\t\t\t\t\tpath[visited++ % FILE_LOCATION_MEMO_ENTRIES] = cur;\n-\t\t\t\t\tcur = relocs.get(cur)?.parent ?? graph.parentCodeOf(cur);\n-\t\t\t\t}\n+\t\treturn this.#fileOfCode(n);\n+\t}\n+\n+\t/** The file a node sits in, in this set: up by where lines were written, through the moves that count here. */\n+\t#fileOfCode(code: number): string {\n+\t\tconst graph = this.graph;\n+\t\tconst skeleton = skeletonOf(graph);\n+\t\tif (!this.#relocsN) {\n+\t\t\t// One moved line at a time, until a walk comes back to a line it passed: then moves loop, and are resolved whole.\n+\t\t\tconst passed = new Set<number>();\n+\t\t\tlet c = code;\n+\t\t\tfor (;;) {\n+\t\t\t\tc = skeleton.climb(c);\n+\t\t\t\tif (c < 0) return graph.nodeOf(c).slice(1);\n+\t\t\t\tif (passed.has(c)) break;\n+\t\t\t\tpassed.add(c);\n+\t\t\t\tconst e = this.#pickOne(c);\n+\t\t\t\tc = e ? e.parent : graph.parentCodeOf(c);\n \t\t\t}\n \t\t}\n-\t\tfor (const l of path) {\n-\t\t\tif (!this.#fileOf.has(l) && this.#fileOf.size >= FILE_LOCATION_MEMO_ENTRIES)\n-\t\t\t\tthis.#fileOf.delete(this.#fileOf.keys().next().value!);\n-\t\t\tthis.#fileOf.set(l, file);\n+\t\tconst relocs = this.#relocationsN();\n+\t\tlet c = code;\n+\t\tfor (;;) {\n+\t\t\tc = skeleton.climb(c);\n+\t\t\tif (c < 0) return graph.nodeOf(c).slice(1);\n+\t\t\tconst e = relocs.get(c);\n+\t\t\tc = e ? e.parent : graph.parentCodeOf(c);\n \t\t}\n-\t\treturn file;\n \t}\n \n \t/** The lines hanging off a node in this set, in layout order. */\n@@ -342,15 +649,35 @@\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\tfor (const r of graph.eachRelocatedToCode(code)) {\n-\t\t\t\tconst n = graph.lineN(r.line);\n-\t\t\t\tconst selected = relocs.get(n)?.r;\n-\t\t\t\tif (!selected || selected.atom !== r.atom || selected.k !== r.k) continue;\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+\t\t\t// (abso Capacity).\n+\t\t\tif (!this.#movedInto) {\n+\t\t\t\tthis.#movedInto = new Map();\n+\t\t\t\tfor (const [n, { parent }] of relocs) {\n+\t\t\t\t\tconst into = this.#movedInto.get(parent);\n+\t\t\t\t\tif (into) into.push(n);\n+\t\t\t\t\telse this.#movedInto.set(parent, [n]);\n+\t\t\t\t}\n+\t\t\t}\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@@ -415,7 +742,8 @@\n \t/** Lay out one file: its line tree from the set's atoms, linearized. */\n \t#layout(path: string, complete: boolean): FileState {\n \t\tconst graph = this.graph;\n-\t\tif (this.#hasMoves && graph.hasOrderedChildren)\n+\t\tif (graph.packedLayout && !this.#scratchMoves()) return this.#layoutPacked(path, complete);\n+\t\tif (this.#scratchMoves())\n \t\t\treturn graph.withProjectionScratch((scratch) =>\n \t\t\t\tthis.#layoutResolved(\n \t\t\t\t\tpath,\n@@ -426,10 +754,193 @@\n \t\treturn this.#layoutResolved(path, complete);\n \t}\n \n+\t/** For each path, the files that lines moved into it were written in: worked out once per view. */\n+\t#movedFrom: Map<string, Set<number>> | undefined;\n+\t#movedInFrom(): Map<string, Set<number>> {\n+\t\tif (this.#movedFrom) return this.#movedFrom;\n+\t\tthis.#relocations();\n+\t\tif (this.#movesTaken) return (this.#movedFrom = this.#movesTaken.#movedInFrom());\n+\t\tconst graph = this.graph;\n+\t\tconst out = new Map<string, Set<number>>();\n+\t\tif (this.#hasMoves) {\n+\t\t\tconst relocs = this.#relocationsN();\n+\t\t\tconst fileOf = (code: number): string => this.#fileOfCode(code);\n+\t\t\tfor (const [n, { parent }] of relocs) {\n+\t\t\t\tconst into = fileOf(parent);\n+\t\t\t\tlet from = out.get(into);\n+\t\t\t\tif (!from) out.set(into, (from = new Set()));\n+\t\t\t\tfrom.add(graph.writtenInIndex(n));\n+\t\t\t}\n+\t\t}\n+\t\tthis.#movedFrom = out;\n+\t\treturn out;\n+\t}\n+\n+\t/**\n+\t * Lay one file out from its packed history (abso Capacity, Graph.packedLines): the lines written in it, in the files\n+\t * its moved-in lines came from, and in the files runs under any of those lines were written in, read a page at a\n+\t * time; the same tree and order as #layoutResolved, built in memory with no read per line.\n+\t */\n+\t#layoutPacked(path: string, complete: boolean): FileState {\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)\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\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+\t\tconst lamportOf = (seq: number): number => {\n+\t\t\tlet lamport = lamports.get(seq);\n+\t\t\tif (lamport === undefined) {\n+\t\t\t\tlamport = graph.lamportOfSeq(seq);\n+\t\t\t\tlamports.set(seq, lamport);\n+\t\t\t}\n+\t\t\treturn lamport;\n+\t\t};\n+\t\tfor (const file of files) {\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+\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\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\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\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+\t\tconst priority = (a: number, b: number) => lamportOf(a) - lamportOf(b) || a - b;\n+\t\tconst seq: number[] = [];\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+\t\t\tif (!list) return;\n+\t\t\tif (list.length > 1) list.sort(order);\n+\t\t\tfor (let i = list.length - 1; i >= 0; i--) stack.push(list[i]!.n);\n+\t\t};\n+\t\tpush(start);\n+\t\twhile (stack.length) {\n+\t\t\tconst n = stack.pop()!;\n+\t\t\tconst by = deleters.get(n);\n+\t\t\tif (by) {\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(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) {\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+\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\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 \t#attributes(path: string): FileAttrs {\n \t\tconst graph = this.graph;\n \t\t// For each attribute, the highest-priority setter in the set wins.\n \t\tconst setters = new Map<string, { id: AtomId; value: string | null }>();\n+\t\tconst file = graph.packedFiles ? graph.pathIndexOf(path) : undefined;\n+\t\tif (graph.packedFiles) {\n+\t\t\tfor (const [seq, name, value] of file === undefined ? [] : graph.packedAttrs(file)) {\n+\t\t\t\tif (!this.#has(seq)) continue;\n+\t\t\t\tconst id = graph.idOfSeq(seq);\n+\t\t\t\tconst best = setters.get(name);\n+\t\t\t\tif (!best || graph.comparePriority(best.id, id) < 0) setters.set(name, { id, value });\n+\t\t\t}\n+\t\t\treturn {\n+\t\t\t\texists: setters.get(\"exists\")?.value === \"1\",\n+\t\t\t\texec: setters.get(\"exec\")?.value === \"1\",\n+\t\t\t\tlink: setters.get(\"link\")?.value ?? null,\n+\t\t\t\tblob: setters.get(\"blob\")?.value ?? null,\n+\t\t\t};\n+\t\t}\n \t\tfor (const id of graph.eachAtomIn(path)) {\n \t\t\tif (!this.#has(graph.atom(id).seq)) continue;\n \t\t\tfor (const a of graph.atom(id).attrs ?? []) {\n@@ -471,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@@ -490,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@@ -506,8 +1038,27 @@\n \t\treturn [...this.#known];\n \t}\n \n+\t/** The file line n sits in, in this set: fileOf by the line's number, with no lookup of its id. */\n+\tfileOfNumber(n: number): string {\n+\t\treturn this.#fileOfN(n);\n+\t}\n+\n+\t/** How many visible lines a file has. */\n+\tvisibleCount(path: string): number {\n+\t\treturn this.#file(path).vis.length;\n+\t}\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@@ -520,9 +1071,9 @@\n \t/** The projected text of a file (\"\" if it has no visible lines). */\n \ttext(path: string): string {\n \t\tif (this.#textCache?.path === path) return this.#textCache.text;\n-\t\tconst vis = this.#file(path).vis;\n+\t\tconst { vis, visText } = this.#file(path);\n \t\tconst lines: string[] = new Array(vis.length);\n-\t\tfor (let i = 0; i < vis.length; i++) lines[i] = this.graph.textOf(vis[i]!);\n+\t\tfor (let i = 0; i < vis.length; i++) lines[i] = visText?.[i] ?? this.graph.textOf(vis[i]!);\n \t\tconst text = joinLines(lines);\n \t\tthis.#textCache = { path, text };\n \t\treturn text;\n@@ -544,30 +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\tconst vis = this.#file(path).vis;\n+\t\t// Texts and writers the packed layout already holds are not read again (abso Capacity).\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 graph.textOf(n);\n+\t\t\t\t\treturn packed ?? graph.textOf(n);\n \t\t\t\t},\n-\t\t\t\tatom: graph.idOfSeq(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@@ -577,11 +1141,17 @@\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 = this.#file(path).vis;\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] = { id: graph.lineIdOf(n), text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };\n+\t\t\tlines[i] = {\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: atomId(visWriter ? visWriter[i]! : graph.writerSeqOf(n)),\n+\t\t\t};\n \t\t}\n \t\tthis.#linesCache = { path, lines };\n \t\treturn lines;\n@@ -593,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@@ -635,7 +1213,7 @@\n \t\t\t}\n \t\t\treturn found && previous !== undefined ? [previous] : [];\n \t\t};\n-\t\tif (this.#hasMoves && graph.hasOrderedChildren)\n+\t\tif (this.#scratchMoves())\n \t\t\treturn graph.withProjectionScratch((scratch) =>\n \t\t\t\tscan(scratchLayoutNodes(graph, (id) => this.#has(graph.atom(id).seq), scratch, graph.nodeOf(start))),\n \t\t\t);\n@@ -654,14 +1232,28 @@\n \n \t/** Index of a line in `lines(path)` (0-based visible line number), or -1 if not visible. */\n \tlineNumber(path: string, line: LineId): number {\n+\t\treturn this.lineNumberOf(path, this.graph.lineN(line));\n+\t}\n+\n+\t/** lineNumber by the line's number, with no lookup of its id. */\n+\tlineNumberOf(path: string, n: number): number {\n \t\tconst f = this.#file(path);\n \t\tif (!f.visIndex) {\n \t\t\tf.visIndex = new Map();\n \t\t\tfor (let i = 0; i < f.vis.length; i++) f.visIndex.set(f.vis[i]!, i);\n \t\t}\n-\t\treturn f.visIndex.get(this.graph.lineN(line)) ?? -1;\n+\t\treturn f.visIndex.get(n) ?? -1;\n \t}\n \n+\t/** The seq of the atom that wrote each visible line, in order: lines() without ids or text. */\n+\tvisibleWriters(path: string): number[] {\n+\t\tconst graph = this.graph;\n+\t\tconst { vis, visWriter } = this.#file(path);\n+\t\t// The packed layout keeps them (abso Capacity): looking each up again read a page per few lines on a stored graph.\n+\t\tif (visWriter) return Array.from(visWriter);\n+\t\treturn Array.from(vis, (n) => graph.writerSeqOf(n));\n+\t}\n+\n \t/** All projected text files, path -> text, in path order. Binary files are in `blobs()`. */\n \tfiles(): Record<string, string> {\n \t\tconst out: Record<string, string> = {};\n@@ -671,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@@ -687,27 +1282,24 @@\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 \t\tn += MAP_HEADER_BYTES; // #files exists from construction.\n-\t\tn += MAP_HEADER_BYTES; // #fileOf exists from construction.\n \t\tif (this.#stable) n += this.#stable.retainedBytes;\n \t\telse if (this.#set) n += OBJECT_BYTES + SET_ENTRY_BYTES * this.#set.size;\n \t\tif (this.#knownList) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#knownList.length;\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.#fileOf.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\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@@ -726,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":[]}