Lines 10–16 at 318 (were 10–16 at 215)
1010// made when a caller asks for them, and the latest asked for is kept. 1212import type { Graph, RelocationInfo } from "./graph.ts"; 13−import { isStartNode, seqOf } from "./keys.ts";
13+import { atomId, isStartNode, lineId, seqOf } from "./keys.ts"; 1414import { resolveScratchMoves, scratchLayoutNodes } from "./relocation-scratch.ts"; 1515import { isClosedSet, StableSet } from "./state-source.ts"; 1616import { joinLines } from "./text.ts"; Lines 49–97 at 318 (were 49–60 at 215)
4949/** Structural read boundary shared by historical and transaction-bound projections. */ 5050export type ReadView = Omit< 5151 Pick<View, keyof View>, 52− "sameText" | "visibleCount" | "shareMoves" | "fileOfNumber" | "lineNumberOf" | "visibleWriters"
59+ | "placedDifferently" 5464 sameText(path: string, other: ReadView): boolean; 57−const NO_ATTRS: FileAttrs = { exists: false, exec: false, link: null, blob: null };
67+const NO_ATTRS: FileAttrs = { 75+ * A file layout's bytes, less its lookup indexes, worked out once per layout (abso Capacity): a layout never changes 76+ * once made, and the view cache weighs every view it holds on every lookup, which summed every line's text again. 78+const layoutSizes = new WeakMap<FileState, number>(); 79+function layoutBytes(f: FileState): number { 80+ let n = layoutSizes.get(f); 81+ if (n !== undefined) return n; 82+ // FileState and FileAttrs are per laid-out file; typed-array headers are separate from their payload byte lengths. 83+ n = OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES; 84+ n += f.seq.byteLength + f.vis.byteLength; 85+ if (f.visText) for (const text of f.visText) n += REFERENCE_BYTES + (text === null ? 0 : stringBytes(text)); 86+ if (f.visWriter) n += 4 * f.visWriter.length; 87+ if (f.visK) n += 4 * f.visK.length; 88+ if (f.seqWriter) n += 4 * f.seqWriter.length; 89+ if (f.seqK) n += 4 * f.seqK.length; 90+ n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + ARRAY_HEADER_BYTES) * f.deletedBy.size; 91+ for (const deleters of f.deletedBy.values()) n += REFERENCE_BYTES * deleters.length; 92+ layoutSizes.set(f, n); 5895const NONE: readonly never[] = Object.freeze([]); 6097interface FileState { Lines 106–118 at 318 (were 69–74 at 215)
69106 readonly deletedBy: ReadonlyMap<number, readonly number[]>; 70107 /** Its visible lines' text, when laid out from the file's packed history: null for a line too long to pack. */ 71108 readonly visText?: readonly (string | null)[]; 109+ /** Its visible lines' writers' seqs, when laid out from the file's packed history. */ 110+ readonly visWriter?: Int32Array; 111+ /** Its visible lines' places in their writers (a line id's k), when laid out from packed history. */ 112+ readonly visK?: Int32Array; 113+ /** The same for every line of seq, when complete and laid out from packed history. */ 114+ readonly seqWriter?: Int32Array; 115+ readonly seqK?: Int32Array; 72116 /** Positions by n, built the first time they're asked for. */ 73117 seqIndex?: Map<number, number>; 74118 visIndex?: Map<number, number>; Lines 128–144 at 318 (were 84–91 at 215)
86130const EMPTY_INTS = new Int32Array(0); 87−const NO_FILE: FileState = { attrs: NO_ATTRS, complete: true, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
131+const NO_FILE: FileState = { 136+ deletedBy: new Map(), 139+/** A count of file uses shared by every view, so a cache can tell which layout across its views was used longest ago. */ 89142// Approximate retained-container costs for View.weight(): reference slots, 90143// object headers, and map/set entries. These are cache-budget units, not V8 91144// heap measurements. Referenced Graph strings and records are not charged. Lines 148–163 at 318 (were 95–101 at 215)
95148const MAP_ENTRY_BYTES = 40; 96149const SET_ENTRY_BYTES = 32; 97150const REFERENCE_BYTES = 8; 151+const STRING_HEADER_BYTES = 16; 153+ * What V8 holds for a string of its own (abso Capacity, measured on abso's layouts): a header, then one byte a 154+ * character when every character fits in Latin-1 and two otherwise, rounded up to 8. 156+export function stringBytes(text: string): number { 157+ const chars = /[^\u0000-\u00ff]/.test(text) ? 2 * text.length : text.length; 158+ return (STRING_HEADER_BYTES + chars + 7) & ~7; 99161 * Moved lines a view resolves once and keeps (abso Capacity): a stored graph otherwise resolved every move again on 100162 * disk for each file it laid out. Game's whole history has 290; past this many, the view goes back to that. Lines 184–193 at 318 (were 122–128 at 215)
122184 readonly #graph: Graph; 124186 /** 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[] }[] = [];
188+ readonly seq: number; 189+ readonly lines: readonly number[]; 126191 /** Every relocation of each moved line, by the line's number, in seq order. */ 127192 readonly byLine = new Map<number, MoveEntry[]>(); 128193 readonly #climbed = new Map<number, number>(); Lines 196–209 at 318 (were 131–139 at 215)
131196 this.#graph = graph; 199+ /** The graph's accepted count when movers were last taken in: nothing to read while it's the same. */ 134202 /** Take in movers the graph gained since. */ 136204 const graph = this.#graph; 205+ if (this.#stamp === graph.accepted) return this; 206+ this.#stamp = graph.accepted; 137207 const all = graph.movers(); 138208 if (this.#seen === all.length) return this; 139209 for (; this.#seen < all.length; this.#seen++) { Lines 211–222 at 318 (were 141–147 at 215)
141211 const lines: number[] = []; 142212 graph.atom(id).moves!.forEach((m, k) => { 143213 const n = graph.lineN(m.line); 144− const r: RelocationInfo = Object.freeze({ atom: id, line: m.line, parent: m.parent, k });
214+ const r: RelocationInfo = Object.freeze({ 145220 const entry: MoveEntry = { n, r, parent: graph.codeOf(m.parent)! }; 146221 const list = this.byLine.get(n); 147222 if (list) list.push(entry); Lines 317–340 at 318 (were 242–264 at 215)
244319 const inSet = new Uint8Array(graph.size); 245− for (const id of set) {
246− const atom = graph.atom(id);
247− inSet[atom.seq] = 1;
248− }
320+ // An atom's number is in its id (abso Capacity): reading the atom for it read the whole history on a stored graph. 321+ const seqIn = (id: AtomId): number => { 322+ const seq = seqOf(id); 323+ return seq >= 0 && seq < inSet.length && graph.idOfSeq(seq) === id ? seq : graph.atom(id).seq; 325+ for (const id of set) inSet[seqIn(id)] = 1; 249326 // Validate one atom at a time. Keeping all decoded atoms here defeats 250327 // storage-backed Graph reads even though the view only needs membership. 251328 for (let seq = 0; seq < inSet.length; seq++) { 252329 if (inSet[seq] !== 1) continue; 253330 const atom = graph.atom(graph.idOfSeq(seq)); 254331 for (const d of atom.deps) { 255− if (inSet[graph.atom(d).seq] !== 1)
256− throw new EngineError("not-closed", `set is not closed: ${atom.id} depends on ${d}`);
332+ if (inSet[seqIn(d)] !== 1) throw new EngineError("not-closed", `set is not closed: ${atom.id} depends on ${d}`); 259335 this.graph = graph; 261− this.#movers = graph.movers().filter((id) => inSet[graph.atom(id).seq] === 1);
337+ this.#movers = graph.movers().filter((id) => inSet[seqOf(id)] === 1); 262338 this.#hasMoves = this.#movers.length > 0; 263339 this.#relocs = this.#movers.length === 0 ? new Map() : undefined; 264340 this.#relocsN = this.#movers.length === 0 ? new Map() : undefined; Lines 371–378 at 318 (were 295–300 at 215)
296372 #relocations(): Map<LineId, RelocationInfo> { 297373 if (this.#relocs) return this.#relocs; 375+ if (this.#relocs) return this.#relocs; 298376 const graph = this.graph; 299377 const skeleton = skeletonOf(graph); 300378 const stronger = (a: RelocationInfo, b: RelocationInfo): boolean => graph.comparePriority(a.atom, b.atom) > 0; Lines 443–499 at 318 (were 365–377 at 215)
365443 return this.#relocs; 446+ /** The view whose moves this one may take, until it needs them all; then the view it took them from. */ 447+ #movesFrom: View | undefined; 448+ #movesTaken: View | undefined; 369451 * Take another view's resolved moves (abso Capacity), when both sets hold the very same atoms that move lines: the 370− * moves then resolve the same, and every line sits where it sits there. Checked here, so any caller may ask.
452+ * moves then resolve the same, and every line sits where it sits there. Checked when this view first needs all its 453+ * moves, so any caller may ask, and a view that only finds a few lines' files never resolves them. 372455 shareMoves(from: View): void { 373− if (from.graph !== this.graph || from.#hasMoves !== this.#hasMoves || !this.#hasMoves) return;
374− if (this.#relocs || from.#scratchMoves()) return;
456+ if (from.graph !== this.graph || from.#hasMoves !== this.#hasMoves || !this.#hasMoves || this.#relocs) return; 457+ this.#movesFrom = from; 461+ * Files laid out as another view lays them out (abso Capacity): a draft's view over its base's, for every file the 462+ * draft doesn't touch. The caller knows what the draft is; `except` is asked once, when a file is first needed, for 463+ * the files the draft touches, or undefined when nothing can be shared (a draft that moves lines). Without it, an 464+ * agent's tree, files and blame laid out every file of the project again for each agent. 466+ shareFiles(from: View, except: () => ReadonlySet<string> | undefined): void { 467+ if (from.graph !== this.graph || from === this || this.#filesFrom !== undefined) return; 468+ this.#filesFrom = { from, except }; 470+ #filesFrom: { from: View; except: () => ReadonlySet<string> | undefined } | null | undefined; 473+ * The files this view's set has, from what the project keeps for a published version (abso Capacity): hasFile 474+ * answers from it, not a layout of the file. Collisions resolving an import tried up to seven names, and each one 475+ * laid its file out. `paths` is asked once, when first needed; undefined lays files out as before. 477+ knowPaths(paths: () => ReadonlySet<string> | undefined): void { 478+ if (this.#pathsFrom === undefined) this.#pathsFrom = paths; 480+ #pathsFrom: (() => ReadonlySet<string> | undefined) | undefined; 481+ #knownPaths: ReadonlySet<string> | null | undefined; 482+ #filesShared: { from: View; except: ReadonlySet<string> } | null | undefined; 483+ #sharedFiles(): { from: View; except: ReadonlySet<string> } | null { 484+ if (this.#filesShared === undefined) { 485+ const share = this.#filesFrom; 486+ const except = share ? share.except() : undefined; 487+ this.#filesShared = share && except ? { from: share.from, except } : null; 489+ return this.#filesShared; 492+ #takeShared(): void { 493+ const from = this.#movesFrom; 495+ this.#movesFrom = undefined; 496+ if (from.#scratchMoves()) return; 375497 for (const id of this.graph.movers()) { 376498 const seq = seqOf(id); 377499 if (this.#has(seq) !== from.#has(seq)) return; Lines 501–529 at 318 (were 379–387 at 215)
379501 this.#relocs = from.#relocations(); 380502 this.#relocsN = from.#relocationsN(); 381503 this.#movesOnDisk = false; 382− this.#movedFrom = from.#movedInFrom();
504+ this.#movesTaken = from; 507+ /** Moved lines' relocations picked one at a time, before the view resolves them all (null: not moved here). */ 508+ readonly #picked = new Map<number, MoveEntry | null>(); 511+ * The relocation of moved line n that counts here, as #relocations picks it before any loop: right whenever the 512+ * walk that reached n goes on to a file's start, since only lines on a loop have their relocation set aside. 514+ #pickOne(n: number): MoveEntry | undefined { 515+ let picked = this.#picked.get(n); 516+ if (picked === undefined) { 517+ const graph = this.graph; 518+ let best: MoveEntry | undefined; 519+ for (const e of skeletonOf(graph).byLine.get(n) ?? []) { 520+ if (this.#has(seqOf(e.r.atom)) && (!best || graph.comparePriority(e.r.atom, best.r.atom) > 0)) best = e; 522+ this.#picked.set(n, (picked = best ?? null)); 524+ return picked ?? undefined; 385527 /** Whether moves are resolved on disk for each layout: only a stored graph's view with more than MOVED_LINES_KEPT. */ 386528 #scratchMoves(): boolean { 387529 if (!this.#hasMoves || !this.graph.hasOrderedChildren) return false; Lines 544–553 at 318 (were 402–408 at 215)
402544 if (this.#relocsN) return this.#relocsN; 403545 const out = new Map<number, { readonly r: RelocationInfo; readonly parent: number }>(); 404546 for (const [line, r] of this.#relocations()) 405− out.set(this.graph.lineN(line), { r, parent: this.graph.codeOf(r.parent)! });
547+ out.set(this.graph.lineN(line), { 549+ parent: this.graph.codeOf(r.parent)!, 406551 this.#relocsN = out; Lines 566–598 at 318 (were 421–434 at 215)
570+ * The files where a moved line sits, in this view or in `other`, when the two place it differently: a different 571+ * relocation wins, or a different parent (abso Capacity). Besides the files a draft's atoms name, these are the only 572+ * ones a draft's moves can lay out differently from its base, since a line's descendants follow it. Undefined when 573+ * either view resolves its moves in scratch, which keeps no map of them to compare. 575+ placedDifferently(other: View): Set<string> | undefined { 576+ if (this.#scratchMoves() || other.#scratchMoves()) return undefined; 577+ const here = this.#hasMoves ? this.#relocationsN() : undefined; 578+ const there = other.#hasMoves ? other.#relocationsN() : undefined; 579+ const files = new Set<string>(); 580+ const add = (n: number) => { 581+ files.add(this.#fileOfN(n)); 582+ files.add(other.#fileOfN(n)); 584+ for (const [n, x] of here ?? []) { 585+ const y = there?.get(n); 586+ if (!y || y.parent !== x.parent || y.r.atom !== x.r.atom) add(n); 588+ for (const n of there?.keys() ?? []) if (!here?.has(n)) add(n); 424592 /** Whether any atom in the set moves lines. */ 425593 hasMoves(): boolean { 426594 return this.#hasMoves; 428− *#eachMover(): IterableIterator<AtomId> {
429− if (this.#movers) yield* this.#movers;
430− else for (const id of this.graph.movers()) if (this.#stable!.has(id)) yield id;
431− }
433597 /** The file a line is in, in this set: the one its chain of parents reaches. */ 434598 fileOf(line: LineId): string { Lines 617–637 at 318 (were 453–460 at 215)
453617 /** The file a node sits in, in this set: up by where lines were written, through the moves that count here. */ 454618 #fileOfCode(code: number): string { 455619 const graph = this.graph; 456− const relocs = this.#relocationsN();
457620 const skeleton = skeletonOf(graph); 621+ if (!this.#relocsN) { 622+ // One moved line at a time, until a walk comes back to a line it passed: then moves loop, and are resolved whole. 623+ const passed = new Set<number>(); 626+ c = skeleton.climb(c); 627+ if (c < 0) return graph.nodeOf(c).slice(1); 628+ if (passed.has(c)) break; 630+ const e = this.#pickOne(c); 631+ c = e ? e.parent : graph.parentCodeOf(c); 634+ const relocs = this.#relocationsN(); 460637 c = skeleton.climb(c); Lines 649–661 at 318 (were 472–478 at 215)
472649 for (const n of graph.eachChildN(code)) { 473650 const seq = graph.writerSeqOf(n); 474651 if (!this.#has(seq) || relocs?.has(n)) continue; 475− out.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) });
654+ replaces: graph.replacesN(n) ? 1 : 0, 655+ lamport: graph.lamportOfSeq(seq), 478661 // The lines this set's moves put under each node, from the moves it already resolved: no row read per node Lines 671–683 at 318 (were 488–494 at 215)
488671 for (const n of this.#movedInto.get(code) ?? NO_LINES) { 489672 const r = relocs.get(n)!.r; 490673 const seq = graph.atom(r.atom).seq; 491− out.push({ n, replaces: 0, lamport: graph.lamportOfSeq(seq), seq, k: r.k });
677+ lamport: graph.lamportOfSeq(seq), 494683 // A line's replacements (runs of atoms that deleted it) first, then the Lines 742–748 at 318 (were 553–559 at 215)
553742 /** Lay out one file: its line tree from the set's atoms, linearized. */ 554743 #layout(path: string, complete: boolean): FileState { 555744 const graph = this.graph; 556− if (graph.packedFiles && !this.#scratchMoves()) return this.#layoutPacked(path, complete);
745+ if (graph.packedLayout && !this.#scratchMoves()) return this.#layoutPacked(path, complete); 557746 if (this.#scratchMoves()) 558747 return graph.withProjectionScratch((scratch) => 559748 this.#layoutResolved( Lines 758–765 at 318 (were 569–574 at 215)
569758 #movedFrom: Map<string, Set<number>> | undefined; 570759 #movedInFrom(): Map<string, Set<number>> { 571760 if (this.#movedFrom) return this.#movedFrom; 761+ this.#relocations(); 762+ if (this.#movesTaken) return (this.#movedFrom = this.#movesTaken.#movedInFrom()); 572763 const graph = this.graph; 573764 const out = new Map<string, Set<number>>(); 574765 if (this.#hasMoves) { Lines 777–888 at 318 (were 586–637 at 215)
589− * Lay one file out from its packed history (abso Capacity, Graph.packedLines): the lines written in it, and in the
590− * files its moved-in lines came from, read a page at a time; the same tree and order as #layoutResolved, built in
591− * memory with no read per line.
780+ * Lay one file out from its packed history (abso Capacity, Graph.packedLines): the lines written in it, in the files 781+ * its moved-in lines came from, and in the files runs under any of those lines were written in, read a page at a 782+ * time; the same tree and order as #layoutResolved, built in memory with no read per line. 593784 #layoutPacked(path: string, complete: boolean): FileState { 594785 const graph = this.graph; 595786 const attrs = this.#attributes(path); 596787 const start = graph.startCodeOf(path); 597− if (start === undefined) return { attrs, complete, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
788+ if (start === undefined) 794+ deletedBy: new Map(), 598796 const relocs = this.#hasMoves ? this.#relocationsN() : undefined; 599797 const files = new Set<number>([-1 - start, ...(this.#movedInFrom().get(path) ?? [])]); 600798 const children = new Map<number, Child[]>(); 601− const texts = new Map<number, string | null>();
799+ // Where each line's text is: its file and its place in that file's packed history. Texts are read after the walk, 800+ // only for the lines it shows (abso Capacity): every text a file and its moved-in sources ever had was read for 801+ // every layout, and abso's worker files, split out of index.ts, read all of index.ts's history each. 802+ const textFile = new Map<number, number>(); 803+ const textPlace = new Map<number, number>(); 804+ const writers = new Map<number, number>(); 805+ const places = new Map<number, number>(); 602806 const deleters = new Map<number, number[]>(); 807+ // Each atom's lamport once per layout: most lines share a few atoms, and a stored graph reads it from a page. 808+ const lamports = new Map<number, number>(); 809+ const lamportOf = (seq: number): number => { 810+ let lamport = lamports.get(seq); 811+ if (lamport === undefined) { 812+ lamport = graph.lamportOfSeq(seq); 813+ lamports.set(seq, lamport); 603817 for (const file of files) { 604− const lineTexts = graph.packedTexts(file);
605− for (const [n, parent, seq, k, replaces] of graph.packedLines(file)) {
606− const text = lineTexts.next().value as string | null;
818+ // The file's history decoded once and kept by the graph (abso Capacity): each view of it read every page again. 819+ const history = graph.packedHistory(file); 820+ const lines = history.lines; 821+ for (let i = 0, place = 0; i < lines.length; i += 5, place++) { 822+ const n = lines[i]!; 823+ const parent = lines[i + 1]!; 824+ const seq = lines[i + 2]!; 825+ const k = lines[i + 3]!; 826+ const replaces = lines[i + 4]!; 607827 if (!this.#has(seq)) continue; 608828 const moved = relocs?.get(n); 610− let child: Child = { n, replaces, lamport: graph.lamportOfSeq(seq), seq, k };
830+ let child: Child = { n, replaces, lamport: lamportOf(seq), seq, k }; 612832 const mover = graph.atom(moved.r.atom).seq; 613− child = { n, replaces: 0, lamport: graph.lamportOfSeq(mover), seq: mover, k: moved.r.k };
836+ lamport: lamportOf(mover), 614840 at = moved.parent; 616842 const list = children.get(at); 617843 if (list) list.push(child); 618844 else children.set(at, [child]); 619− texts.set(n, text);
845+ textFile.set(n, file); 846+ textPlace.set(n, place); 847+ writers.set(n, seq); 621− for (const [n, deleter] of graph.packedDeletes(file)) {
850+ const dels = history.deletes; 851+ for (let at = 0; at < dels.length; at += 2) { 852+ const n = dels[at]!; 853+ const deleter = dels[at + 1]!; 622854 if (!this.#has(deleter)) continue; 623855 const by = deleters.get(n); 624856 if (by) by.push(deleter); 625857 else deleters.set(n, [deleter]); 859+ // Runs written elsewhere under this file's lines follow them; the loop reads their files too. 860+ const crossings = history.crossings; 861+ for (let at = 0; at < crossings.length; at += 2) if (writers.has(crossings[at]!)) files.add(crossings[at + 1]!); 628863 const order = (a: Child, b: Child) => 629864 b.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k; 630− const priority = (a: number, b: number) => graph.lamportOfSeq(a) - graph.lamportOfSeq(b) || a - b;
865+ const priority = (a: number, b: number) => lamportOf(a) - lamportOf(b) || a - b; 631866 const seq: number[] = []; 632867 const vis: number[] = []; 633868 const visText: (string | null)[] = []; 869+ const visWriter: number[] = []; 870+ const visK: number[] = []; 871+ const seqWriter: number[] = []; 872+ const seqK: number[] = []; 634873 const deletedBy = new Map<number, readonly number[]>(); 874+ // The text pages read for this layout, by file and page: each at most once. 875+ const pages = new Map<number, Map<number, readonly (string | null)[]>>(); 876+ const textOf = (n: number): string | null => { 877+ const file = textFile.get(n)!; 878+ const place = textPlace.get(n)!; 879+ const page = Math.floor(place / 256); 880+ let byPage = pages.get(file); 881+ if (!byPage) pages.set(file, (byPage = new Map())); 882+ let texts = byPage.get(page); 883+ if (!texts) byPage.set(page, (texts = graph.packedTextPage(file, page))); 884+ return texts[place % 256] ?? null; 635886 const stack: number[] = []; 636887 const push = (code: number) => { 637888 const list = children.get(code); Lines 898–925 at 318 (were 647–658 at 215)
647898 if (complete) deletedBy.set(seq.length, by.length > 1 ? by.sort(priority) : by); 650− visText.push(texts.get(n) ?? null);
901+ visText.push(textOf(n)); 902+ visWriter.push(writers.get(n)!); 903+ visK.push(places.get(n)!); 652− if (complete) seq.push(n);
907+ seqWriter.push(writers.get(n)!); 908+ seqK.push(places.get(n)!); 655− return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy, visText };
915+ seq: Int32Array.from(seq), 916+ vis: Int32Array.from(vis), 919+ visWriter: Int32Array.from(visWriter), 920+ visK: Int32Array.from(visK), 921+ ...(complete ? { seqWriter: Int32Array.from(seqWriter), seqK: Int32Array.from(seqK) } : {}), 658925 #attributes(path: string): FileAttrs { Lines 982–994 at 318 (were 715–721 at 215)
715982 if (complete) seq.push(n); 718− return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy };
988+ seq: Int32Array.from(seq), 989+ vis: Int32Array.from(vis), 721994 get #known(): string[] { Lines 1007–1032 at 318 (were 734–744 at 215)
7351008 /** Files with at least one visible line, sorted. */ 7361009 paths(): string[] { 1010+ // Without walking every file the graph has ever had (abso Capacity): a draft's view over its base's has the base's 1011+ // files, with the ones its draft touches checked here; a published version's are kept (knowPaths). 1012+ const shared = this.#filesFrom === undefined ? null : this.#sharedFiles(); 1014+ const out = shared.from.paths().filter((p) => !shared.except.has(p)); 1015+ for (const p of shared.except) if (this.hasFile(p)) out.push(p); 1018+ if (this.#pathsFrom !== undefined) { 1019+ if (this.#knownPaths === undefined) this.#knownPaths = this.#pathsFrom() ?? null; 1020+ if (this.#knownPaths) return [...this.#knownPaths].sort(); 7371022 if (this.#stable) { 7381023 const paths: string[] = []; 7391024 for (const path of this.graph.knownFiles()) { 7401025 if (this.hasFile(path)) paths.push(path); 741− else this.#files.delete(path);
1027+ this.#files.delete(path); 1028+ this.#used.delete(path); 7431031 return paths.sort(); Lines 1050–1064 at 318 (were 762–767 at 215)
7631051 /** Whether the file exists in this projection (has a visible line). */ 7641052 hasFile(path: string): boolean { 1053+ if (!this.#files.has(path)) { 1054+ // A file the draft doesn't touch exists as it does in the base; a published version's files are kept. 1055+ const shared = this.#filesFrom === undefined ? null : this.#sharedFiles(); 1056+ if (shared && !shared.except.has(path)) return shared.from.hasFile(path); 1057+ if (this.#pathsFrom !== undefined) { 1058+ if (this.#knownPaths === undefined) this.#knownPaths = this.#pathsFrom() ?? null; 1059+ if (this.#knownPaths) return this.#knownPaths.has(path); 7651062 const attrs = this.attrs(path); 7661063 return attrs.exists || attrs.link !== null || attrs.blob !== null || this.#file(path).vis.length > 0; Lines 1095–1137 at 318 (were 798–827 at 215)
8001097 *lineChunks(path: string): IterableIterator<() => Iterable<string>> { 801− for (const n of this.#file(path).vis) yield () => this.graph.textChunks(n);
1098+ // The texts the packed layout already holds, read a page at a time (abso Capacity, 714): a record's diff read each 1099+ // line's own row, 11,873 statements for abso's index.ts. Only a line too long to pack is read in chunks. 1100+ const { vis, visText } = this.#file(path); 1101+ for (let i = 0; i < vis.length; i++) { 1102+ const packed = visText?.[i]; 1103+ if (packed != null) yield () => [packed]; 1104+ else yield () => this.graph.textChunks(vis[i]!); 8041108 /** Visible lines without retaining their text in the view cache. */ 8051109 *visibleLines(path: string): IterableIterator<VisibleLine> { 8061110 const graph = this.graph; 8071111 for (const n of this.#file(path).vis) 808− yield { id: graph.lineIdOf(n), text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
1113+ id: graph.lineIdOf(n), 1114+ text: graph.textOf(n), 1115+ atom: atomId(graph.writerSeqOf(n)), 8111119 /** Atom-building line handles; text is loaded only when requested. */ 8121120 buildLines(path: string): readonly VisibleLine[] { 8131121 const graph = this.graph; 814− const vis = this.#file(path).vis;
1122+ // Texts and writers the packed layout already holds are not read again (abso Capacity). 1123+ const { vis, visText, visWriter, visK } = this.#file(path); 8151124 const lines: VisibleLine[] = new Array(vis.length); 8161125 for (let i = 0; i < vis.length; i++) { 8171126 const n = vis[i]!; 1127+ const packed = visText?.[i]; 819− id: graph.lineIdOf(n),
1129+ id: visWriter && visK ? lineId(atomId(visWriter[i]!), visK[i]!) : graph.lineIdOf(n), 8201130 textChunks: () => graph.textChunks(n), 822− return graph.textOf(n);
1132+ return packed ?? graph.textOf(n); 824− atom: graph.idOfSeq(graph.writerSeqOf(n)),
1134+ atom: atomId(visWriter ? visWriter[i]! : graph.writerSeqOf(n)), Lines 1141–1157 at 318 (were 831–841 at 215)
8311141 lines(path: string): readonly VisibleLine[] { 8321142 if (this.#linesCache?.path === path) return this.#linesCache.lines; 8331143 const graph = this.graph; 834− const vis = this.#file(path).vis;
1144+ const { vis, visText, visWriter, visK } = this.#file(path); 8351145 const lines: VisibleLine[] = new Array(vis.length); 1146+ // Writer seqs come from the graph's own layout, so they're named with atomId, not checked by idOfSeq (abso 738): 1147+ // on a stored graph its check read the atom count once a line, about 0.9 s of a warm draft-state on abso. 8361148 for (let i = 0; i < vis.length; i++) { 8371149 const n = vis[i]!; 838− lines[i] = { id: graph.lineIdOf(n), text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
1151+ id: visWriter && visK ? lineId(atomId(visWriter[i]!), visK[i]!) : graph.lineIdOf(n), 1152+ text: visText?.[i] ?? graph.textOf(n), 1153+ atom: atomId(visWriter ? visWriter[i]! : graph.writerSeqOf(n)), 8401156 this.#linesCache = { path, lines }; Lines 1163–1184 at 318 (were 847–860 at 215)
8471163 const graph = this.graph; 8481164 const f = this.#file(path, true); 8491165 const out: SeqLine[] = new Array(f.seq.length); 1166+ // Ids and writers from the packed layout, and each text read only when asked (abso Capacity): callers walk a few 1167+ // lines of a file's history, and reading every line's text was a row each on a stored graph. 1168+ const { seqWriter, seqK } = f; 8501169 for (let i = 0; i < f.seq.length; i++) { 8511170 const n = f.seq[i]!; 8521171 const by = f.deletedBy.get(i); 1172+ const writer = seqWriter ? seqWriter[i]! : graph.writerSeqOf(n); 1173+ let text: string | undefined; 854− id: graph.lineIdOf(n),
855− text: graph.textOf(n),
856− atom: graph.idOfSeq(graph.writerSeqOf(n)),
857− deletedBy: by ? by.map((s) => graph.idOfSeq(s)) : NONE,
1175+ id: seqWriter && seqK ? lineId(atomId(writer), seqK[i]!) : graph.lineIdOf(n), 1177+ text ??= graph.textOf(n); 1180+ atom: atomId(writer), 1181+ deletedBy: by ? by.map((s) => atomId(s)) : NONE, 8581182 visible: by === undefined, Lines 1248–1257 at 318 (were 924–930 at 215)
9241248 /** The seq of the atom that wrote each visible line, in order: lines() without ids or text. */ 9251249 visibleWriters(path: string): number[] { 9261250 const graph = this.graph; 927− return Array.from(this.#file(path).vis, (n) => graph.writerSeqOf(n));
1251+ const { vis, visWriter } = this.#file(path); 1252+ // The packed layout keeps them (abso Capacity): looking each up again read a page per few lines on a stored graph. 1253+ if (visWriter) return Array.from(visWriter); 1254+ return Array.from(vis, (n) => graph.writerSeqOf(n)); 9301257 /** All projected text files, path -> text, in path order. Binary files are in `blobs()`. */ Lines 1263–1274 at 318 (were 936–944 at 215)
9371264 /** Binary files, path -> the SHA-256 of their bytes, in path order. */ 9381265 blobs(): Record<string, string> { 1266+ // From attributes alone (abso Capacity): a file with a blob exists, so no file is laid out to find them. Going 1267+ // through paths() laid out every file in the set, and shared() asks for blobs on every call. 9391268 const out: Record<string, string> = {}; 940− for (const path of this.paths()) {
941− const blob = this.#file(path).attrs.blob;
1269+ const candidates = this.#stable ? [...this.graph.knownFiles()].sort() : this.#known; 1270+ for (const path of candidates) { 1271+ const blob = this.attrs(path).blob; 9421272 if (blob !== null) out[path] = blob; Lines 1282–1288 at 318 (were 952–958 at 215)
9521282 * populate lazy caches, enumerate the graph, or count temporary traversal 9531283 * containers and shared empty singleton payloads as retained view state. 955− weight(): number {
1285+ weight(seen?: Set<object>): number { 9561286 let n = this.#in.byteLength; 9571287 n += 2 * ARRAY_HEADER_BYTES + this.#membershipSeq.byteLength + this.#membershipValue.byteLength; 9581288 if (this.#movers) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length; Lines 1292–1305 at 318 (were 962–977 at 215)
9621292 if (this.#knownList) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#knownList.length; 9631293 if (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size; 9641294 if (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size; 1295+ n += MAP_ENTRY_BYTES * this.#picked.size; 1296+ n += MAP_ENTRY_BYTES * this.#used.size; 9651297 for (const f of this.#files.values()) { 9661298 n += MAP_ENTRY_BYTES; 9671299 if (f !== NO_FILE) { 968− // FileState and FileAttrs are per laid-out file; typed-array
969− // headers are separate from their payload byte lengths below.
970− n += OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES;
971− n += f.seq.byteLength + f.vis.byteLength;
972− if (f.visText) for (const text of f.visText) n += REFERENCE_BYTES + 2 * (text?.length ?? 0);
973− n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + ARRAY_HEADER_BYTES) * f.deletedBy.size;
974− for (const deleters of f.deletedBy.values()) n += REFERENCE_BYTES * deleters.length;
1300+ // A layout another view counted already (`seen`, across a cache's views) isn't counted again. 1301+ if (!seen?.has(f)) n += layoutBytes(f); 9751303 if (f.seqIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size; 9761304 if (f.visIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size; Lines 1318–1362 at 318 (were 990–1000 at 215)
9901318 #file(path: string, complete = false): FileState { 9911319 let state = this.#files.get(path); 9921320 if (!state || (complete && !state.complete)) { 993− state = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE;
1321+ const shared = this.#filesFrom === undefined ? null : this.#sharedFiles(); 1322+ if (shared && !shared.except.has(path)) state = shared.from.#file(path, complete); 1323+ else state = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE; 9941324 this.#files.set(path, state); 1326+ if (state !== NO_FILE) this.#used.set(path, ++fileUses); 1330+ /** When each laid-out file was last used (fileUses). */ 1331+ readonly #used = new Map<string, number>(); 1334+ * Each laid-out file: its layout, when it was last used (a count shared by every view) and what it holds, for a 1335+ * cache that trims the least recently used layouts across its views before whole views (views.ts, abso Capacity). 1337+ *laidOut(): IterableIterator<{ 1343+ for (const [path, f] of this.#files) { 1344+ if (f === NO_FILE) continue; 1345+ let bytes = 2 * MAP_ENTRY_BYTES + layoutBytes(f); 1346+ if (f.seqIndex) bytes += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size; 1347+ if (f.visIndex) bytes += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size; 1348+ yield { path, layout: f, used: this.#used.get(path) ?? 0, bytes }; 1352+ /** Let go of a file's layout: the next ask lays it out again, the same. */ 1353+ forget(path: string): void { 1354+ this.#files.delete(path); 1355+ this.#used.delete(path); 1356+ if (this.#linesCache?.path === path) this.#linesCache = undefined; 1357+ if (this.#seqCache?.path === path) this.#seqCache = undefined; 1358+ if (this.#textCache?.path === path) this.#textCache = undefined; 10001362/** The files of a closed set, path -> text. */