--- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -95,7 +95,6 @@
9595const MAP_ENTRY_BYTES = 40; 9696const SET_ENTRY_BYTES = 32; 9797const REFERENCE_BYTES = 8; 98−const FILE_LOCATION_MEMO_ENTRIES = 256;
10099 * Moved lines a view resolves once and keeps (abso Capacity): a stored graph otherwise resolved every move again on 101100 * disk for each file it laid out. Game's whole history has 290; past this many, the view goes back to that. @@ -103,10 +102,86 @@
103102export const MOVED_LINES_KEPT = 20_000; 104103class TooManyMoves extends Error {} 105104const NO_LINES: readonly number[] = Object.freeze([]); 106−/** Lines whose place views sharing moves remember, at most: about 40 bytes each. */
107−const SHARED_FILE_MEMO = 200_000;
105+/** Nodes whose nearest moved line above a graph remembers, at most: about 40 bytes each. */ 106+const CLIMB_MEMO = 200_000; 108+/** One relocation of a moved line, with its line's number and its new parent's code. */ 109+interface MoveEntry { 111+ readonly r: RelocationInfo; 112+ readonly parent: number; 116+ * A graph's moves, whatever the set (abso Capacity): each mover's moved lines, each moved line's relocations in seq 117+ * order, and for any node the nearest node at or above it, by where lines were written, that a move ever relocated, 118+ * or its file's start. Worked out once per graph and extended as it grows. A view's moves then resolve over the moved 119+ * lines alone: before, every view walked each moved line's file up to its start, a read per line. 122+ readonly #graph: Graph; 124+ /** Movers in seq order, with the numbers of the lines each moves, in its moves' order. */ 125+ readonly movers: { readonly seq: number; readonly lines: readonly number[] }[] = []; 126+ /** Every relocation of each moved line, by the line's number, in seq order. */ 127+ readonly byLine = new Map<number, MoveEntry[]>(); 128+ readonly #climbed = new Map<number, number>(); 130+ constructor(graph: Graph) { 131+ this.#graph = graph; 134+ /** Take in movers the graph gained since. */ 136+ const graph = this.#graph; 137+ const all = graph.movers(); 138+ if (this.#seen === all.length) return this; 139+ for (; this.#seen < all.length; this.#seen++) { 140+ const id = all[this.#seen]!; 141+ const lines: number[] = []; 142+ graph.atom(id).moves!.forEach((m, k) => { 143+ const n = graph.lineN(m.line); 144+ const r: RelocationInfo = Object.freeze({ atom: id, line: m.line, parent: m.parent, k }); 145+ const entry: MoveEntry = { n, r, parent: graph.codeOf(m.parent)! }; 146+ const list = this.byLine.get(n); 147+ if (list) list.push(entry); 148+ else this.byLine.set(n, [entry]); 151+ this.movers.push({ seq: seqOf(id), lines }); 153+ // A new moved line can sit between a node and the one it climbed to. 154+ this.#climbed.clear(); 158+ /** The nearest node at or above this one, by where lines were written, that a move ever relocated; or its file's start. */ 159+ climb(code: number): number { 160+ const walked: number[] = []; 162+ while (c >= 0 && !this.byLine.has(c)) { 163+ const known = this.#climbed.get(c); 164+ if (known !== undefined) { 169+ c = this.#graph.parentCodeOf(c); 171+ if (this.#climbed.size + walked.length > CLIMB_MEMO) this.#climbed.clear(); 172+ for (const w of walked) this.#climbed.set(w, c); 177+const skeletons = new WeakMap<Graph, MoveSkeleton>(); 178+function skeletonOf(graph: Graph): MoveSkeleton { 179+ let skeleton = skeletons.get(graph); 180+ if (!skeleton) skeletons.set(graph, (skeleton = new MoveSkeleton(graph))); 181+ return skeleton.update(); 110185 * The projection of a closed set. Construct once, then ask for files, lines 111186 * and sequences. Throws `not-closed` if some atom's dependency is missing. 112187 * Each file is laid out the first time it is asked for, so a view of a large @@ -136,8 +211,6 @@
136211 #movedInto: Map<number, number[]> | undefined; 137212 /** Whether this view's moves are too many to keep, so they're resolved on disk per layout; undefined until asked. */ 138213 #movesOnDisk: boolean | undefined; 139− /** Bounded memo: ancestry can span the entire history even for one visible line. */
140− #fileOf = new Map<number, string>();
141214 /** The latest records asked for: one file's lines, one file's sequence, one file's text. */ 142215 #linesCache: { path: string; lines: readonly VisibleLine[] } | undefined; 143216 #seqCache: { path: string; seq: readonly SeqLine[] } | undefined; @@ -223,34 +296,46 @@
223296 #relocations(): Map<LineId, RelocationInfo> { 224297 if (this.#relocs) return this.#relocs; 225298 const graph = this.graph; 299+ const skeleton = skeletonOf(graph); 226300 const stronger = (a: RelocationInfo, b: RelocationInfo): boolean => graph.comparePriority(a.atom, b.atom) > 0; 227− // Persistent reads decode new objects. Cycle suppression must identify the
228− // logical move, not one particular decoded JavaScript object.
229301 const identity = (r: RelocationInfo): string => JSON.stringify([r.atom, r.k]); 230302 const ignored = new Set<string>(); 231− const pick = (line: LineId): RelocationInfo | undefined => {
232− let best: RelocationInfo | undefined;
233− for (const r of graph.eachRelocationOf(line)) {
234− if (this.#has(graph.atom(r.atom).seq) && !ignored.has(identity(r)) && (!best || stronger(r, best))) best = r;
303+ const pick = (n: number): MoveEntry | undefined => { 304+ let best: MoveEntry | undefined; 305+ for (const e of skeleton.byLine.get(n) ?? []) { 306+ if (this.#has(seqOf(e.r.atom)) && !ignored.has(identity(e.r)) && (!best || stronger(e.r, best.r))) best = e; 238− const relocs = new Map<LineId, RelocationInfo>();
239− for (const id of this.#eachMover()) {
240− for (const m of graph.atom(id).moves!) {
241− if (!relocs.has(m.line)) relocs.set(m.line, pick(m.line)!);
310+ // By the moved line's number, in the order its movers come (seq order, then each mover's moves). 311+ const relocs = new Map<number, MoveEntry>(); 312+ for (const mover of skeleton.movers) { 313+ if (!this.#has(mover.seq)) continue; 314+ for (const n of mover.lines) { 315+ if (!relocs.has(n)) relocs.set(n, pick(n)!); 242316 if (graph.hasOrderedChildren && relocs.size > MOVED_LINES_KEPT) throw new TooManyMoves(); 245− const parentOf = (line: LineId): NodeId => relocs.get(line)?.parent ?? graph.lineParent(line)!;
246− const reaches = new Set<LineId>();
319+ // From a node, the next node that matters for loops: a line moved in this set, or a file's start. 320+ const hop = (code: number): number => { 321+ let c = skeleton.climb(code); 322+ while (c >= 0 && !relocs.has(c)) c = skeleton.climb(graph.parentCodeOf(c)); 325+ const step = (n: number): number => { 326+ const e = relocs.get(n); 327+ return hop(e ? e.parent : graph.parentCodeOf(n)); 329+ // Two moves made apart can loop lines into each other, so no start node reaches them; then the oldest 330+ // relocation on the loop is ignored, and the next newest of that line counts, until none loop. 331+ const reaches = new Set<number>(); 247332 for (const line of [...relocs.keys()]) { 249− const path: LineId[] = [];
250− const onPath = new Map<LineId, number>();
251− let cur: NodeId = line;
334+ const path: number[] = []; 335+ const onPath = new Map<number, number>(); 253− while (!isStartNode(cur) && !reaches.has(cur)) {
338+ while (cur >= 0 && !reaches.has(cur)) { 254339 const at = onPath.get(cur); 255340 if (at !== undefined) { @@ -258,30 +343,28 @@
259344 onPath.set(cur, path.length); 261− cur = parentOf(cur);
264349 for (const l of path) reaches.add(l); 267− let weakest: RelocationInfo | undefined;
352+ let weakest: MoveEntry | undefined; 268353 for (const l of path.slice(loopAt)) { 269− const r = relocs.get(l);
270− if (r && (!weakest || stronger(weakest, r))) weakest = r;
354+ const e = relocs.get(l); 355+ if (e && (!weakest || stronger(weakest.r, e.r))) weakest = e; 272− ignored.add(identity(weakest!));
273− const next = pick(weakest!.line);
274− if (next) relocs.set(weakest!.line, next);
275− else relocs.delete(weakest!.line);
357+ ignored.add(identity(weakest!.r)); 358+ const next = pick(weakest!.n); 359+ if (next) relocs.set(weakest!.n, next); 360+ else relocs.delete(weakest!.n); 278− this.#relocs = relocs;
279− return relocs;
363+ this.#relocsN = new Map([...relocs].map(([n, e]) => [n, { r: e.r, parent: e.parent }])); 364+ this.#relocs = new Map([...relocs.values()].map((e) => [e.r.line, e.r])); 365+ return this.#relocs; 282− /** Where lines sit, shared with views that took this one's moves (shareMoves): unbounded up to SHARED_FILE_MEMO. */
283− #fileOfShared: Map<number, string> | undefined;
284−
286369 * Take another view's resolved moves (abso Capacity), when both sets hold the very same atoms that move lines: the 287370 * moves then resolve the same, and every line sits where it sits there. Checked here, so any caller may ask. @@ -297,8 +380,6 @@
297380 this.#relocsN = from.#relocationsN(); 298381 this.#movesOnDisk = false; 299382 this.#movedFrom = from.#movedInFrom(); 300− from.#fileOfShared ??= new Map();
301− this.#fileOfShared = from.#fileOfShared;
304385 /** Whether moves are resolved on disk for each layout: only a stored graph's view with more than MOVED_LINES_KEPT. */ @@ -366,36 +447,21 @@
366447 while (!isStartNode(current)) current = moves.parent(current); 367448 return current.slice(1); 450+ return this.#fileOfCode(n); 453+ /** The file a node sits in, in this set: up by where lines were written, through the moves that count here. */ 454+ #fileOfCode(code: number): string { 455+ const graph = this.graph; 369456 const relocs = this.#relocationsN(); 370− // Keep only a fixed tail of the walk for path compression. The selected
371− // relocation graph is acyclic, so traversal needs no history-sized stack.
372− const path: number[] = [];
373− let visited = 0;
374− let cur = n;
375− let file: string | undefined;
376− while (file === undefined) {
377− if (cur < 0) file = graph.nodeOf(cur).slice(1);
378− else {
379− file = (this.#fileOfShared ?? this.#fileOf).get(cur);
380− if (file === undefined) {
381− if (this.#fileOfShared) path.push(cur);
382− else path[visited++ % FILE_LOCATION_MEMO_ENTRIES] = cur;
383− cur = relocs.get(cur)?.parent ?? graph.parentCodeOf(cur);
384− }
385− }
457+ const skeleton = skeletonOf(graph); 460+ c = skeleton.climb(c); 461+ if (c < 0) return graph.nodeOf(c).slice(1); 462+ const e = relocs.get(c); 463+ c = e ? e.parent : graph.parentCodeOf(c); 387− const shared = this.#fileOfShared;
388− if (shared) {
389− if (shared.size > SHARED_FILE_MEMO) shared.clear();
390− for (const l of path) shared.set(l, file);
391− return file;
392− }
393− for (const l of path) {
394− if (!this.#fileOf.has(l) && this.#fileOf.size >= FILE_LOCATION_MEMO_ENTRIES)
395− this.#fileOf.delete(this.#fileOf.keys().next().value!);
396− this.#fileOf.set(l, file);
397− }
398− return file;
401467 /** The lines hanging off a node in this set, in layout order. */ @@ -507,25 +573,7 @@
507573 const out = new Map<string, Set<number>>(); 508574 if (this.#hasMoves) { 509575 const relocs = this.#relocationsN(); 510− // Where each node sits, walked once for the whole view: through the moves, up to a file's start.
511− const memo = new Map<number, string>();
512− const fileOf = (code: number): string => {
513− const walked: number[] = [];
514− let cur = code;
515− let file: string | undefined;
516− while (file === undefined) {
517− if (cur < 0) file = graph.nodeOf(cur).slice(1);
518− else {
519− file = memo.get(cur);
520− if (file === undefined) {
521− walked.push(cur);
522− cur = relocs.get(cur)?.parent ?? graph.parentCodeOf(cur);
523− }
524− }
525− }
526− for (const n of walked) memo.set(n, file);
527− return file;
528− };
576+ const fileOf = (code: number): string => this.#fileOfCode(code); 529577 for (const [n, { parent }] of relocs) { 530578 const into = fileOf(parent); 531579 let from = out.get(into); @@ -909,13 +957,11 @@
909957 n += 2 * ARRAY_HEADER_BYTES + this.#membershipSeq.byteLength + this.#membershipValue.byteLength; 910958 if (this.#movers) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length; 911959 n += MAP_HEADER_BYTES; // #files exists from construction. 912− n += MAP_HEADER_BYTES; // #fileOf exists from construction.
913960 if (this.#stable) n += this.#stable.retainedBytes; 914961 else if (this.#set) n += OBJECT_BYTES + SET_ENTRY_BYTES * this.#set.size; 915962 if (this.#knownList) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#knownList.length; 916963 if (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size; 917964 if (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size; 918− n += MAP_ENTRY_BYTES * this.#fileOf.size;
919965 for (const f of this.#files.values()) { 920966 n += MAP_ENTRY_BYTES; 921967 if (f !== NO_FILE) {