Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
show on this site's /mcp
CLI
abso show 263 --json
JSON
/christopher/abso-core/versions/263?format=json

The view cache trims the least recently used file layouts across its views before whole views (abso Capacity, issue 664).

of shared·capacity··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 @@
5454 | "shareMoves"
5555 | "shareFiles"
5656 | "knowPaths"
57+ | "laidOut"
58+ | "forget"
5759 | "fileOfNumber"
5860 | "lineNumberOf"
5961 | "visibleWriters"
@@ -122,6 +124,9 @@
122124const EMPTY_INTS = new Int32Array(0);
123125const NO_FILE: FileState = { attrs: NO_ATTRS, complete: true, seq: EMPTY_INTS, vis: EMPTY_INTS, deletedBy: new Map() };
124126
127+/** A count of file uses shared by every view, so a cache can tell which layout across its views was used longest ago. */
128+let fileUses = 0;
125130// Approximate retained-container costs for View.weight(): reference slots,
126131// object headers, and map/set entries. These are cache-budget units, not V8
127132// heap measurements. Referenced Graph strings and records are not charged.
@@ -914,7 +919,10 @@
914919 const paths: string[] = [];
915920 for (const path of this.graph.knownFiles()) {
916921 if (this.hasFile(path)) paths.push(path);
917− else this.#files.delete(path);
922+ else {
923+ this.#files.delete(path);
924+ this.#used.delete(path);
925+ }
918926 }
919927 return paths.sort();
920928 }
@@ -1168,6 +1176,7 @@
11681176 if (this.#relocs) n += MAP_HEADER_BYTES + MAP_ENTRY_BYTES * this.#relocs.size;
11691177 if (this.#relocsN) n += MAP_HEADER_BYTES + (MAP_ENTRY_BYTES + OBJECT_BYTES) * this.#relocsN.size;
11701178 n += MAP_ENTRY_BYTES * this.#picked.size;
1179+ n += MAP_ENTRY_BYTES * this.#used.size;
11711180 for (const f of this.#files.values()) {
11721181 n += MAP_ENTRY_BYTES;
11731182 if (f !== NO_FILE) {
@@ -1197,8 +1206,35 @@
11971206 else state = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE;
11981207 this.#files.set(path, state);
11991208 }
1209+ if (state !== NO_FILE) this.#used.set(path, ++fileUses);
12001210 return state;
12011211 }
1213+ /** When each laid-out file was last used (fileUses). */
1214+ readonly #used = new Map<string, number>();
1216+ /**
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).
1219+ */
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 };
1227+ }
1228+ }
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;
1237+ }
12021238}
12031239
12041240/** 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 @@
144144 for (let i = 0; i < 40; i++) cache.keyed(`other-${i}`, () => set);
145145 assert.deepEqual(cache.view(set).files(), new View(graph, set).files());
146146});
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("");
151+ graph.add(
152+ buildBase({ "a.txt": lines("a"), "b.txt": lines("b"), "c.txt": lines("c") }, { author: "test", message: "base" }),
153+ );
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);
159+ x.text("a.txt");
160+ y.text("b.txt");
161+ x.text("c.txt");
162+ return { x: x as View, y: y as View };
163+ };
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);
168+ cache.trim();
169+ assert.deepEqual(
170+ [...x.laidOut()].map((f) => f.path),
171+ ["c.txt"],
172+ );
173+ assert.deepEqual(
174+ [...y.laidOut()].map((f) => f.path),
175+ ["b.txt"],
176+ );
177+ assert.equal(
178+ cache.keyed("x", () => set),
179+ x,
180+ "both views are still cached",
181+ );
182+ assert.equal(
183+ cache.keyed("y", () => set),
184+ y,
185+ );
186+ assert.equal(x.text("a.txt"), lines("a"), "a.txt is laid out again, the same");
187+});
--- a/worker/project/views.ts
+++ b/worker/project/views.ts
@@ -30,9 +30,12 @@
3030 readonly #provider?: ReadViewProvider;
3131 readonly #cache = new Map<string | StableSet<AtomId>, View>();
3232
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) {
3436 this.#graph = graph;
3537 this.#provider = provider?.(graph);
38+ this.#budget = budget;
3639 }
3740 assertUnkeyedAllowed(): void {
3841 if (this.#provider) throw Error("unsupported unkeyed projection membership");
@@ -43,10 +46,40 @@
4346 * and #get weighs a view only when it is asked for again. A kept reader trims after each call.
4447 */
4548 trim(): void {
49+ const seen = new Set<object>();
50+ let total = 0;
51+ for (const v of this.#cache.values()) total += v.weight(seen);
52+ if (total > this.#budget) this.#shed(total - this.#budget);
4653 this.#trim();
4754 }
4855
4956 /**
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.
61+ */
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);
69+ if (entry) {
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 }] });
73+ }
74+ let freed = 0;
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;
79+ }
80+ }
82+ /**
5083 * Keep the cached views under VIEW_BUDGET bytes (the website project, #43,
5184 * #46): each view holds every line its files ever had, so a few views of a
5285 * large generated file add up. At most VIEW_COUNT.
@@ -62,7 +95,7 @@
6295 for (const [key, v] of [...this.#cache].reverse()) {
6396 if (!full) {
6497 const w = v.weight(seen);
65− if (total + w <= VIEW_BUDGET && kept < VIEW_COUNT) {
98+ if (total + w <= this.#budget && kept < VIEW_COUNT) {
6699 total += w;
67100 kept++;
68101 continue;