{"draft":null,"from":205,"to":206,"files":[{"path":"src/engine/graph.ts","added":94,"removed":0,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -256,6 +256,15 @@\n \treturn { seq, k };\n }\n \n+/** A line in its file's packed history: [n, parent code, writer seq, k, 1 when it replaces its parent]. */\n+export type PackedLine = [number, number, number, number, number];\n+/** A delete of a line in a file's packed history: [n, deleter seq]. */\n+export type PackedDelete = [number, number];\n+/** An attribute set on a file: [seq, name, value]. */\n+export type PackedAttr = [number, string, string | null];\n+/** Lines longer than this aren't packed with their file; their text is read on its own. */\n+const PACKED_TEXT = 4096;\n+\n export class Graph {\n \treadonly #storage?: GraphStateStorage;\n \treadonly #childCounts?: ChildCounts;\n@@ -294,6 +303,8 @@\n \t\t\tstorage?: GraphStateStorage;\n \t\t\t/** Optional derived indexing inside persistent atom acceptance; must be synchronous. */\n \t\t\tindexAcceptedAtom?: (atom: Atom, files: readonly string[], storage: GraphStateStorage) => void;\n+\t\t\t/** false: a new stored graph keeps no packed file history, and lays files out line by line (tests of that path). */\n+\t\t\tpackFiles?: boolean;\n \t\t} = {},\n \t) {\n \t\tif (options.indexAcceptedAtom && !options.storage)\n@@ -583,9 +594,58 @@\n \t\t\t\tJSON.stringify([\"number\", Number(key)]),\n \t\t\t);\n \t\t\tthis.#labels = storedMap(storage, \"labels\");\n+\t\t\tthis.#fileLines = storedLists<PackedLine>(storage, \"file-lines\");\n+\t\t\tthis.#fileTexts = storedLists<string | null>(storage, \"file-texts\");\n+\t\t\tthis.#fileDeletes = storedLists<PackedDelete>(storage, \"file-deletes\");\n+\t\t\tthis.#fileAttrs = storedLists<PackedAttr>(storage, \"file-attrs\");\n+\t\t\tthis.#packFiles = options.packFiles ?? true;\n \t\t}\n \t}\n \n+\t// Each file's line history packed, in pages (abso Capacity) ---------------------------------------------------\n+\t// For each file, by its path index: every line written in it ([n, parent code, writer seq, k, replaces]) and its\n+\t// text, every delete of one of those lines ([n, deleter seq]), and every attribute set on it ([seq, name, value]).\n+\t// Appended as atoms are added, so a view lays a file out from a few pages instead of a row read per line. Kept from\n+\t// a fresh adoption on; a graph adopted before has none, and views lay out as before.\n+\n+\t#fileLines: ReturnType<typeof storedLists<PackedLine>> | undefined;\n+\t#fileTexts: ReturnType<typeof storedLists<string | null>> | undefined;\n+\t#fileDeletes: ReturnType<typeof storedLists<PackedDelete>> | undefined;\n+\t#fileAttrs: ReturnType<typeof storedLists<PackedAttr>> | undefined;\n+\t/** Whether a new stored graph starts packing (GraphOptions.packFiles). */\n+\t#packFiles = true;\n+\t/** Whether this graph's files are packed: read once, when first asked, so opening reads nothing. */\n+\t#packedKnown: boolean | undefined;\n+\tget #packed(): boolean {\n+\t\tthis.#packedKnown ??= !!this.#storage && this.#storage.read(\"file-lines:meta\", \"format\") === 1;\n+\t\treturn this.#packedKnown;\n+\t}\n+\n+\t/** Whether this graph keeps each file's line history packed. */\n+\tget packedFiles(): boolean {\n+\t\treturn this.#packed;\n+\t}\n+\t/** A path's index, or undefined when nothing was ever written or set in it. */\n+\tpathIndexOf(path: string): number | undefined {\n+\t\treturn this.#pathIndex.get(path);\n+\t}\n+\t/** The index of the path line n was written in. */\n+\twrittenInIndex(n: number): number {\n+\t\treturn this.#lineFile.at(n);\n+\t}\n+\t*packedLines(file: number): IterableIterator<PackedLine> {\n+\t\tyield* this.#fileLines!.values(String(file));\n+\t}\n+\t*packedTexts(file: number): IterableIterator<string | null> {\n+\t\tyield* this.#fileTexts!.values(String(file));\n+\t}\n+\t*packedDeletes(file: number): IterableIterator<PackedDelete> {\n+\t\tyield* this.#fileDeletes!.values(String(file));\n+\t}\n+\t*packedAttrs(file: number): IterableIterator<PackedAttr> {\n+\t\tyield* this.#fileAttrs!.values(String(file));\n+\t}\n+\n \t/** Where an atom's key and delete lists are when the graph doesn't keep them (#46): the stored atom. */\n \t#lists: ((seq: number) => AtomLists) | undefined;\n \t/** Recently read atoms' lists, most recent last, up to LIST_CACHE atoms. */\n@@ -1377,6 +1437,10 @@\n \t\t\tif (this.#atoms.length !== 0) throw new Error(\"ordered children require fresh adoption\");\n \t\t\tthis.#storage.write(\"ordered-children\", \"format\", 2);\n \t\t}\n+\t\tif (this.#storage && this.#atoms.length === 0 && this.#packFiles) {\n+\t\t\tthis.#storage.write(\"file-lines:meta\", \"format\", 1);\n+\t\t\tthis.#packedKnown = true;\n+\t\t}\n \n \t\tif (this.#storage) this.withProjectionScratch((scratch) => this.#validateAcceptedAtom(atom, scratch));\n \t\telse this.#validateAcceptedAtom(atom);\n@@ -1425,11 +1489,19 @@\n \t\t\tif (deletedN) yield* deletedN;\n \t\t\telse for (const line of atom.deletes) yield graph.#lineN(line);\n \t\t};\n+\t\tconst packedDeletes = new Map<number, PackedDelete[]>();\n \t\tfor (const n of deletedNumbers(this)) {\n+\t\t\tif (this.#packed) {\n+\t\t\t\tconst file = this.#lineFile.at(n);\n+\t\t\t\tconst list = packedDeletes.get(file);\n+\t\t\t\tif (list) list.push([n, seq]);\n+\t\t\t\telse packedDeletes.set(file, [[n, seq]]);\n+\t\t\t}\n \t\t\tif (this.#deleter.at(n) < 0) this.#deleter.set(n, seq);\n \t\t\telse if (this.#moreDeleterPages) this.#moreDeleterPages.append(String(n), seq);\n \t\t\telse pushIndex(this.#moreDeleters, n, seq);\n \t\t}\n+\t\tfor (const [file, list] of packedDeletes) this.#fileDeletes!.appendAll(String(file), list);\n \t\tthis.#lineBase.push(this.#lineTotal);\n \t\tfor (const run of atom.inserts) {\n \t\t\tconst file = this.#pathOf(run.file);\n@@ -1450,6 +1522,26 @@\n \n \t\t\tif (this.#runStartPages) this.#runStartPages.append(String(parent), first);\n \t\t\telse pushOne(this.#runStarts, parent, first);\n+\t\t\tconst replacesParent = deletes(run.parent) ? 1 : 0;\n+\t\t\tif (this.#packed) {\n+\t\t\t\tconst base = this.#lineBase[seq]!;\n+\t\t\t\tthis.#fileLines!.appendAll(\n+\t\t\t\t\tString(file),\n+\t\t\t\t\trun.lines.map(\n+\t\t\t\t\t\t(_, i): PackedLine => [\n+\t\t\t\t\t\t\tfirst + i,\n+\t\t\t\t\t\t\ti === 0 ? parent : first + i - 1,\n+\t\t\t\t\t\t\tseq,\n+\t\t\t\t\t\t\tfirst + i - base,\n+\t\t\t\t\t\t\ti === 0 ? replacesParent : 0,\n+\t\t\t\t\t\t],\n+\t\t\t\t\t),\n+\t\t\t\t);\n+\t\t\t\tthis.#fileTexts!.appendAll(\n+\t\t\t\t\tString(file),\n+\t\t\t\t\trun.lines.map((text) => (typeof text === \"string\" && text.length <= PACKED_TEXT ? text : null)),\n+\t\t\t\t);\n+\t\t\t}\n \t\t\tfor (let i = 0; i < run.lines.length; i++) {\n \t\t\t\tconst n = this.#lineTotal;\n \t\t\t\tthis.#childCounts?.append(n);\n@@ -1473,6 +1565,8 @@\n \t\t\telse pushIndex(this.#relocatedToCode, parentCode, info);\n \t\t});\n \t\tif (frozen.moves) this.#movers.push(frozen.id);\n+\t\tif (this.#packed)\n+\t\t\tfor (const a of frozen.attrs ?? []) this.#fileAttrs!.append(String(this.#pathOf(a.file)), [seq, a.name, a.value]);\n \t\tfor (const key of writes) {\n \t\t\tconst code = this.#code(key);\n \t\t\tif (this.#writersPages) {"}],"unmatched":[]}