Lines 12–18 at 205 (were 12–18 at 204)
1212import type { Graph, RelocationInfo } from "./graph.ts"; 1313import { isStartNode } from "./keys.ts"; 1414import { resolveScratchMoves, scratchLayoutNodes } from "./relocation-scratch.ts"; 15−import { StableSet } from "./state-source.ts";
15+import { isClosedSet, StableSet } from "./state-source.ts"; 1616import { joinLines } from "./text.ts"; 1717import { type AtomId, type AtomSet, EngineError, type LineId, type NodeId } from "./types.ts"; Lines 91–103 at 205 (were 91–96 at 204)
9191const SET_ENTRY_BYTES = 32; 9292const REFERENCE_BYTES = 8; 9393const FILE_LOCATION_MEMO_ENTRIES = 256; 95+ * Moved lines a view resolves once and keeps (abso Capacity): a stored graph otherwise resolved every move again on 96+ * disk for each file it laid out. Game's whole history has 290; past this many, the view goes back to that. 98+export const MOVED_LINES_KEPT = 20_000; 99+class TooManyMoves extends Error {} 100+const NO_LINES: readonly number[] = Object.freeze([]); 96103 * The projection of a closed set. Construct once, then ask for files, lines Lines 125–134 at 205 (were 118–123 at 204)
118125 #relocs: Map<LineId, RelocationInfo> | undefined; 119126 /** The same, by n, with the new parent's code. */ 120127 #relocsN: Map<number, { readonly r: RelocationInfo; readonly parent: number }> | undefined; 128+ /** The moved lines under each node, by its code, made from the relocations on first use. */ 129+ #movedInto: Map<number, number[]> | undefined; 130+ /** Whether this view's moves are too many to keep, so they're resolved on disk per layout; undefined until asked. */ 131+ #movesOnDisk: boolean | undefined; 121132 /** Bounded memo: ancestry can span the entire history even for one visible line. */ 122133 #fileOf = new Map<number, string>(); 123134 /** The latest records asked for: one file's lines, one file's sequence, one file's text. */ Lines 142–149 at 205 (were 131–137 at 204)
131142 this.#stable = set; 133144 this.#in = new Uint8Array(0); 134− for (const id of set) {
145+ // A set kept closed when it was written needs no proof here (abso Capacity). 146+ for (const id of isClosedSet(set) ? [] : set) { 135147 const atom = graph.atom(id); 136148 for (const dependency of atom.deps) { 137149 graph.atom(dependency); Lines 232–238 at 205 (were 220–225 at 204)
220232 for (const id of this.#eachMover()) { 221233 for (const m of graph.atom(id).moves!) { 222234 if (!relocs.has(m.line)) relocs.set(m.line, pick(m.line)!); 235+ if (graph.hasOrderedChildren && relocs.size > MOVED_LINES_KEPT) throw new TooManyMoves(); 225238 const parentOf = (line: LineId): NodeId => relocs.get(line)?.parent ?? graph.lineParent(line)!; Lines 272–292 at 205 (were 259–264 at 204)
275+ /** Whether moves are resolved on disk for each layout: only a stored graph's view with more than MOVED_LINES_KEPT. */ 276+ #scratchMoves(): boolean { 277+ if (!this.#hasMoves || !this.graph.hasOrderedChildren) return false; 278+ if (this.#movesOnDisk === undefined) { 280+ this.#relocations(); 281+ this.#movesOnDisk = false; 283+ if (!(error instanceof TooManyMoves)) throw error; 284+ this.#movesOnDisk = true; 287+ return this.#movesOnDisk; 262290 /** The relocations by n, with each new parent's code. */ 263291 #relocationsN(): Map<number, { readonly r: RelocationInfo; readonly parent: number }> { 264292 if (this.#relocsN) return this.#relocsN; Lines 300–306 at 205 (were 272–278 at 204)
272300 /** The node a line hangs off in this set: where it was written, or where a move put it. */ 273301 parentOf(line: LineId): NodeId { 274302 if (this.graph.lineParent(line) === undefined) throw new EngineError("unknown-line", `no line ${line}`); 275− if (this.#hasMoves && this.graph.hasOrderedChildren)
303+ if (this.#scratchMoves()) 276304 return this.graph.withProjectionScratch((scratch) => 277305 resolveScratchMoves(this.graph, (id) => this.#has(this.graph.atom(id).seq), scratch).parent(line), Lines 330–336 at 205 (were 302–308 at 204)
302330 #fileOfN(n: number): string { 303331 const graph = this.graph; 304332 if (!this.#hasMoves) return graph.writtenIn(n); 305− if (graph.hasOrderedChildren)
333+ if (this.#scratchMoves()) 306334 return graph.withProjectionScratch((scratch) => { 307335 const moves = resolveScratchMoves(graph, (id) => this.#has(graph.atom(id).seq), scratch); 308336 let current = graph.nodeOf(n); Lines 373–390 at 205 (were 345–354 at 204)
345373 out.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) }); 348− for (const r of graph.eachRelocatedToCode(code)) {
349− const n = graph.lineN(r.line);
350− const selected = relocs.get(n)?.r;
351− if (!selected || selected.atom !== r.atom || selected.k !== r.k) continue;
376+ // The lines this set's moves put under each node, from the moves it already resolved: no row read per node 378+ if (!this.#movedInto) { 379+ this.#movedInto = new Map(); 380+ for (const [n, { parent }] of relocs) { 381+ const into = this.#movedInto.get(parent); 382+ if (into) into.push(n); 383+ else this.#movedInto.set(parent, [n]); 386+ for (const n of this.#movedInto.get(code) ?? NO_LINES) { 387+ const r = relocs.get(n)!.r; 352388 const seq = graph.atom(r.atom).seq; 353389 out.push({ n, replaces: 0, lamport: graph.lamportOfSeq(seq), seq, k: r.k }); Lines 451–457 at 205 (were 415–421 at 204)
415451 /** Lay out one file: its line tree from the set's atoms, linearized. */ 416452 #layout(path: string, complete: boolean): FileState { 417453 const graph = this.graph; 418− if (this.#hasMoves && graph.hasOrderedChildren)
454+ if (this.#scratchMoves()) 419455 return graph.withProjectionScratch((scratch) => 420456 this.#layoutResolved( Lines 671–677 at 205 (were 635–641 at 204)
636672 return found && previous !== undefined ? [previous] : []; 638− if (this.#hasMoves && graph.hasOrderedChildren)
674+ if (this.#scratchMoves()) 639675 return graph.withProjectionScratch((scratch) => 640676 scan(scratchLayoutNodes(graph, (id) => this.#has(graph.atom(id).seq), scratch, graph.nodeOf(start))),