{"version":263,"agent":"capacity","message":"The view cache trims the least recently used file layouts across its views before whole views (abso Capacity, issue 664).\n\nAfter each read, a kept reader's trim (ViewCache.trim, from afterRead) now lets go of file layouts in the order they were last used, across every cached view, until the views fit; a layout several views share (a draft's view borrows its base's) counts once and goes from all of them. Only then does it evict whole views. It evicted whole views before, so a call that laid out two big versions lost one of them entirely. Within a read, the cache trims whole views as before. A forgotten layout is laid out again, the same, when next asked.\n\nOn the abso export, collisions then compare 612 to 1224 asked again (Testing's order): 4.4 s and 17,349 queries, from 7.4 to 9.5 s. Compare alone needs about 25 MB of layouts, more than the 24 MB budget, so about half are laid out again; the rest needs a bigger budget or lighter layouts.\n\nView records each file's last use (a count shared by all views) and can forget one (laidOut, forget); ViewCache takes an optional budget, for tests. Tests: view-cache (new: the least recently used layout goes first, both views stay cached, and the forgotten file reads the same); view's retained-state test still holds. All of src/engine/test (294) and worker/project/test (354) pass.","time":1792096802015,"timeIso":"2026-10-15T20:40:02.015Z","changes":5,"change":4043,"files":["src/engine/view.ts","worker/project/test/view-cache.test.ts","worker/project/views.ts"],"draft":null,"diff":[{"path":"src/engine/view.ts","added":37,"removed":1,"diff":"--- a/src/engine/view.ts\n+++ b/src/engine/view.ts\n@@ -54,6 +54,8 @@\n \t| \"shareMoves\"\n \t| \"shareFiles\"\n \t| \"knowPaths\"\n+\t| \"laidOut\"\n+\t| \"forget\"\n \t| \"fileOfNumber\"\n \t| \"lineNumberOf\"\n \t| \"visibleWriters\"\n@@ -122,6 +124,9 @@\n const EMPTY_INTS = new Int32Array(0);\n const NO_FILE: FileState = { attrs: NO_ATTRS, complete: true, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };\n \n+/** A count of file uses shared by every view, so a cache can tell which layout across its views was used longest ago. */\n+let fileUses = 0;\n+\n // Approximate retained-container costs for View.weight(): reference slots,\n // object headers, and map/set entries. These are cache-budget units, not V8\n // heap measurements. Referenced Graph strings and records are not charged.\n@@ -914,7 +919,10 @@\n \t\t\tconst paths: string[] = [];\n \t\t\tfor (const path of this.graph.knownFiles()) {\n \t\t\t\tif (this.hasFile(path)) paths.push(path);\n-\t\t\t\telse this.#files.delete(path);\n+\t\t\t\telse {\n+\t\t\t\t\tthis.#files.delete(path);\n+\t\t\t\t\tthis.#used.delete(path);\n+\t\t\t\t}\n \t\t\t}\n \t\t\treturn paths.sort();\n \t\t}\n@@ -1168,6 +1176,7 @@\n \t\tif (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size;\n \t\tif (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size;\n \t\tn += MAP_ENTRY_BYTES * this.#picked.size;\n+\t\tn += MAP_ENTRY_BYTES * this.#used.size;\n \t\tfor (const f of this.#files.values()) {\n \t\t\tn += MAP_ENTRY_BYTES;\n \t\t\tif (f !== NO_FILE) {\n@@ -1197,8 +1206,35 @@\n \t\t\telse state = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE;\n \t\t\tthis.#files.set(path, state);\n \t\t}\n+\t\tif (state !== NO_FILE) this.#used.set(path, ++fileUses);\n \t\treturn state;\n \t}\n+\n+\t/** When each laid-out file was last used (fileUses). */\n+\treadonly #used = new Map<string, number>();\n+\n+\t/**\n+\t * Each laid-out file: its layout, when it was last used (a count shared by every view) and what it holds, for a\n+\t * cache that trims the least recently used layouts across its views before whole views (views.ts, abso Capacity).\n+\t */\n+\t*laidOut(): IterableIterator<{ path: string; layout: object; used: number; bytes: number }> {\n+\t\tfor (const [path, f] of this.#files) {\n+\t\t\tif (f === NO_FILE) continue;\n+\t\t\tlet bytes = 2 * MAP_ENTRY_BYTES + layoutBytes(f);\n+\t\t\tif (f.seqIndex) bytes += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size;\n+\t\t\tif (f.visIndex) bytes += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size;\n+\t\t\tyield { path, layout: f, used: this.#used.get(path) ?? 0, bytes };\n+\t\t}\n+\t}\n+\n+\t/** Let go of a file's layout: the next ask lays it out again, the same. */\n+\tforget(path: string): void {\n+\t\tthis.#files.delete(path);\n+\t\tthis.#used.delete(path);\n+\t\tif (this.#linesCache?.path === path) this.#linesCache = undefined;\n+\t\tif (this.#seqCache?.path === path) this.#seqCache = undefined;\n+\t\tif (this.#textCache?.path === path) this.#textCache = undefined;\n+\t}\n }\n \n /** The files of a closed set, path -> text. */"},{"path":"worker/project/test/view-cache.test.ts","added":41,"removed":0,"diff":"--- a/worker/project/test/view-cache.test.ts\n+++ b/worker/project/test/view-cache.test.ts\n@@ -144,3 +144,44 @@\n \tfor (let i = 0; i < 40; i++) cache.keyed(`other-${i}`, () => set);\n \tassert.deepEqual(cache.view(set).files(), new View(graph, set).files());\n });\n+\n+test(\"trim lets go of the least recently used file layouts across views before whole views (abso Capacity)\", () => {\n+\tconst graph = new Graph();\n+\tconst lines = (name: string) => Array.from({ length: 40 }, (_, i) => `${name} line ${i}\\n`).join(\"\");\n+\tgraph.add(\n+\t\tbuildBase({ \"a.txt\": lines(\"a\"), \"b.txt\": lines(\"b\"), \"c.txt\": lines(\"c\") }, { author: \"test\", message: \"base\" }),\n+\t);\n+\tconst set = new Set([\"a0\"]);\n+\t// a.txt in x first, then b.txt in y, then c.txt in x: a.txt is the least recently used.\n+\tconst use = (cache: ViewCache) => {\n+\t\tconst x = cache.keyed(\"x\", () => set);\n+\t\tconst y = cache.keyed(\"y\", () => set);\n+\t\tx.text(\"a.txt\");\n+\t\ty.text(\"b.txt\");\n+\t\tx.text(\"c.txt\");\n+\t\treturn { x: x as View, y: y as View };\n+\t};\n+\tconst probe = use(new ViewCache(graph));\n+\tconst total = probe.x.weight() + probe.y.weight();\n+\tconst cache = new ViewCache(graph, undefined, total - 1);\n+\tconst { x, y } = use(cache);\n+\tcache.trim();\n+\tassert.deepEqual(\n+\t\t[...x.laidOut()].map((f) => f.path),\n+\t\t[\"c.txt\"],\n+\t);\n+\tassert.deepEqual(\n+\t\t[...y.laidOut()].map((f) => f.path),\n+\t\t[\"b.txt\"],\n+\t);\n+\tassert.equal(\n+\t\tcache.keyed(\"x\", () => set),\n+\t\tx,\n+\t\t\"both views are still cached\",\n+\t);\n+\tassert.equal(\n+\t\tcache.keyed(\"y\", () => set),\n+\t\ty,\n+\t);\n+\tassert.equal(x.text(\"a.txt\"), lines(\"a\"), \"a.txt is laid out again, the same\");\n+});"},{"path":"worker/project/views.ts","added":35,"removed":2,"diff":"--- a/worker/project/views.ts\n+++ b/worker/project/views.ts\n@@ -30,9 +30,12 @@\n \treadonly #provider?: ReadViewProvider;\n \treadonly #cache = new Map<string | StableSet<AtomId>, View>();\n \n-\tconstructor(graph: Graph, provider?: (graph: Graph) => ReadViewProvider) {\n+\t/** VIEW_BUDGET unless a test asks for less. */\n+\treadonly #budget: number;\n+\tconstructor(graph: Graph, provider?: (graph: Graph) => ReadViewProvider, budget = VIEW_BUDGET) {\n \t\tthis.#graph = graph;\n \t\tthis.#provider = provider?.(graph);\n+\t\tthis.#budget = budget;\n \t}\n \tassertUnkeyedAllowed(): void {\n \t\tif (this.#provider) throw Error(\"unsupported unkeyed projection membership\");\n@@ -43,10 +46,40 @@\n \t * and #get weighs a view only when it is asked for again. A kept reader trims after each call.\n \t */\n \ttrim(): void {\n+\t\tconst seen = new Set<object>();\n+\t\tlet total = 0;\n+\t\tfor (const v of this.#cache.values()) total += v.weight(seen);\n+\t\tif (total > this.#budget) this.#shed(total - this.#budget);\n \t\tthis.#trim();\n \t}\n \n \t/**\n+\t * Let go of the least recently used file layouts across every view, until about `over` bytes are freed (abso\n+\t * Capacity). Evicting whole views dropped layouts just made along with old ones: compare 612 to 1224 on abso needs\n+\t * both versions' layouts, about 25 MB, and evicting one whole after the call meant asking again laid it all out again.\n+\t * A layout several views share goes from all of them.\n+\t */\n+\t#shed(over: number): void {\n+\t\t// One entry per layout: a layout several views hold (a draft's view borrows its base's) is used as recently as\n+\t\t// any of them used it, and goes from all of them at once, since only that frees it.\n+\t\tconst layouts = new Map<object, { used: number; bytes: number; holders: { view: View; path: string }[] }>();\n+\t\tfor (const view of this.#cache.values())\n+\t\t\tfor (const f of view.laidOut()) {\n+\t\t\t\tconst entry = layouts.get(f.layout);\n+\t\t\t\tif (entry) {\n+\t\t\t\t\tentry.used = Math.max(entry.used, f.used);\n+\t\t\t\t\tentry.holders.push({ view, path: f.path });\n+\t\t\t\t} else layouts.set(f.layout, { used: f.used, bytes: f.bytes, holders: [{ view, path: f.path }] });\n+\t\t\t}\n+\t\tlet freed = 0;\n+\t\tfor (const entry of [...layouts.values()].sort((a, b) => a.used - b.used)) {\n+\t\t\tif (freed >= over) break;\n+\t\t\tfor (const { view, path } of entry.holders) view.forget(path);\n+\t\t\tfreed += entry.bytes;\n+\t\t}\n+\t}\n+\n+\t/**\n \t * Keep the cached views under VIEW_BUDGET bytes (the website project, #43,\n \t * #46): each view holds every line its files ever had, so a few views of a\n \t * large generated file add up. At most VIEW_COUNT.\n@@ -62,7 +95,7 @@\n \t\tfor (const [key, v] of [...this.#cache].reverse()) {\n \t\t\tif (!full) {\n \t\t\t\tconst w = v.weight(seen);\n-\t\t\t\tif (total + w <= VIEW_BUDGET && kept < VIEW_COUNT) {\n+\t\t\t\tif (total + w <= this.#budget && kept < VIEW_COUNT) {\n \t\t\t\t\ttotal += w;\n \t\t\t\t\tkept++;\n \t\t\t\t\tcontinue;"}],"ci":null}