Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 211 to 318

Compare

+579 −149 in 29 places
Lines 10–16 at 318 (were 10–16 at 211)
1010// made when a caller asks for them, and the latest asked for is kept.
1111
1212import type { Graph, RelocationInfo } from "./graph.ts";
13−import { isStartNode } 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 47–97 at 318 (were 47–57 at 211)
4747}
4848
4949/** Structural read boundary shared by historical and transaction-bound projections. */
50−export type ReadView = Omit<Pick<View, keyof View>, "sameText" | "visibleCount" | "shareMoves"> & {
50+export type ReadView = Omit<
51+ Pick<View, keyof View>,
52+ | "sameText"
53+ | "visibleCount"
54+ | "shareMoves"
55+ | "shareFiles"
56+ | "knowPaths"
57+ | "laidOut"
58+ | "forget"
59+ | "placedDifferently"
60+ | "fileOfNumber"
61+ | "lineNumberOf"
62+ | "visibleWriters"
63+> & {
5164 sameText(path: string, other: ReadView): boolean;
5265};
5366
54−const NO_ATTRS: FileAttrs = { exists: false, exec: false, link: null, blob: null };
67+const NO_ATTRS: FileAttrs = {
68+ exists: false,
69+ exec: false,
70+ link: null,
71+ blob: null,
72+};
74+/**
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.
77+ */
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);
93+ return n;
94+}
5595const NONE: readonly never[] = Object.freeze([]);
5696
5797interface FileState {
Lines 106–118 at 318 (were 66–71 at 211)
66106 readonly deletedBy: ReadonlyMap<number, readonly number[]>;
67107 /** Its visible lines' text, when laid out from the file's packed history: null for a line too long to pack. */
68108 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;
69116 /** Positions by n, built the first time they're asked for. */
70117 seqIndex?: Map<number, number>;
71118 visIndex?: Map<number, number>;
Lines 128–144 at 318 (were 81–88 at 211)
81128}
82129
83130const EMPTY_INTS = new Int32Array(0);
84−const NO_FILE: FileState = { attrs: NO_ATTRS, complete: true, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
131+const NO_FILE: FileState = {
132+ attrs: NO_ATTRS,
133+ complete: true,
134+ seq: EMPTY_INTS,
135+ vis: EMPTY_INTS,
136+ deletedBy: new Map(),
137+};
85138
139+/** A count of file uses shared by every view, so a cache can tell which layout across its views was used longest ago. */
140+let fileUses = 0;
86142// Approximate retained-container costs for View.weight(): reference slots,
87143// object headers, and map/set entries. These are cache-budget units, not V8
88144// heap measurements. Referenced Graph strings and records are not charged.
Lines 148–262 at 318 (were 92–109 at 211)
92148const MAP_ENTRY_BYTES = 40;
93149const SET_ENTRY_BYTES = 32;
94150const REFERENCE_BYTES = 8;
95−const FILE_LOCATION_MEMO_ENTRIES = 256;
151+const STRING_HEADER_BYTES = 16;
96152/**
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.
155+ */
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;
159+}
160+/**
97161 * Moved lines a view resolves once and keeps (abso Capacity): a stored graph otherwise resolved every move again on
98162 * disk for each file it laid out. Game's whole history has 290; past this many, the view goes back to that.
99163 */
100164export const MOVED_LINES_KEPT = 20_000;
101165class TooManyMoves extends Error {}
102166const NO_LINES: readonly number[] = Object.freeze([]);
103−/** Lines whose place views sharing moves remember, at most: about 40 bytes each. */
104−const SHARED_FILE_MEMO = 200_000;
167+/** Nodes whose nearest moved line above a graph remembers, at most: about 40 bytes each. */
168+const CLIMB_MEMO = 200_000;
105169
170+/** One relocation of a moved line, with its line's number and its new parent's code. */
171+interface MoveEntry {
172+ readonly n: number;
173+ readonly r: RelocationInfo;
174+ readonly parent: number;
175+}
106177/**
178+ * A graph's moves, whatever the set (abso Capacity): each mover's moved lines, each moved line's relocations in seq
179+ * order, and for any node the nearest node at or above it, by where lines were written, that a move ever relocated,
180+ * or its file's start. Worked out once per graph and extended as it grows. A view's moves then resolve over the moved
181+ * lines alone: before, every view walked each moved line's file up to its start, a read per line.
182+ */
183+class MoveSkeleton {
184+ readonly #graph: Graph;
185+ #seen = 0;
186+ /** Movers in seq order, with the numbers of the lines each moves, in its moves' order. */
187+ readonly movers: {
188+ readonly seq: number;
189+ readonly lines: readonly number[];
190+ }[] = [];
191+ /** Every relocation of each moved line, by the line's number, in seq order. */
192+ readonly byLine = new Map<number, MoveEntry[]>();
193+ readonly #climbed = new Map<number, number>();
195+ constructor(graph: Graph) {
196+ this.#graph = graph;
197+ }
199+ /** The graph's accepted count when movers were last taken in: nothing to read while it's the same. */
200+ #stamp = -1;
202+ /** Take in movers the graph gained since. */
203+ update(): this {
204+ const graph = this.#graph;
205+ if (this.#stamp === graph.accepted) return this;
206+ this.#stamp = graph.accepted;
207+ const all = graph.movers();
208+ if (this.#seen === all.length) return this;
209+ for (; this.#seen < all.length; this.#seen++) {
210+ const id = all[this.#seen]!;
211+ const lines: number[] = [];
212+ graph.atom(id).moves!.forEach((m, k) => {
213+ const n = graph.lineN(m.line);
214+ const r: RelocationInfo = Object.freeze({
215+ atom: id,
216+ line: m.line,
217+ parent: m.parent,
218+ k,
219+ });
220+ const entry: MoveEntry = { n, r, parent: graph.codeOf(m.parent)! };
221+ const list = this.byLine.get(n);
222+ if (list) list.push(entry);
223+ else this.byLine.set(n, [entry]);
224+ lines.push(n);
225+ });
226+ this.movers.push({ seq: seqOf(id), lines });
227+ }
228+ // A new moved line can sit between a node and the one it climbed to.
229+ this.#climbed.clear();
230+ return this;
231+ }
233+ /** The nearest node at or above this one, by where lines were written, that a move ever relocated; or its file's start. */
234+ climb(code: number): number {
235+ const walked: number[] = [];
236+ let c = code;
237+ while (c >= 0 && !this.byLine.has(c)) {
238+ const known = this.#climbed.get(c);
239+ if (known !== undefined) {
240+ c = known;
241+ break;
242+ }
243+ walked.push(c);
244+ c = this.#graph.parentCodeOf(c);
245+ }
246+ if (this.#climbed.size + walked.length > CLIMB_MEMO) this.#climbed.clear();
247+ for (const w of walked) this.#climbed.set(w, c);
248+ return c;
249+ }
250+}
252+const skeletons = new WeakMap<Graph, MoveSkeleton>();
253+function skeletonOf(graph: Graph): MoveSkeleton {
254+ let skeleton = skeletons.get(graph);
255+ if (!skeleton) skeletons.set(graph, (skeleton = new MoveSkeleton(graph)));
256+ return skeleton.update();
257+}
259+/**
107260 * The projection of a closed set. Construct once, then ask for files, lines
108261 * and sequences. Throws `not-closed` if some atom's dependency is missing.
109262 * Each file is laid out the first time it is asked for, so a view of a large
Lines 286–291 at 318 (were 133–140 at 211)
133286 #movedInto: Map<number, number[]> | undefined;
134287 /** Whether this view's moves are too many to keep, so they're resolved on disk per layout; undefined until asked. */
135288 #movesOnDisk: boolean | undefined;
136− /** Bounded memo: ancestry can span the entire history even for one visible line. */
137− #fileOf = new Map<number, string>();
138289 /** The latest records asked for: one file's lines, one file's sequence, one file's text. */
139290 #linesCache: { path: string; lines: readonly VisibleLine[] } | undefined;
140291 #seqCache: { path: string; seq: readonly SeqLine[] } | undefined;
Lines 317–340 at 318 (were 166–188 at 211)
166317 return;
167318 }
168319 const inSet = new Uint8Array(graph.size);
169− for (const id of set) {
170− const atom = graph.atom(id);
171− inSet[atom.seq] = 1;
172− }
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;
324+ };
325+ for (const id of set) inSet[seqIn(id)] = 1;
173326 // Validate one atom at a time. Keeping all decoded atoms here defeats
174327 // storage-backed Graph reads even though the view only needs membership.
175328 for (let seq = 0; seq < inSet.length; seq++) {
176329 if (inSet[seq] !== 1) continue;
177330 const atom = graph.atom(graph.idOfSeq(seq));
178331 for (const d of atom.deps) {
179− if (inSet[graph.atom(d).seq] !== 1)
180− 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}`);
181333 }
182334 }
183335 this.graph = graph;
184336 this.#in = inSet;
185− this.#movers = graph.movers().filter((id) => inSet[graph.atom(id).seq] === 1);
337+ this.#movers = graph.movers().filter((id) => inSet[seqOf(id)] === 1);
186338 this.#hasMoves = this.#movers.length > 0;
187339 this.#relocs = this.#movers.length === 0 ? new Map() : undefined;
188340 this.#relocsN = this.#movers.length === 0 ? new Map() : undefined;
Lines 371–419 at 318 (were 219–253 at 211)
219371 */
220372 #relocations(): Map<LineId, RelocationInfo> {
221373 if (this.#relocs) return this.#relocs;
374+ this.#takeShared();
375+ if (this.#relocs) return this.#relocs;
222376 const graph = this.graph;
377+ const skeleton = skeletonOf(graph);
223378 const stronger = (a: RelocationInfo, b: RelocationInfo): boolean => graph.comparePriority(a.atom, b.atom) > 0;
224− // Persistent reads decode new objects. Cycle suppression must identify the
225− // logical move, not one particular decoded JavaScript object.
226379 const identity = (r: RelocationInfo): string => JSON.stringify([r.atom, r.k]);
227380 const ignored = new Set<string>();
228− const pick = (line: LineId): RelocationInfo | undefined => {
229− let best: RelocationInfo | undefined;
230− for (const r of graph.eachRelocationOf(line)) {
231− if (this.#has(graph.atom(r.atom).seq) && !ignored.has(identity(r)) && (!best || stronger(r, best))) best = r;
381+ const pick = (n: number): MoveEntry | undefined => {
382+ let best: MoveEntry | undefined;
383+ for (const e of skeleton.byLine.get(n) ?? []) {
384+ if (this.#has(seqOf(e.r.atom)) && !ignored.has(identity(e.r)) && (!best || stronger(e.r, best.r))) best = e;
232385 }
233386 return best;
234387 };
235− const relocs = new Map<LineId, RelocationInfo>();
236− for (const id of this.#eachMover()) {
237− for (const m of graph.atom(id).moves!) {
238− if (!relocs.has(m.line)) relocs.set(m.line, pick(m.line)!);
388+ // By the moved line's number, in the order its movers come (seq order, then each mover's moves).
389+ const relocs = new Map<number, MoveEntry>();
390+ for (const mover of skeleton.movers) {
391+ if (!this.#has(mover.seq)) continue;
392+ for (const n of mover.lines) {
393+ if (!relocs.has(n)) relocs.set(n, pick(n)!);
239394 if (graph.hasOrderedChildren && relocs.size > MOVED_LINES_KEPT) throw new TooManyMoves();
240395 }
241396 }
242− const parentOf = (line: LineId): NodeId => relocs.get(line)?.parent ?? graph.lineParent(line)!;
243− const reaches = new Set<LineId>();
397+ // From a node, the next node that matters for loops: a line moved in this set, or a file's start.
398+ const hop = (code: number): number => {
399+ let c = skeleton.climb(code);
400+ while (c >= 0 && !relocs.has(c)) c = skeleton.climb(graph.parentCodeOf(c));
401+ return c;
402+ };
403+ const step = (n: number): number => {
404+ const e = relocs.get(n);
405+ return hop(e ? e.parent : graph.parentCodeOf(n));
406+ };
407+ // Two moves made apart can loop lines into each other, so no start node reaches them; then the oldest
408+ // relocation on the loop is ignored, and the next newest of that line counts, until none loop.
409+ const reaches = new Set<number>();
244410 for (const line of [...relocs.keys()]) {
245411 for (;;) {
246− const path: LineId[] = [];
247− const onPath = new Map<LineId, number>();
248− let cur: NodeId = line;
412+ const path: number[] = [];
413+ const onPath = new Map<number, number>();
414+ let cur = line;
249415 let loopAt = -1;
250− while (!isStartNode(cur) && !reaches.has(cur)) {
416+ while (cur >= 0 && !reaches.has(cur)) {
251417 const at = onPath.get(cur);
252418 if (at !== undefined) {
253419 loopAt = at;
Lines 421–529 at 318 (were 255–300 at 211)
255421 }
256422 onPath.set(cur, path.length);
257423 path.push(cur);
258− cur = parentOf(cur);
424+ cur = step(cur);
259425 }
260426 if (loopAt < 0) {
261427 for (const l of path) reaches.add(l);
262428 break;
263429 }
264− let weakest: RelocationInfo | undefined;
430+ let weakest: MoveEntry | undefined;
265431 for (const l of path.slice(loopAt)) {
266− const r = relocs.get(l);
267− if (r && (!weakest || stronger(weakest, r))) weakest = r;
432+ const e = relocs.get(l);
433+ if (e && (!weakest || stronger(weakest.r, e.r))) weakest = e;
268434 }
269− ignored.add(identity(weakest!));
270− const next = pick(weakest!.line);
271− if (next) relocs.set(weakest!.line, next);
272− else relocs.delete(weakest!.line);
435+ ignored.add(identity(weakest!.r));
436+ const next = pick(weakest!.n);
437+ if (next) relocs.set(weakest!.n, next);
438+ else relocs.delete(weakest!.n);
273439 }
274440 }
275− this.#relocs = relocs;
276− return relocs;
441+ this.#relocsN = new Map([...relocs].map(([n, e]) => [n, { r: e.r, parent: e.parent }]));
442+ this.#relocs = new Map([...relocs.values()].map((e) => [e.r.line, e.r]));
443+ return this.#relocs;
277444 }
278445
279− /** Where lines sit, shared with views that took this one's moves (shareMoves): unbounded up to SHARED_FILE_MEMO. */
280− #fileOfShared: Map<number, string> | undefined;
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;
281449
282450 /**
283− * Take another view's resolved moves (abso Capacity). Only for a set that differs from that view's by atoms that
284− * move nothing, drop nothing and change no closure: its moves resolve the same, and every line it shares with that
285− * view sits where it sat there.
451+ * Take another view's resolved moves (abso Capacity), when both sets hold the very same atoms that move lines: the
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.
286454 */
287455 shareMoves(from: View): void {
288− if (from.graph !== this.graph || from.#hasMoves !== this.#hasMoves || !this.#hasMoves) return;
456+ if (from.graph !== this.graph || from.#hasMoves !== this.#hasMoves || !this.#hasMoves || this.#relocs) return;
457+ this.#movesFrom = from;
458+ }
460+ /**
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.
465+ */
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 };
469+ }
470+ #filesFrom: { from: View; except: () => ReadonlySet<string> | undefined } | null | undefined;
472+ /**
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.
476+ */
477+ knowPaths(paths: () => ReadonlySet<string> | undefined): void {
478+ if (this.#pathsFrom === undefined) this.#pathsFrom = paths;
479+ }
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;
488+ }
489+ return this.#filesShared;
490+ }
492+ #takeShared(): void {
493+ const from = this.#movesFrom;
494+ if (!from) return;
495+ this.#movesFrom = undefined;
289496 if (from.#scratchMoves()) return;
497+ for (const id of this.graph.movers()) {
498+ const seq = seqOf(id);
499+ if (this.#has(seq) !== from.#has(seq)) return;
500+ }
290501 this.#relocs = from.#relocations();
291502 this.#relocsN = from.#relocationsN();
292503 this.#movesOnDisk = false;
293− this.#movedFrom = from.#movedInFrom();
294− from.#fileOfShared ??= new Map();
295− this.#fileOfShared = from.#fileOfShared;
504+ this.#movesTaken = from;
296505 }
297506
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>();
510+ /**
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.
513+ */
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;
521+ }
522+ this.#picked.set(n, (picked = best ?? null));
523+ }
524+ return picked ?? undefined;
525+ }
298527 /** Whether moves are resolved on disk for each layout: only a stored graph's view with more than MOVED_LINES_KEPT. */
299528 #scratchMoves(): boolean {
300529 if (!this.#hasMoves || !this.graph.hasOrderedChildren) return false;
Lines 544–553 at 318 (were 315–321 at 211)
315544 if (this.#relocsN) return this.#relocsN;
316545 const out = new Map<number, { readonly r: RelocationInfo; readonly parent: number }>();
317546 for (const [line, r] of this.#relocations())
318− out.set(this.graph.lineN(line), { r, parent: this.graph.codeOf(r.parent)! });
547+ out.set(this.graph.lineN(line), {
548+ r,
549+ parent: this.graph.codeOf(r.parent)!,
550+ });
319551 this.#relocsN = out;
320552 return out;
321553 }
Lines 566–598 at 318 (were 334–347 at 211)
334566 return parent;
335567 }
336568
569+ /**
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.
574+ */
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));
583+ };
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);
587+ }
588+ for (const n of there?.keys() ?? []) if (!here?.has(n)) add(n);
589+ return files;
590+ }
337592 /** Whether any atom in the set moves lines. */
338593 hasMoves(): boolean {
339594 return this.#hasMoves;
340595 }
341− *#eachMover(): IterableIterator<AtomId> {
342− if (this.#movers) yield* this.#movers;
343− else for (const id of this.graph.movers()) if (this.#stable!.has(id)) yield id;
344− }
345596
346597 /** The file a line is in, in this set: the one its chain of parents reaches. */
347598 fileOf(line: LineId): string {
Lines 611–644 at 318 (were 360–395 at 211)
360611 while (!isStartNode(current)) current = moves.parent(current);
361612 return current.slice(1);
362613 });
363− const relocs = this.#relocationsN();
364− // Keep only a fixed tail of the walk for path compression. The selected
365− // relocation graph is acyclic, so traversal needs no history-sized stack.
366− const path: number[] = [];
367− let visited = 0;
368− let cur = n;
369− let file: string | undefined;
370− while (file === undefined) {
371− if (cur < 0) file = graph.nodeOf(cur).slice(1);
372− else {
373− file = (this.#fileOfShared ?? this.#fileOf).get(cur);
374− if (file === undefined) {
375− if (this.#fileOfShared) path.push(cur);
376− else path[visited++ % FILE_LOCATION_MEMO_ENTRIES] = cur;
377− cur = relocs.get(cur)?.parent ?? graph.parentCodeOf(cur);
378− }
614+ return this.#fileOfCode(n);
615+ }
617+ /** The file a node sits in, in this set: up by where lines were written, through the moves that count here. */
618+ #fileOfCode(code: number): string {
619+ const graph = this.graph;
620+ 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>();
624+ let c = code;
625+ for (;;) {
626+ c = skeleton.climb(c);
627+ if (c < 0) return graph.nodeOf(c).slice(1);
628+ if (passed.has(c)) break;
629+ passed.add(c);
630+ const e = this.#pickOne(c);
631+ c = e ? e.parent : graph.parentCodeOf(c);
379632 }
380633 }
381− const shared = this.#fileOfShared;
382− if (shared) {
383− if (shared.size > SHARED_FILE_MEMO) shared.clear();
384− for (const l of path) shared.set(l, file);
385− return file;
634+ const relocs = this.#relocationsN();
635+ let c = code;
636+ for (;;) {
637+ c = skeleton.climb(c);
638+ if (c < 0) return graph.nodeOf(c).slice(1);
639+ const e = relocs.get(c);
640+ c = e ? e.parent : graph.parentCodeOf(c);
386641 }
387− for (const l of path) {
388− if (!this.#fileOf.has(l) && this.#fileOf.size >= FILE_LOCATION_MEMO_ENTRIES)
389− this.#fileOf.delete(this.#fileOf.keys().next().value!);
390− this.#fileOf.set(l, file);
391− }
392− return file;
393642 }
394643
395644 /** The lines hanging off a node in this set, in layout order. */
Lines 649–661 at 318 (were 400–406 at 211)
400649 for (const n of graph.eachChildN(code)) {
401650 const seq = graph.writerSeqOf(n);
402651 if (!this.#has(seq) || relocs?.has(n)) continue;
403− out.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) });
652+ out.push({
653+ n,
654+ replaces: graph.replacesN(n) ? 1 : 0,
655+ lamport: graph.lamportOfSeq(seq),
656+ seq,
657+ k: graph.kOf(n),
658+ });
404659 }
405660 if (relocs) {
406661 // 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 416–422 at 211)
416671 for (const n of this.#movedInto.get(code) ?? NO_LINES) {
417672 const r = relocs.get(n)!.r;
418673 const seq = graph.atom(r.atom).seq;
419− out.push({ n, replaces: 0, lamport: graph.lamportOfSeq(seq), seq, k: r.k });
674+ out.push({
675+ n,
676+ replaces: 0,
677+ lamport: graph.lamportOfSeq(seq),
678+ seq,
679+ k: r.k,
680+ });
420681 }
421682 }
422683 // A line's replacements (runs of atoms that deleted it) first, then the
Lines 742–748 at 318 (were 481–487 at 211)
481742 /** Lay out one file: its line tree from the set's atoms, linearized. */
482743 #layout(path: string, complete: boolean): FileState {
483744 const graph = this.graph;
484− if (graph.packedFiles && !this.#scratchMoves()) return this.#layoutPacked(path, complete);
745+ if (graph.packedLayout && !this.#scratchMoves()) return this.#layoutPacked(path, complete);
485746 if (this.#scratchMoves())
486747 return graph.withProjectionScratch((scratch) =>
487748 this.#layoutResolved(
Lines 758–770 at 318 (were 497–525 at 211)
497758 #movedFrom: Map<string, Set<number>> | undefined;
498759 #movedInFrom(): Map<string, Set<number>> {
499760 if (this.#movedFrom) return this.#movedFrom;
761+ this.#relocations();
762+ if (this.#movesTaken) return (this.#movedFrom = this.#movesTaken.#movedInFrom());
500763 const graph = this.graph;
501764 const out = new Map<string, Set<number>>();
502765 if (this.#hasMoves) {
503766 const relocs = this.#relocationsN();
504− // Where each node sits, walked once for the whole view: through the moves, up to a file's start.
505− const memo = new Map<number, string>();
506− const fileOf = (code: number): string => {
507− const walked: number[] = [];
508− let cur = code;
509− let file: string | undefined;
510− while (file === undefined) {
511− if (cur < 0) file = graph.nodeOf(cur).slice(1);
512− else {
513− file = memo.get(cur);
514− if (file === undefined) {
515− walked.push(cur);
516− cur = relocs.get(cur)?.parent ?? graph.parentCodeOf(cur);
517− }
518− }
519− }
520− for (const n of walked) memo.set(n, file);
521− return file;
522− };
767+ const fileOf = (code: number): string => this.#fileOfCode(code);
523768 for (const [n, { parent }] of relocs) {
524769 const into = fileOf(parent);
525770 let from = out.get(into);
Lines 777–888 at 318 (were 532–583 at 211)
532777 }
533778
534779 /**
535− * Lay one file out from its packed history (abso Capacity, Graph.packedLines): the lines written in it, and in the
536− * files its moved-in lines came from, read a page at a time; the same tree and order as #layoutResolved, built in
537− * 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.
538783 */
539784 #layoutPacked(path: string, complete: boolean): FileState {
540785 const graph = this.graph;
541786 const attrs = this.#attributes(path);
542787 const start = graph.startCodeOf(path);
543− if (start === undefined) return { attrs, complete, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
788+ if (start === undefined)
789+ return {
790+ attrs,
791+ complete,
792+ seq: EMPTY_INTS,
793+ vis: EMPTY_INTS,
794+ deletedBy: new Map(),
795+ };
544796 const relocs = this.#hasMoves ? this.#relocationsN() : undefined;
545797 const files = new Set<number>([-1 - start, ...(this.#movedInFrom().get(path) ?? [])]);
546798 const children = new Map<number, Child[]>();
547− 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>();
548806 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);
814+ }
815+ return lamport;
816+ };
549817 for (const file of files) {
550− const lineTexts = graph.packedTexts(file);
551− for (const [n, parent, seq, k, replaces] of graph.packedLines(file)) {
552− 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]!;
553827 if (!this.#has(seq)) continue;
554828 const moved = relocs?.get(n);
555829 let at = parent;
556− let child: Child = { n, replaces, lamport: graph.lamportOfSeq(seq), seq, k };
830+ let child: Child = { n, replaces, lamport: lamportOf(seq), seq, k };
557831 if (moved) {
558832 const mover = graph.atom(moved.r.atom).seq;
559− child = { n, replaces: 0, lamport: graph.lamportOfSeq(mover), seq: mover, k: moved.r.k };
833+ child = {
834+ n,
835+ replaces: 0,
836+ lamport: lamportOf(mover),
837+ seq: mover,
838+ k: moved.r.k,
839+ };
560840 at = moved.parent;
561841 }
562842 const list = children.get(at);
563843 if (list) list.push(child);
564844 else children.set(at, [child]);
565− texts.set(n, text);
845+ textFile.set(n, file);
846+ textPlace.set(n, place);
847+ writers.set(n, seq);
848+ places.set(n, k);
566849 }
567− 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]!;
568854 if (!this.#has(deleter)) continue;
569855 const by = deleters.get(n);
570856 if (by) by.push(deleter);
571857 else deleters.set(n, [deleter]);
572858 }
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]!);
573862 }
574863 const order = (a: Child, b: Child) =>
575864 b.replaces - a.replaces || b.lamport - a.lamport || b.seq - a.seq || a.k - b.k;
576− 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;
577866 const seq: number[] = [];
578867 const vis: number[] = [];
579868 const visText: (string | null)[] = [];
869+ const visWriter: number[] = [];
870+ const visK: number[] = [];
871+ const seqWriter: number[] = [];
872+ const seqK: number[] = [];
580873 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;
885+ };
581886 const stack: number[] = [];
582887 const push = (code: number) => {
583888 const list = children.get(code);
Lines 898–925 at 318 (were 593–604 at 211)
593898 if (complete) deletedBy.set(seq.length, by.length > 1 ? by.sort(priority) : by);
594899 } else {
595900 vis.push(n);
596− visText.push(texts.get(n) ?? null);
901+ visText.push(textOf(n));
902+ visWriter.push(writers.get(n)!);
903+ visK.push(places.get(n)!);
597904 }
598− if (complete) seq.push(n);
905+ if (complete) {
906+ seq.push(n);
907+ seqWriter.push(writers.get(n)!);
908+ seqK.push(places.get(n)!);
909+ }
599910 push(n);
600911 }
601− return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy, visText };
912+ return {
913+ attrs,
914+ complete,
915+ seq: Int32Array.from(seq),
916+ vis: Int32Array.from(vis),
917+ deletedBy,
918+ visText,
919+ visWriter: Int32Array.from(visWriter),
920+ visK: Int32Array.from(visK),
921+ ...(complete ? { seqWriter: Int32Array.from(seqWriter), seqK: Int32Array.from(seqK) } : {}),
922+ };
602923 }
603924
604925 #attributes(path: string): FileAttrs {
Lines 982–994 at 318 (were 661–667 at 211)
661982 if (complete) seq.push(n);
662983 }
663984
664− return { attrs, complete, seq: Int32Array.from(seq), vis: Int32Array.from(vis), deletedBy };
985+ return {
986+ attrs,
987+ complete,
988+ seq: Int32Array.from(seq),
989+ vis: Int32Array.from(vis),
990+ deletedBy,
991+ };
665992 }
666993
667994 get #known(): string[] {
Lines 1007–1032 at 318 (were 680–690 at 211)
6801007
6811008 /** Files with at least one visible line, sorted. */
6821009 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();
1013+ if (shared) {
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);
1016+ return out.sort();
1017+ }
1018+ if (this.#pathsFrom !== undefined) {
1019+ if (this.#knownPaths === undefined) this.#knownPaths = this.#pathsFrom() ?? null;
1020+ if (this.#knownPaths) return [...this.#knownPaths].sort();
1021+ }
6831022 if (this.#stable) {
6841023 const paths: string[] = [];
6851024 for (const path of this.graph.knownFiles()) {
6861025 if (this.hasFile(path)) paths.push(path);
687− else this.#files.delete(path);
1026+ else {
1027+ this.#files.delete(path);
1028+ this.#used.delete(path);
1029+ }
6881030 }
6891031 return paths.sort();
6901032 }
Lines 1038–1048 at 318 (were 696–701 at 211)
6961038 return [...this.#known];
6971039 }
6981040
1041+ /** The file line n sits in, in this set: fileOf by the line's number, with no lookup of its id. */
1042+ fileOfNumber(n: number): string {
1043+ return this.#fileOfN(n);
1044+ }
6991046 /** How many visible lines a file has. */
7001047 visibleCount(path: string): number {
7011048 return this.#file(path).vis.length;
Lines 1050–1064 at 318 (were 703–708 at 211)
7031050
7041051 /** Whether the file exists in this projection (has a visible line). */
7051052 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);
1060+ }
1061+ }
7061062 const attrs = this.attrs(path);
7071063 return attrs.exists || attrs.link !== null || attrs.blob !== null || this.#file(path).vis.length > 0;
7081064 }
Lines 1095–1137 at 318 (were 739–768 at 211)
7391095 }
7401096
7411097 *lineChunks(path: string): IterableIterator<() => Iterable<string>> {
742− 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]!);
1105+ }
7431106 }
7441107
7451108 /** Visible lines without retaining their text in the view cache. */
7461109 *visibleLines(path: string): IterableIterator<VisibleLine> {
7471110 const graph = this.graph;
7481111 for (const n of this.#file(path).vis)
749− yield { id: graph.lineIdOf(n), text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
1112+ yield {
1113+ id: graph.lineIdOf(n),
1114+ text: graph.textOf(n),
1115+ atom: atomId(graph.writerSeqOf(n)),
1116+ };
7501117 }
7511118
7521119 /** Atom-building line handles; text is loaded only when requested. */
7531120 buildLines(path: string): readonly VisibleLine[] {
7541121 const graph = this.graph;
755− 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);
7561124 const lines: VisibleLine[] = new Array(vis.length);
7571125 for (let i = 0; i < vis.length; i++) {
7581126 const n = vis[i]!;
1127+ const packed = visText?.[i];
7591128 lines[i] = {
760− id: graph.lineIdOf(n),
1129+ id: visWriter && visK ? lineId(atomId(visWriter[i]!), visK[i]!) : graph.lineIdOf(n),
7611130 textChunks: () => graph.textChunks(n),
7621131 get text() {
763− return graph.textOf(n);
1132+ return packed ?? graph.textOf(n);
7641133 },
765− atom: graph.idOfSeq(graph.writerSeqOf(n)),
1134+ atom: atomId(visWriter ? visWriter[i]! : graph.writerSeqOf(n)),
7661135 };
7671136 }
7681137 return lines;
Lines 1141–1157 at 318 (were 772–782 at 211)
7721141 lines(path: string): readonly VisibleLine[] {
7731142 if (this.#linesCache?.path === path) return this.#linesCache.lines;
7741143 const graph = this.graph;
775− const vis = this.#file(path).vis;
1144+ const { vis, visText, visWriter, visK } = this.#file(path);
7761145 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.
7771148 for (let i = 0; i < vis.length; i++) {
7781149 const n = vis[i]!;
779− lines[i] = { id: graph.lineIdOf(n), text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
1150+ lines[i] = {
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)),
1154+ };
7801155 }
7811156 this.#linesCache = { path, lines };
7821157 return lines;
Lines 1163–1184 at 318 (were 788–801 at 211)
7881163 const graph = this.graph;
7891164 const f = this.#file(path, true);
7901165 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;
7911169 for (let i = 0; i < f.seq.length; i++) {
7921170 const n = f.seq[i]!;
7931171 const by = f.deletedBy.get(i);
1172+ const writer = seqWriter ? seqWriter[i]! : graph.writerSeqOf(n);
1173+ let text: string | undefined;
7941174 out[i] = {
795− id: graph.lineIdOf(n),
796− text: graph.textOf(n),
797− atom: graph.idOfSeq(graph.writerSeqOf(n)),
798− deletedBy: by ? by.map((s) => graph.idOfSeq(s)) : NONE,
1175+ id: seqWriter && seqK ? lineId(atomId(writer), seqK[i]!) : graph.lineIdOf(n),
1176+ get text() {
1177+ text ??= graph.textOf(n);
1178+ return text;
1179+ },
1180+ atom: atomId(writer),
1181+ deletedBy: by ? by.map((s) => atomId(s)) : NONE,
7991182 visible: by === undefined,
8001183 };
8011184 }
Lines 1232–1259 at 318 (were 849–862 at 211)
8491232
8501233 /** Index of a line in `lines(path)` (0-based visible line number), or -1 if not visible. */
8511234 lineNumber(path: string, line: LineId): number {
1235+ return this.lineNumberOf(path, this.graph.lineN(line));
1236+ }
1238+ /** lineNumber by the line's number, with no lookup of its id. */
1239+ lineNumberOf(path: string, n: number): number {
8521240 const f = this.#file(path);
8531241 if (!f.visIndex) {
8541242 f.visIndex = new Map();
8551243 for (let i = 0; i < f.vis.length; i++) f.visIndex.set(f.vis[i]!, i);
8561244 }
857− return f.visIndex.get(this.graph.lineN(line)) ?? -1;
1245+ return f.visIndex.get(n) ?? -1;
8581246 }
8591247
1248+ /** The seq of the atom that wrote each visible line, in order: lines() without ids or text. */
1249+ visibleWriters(path: string): number[] {
1250+ const graph = this.graph;
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));
1255+ }
8601257 /** All projected text files, path -> text, in path order. Binary files are in `blobs()`. */
8611258 files(): Record<string, string> {
8621259 const out: Record<string, string> = {};
Lines 1263–1274 at 318 (were 866–874 at 211)
8661263
8671264 /** Binary files, path -> the SHA-256 of their bytes, in path order. */
8681265 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.
8691268 const out: Record<string, string> = {};
870− for (const path of this.paths()) {
871− 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;
8721272 if (blob !== null) out[path] = blob;
8731273 }
8741274 return out;
Lines 1282–1305 at 318 (were 882–909 at 211)
8821282 * populate lazy caches, enumerate the graph, or count temporary traversal
8831283 * containers and shared empty singleton payloads as retained view state.
8841284 */
885− weight(): number {
1285+ weight(seen?: Set<object>): number {
8861286 let n = this.#in.byteLength;
8871287 n += 2 * ARRAY_HEADER_BYTES + this.#membershipSeq.byteLength + this.#membershipValue.byteLength;
8881288 if (this.#movers) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length;
8891289 n += MAP_HEADER_BYTES; // #files exists from construction.
890− n += MAP_HEADER_BYTES; // #fileOf exists from construction.
8911290 if (this.#stable) n += this.#stable.retainedBytes;
8921291 else if (this.#set) n += OBJECT_BYTES + SET_ENTRY_BYTES * this.#set.size;
8931292 if (this.#knownList) n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#knownList.length;
8941293 if (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size;
8951294 if (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size;
896− n += MAP_ENTRY_BYTES * this.#fileOf.size;
1295+ n += MAP_ENTRY_BYTES * this.#picked.size;
1296+ n += MAP_ENTRY_BYTES * this.#used.size;
8971297 for (const f of this.#files.values()) {
8981298 n += MAP_ENTRY_BYTES;
8991299 if (f !== NO_FILE) {
900− // FileState and FileAttrs are per laid-out file; typed-array
901− // headers are separate from their payload byte lengths below.
902− n += OBJECT_BYTES + OBJECT_BYTES + 2 * ARRAY_HEADER_BYTES;
903− n += f.seq.byteLength + f.vis.byteLength;
904− if (f.visText) for (const text of f.visText) n += REFERENCE_BYTES + 2 * (text?.length ?? 0);
905− n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + ARRAY_HEADER_BYTES) * f.deletedBy.size;
906− 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);
1302+ seen?.add(f);
9071303 if (f.seqIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size;
9081304 if (f.visIndex) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size;
9091305 }
Lines 1318–1362 at 318 (were 922–932 at 211)
9221318 #file(path: string, complete = false): FileState {
9231319 let state = this.#files.get(path);
9241320 if (!state || (complete && !state.complete)) {
925− 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;
9261324 this.#files.set(path, state);
9271325 }
1326+ if (state !== NO_FILE) this.#used.set(path, ++fileUses);
9281327 return state;
9291328 }
1330+ /** When each laid-out file was last used (fileUses). */
1331+ readonly #used = new Map<string, number>();
1333+ /**
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).
1336+ */
1337+ *laidOut(): IterableIterator<{
1338+ path: string;
1339+ layout: object;
1340+ used: number;
1341+ bytes: number;
1342+ }> {
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 };
1349+ }
1350+ }
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;
1359+ }
9301360}
9311361
9321362/** The files of a closed set, path -> text. */
MCP
compare on this site's /mcp
CLI
abso diff 211 318 src/engine/view.ts --json
JSON
/christopher/abso-core/compare/211..318?file=src%2Fengine%2Fview.ts&format=json