Lines 10–16 at 318 (were 10–16 at 264)
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 56–62 at 318 (were 56–61 at 264)
59+ | "placedDifferently" Lines 64–75 at 318 (were 63–69 at 264)
6364 sameText(path: string, other: ReadView): boolean; 66−const NO_ATTRS: FileAttrs = { exists: false, exec: false, link: null, blob: null };
67+const NO_ATTRS: FileAttrs = { 6975 * A file layout's bytes, less its lookup indexes, worked out once per layout (abso Capacity): a layout never changes Lines 128–140 at 318 (were 122–128 at 264)
124130const EMPTY_INTS = new Int32Array(0); 125−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(), 127139/** A count of file uses shared by every view, so a cache can tell which layout across its views was used longest ago. */ Lines 184–193 at 318 (were 172–178 at 264)
172184 readonly #graph: Graph; 174186 /** Movers in seq order, with the numbers of the lines each moves, in its moves' order. */ 175− readonly movers: { readonly seq: number; readonly lines: readonly number[] }[] = [];
188+ readonly seq: number; 189+ readonly lines: readonly number[]; 176191 /** Every relocation of each moved line, by the line's number, in seq order. */ 177192 readonly byLine = new Map<number, MoveEntry[]>(); 178193 readonly #climbed = new Map<number, number>(); Lines 211–222 at 318 (were 196–202 at 264)
196211 const lines: number[] = []; 197212 graph.atom(id).moves!.forEach((m, k) => { 198213 const n = graph.lineN(m.line); 199− const r: RelocationInfo = Object.freeze({ atom: id, line: m.line, parent: m.parent, k });
214+ const r: RelocationInfo = Object.freeze({ 200220 const entry: MoveEntry = { n, r, parent: graph.codeOf(m.parent)! }; 201221 const list = this.byLine.get(n); 202222 if (list) list.push(entry); Lines 544–553 at 318 (were 524–530 at 264)
524544 if (this.#relocsN) return this.#relocsN; 525545 const out = new Map<number, { readonly r: RelocationInfo; readonly parent: number }>(); 526546 for (const [line, r] of this.#relocations()) 527− 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)!, 528551 this.#relocsN = out; Lines 566–594 at 318 (were 543–548 at 264)
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); 546592 /** Whether any atom in the set moves lines. */ 547593 hasMoves(): boolean { 548594 return this.#hasMoves; Lines 649–661 at 318 (were 603–609 at 264)
603649 for (const n of graph.eachChildN(code)) { 604650 const seq = graph.writerSeqOf(n); 605651 if (!this.#has(seq) || relocs?.has(n)) continue; 606− 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), 609661 // 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 619–625 at 264)
619671 for (const n of this.#movedInto.get(code) ?? NO_LINES) { 620672 const r = relocs.get(n)!.r; 621673 const seq = graph.atom(r.atom).seq; 622− out.push({ n, replaces: 0, lamport: graph.lamportOfSeq(seq), seq, k: r.k });
677+ lamport: graph.lamportOfSeq(seq), 625683 // A line's replacements (runs of atoms that deleted it) first, then the Lines 785–806 at 318 (were 727–737 at 264)
727785 const graph = this.graph; 728786 const attrs = this.#attributes(path); 729787 const start = graph.startCodeOf(path); 730− if (start === undefined) return { attrs, complete, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
788+ if (start === undefined) 794+ deletedBy: new Map(), 731796 const relocs = this.#hasMoves ? this.#relocationsN() : undefined; 732797 const files = new Set<number>([-1 - start, ...(this.#movedInFrom().get(path) ?? [])]); 733798 const children = new Map<number, Child[]>(); 734− 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>(); 735804 const writers = new Map<number, number>(); 736805 const places = new Map<number, number>(); 737806 const deleters = new Map<number, number[]>(); Lines 815–864 at 318 (were 746–778 at 264)
748817 for (const file of files) { 749− const lineTexts = graph.packedTexts(file);
750− for (const [n, parent, seq, k, replaces] of graph.packedLines(file)) {
751− 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]!; 752827 if (!this.#has(seq)) continue; 753828 const moved = relocs?.get(n); 755830 let child: Child = { n, replaces, lamport: lamportOf(seq), seq, k }; 757832 const mover = graph.atom(moved.r.atom).seq; 758− child = { n, replaces: 0, lamport: lamportOf(mover), seq: mover, k: moved.r.k };
836+ lamport: lamportOf(mover), 759840 at = moved.parent; 761842 const list = children.get(at); 762843 if (list) list.push(child); 763844 else children.set(at, [child]); 764− texts.set(n, text);
845+ textFile.set(n, file); 846+ textPlace.set(n, place); 765847 writers.set(n, seq); 768− 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]!; 769854 if (!this.#has(deleter)) continue; 770855 const by = deleters.get(n); 771856 if (by) by.push(deleter); 772857 else deleters.set(n, [deleter]); 774859 // Runs written elsewhere under this file's lines follow them; the loop reads their files too. 775− 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]!); 777863 const order = (a: Child, b: Child) => 778864 b.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k; Lines 871–888 at 318 (were 785–790 at 264)
785871 const seqWriter: number[] = []; 786872 const seqK: number[] = []; 787873 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; 788886 const stack: number[] = []; 789887 const push = (code: number) => { 790888 const list = children.get(code); Lines 898–904 at 318 (were 800–806 at 264)
800898 if (complete) deletedBy.set(seq.length, by.length > 1 ? by.sort(priority) : by); 803− visText.push(texts.get(n) ?? null);
901+ visText.push(textOf(n)); 804902 visWriter.push(writers.get(n)!); 805903 visK.push(places.get(n)!); Lines 982–994 at 318 (were 884–890 at 264)
884982 if (complete) seq.push(n); 887− return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy };
988+ seq: Int32Array.from(seq), 989+ vis: Int32Array.from(vis), 890994 get #known(): string[] { Lines 1095–1119 at 318 (were 991–1004 at 264)
9931097 *lineChunks(path: string): IterableIterator<() => Iterable<string>> { 994− 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]!); 9971108 /** Visible lines without retaining their text in the view cache. */ 9981109 *visibleLines(path: string): IterableIterator<VisibleLine> { 9991110 const graph = this.graph; 10001111 for (const n of this.#file(path).vis) 1001− 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)), 10041119 /** Atom-building line handles; text is loaded only when requested. */ Lines 1126–1137 at 318 (were 1011–1022 at 264)
10111126 const n = vis[i]!; 10121127 const packed = visText?.[i]; 1014− 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), 10151130 textChunks: () => graph.textChunks(n), 10171132 return packed ?? graph.textOf(n); 1019− atom: graph.idOfSeq(visWriter ? visWriter[i]! : graph.writerSeqOf(n)),
1134+ atom: atomId(visWriter ? visWriter[i]! : graph.writerSeqOf(n)), Lines 1143–1156 at 318 (were 1028–1039 at 264)
10281143 const graph = this.graph; 10291144 const { vis, visText, visWriter, visK } = this.#file(path); 10301145 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. 10311148 for (let i = 0; i < vis.length; i++) { 10321149 const n = vis[i]!; 1034− 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), 10351152 text: visText?.[i] ?? graph.textOf(n), 1036− atom: graph.idOfSeq(visWriter ? visWriter[i]! : graph.writerSeqOf(n)),
1153+ atom: atomId(visWriter ? visWriter[i]! : graph.writerSeqOf(n)), 10391156 this.#linesCache = { path, lines }; Lines 1172–1184 at 318 (were 1055–1067 at 264)
10551172 const writer = seqWriter ? seqWriter[i]! : graph.writerSeqOf(n); 10561173 let text: string | undefined; 1058− id: seqWriter && seqK ? lineId(graph.idOfSeq(writer), seqK[i]!) : graph.lineIdOf(n),
1175+ id: seqWriter && seqK ? lineId(atomId(writer), seqK[i]!) : graph.lineIdOf(n), 10601177 text ??= graph.textOf(n); 1063− atom: graph.idOfSeq(writer),
1064− deletedBy: by ? by.map((s) => graph.idOfSeq(s)) : NONE,
1180+ atom: atomId(writer), 1181+ deletedBy: by ? by.map((s) => atomId(s)) : NONE, 10651182 visible: by === undefined, Lines 1334–1345 at 318 (were 1217–1223 at 264)
12171334 * Each laid-out file: its layout, when it was last used (a count shared by every view) and what it holds, for a 12181335 * cache that trims the least recently used layouts across its views before whole views (views.ts, abso Capacity). 1220− *laidOut(): IterableIterator<{ path: string; layout: object; used: number; bytes: number }> {
1337+ *laidOut(): IterableIterator<{ 12211343 for (const [path, f] of this.#files) { 12221344 if (f === NO_FILE) continue; 12231345 let bytes = 2 * MAP_ENTRY_BYTES + layoutBytes(f);