The view cache trims the least recently used file layouts across its views before whole views (abso Capacity, issue 664). Version 263 of shared· capacity· Oct 15 (2026-10-15 20:40 UTC) · Not checked
After 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.
On 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.
View 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.
Version 262 · Files at version 263
3 files, 113 lines added, 3 removed.
--- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -54,6 +54,8 @@
@@ -122,6 +124,9 @@
122 124 const EMPTY_INTS = new Int32Array(0); 123 125 const NO_FILE: FileState = { attrs: NO_ATTRS, complete: true, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() }; 127 + /** A count of file uses shared by every view, so a cache can tell which layout across its views was used longest ago. */ 125 130 // Approximate retained-container costs for View.weight(): reference slots, 126 131 // object headers, and map/set entries. These are cache-budget units, not V8 127 132 // heap measurements. Referenced Graph strings and records are not charged. @@ -914,7 +919,10 @@
914 919 const paths: string[] = []; 915 920 for (const path of this.graph.knownFiles()) { 916 921 if (this.hasFile(path)) paths.push(path); 917 − else this.#files.delete(path);
923 + this.#files.delete(path); 924 + this.#used.delete(path); 919 927 return paths.sort(); @@ -1168,6 +1176,7 @@
1168 1176 if (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size; 1169 1177 if (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size; 1170 1178 n += MAP_ENTRY_BYTES * this.#picked.size; 1179 + n += MAP_ENTRY_BYTES * this.#used.size; 1171 1180 for (const f of this.#files.values()) { 1172 1181 n += MAP_ENTRY_BYTES; 1173 1182 if (f !== NO_FILE) { @@ -1197,8 +1206,35 @@
1197 1206 else state = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE; 1198 1207 this.#files.set(path, state); 1209 + if (state !== NO_FILE) this.#used.set(path, ++fileUses); 1213 + /** When each laid-out file was last used (fileUses). */ 1214 + readonly #used = new Map<string, number>(); 1217 + * Each laid-out file: its layout, when it was last used (a count shared by every view) and what it holds, for a 1218 + * cache that trims the least recently used layouts across its views before whole views (views.ts, abso Capacity). 1220 + *laidOut(): IterableIterator<{ path: string; layout: object; used: number; bytes: number }> { 1221 + for (const [path, f] of this.#files) { 1222 + if (f === NO_FILE) continue; 1223 + let bytes = 2 * MAP_ENTRY_BYTES + layoutBytes(f); 1224 + if (f.seqIndex) bytes += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.seqIndex.size; 1225 + if (f.visIndex) bytes += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * f.visIndex.size; 1226 + yield { path, layout: f, used: this.#used.get(path) ?? 0, bytes }; 1230 + /** Let go of a file's layout: the next ask lays it out again, the same. */ 1231 + forget(path: string): void { 1232 + this.#files.delete(path); 1233 + this.#used.delete(path); 1234 + if (this.#linesCache?.path === path) this.#linesCache = undefined; 1235 + if (this.#seqCache?.path === path) this.#seqCache = undefined; 1236 + if (this.#textCache?.path === path) this.#textCache = undefined; 1204 1240 /** The files of a closed set, path -> text. */ --- a/worker/project/test/view-cache.test.ts
+++ b/worker/project/test/view-cache.test.ts
@@ -144,3 +144,44 @@
144 144 for (let i = 0; i < 40; i++) cache.keyed(`other-${i}`, () => set); 145 145 assert.deepEqual(cache.view(set).files(), new View(graph, set).files()); 148 + test("trim lets go of the least recently used file layouts across views before whole views (abso Capacity)", () => { 149 + const graph = new Graph(); 150 + const lines = (name: string) => Array.from({ length: 40 }, (_, i) => `${name} line ${i}\n`).join(""); 152 + buildBase({ "a.txt": lines("a"), "b.txt": lines("b"), "c.txt": lines("c") }, { author: "test", message: "base" }), 154 + const set = new Set(["a0"]); 155 + // a.txt in x first, then b.txt in y, then c.txt in x: a.txt is the least recently used. 156 + const use = (cache: ViewCache) => { 157 + const x = cache.keyed("x", () => set); 158 + const y = cache.keyed("y", () => set); 162 + return { x: x as View, y: y as View }; 164 + const probe = use(new ViewCache(graph)); 165 + const total = probe.x.weight() + probe.y.weight(); 166 + const cache = new ViewCache(graph, undefined, total - 1); 167 + const { x, y } = use(cache); 170 + [...x.laidOut()].map((f) => f.path), 174 + [...y.laidOut()].map((f) => f.path), 178 + cache.keyed("x", () => set), 180 + "both views are still cached", 183 + cache.keyed("y", () => set), 186 + assert.equal(x.text("a.txt"), lines("a"), "a.txt is laid out again, the same"); --- a/worker/project/views.ts
+++ b/worker/project/views.ts
@@ -30,9 +30,12 @@
30 30 readonly #provider?: ReadViewProvider; 31 31 readonly #cache = new Map<string | StableSet<AtomId>, View>(); 33 − constructor(graph: Graph, provider?: (graph: Graph) => ReadViewProvider) {
33 + /** VIEW_BUDGET unless a test asks for less. */ 34 + readonly #budget: number; 35 + constructor(graph: Graph, provider?: (graph: Graph) => ReadViewProvider, budget = VIEW_BUDGET) { 34 36 this.#graph = graph; 35 37 this.#provider = provider?.(graph); 38 + this.#budget = budget; 37 40 assertUnkeyedAllowed(): void { 38 41 if (this.#provider) throw Error("unsupported unkeyed projection membership"); @@ -43,10 +46,40 @@
43 46 * and #get weighs a view only when it is asked for again. A kept reader trims after each call. 49 + const seen = new Set<object>(); 51 + for (const v of this.#cache.values()) total += v.weight(seen); 52 + if (total > this.#budget) this.#shed(total - this.#budget); 57 + * Let go of the least recently used file layouts across every view, until about `over` bytes are freed (abso 58 + * Capacity). Evicting whole views dropped layouts just made along with old ones: compare 612 to 1224 on abso needs 59 + * both versions' layouts, about 25 MB, and evicting one whole after the call meant asking again laid it all out again. 60 + * A layout several views share goes from all of them. 62 + #shed(over: number): void { 63 + // One entry per layout: a layout several views hold (a draft's view borrows its base's) is used as recently as 64 + // any of them used it, and goes from all of them at once, since only that frees it. 65 + const layouts = new Map<object, { used: number; bytes: number; holders: { view: View; path: string }[] }>(); 66 + for (const view of this.#cache.values()) 67 + for (const f of view.laidOut()) { 68 + const entry = layouts.get(f.layout); 70 + entry.used = Math.max(entry.used, f.used); 71 + entry.holders.push({ view, path: f.path }); 72 + } else layouts.set(f.layout, { used: f.used, bytes: f.bytes, holders: [{ view, path: f.path }] }); 75 + for (const entry of [...layouts.values()].sort((a, b) => a.used - b.used)) { 76 + if (freed >= over) break; 77 + for (const { view, path } of entry.holders) view.forget(path); 78 + freed += entry.bytes; 50 83 * Keep the cached views under VIEW_BUDGET bytes (the website project, #43, 51 84 * #46): each view holds every line its files ever had, so a few views of a 52 85 * large generated file add up. At most VIEW_COUNT. @@ -62,7 +95,7 @@
62 95 for (const [key, v] of [...this.#cache].reverse()) { 64 97 const w = v.weight(seen); 65 − if (total + w <= VIEW_BUDGET && kept < VIEW_COUNT) {
98 + if (total + w <= this.#budget && kept < VIEW_COUNT) {