{"draft":null,"from":175,"to":318,"files":[{"path":"worker/project/views.ts","added":67,"removed":7,"diff":"--- a/worker/project/views.ts\n+++ b/worker/project/views.ts\n@@ -12,36 +12,96 @@\n  */\n const VIEW_BUDGET = 24_000_000;\n \n+/**\n+ * At most this many views (abso Capacity). where visits every agent's draft view in turn: with more agents than views,\n+ * each visit evicted the next one, and a warm where on abso laid every view out again (4.5 s against 0.33 s). The byte\n+ * budget still bounds what they hold.\n+ */\n+export const VIEW_COUNT = 128;\n+\n /** Internal opt-in. The owner validates identity and lifetime on every returned reader. */\n export interface ReadViewProvider {\n \tkeyed(key: string): ReadView;\n }\n \n-/** Views by key, most recently used last, held under VIEW_BUDGET bytes and 32 views. */\n+/** Views by key, most recently used last, held under VIEW_BUDGET bytes and VIEW_COUNT views. */\n export class ViewCache {\n \treadonly #graph: Graph;\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 \t}\n \n \t/**\n+\t * Weigh every cached view again and evict until they fit (abso Capacity): views grow while a call lays files out,\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 32.\n+\t * large generated file add up. At most VIEW_COUNT.\n \t */\n \t#trim(): void {\n+\t\t// The newest views that fit, as evicting oldest first leaves them. A file layout two views share (a draft's view\n+\t\t// takes its base's, View.shareFiles) counts once (abso Capacity): counted per view, they filled the budget twice\n+\t\t// over, and a kept writer laid abso's drafts out again on every collisions call.\n+\t\tconst seen = new Set<object>();\n \t\tlet total = 0;\n-\t\tfor (const v of this.#cache.values()) total += v.weight();\n-\t\tfor (const [key, v] of this.#cache) {\n-\t\t\tif (total <= VIEW_BUDGET && this.#cache.size <= 32) break;\n-\t\t\ttotal -= v.weight();\n+\t\tlet kept = 0;\n+\t\tlet full = false;\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 <= this.#budget && kept < VIEW_COUNT) {\n+\t\t\t\t\ttotal += w;\n+\t\t\t\t\tkept++;\n+\t\t\t\t\tcontinue;\n+\t\t\t\t}\n+\t\t\t\tfull = true;\n+\t\t\t}\n \t\t\tthis.#cache.delete(key);\n \t\t}\n \t}"}],"unmatched":[]}