{"draft":null,"from":108,"to":109,"files":[{"path":"src/engine/view.ts","added":37,"removed":8,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -11,6 +11,7 @@\n \n import type { Graph, RelocationInfo } from \"./graph.ts\";\n import { isStartNode } from \"./keys.ts\";\n+import { 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@@ -90,7 +91,8 @@\n export class View {\n \treadonly graph: Graph;\n \t/** The set, made from #in the first time it's asked for: a view keeps a byte per atom, not a copy of the set (#46). */\n-\t#set: Set<AtomId> | undefined;\n+\t#set: AtomSet | undefined;\n+\treadonly #stable: StableSet<AtomId> | undefined;\n \t#files = new Map<string, FileState>();\n \t/** Every file the set wrote in, sorted; worked out the first time it's asked for. */\n \t#knownList: string[] | undefined;\n@@ -109,14 +111,34 @@\n \t#textCache: { path: string; text: string } | undefined;\n \n \tconstructor(graph: Graph, set: Iterable<AtomId>) {\n+\t\tthis.graph = graph;\n+\t\tif (set instanceof StableSet) {\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\tconst atom = graph.atom(id);\n+\t\t\t\tfor (const dependency of atom.deps) {\n+\t\t\t\t\tgraph.atom(dependency);\n+\t\t\t\t\tif (!set.has(dependency))\n+\t\t\t\t\t\tthrow new EngineError(\"not-closed\", `set is not closed: ${id} depends on ${dependency}`);\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tthis.#movers = graph.movers().filter((id) => set.has(id));\n+\t\t\tthis.#relocs = this.#movers.length === 0 ? new Map() : undefined;\n+\t\t\tthis.#relocsN = this.#movers.length === 0 ? new Map() : undefined;\n+\t\t\treturn;\n+\t\t}\n \t\tconst inSet = new Uint8Array(graph.size);\n-\t\tconst atoms: { readonly id: AtomId; readonly deps: readonly AtomId[] }[] = [];\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\tatoms.push(atom);\n \t\t}\n-\t\tfor (const atom of atoms) {\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@@ -141,6 +163,7 @@\n \n \t/** Whether the atom with this seq is in the set. */\n \t#has(seq: number): boolean {\n+\t\tif (this.#stable) return this.#stable.has(this.graph.idOfSeq(seq));\n \t\treturn seq < this.#in.length && this.#in[seq] === 1;\n \t}\n \n@@ -272,7 +295,8 @@\n \t\tif (relocs) {\n \t\t\tfor (const r of graph.relocatedToCode(code)) {\n \t\t\t\tconst n = graph.lineN(r.line);\n-\t\t\t\tif (relocs.get(n)?.r !== r) continue;\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\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}\n@@ -336,8 +360,12 @@\n \tget #known(): string[] {\n \t\tif (!this.#knownList) {\n \t\t\tconst known = new Set<string>();\n-\t\t\tfor (let seq = 0; seq < this.#in.length; seq++)\n-\t\t\t\tif (this.#in[seq] === 1) for (const file of this.graph.filesOf(this.graph.idOfSeq(seq))) known.add(file);\n+\t\t\tif (this.#stable) {\n+\t\t\t\tfor (const id of this.#stable) for (const file of this.graph.filesOf(id)) known.add(file);\n+\t\t\t} else {\n+\t\t\t\tfor (let seq = 0; seq < this.#in.length; seq++)\n+\t\t\t\t\tif (this.#in[seq] === 1) for (const file of this.graph.filesOf(this.graph.idOfSeq(seq))) known.add(file);\n+\t\t\t}\n \t\t\tthis.#knownList = [...known].sort();\n \t\t}\n \t\treturn this.#knownList;\n@@ -470,7 +498,8 @@\n \t\tn += 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.#set) n += OBJECT_BYTES + SET_ENTRY_BYTES * this.#set.size;\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;"}],"unmatched":[]}