Lines 10–16 at 318 (were 10–16 at 240)
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, lineId, 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 240)
4949/** Structural read boundary shared by historical and transaction-bound projections. */ 5050export type ReadView = Omit< 5151 Pick<View, keyof View>, 52− "sameText" | "visibleCount" | "shareMoves" | "shareFiles" | "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 128–144 at 318 (were 91–98 at 240)
93130const EMPTY_INTS = new Int32Array(0); 94−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. */ 96142// Approximate retained-container costs for View.weight(): reference slots, 97143// object headers, and map/set entries. These are cache-budget units, not V8 98144// heap measurements. Referenced Graph strings and records are not charged. Lines 148–163 at 318 (were 102–108 at 240)
102148const MAP_ENTRY_BYTES = 40; 103149const SET_ENTRY_BYTES = 32; 104150const 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; 106161 * Moved lines a view resolves once and keeps (abso Capacity): a stored graph otherwise resolved every move again on 107162 * 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 129–135 at 240)
129184 readonly #graph: Graph; 131186 /** Movers in seq order, with the numbers of the lines each moves, in its moves' order. */ 132− readonly movers: { readonly seq: number; readonly lines: readonly number[] }[] = [];
188+ readonly seq: number; 189+ readonly lines: readonly number[]; 133191 /** Every relocation of each moved line, by the line's number, in seq order. */ 134192 readonly byLine = new Map<number, MoveEntry[]>(); 135193 readonly #climbed = new Map<number, number>(); Lines 211–222 at 318 (were 153–159 at 240)
153211 const lines: number[] = []; 154212 graph.atom(id).moves!.forEach((m, k) => { 155213 const n = graph.lineN(m.line); 156− const r: RelocationInfo = Object.freeze({ atom: id, line: m.line, parent: m.parent, k });
214+ const r: RelocationInfo = Object.freeze({ 157220 const entry: MoveEntry = { n, r, parent: graph.codeOf(m.parent)! }; 158221 const list = this.byLine.get(n); 159222 if (list) list.push(entry); Lines 468–484 at 318 (were 405–410 at 240)
405468 this.#filesFrom = { from, except }; 407470 #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; 408482 #filesShared: { from: View; except: ReadonlySet<string> } | null | undefined; 409483 #sharedFiles(): { from: View; except: ReadonlySet<string> } | null { 410484 if (this.#filesShared === undefined) { Lines 544–553 at 318 (were 470–476 at 240)
470544 if (this.#relocsN) return this.#relocsN; 471545 const out = new Map<number, { readonly r: RelocationInfo; readonly parent: number }>(); 472546 for (const [line, r] of this.#relocations()) 473− 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)!, 474551 this.#relocsN = out; Lines 566–594 at 318 (were 489–494 at 240)
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); 492592 /** Whether any atom in the set moves lines. */ 493593 hasMoves(): boolean { 494594 return this.#hasMoves; Lines 649–661 at 318 (were 549–555 at 240)
549649 for (const n of graph.eachChildN(code)) { 550650 const seq = graph.writerSeqOf(n); 551651 if (!this.#has(seq) || relocs?.has(n)) continue; 552− 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), 555661 // 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 565–571 at 240)
565671 for (const n of this.#movedInto.get(code) ?? NO_LINES) { 566672 const r = relocs.get(n)!.r; 567673 const seq = graph.atom(r.atom).seq; 568− out.push({ n, replaces: 0, lamport: graph.lamportOfSeq(seq), seq, k: r.k });
677+ lamport: graph.lamportOfSeq(seq), 571683 // A line's replacements (runs of atoms that deleted it) first, then the Lines 785–806 at 318 (were 673–683 at 240)
673785 const graph = this.graph; 674786 const attrs = this.#attributes(path); 675787 const start = graph.startCodeOf(path); 676− if (start === undefined) return { attrs, complete, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
788+ if (start === undefined) 794+ deletedBy: new Map(), 677796 const relocs = this.#hasMoves ? this.#relocationsN() : undefined; 678797 const files = new Set<number>([-1 - start, ...(this.#movedInFrom().get(path) ?? [])]); 679798 const children = new Map<number, Child[]>(); 680− 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>(); 681804 const writers = new Map<number, number>(); 682805 const places = new Map<number, number>(); 683806 const deleters = new Map<number, number[]>(); Lines 815–864 at 318 (were 692–724 at 240)
694817 for (const file of files) { 695− const lineTexts = graph.packedTexts(file);
696− for (const [n, parent, seq, k, replaces] of graph.packedLines(file)) {
697− 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]!; 698827 if (!this.#has(seq)) continue; 699828 const moved = relocs?.get(n); 701830 let child: Child = { n, replaces, lamport: lamportOf(seq), seq, k }; 703832 const mover = graph.atom(moved.r.atom).seq; 704− child = { n, replaces: 0, lamport: lamportOf(mover), seq: mover, k: moved.r.k };
836+ lamport: lamportOf(mover), 705840 at = moved.parent; 707842 const list = children.get(at); 708843 if (list) list.push(child); 709844 else children.set(at, [child]); 710− texts.set(n, text);
845+ textFile.set(n, file); 846+ textPlace.set(n, place); 711847 writers.set(n, seq); 714− 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]!; 715854 if (!this.#has(deleter)) continue; 716855 const by = deleters.get(n); 717856 if (by) by.push(deleter); 718857 else deleters.set(n, [deleter]); 720859 // Runs written elsewhere under this file's lines follow them; the loop reads their files too. 721− for (const [parent, into] of graph.packedCrossings(file)) if (texts.has(parent)) files.add(into);
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]!); 723863 const order = (a: Child, b: Child) => 724864 b.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k; Lines 871–888 at 318 (were 731–736 at 240)
731871 const seqWriter: number[] = []; 732872 const seqK: number[] = []; 733873 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; 734886 const stack: number[] = []; 735887 const push = (code: number) => { 736888 const list = children.get(code); Lines 898–904 at 318 (were 746–752 at 240)
746898 if (complete) deletedBy.set(seq.length, by.length > 1 ? by.sort(priority) : by); 749− visText.push(texts.get(n) ?? null);
901+ visText.push(textOf(n)); 750902 visWriter.push(writers.get(n)!); 751903 visK.push(places.get(n)!); Lines 982–994 at 318 (were 830–836 at 240)
830982 if (complete) seq.push(n); 833− return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy };
988+ seq: Int32Array.from(seq), 989+ vis: Int32Array.from(vis), 836994 get #known(): string[] { Lines 1007–1032 at 318 (were 849–859 at 240)
8501008 /** Files with at least one visible line, sorted. */ 8511009 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(); 8521022 if (this.#stable) { 8531023 const paths: string[] = []; 8541024 for (const path of this.graph.knownFiles()) { 8551025 if (this.hasFile(path)) paths.push(path); 856− else this.#files.delete(path);
1027+ this.#files.delete(path); 1028+ this.#used.delete(path); 8581031 return paths.sort(); Lines 1050–1064 at 318 (were 877–882 at 240)
8781051 /** Whether the file exists in this projection (has a visible line). */ 8791052 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); 8801062 const attrs = this.attrs(path); 8811063 return attrs.exists || attrs.link !== null || attrs.blob !== null || this.#file(path).vis.length > 0; Lines 1095–1119 at 318 (were 913–926 at 240)
9151097 *lineChunks(path: string): IterableIterator<() => Iterable<string>> { 916− 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]!); 9191108 /** Visible lines without retaining their text in the view cache. */ 9201109 *visibleLines(path: string): IterableIterator<VisibleLine> { 9211110 const graph = this.graph; 9221111 for (const n of this.#file(path).vis) 923− 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)), 9261119 /** Atom-building line handles; text is loaded only when requested. */ Lines 1126–1137 at 318 (were 933–944 at 240)
9331126 const n = vis[i]!; 9341127 const packed = visText?.[i]; 936− id: visWriter && visK ? lineId(graph.idOfSeq(visWriter[i]!), visK[i]!) : graph.lineIdOf(n),
1129+ id: visWriter && visK ? lineId(atomId(visWriter[i]!), visK[i]!) : graph.lineIdOf(n), 9371130 textChunks: () => graph.textChunks(n), 9391132 return packed ?? graph.textOf(n); 941− atom: graph.idOfSeq(visWriter ? visWriter[i]! : graph.writerSeqOf(n)),
1134+ atom: atomId(visWriter ? visWriter[i]! : graph.writerSeqOf(n)), Lines 1143–1156 at 318 (were 950–961 at 240)
9501143 const graph = this.graph; 9511144 const { vis, visText, visWriter, visK } = this.#file(path); 9521145 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. 9531148 for (let i = 0; i < vis.length; i++) { 9541149 const n = vis[i]!; 956− id: visWriter && visK ? lineId(graph.idOfSeq(visWriter[i]!), visK[i]!) : graph.lineIdOf(n),
1151+ id: visWriter && visK ? lineId(atomId(visWriter[i]!), visK[i]!) : graph.lineIdOf(n), 9571152 text: visText?.[i] ?? graph.textOf(n), 958− atom: graph.idOfSeq(visWriter ? visWriter[i]! : graph.writerSeqOf(n)),
1153+ atom: atomId(visWriter ? visWriter[i]! : graph.writerSeqOf(n)), 9611156 this.#linesCache = { path, lines }; Lines 1172–1184 at 318 (were 977–989 at 240)
9771172 const writer = seqWriter ? seqWriter[i]! : graph.writerSeqOf(n); 9781173 let text: string | undefined; 980− id: seqWriter && seqK ? lineId(graph.idOfSeq(writer), seqK[i]!) : graph.lineIdOf(n),
1175+ id: seqWriter && seqK ? lineId(atomId(writer), seqK[i]!) : graph.lineIdOf(n), 9821177 text ??= graph.textOf(n); 985− atom: graph.idOfSeq(writer),
986− deletedBy: by ? by.map((s) => graph.idOfSeq(s)) : NONE,
1180+ atom: atomId(writer), 1181+ deletedBy: by ? by.map((s) => atomId(s)) : NONE, 9871182 visible: by === undefined, Lines 1282–1288 at 318 (were 1087–1093 at 240)
10871282 * populate lazy caches, enumerate the graph, or count temporary traversal 10881283 * containers and shared empty singleton payloads as retained view state. 1090− weight(): number {
1285+ weight(seen?: Set<object>): number { 10911286 let n = this.#in.byteLength; 10921287 n += 2 * ARRAY_HEADER_BYTES + this.#membershipSeq.byteLength + this.#membershipValue.byteLength; 10931288 if (this.#movers) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length; Lines 1293–1305 at 318 (were 1098–1117 at 240)
10981293 if (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size; 10991294 if (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size; 11001295 n += MAP_ENTRY_BYTES * this.#picked.size; 1296+ n += MAP_ENTRY_BYTES * this.#used.size; 11011297 for (const f of this.#files.values()) { 11021298 n += MAP_ENTRY_BYTES; 11031299 if (f !== NO_FILE) { 1104− // FileState and FileAttrs are per laid-out file; typed-array
1105− // headers are separate from their payload byte lengths below.
1106− n += OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES;
1107− n += f.seq.byteLength + f.vis.byteLength;
1108− if (f.visText) for (const text of f.visText) n += REFERENCE_BYTES + 2 * (text?.length ?? 0);
1109− if (f.visWriter) n += 4 * f.visWriter.length;
1110− if (f.visK) n += 4 * f.visK.length;
1111− if (f.seqWriter) n += 4 * f.seqWriter.length;
1112− if (f.seqK) n += 4 * f.seqK.length;
1113− n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + ARRAY_HEADER_BYTES) * f.deletedBy.size;
1114− 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); 11151303 if (f.seqIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size; 11161304 if (f.visIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size; Lines 1323–1362 at 318 (were 1135–1142 at 240)
11351323 else state = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE; 11361324 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; 11421362/** The files of a closed set, path -> text. */