{"draft":null,"from":291,"to":318,"files":[{"path":"src/engine/graph.ts","added":53,"removed":52,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -1524,43 +1524,39 @@\n \t * the search never descends below the target's lamport.\n \t */\n \t#reaches(from: Iterable<AtomId>, target: AtomId): boolean {\n-\t\tconst floor = this.#lamportAt(this.#seq(target));\n-\t\tif (this.#storage) {\n-\t\t\treturn this.withProjectionScratch((scratch) => {\n-\t\t\t\tlet length = 0;\n-\t\t\t\tconst push = (id: AtomId): void => {\n-\t\t\t\t\tif (scratch.read(\"seen\", id)) return;\n-\t\t\t\t\tscratch.write(\"seen\", id, true);\n-\t\t\t\t\tscratch.write(\"stack\", String(length++), id);\n-\t\t\t\t};\n-\t\t\t\tfor (const id of from) {\n-\t\t\t\t\tif (id === target) return true;\n-\t\t\t\t\tif (this.#lamportAt(this.#seq(id)) > floor) push(id);\n-\t\t\t\t}\n-\t\t\t\twhile (length > 0) {\n-\t\t\t\t\tconst key = String(--length);\n-\t\t\t\t\tconst current = scratch.read(\"stack\", key) as AtomId;\n-\t\t\t\t\tscratch.remove(\"stack\", key);\n-\t\t\t\t\tif (this.#lamportAt(this.#seq(current)) <= floor) continue;\n-\t\t\t\t\tfor (const dependency of this.atom(current).deps) {\n-\t\t\t\t\t\tif (dependency === target) return true;\n-\t\t\t\t\t\tpush(dependency);\n-\t\t\t\t\t}\n-\t\t\t\t}\n-\t\t\t\treturn false;\n-\t\t\t});\n+\t\treturn this.#reachable(from, new Set([target])).has(target);\n+\t}\n+\n+\t/**\n+\t * Which of `targets` are reachable along deps from any of `from` (themselves included), in one walk: it never\n+\t * descends below the lowest target's lamport, and stops once every target is found (abso 714, piece 4: record\n+\t * walked once per line writer).\n+\t * The walk's seen marks and stack are in memory, by atom number: one byte and four bytes an atom at most, so about\n+\t * 62 KB on abso's 12,454 atoms and 50 MB at 10 million, under a Worker's 128 MB. They were scratch rows in storage\n+\t * (abso-core 139, bounding memory with the graph), which cost a read and a write per atom visited: on abso's Oct 14\n+\t * tables, 41,000 of a 1470-shaped record's 77,767 statements.\n+\t */\n+\t#reachable(from: Iterable<AtomId>, targets: ReadonlySet<AtomId>): Set<AtomId> {\n+\t\tconst found = new Set<AtomId>();\n+\t\tlet floor = Number.POSITIVE_INFINITY;\n+\t\tfor (const target of targets) floor = Math.min(floor, this.#lamportAt(this.#seq(target)));\n+\t\tconst seen = new Uint8Array(this.#atoms.length);\n+\t\tconst stack = new Int32Array(this.#atoms.length);\n+\t\tlet length = 0;\n+\t\tconst visit = (id: AtomId): void => {\n+\t\t\tif (targets.has(id)) found.add(id);\n+\t\t\tconst seq = this.#seq(id);\n+\t\t\tif (seen[seq]) return;\n+\t\t\tseen[seq] = 1;\n+\t\t\tstack[length++] = seq;\n+\t\t};\n+\t\tfor (const id of from) visit(id);\n+\t\twhile (length > 0 && found.size < targets.size) {\n+\t\t\tconst seq = stack[--length]!;\n+\t\t\tif (this.#lamportAt(seq) <= floor) continue;\n+\t\t\tfor (const dependency of this.atom(atomId(seq)).deps) visit(dependency);\n \t\t}\n-\t\tconst seen = new Set<AtomId>();\n-\t\tconst stack = [...from];\n-\t\twhile (stack.length > 0) {\n-\t\t\tconst cur = stack.pop()!;\n-\t\t\tif (cur === target) return true;\n-\t\t\tif (seen.has(cur)) continue;\n-\t\t\tseen.add(cur);\n-\t\t\tif (this.#lamportAt(this.#seq(cur)) <= floor) continue;\n-\t\t\tfor (const d of this.atom(cur).deps) if (!seen.has(d)) stack.push(d);\n-\t\t}\n-\t\treturn false;\n+\t\treturn found;\n \t}\n \n \t/**\n@@ -1610,23 +1606,28 @@\n \t\tfor (const r of atom.reasons) if (!this.has(r.to)) bad(`reason points at unknown atom ${r.to}`);\n \n \t\t// Everything it touches must come from its closure. Derived deps name the\n-\t\t// writers directly, so this is usually one lookup per writer.\n-\t\tconst memoryBelow = scratch ? undefined : new Map<AtomId, boolean>();\n-\t\tconst below = {\n-\t\t\tget: (key: AtomId) => (scratch ? (scratch.read(\"below\", key) as boolean | undefined) : memoryBelow!.get(key)),\n-\t\t\tset: (key: AtomId, value: boolean) => {\n-\t\t\t\tif (scratch) scratch.write(\"below\", key, value);\n-\t\t\t\telse memoryBelow!.set(key, value);\n-\t\t\t},\n-\t\t};\n-\t\tconst inClosure = (writer: AtomId): boolean => {\n-\t\t\tlet hit = below.get(writer);\n-\t\t\tif (hit === undefined) {\n-\t\t\t\thit = deps.has(writer) || this.#reaches(atom.deps, writer);\n-\t\t\t\tbelow.set(writer, hit);\n+\t\t// writers directly, so this is usually one lookup per writer. The writers\n+\t\t// that aren't deps are walked for together, once (abso 714, piece 4).\n+\t\tconst touched: LineId[] = [];\n+\t\tfor (const run of atom.inserts) if (!isStartNode(run.parent)) touched.push(run.parent);\n+\t\tfor (const m of atom.moves ?? []) {\n+\t\t\ttouched.push(m.line);\n+\t\t\tif (!isStartNode(m.parent)) touched.push(m.parent);\n+\t\t}\n+\t\ttouched.push(...atom.deletes);\n+\t\tfor (const list of [atom.writes, atom.reads, atom.holds])\n+\t\t\tfor (const key of list) {\n+\t\t\t\tconst parsed = parseKey(key);\n+\t\t\t\tif (parsed.kind === \"line\" || ((parsed.kind === \"gap\" || parsed.kind === \"pos\") && !isStartNode(parsed.node)))\n+\t\t\t\t\ttouched.push(parsed.node);\n \t\t\t}\n-\t\t\treturn hit;\n-\t\t};\n+\t\tconst beyond = new Set<AtomId>();\n+\t\tfor (const line of touched) {\n+\t\t\tconst writer = this.lineAtom(line);\n+\t\t\tif (writer !== undefined && !deps.has(writer)) beyond.add(writer);\n+\t\t}\n+\t\tconst reached = beyond.size > 0 ? this.#reachable(atom.deps, beyond) : beyond;\n+\t\tconst inClosure = (writer: AtomId): boolean => deps.has(writer) || reached.has(writer);\n \t\tconst checkLine = (line: LineId, what: string): void => {\n \t\t\tconst writer = this.lineAtom(line);\n \t\t\tif (writer === undefined) return bad(`${what} names unknown line ${line}`);"},{"path":"src/engine/view.ts","added":19,"removed":10,"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@@ -1095,7 +1095,14 @@\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@@ -1105,7 +1112,7 @@\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: graph.idOfSeq(graph.writerSeqOf(n)),\n+\t\t\t\tatom: atomId(graph.writerSeqOf(n)),\n \t\t\t};\n \t}\n \n@@ -1119,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@@ -1136,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@@ -1163,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}"}],"unmatched":[]}