Lines 266–301 at 276 (were 266–271 at 275)
266266export type PackedAttr = [number, string, string | null]; 267267/** A run written in another file under one of a file's lines: [the parent's n, the run's file index]. */ 268268export type PackedCrossing = [number, number]; 271+ * A file's packed history decoded, without texts (Graph.packedHistory): PackedLine's five numbers per line, then 272+ * PackedDelete's two per delete and PackedCrossing's two per crossing, each list flat. 274+export interface PackedHistory { 275+ readonly lines: ArrayLike<number>; 276+ readonly deletes: ArrayLike<number>; 277+ readonly crossings: ArrayLike<number>; 281+ * Bytes of decoded file histories a stored graph keeps between layouts (abso Capacity). Every view of a file read its 282+ * whole history from pages: compare lays each file out once per side, collisions once per draft touching it, and a 283+ * file whose lines moved in from another read that one's history too (abso's worker files, split out of index.ts). 284+ * abso's index.ts, 46,000 lines of history, takes about 1 MB. 286+export const PACKED_HISTORY_BUDGET = 4_000_000; 288+/** Numbers packed flat: 4 bytes each while they fit in 32 bits, else 8. */ 289+function packNumbers(rows: Iterable<readonly number[]>): Int32Array | Float64Array { 290+ const flat: number[] = []; 292+ for (const row of rows) 293+ for (const value of row) { 294+ if ((value | 0) !== value) small = false; 297+ return small ? Int32Array.from(flat) : Float64Array.from(flat); 269299const EMPTY_NUMBERS: readonly number[] = Object.freeze([]); 270300/** At most this many path names kept in memory (Graph.#pathName). */ 271301const PATH_NAMES = 4096; Lines 470–480 at 276 (were 440–446 at 275)
440470 if (encoded.insertionFormat === 1) { 441471 const collection = `atom-inserts:${atom.seq}`; 442472 inserts = readonlyStoredArray(storage, collection, (raw, index) => { 443− const run = raw as { file: string; parent: string; lines: string };
473+ const run = raw as { 444478 const lines = `${collection}:lines:${index}`; 445479 if (run.lines !== lines || typeof run.file !== "string" || typeof run.parent !== "string") 446480 throw Error("invalid stored insertion run"); Lines 510–520 at 276 (were 476–482 at 275)
476510 const collection = `atom-inserts:${atom.seq}`; 477511 if (encoded.insertionRuns !== collection) throw Error("invalid stored insertion reference"); 478512 const inserts = readonlyStoredArray<InsertRun>(storage, collection, (raw, index) => { 479− const run = raw as { file: string; parent: InsertRun["parent"]; lines: string };
513+ const run = raw as { 515+ parent: InsertRun["parent"]; 480518 const lines = `${collection}:lines:${index}`; 481519 if (run.lines !== lines || typeof run.file !== "string" || typeof run.parent !== "string") 482520 throw Error("invalid stored insertion run"); Lines 531–540 at 276 (were 493–499 at 275)
496− Object.defineProperty(result, "inserts", { value: inserts, enumerable: true });
534+ Object.defineProperty(result, "inserts", { 498539 for (const field of listFields) { 499540 const collection = encoded.lists?.[field]; Lines 545–554 at 276 (were 504–510 at 275)
504545 encoded.listFormat === 2 505546 ? readonlyPagedArray(storage, collection) 506547 : readonlyStoredArray(storage, collection); 507− Object.defineProperty(result, field, { value: list, enumerable: true });
548+ Object.defineProperty(result, field, { 509553 return streamed(Object.freeze(result)); Lines 586–596 at 276 (were 542–548 at 275)
542586 lines.push({ text }); 543587 } else lines.push(line); 545− runs.push({ file: run.file, parent: run.parent, lines: lineCollection });
592+ lines: lineCollection, 548596 insertionFormat: 1, Lines 636–645 at 276 (were 588–594 at 275)
590638 if (typeof value === "string") return value; 591− const reference = value as unknown as { format: number; text: string };
639+ const reference = value as unknown as { 592643 if (reference?.format !== 1 || typeof reference.text !== "string") throw Error("invalid long line reference"); 593644 return longText.read(reference.text); Lines 759–811 at 276 (were 708–713 at 275)
708759 yield* this.#fileCrossings!.values(String(file)); 762+ /** Decoded file histories by path index, most recently used last, under PACKED_HISTORY_BUDGET bytes. */ 763+ readonly #histories = new Map<number, { counts: string; history: PackedHistory; bytes: number }>(); 767+ * A file's line, delete and crossing history, decoded once and kept while it fits (abso Capacity). Kept under its 768+ * three lists' lengths: the lists only grow, and add() lets every kept history go, so a kept one is always whole. 770+ packedHistory(file: number): PackedHistory { 771+ const key = String(file); 772+ const counts = `${this.#fileLines!.length(key)} ${this.#fileDeletes!.length(key)} ${this.#fileCrossings!.length(key)}`; 773+ const kept = this.#histories.get(file); 775+ this.#histories.delete(file); 776+ if (kept.counts === counts) { 777+ this.#histories.set(file, kept); 778+ return kept.history; 780+ this.#historyBytes -= kept.bytes; 782+ const history: PackedHistory = { 783+ lines: packNumbers(this.#fileLines!.values(key)), 784+ deletes: packNumbers(this.#fileDeletes!.values(key)), 785+ crossings: packNumbers(this.#fileCrossings!.values(key)), 788+ (history.lines as Int32Array).byteLength + 789+ (history.deletes as Int32Array).byteLength + 790+ (history.crossings as Int32Array).byteLength + 792+ if (bytes <= PACKED_HISTORY_BUDGET) { 793+ this.#histories.set(file, { counts, history, bytes }); 794+ this.#historyBytes += bytes; 795+ for (const [old, entry] of this.#histories) { 796+ if (this.#historyBytes <= PACKED_HISTORY_BUDGET) break; 797+ this.#histories.delete(old); 798+ this.#historyBytes -= entry.bytes; 804+ /** Bytes of decoded file histories kept (packedHistory). */ 805+ get historyBytes(): number { 806+ return this.#historyBytes; 711809 /** Where an atom's key and delete lists are when the graph doesn't keep them (#46): the stored atom. */ 712810 #lists: ((seq: number) => AtomLists) | undefined; 713811 /** Recently read atoms' lists, most recent last, up to LIST_CACHE atoms. */ Lines 1569–1577 at 276 (were 1471–1476 at 275)
14711569 * The atom is frozen. Adding to the graph does not add it to any set. 14731571 add(atom: Atom): void { 1572+ // A new atom appends to its files' histories; a failed one may leave a kept history longer than the lists. 1573+ this.#histories.clear(); 1574+ this.#historyBytes = 0; 14741575 if (this.#storage) 14751576 this.#storage.transaction(() => { 14761577 this.#add(atom); Lines 1585–1594 at 276 (were 1484–1490 at 275)
14841585 #validateAcceptedAtom(atom: Atom, scratch?: GraphStateStorage): void { 14851586 const membership = <T extends string>(name: string): { has(key: T): boolean; add(key: T): unknown } => { 14861587 if (!scratch) return new Set<T>(); 1487− return { has: (key) => scratch.read(name, key) === true, add: (key) => scratch.write(name, key, true) };
1589+ has: (key) => scratch.read(name, key) === true, 1590+ add: (key) => scratch.write(name, key, true), 14891593 validateShape(atom); 14901594 const seq = this.#atoms.length; Lines 1802–1813 at 276 (were 1698–1704 at 275)
17001804 (frozen.moves ?? []).forEach((m, i) => { 1701− const info: RelocationInfo = Object.freeze({ atom: frozen.id, line: m.line, parent: m.parent, k: i });
1805+ const info: RelocationInfo = Object.freeze({ 17021811 if (this.#relocationPages) this.#relocationPages.append(m.line, info); 17031812 else pushIndex(this.#relocations, m.line, info); 17041813 if (this.#relocatedToPages) this.#relocatedToPages.append(m.parent, info); Lines 1955–1966 at 276 (were 1846–1852 at 275)
18461955 // JSON ------------------------------------------------------------------- 18481957 toJSON(): GraphJSON { 1849− return { format: GRAPH_FORMAT, version: 1, atoms: this.#atoms, labels: this.labels() };
1959+ format: GRAPH_FORMAT, 1961+ atoms: this.#atoms, 1962+ labels: this.labels(), 18521966 /** Load a saved graph. Every atom is re-validated as it is added. */ Lines 2060–2071 at 276 (were 1946–1952 at 275)
19462060 // With text kept elsewhere (#46), a run's lines are read when asked for. 19482062 const run = (r: InsertRun): InsertRun => { 1949− if (!text) return Object.freeze({ file: r.file, parent: r.parent, lines: Object.freeze([...r.lines]) });
2064+ return Object.freeze({ 2067+ lines: Object.freeze([...r.lines]), 19502069 const start = k; 19512070 const length = r.lines.length; Lines 2080–2104 at 276 (were 1961–1976 at 275)
19612080 inserts: Object.freeze(atom.inserts.map(run)), 1964− : { deletes: list(atom.deletes), writes: list(atom.writes), reads: list(atom.reads), holds: list(atom.holds) }),
2084+ deletes: list(atom.deletes), 2085+ writes: list(atom.writes), 2086+ reads: list(atom.reads), 2087+ holds: list(atom.holds), 19652089 needs: list(atom.needs, canon), 19662090 resolves: list(atom.resolves, canon), 19672091 reasons: list(atom.reasons, (r) => Object.freeze({ to: canon(r.to), kind: r.kind, text: r.text })), 19682092 deps: list(atom.deps, canon), 19692093 ...(atom.attrs && atom.attrs.length > 0 1970− ? { attrs: Object.freeze(atom.attrs.map((a) => Object.freeze({ file: a.file, name: a.name, value: a.value }))) }
2095+ attrs: Object.freeze(atom.attrs.map((a) => Object.freeze({ file: a.file, name: a.name, value: a.value }))), 1973− ? { moves: Object.freeze(atom.moves.map((m) => Object.freeze({ line: m.line, parent: m.parent }))) }
2100+ moves: Object.freeze(atom.moves.map((m) => Object.freeze({ line: m.line, parent: m.parent }))), 19762104 return Object.freeze(lists ? (new ListedAtom(copy, lists) as unknown as Atom) : copy);