{"draft":null,"from":263,"to":318,"files":[{"path":"src/engine/view.ts","added":151,"removed":29,"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, lineId, 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@@ -56,6 +56,7 @@\n \t| \"knowPaths\"\n \t| \"laidOut\"\n \t| \"forget\"\n+\t| \"placedDifferently\"\n \t| \"fileOfNumber\"\n \t| \"lineNumberOf\"\n \t| \"visibleWriters\"\n@@ -63,7 +64,12 @@\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@@ -122,7 +128,13 @@\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@@ -172,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@@ -196,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@@ -524,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@@ -543,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@@ -603,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@@ -619,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@@ -727,11 +785,22 @@\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@@ -746,33 +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@@ -785,6 +871,18 @@\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@@ -800,7 +898,7 @@\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@@ -884,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@@ -991,14 +1095,25 @@\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@@ -1011,12 +1126,12 @@\n \t\t\tconst n = vis[i]!;\n \t\t\tconst packed = visText?.[i];\n \t\t\tlines[i] = {\n-\t\t\t\tid: visWriter && visK ? lineId(graph.idOfSeq(visWriter[i]!), visK[i]!) : 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@@ -1028,12 +1143,14 @@\n \t\tconst graph = this.graph;\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: visWriter && visK ? lineId(graph.idOfSeq(visWriter[i]!), visK[i]!) : 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@@ -1055,13 +1172,13 @@\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: seqWriter && seqK ? lineId(graph.idOfSeq(writer), seqK[i]!) : graph.lineIdOf(n),\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: graph.idOfSeq(writer),\n-\t\t\t\tdeletedBy: by ? by.map((s) => graph.idOfSeq(s)) : NONE,\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@@ -1217,7 +1334,12 @@\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<{ path: string; layout: object; used: number; bytes: number }> {\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);"}],"unmatched":[]}