{"draft":null,"from":205,"to":206,"files":[{"path":"src/engine/view.ts","added":129,"removed":2,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -64,6 +64,8 @@\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/** Positions by n, built the first time they're asked for. */\n \tseqIndex?: Map<number, number>;\n \tvisIndex?: Map<number, number>;\n@@ -451,6 +453,7 @@\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 (graph.packedFiles && !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@@ -462,10 +465,133 @@\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\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\t// Where each node sits, walked once for the whole view: through the moves, up to a file's start.\n+\t\t\tconst memo = new Map<number, string>();\n+\t\t\tconst fileOf = (code: number): string => {\n+\t\t\t\tconst walked: number[] = [];\n+\t\t\t\tlet cur = code;\n+\t\t\t\tlet file: string | undefined;\n+\t\t\t\twhile (file === undefined) {\n+\t\t\t\t\tif (cur < 0) file = graph.nodeOf(cur).slice(1);\n+\t\t\t\t\telse {\n+\t\t\t\t\t\tfile = memo.get(cur);\n+\t\t\t\t\t\tif (file === undefined) {\n+\t\t\t\t\t\t\twalked.push(cur);\n+\t\t\t\t\t\t\tcur = relocs.get(cur)?.parent ?? graph.parentCodeOf(cur);\n+\t\t\t\t\t\t}\n+\t\t\t\t\t}\n+\t\t\t\t}\n+\t\t\t\tfor (const n of walked) memo.set(n, file);\n+\t\t\t\treturn file;\n+\t\t\t};\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, and in the\n+\t * files its moved-in lines came from, read a page at a time; the same tree and order as #layoutResolved, built in\n+\t * 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) return { attrs, complete, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };\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\tconst deleters = new Map<number, number[]>();\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\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: graph.lamportOfSeq(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: graph.lamportOfSeq(mover), seq: mover, k: moved.r.k };\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}\n+\t\t\tfor (const [n, deleter] of graph.packedDeletes(file)) {\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}\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) => graph.lamportOfSeq(a) - graph.lamportOfSeq(b) || a - b;\n+\t\tconst seq: number[] = [];\n+\t\tconst vis: number[] = [];\n+\t\tconst visText: (string | null)[] = [];\n+\t\tconst deletedBy = new Map<number, readonly number[]>();\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(texts.get(n) ?? null);\n+\t\t\t}\n+\t\t\tif (complete) seq.push(n);\n+\t\t\tpush(n);\n+\t\t}\n+\t\treturn { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy, visText };\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@@ -556,9 +682,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@@ -742,6 +868,7 @@\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\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\tif (f.seqIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size;"}],"unmatched":[]}