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 1 --draft perf-cache-older-half --json
JSON
/christopher/abso-core/versions/1?draft=perf-cache-older-half&format=json

Row cache: a hit moves its row to the back only when the row is in the older half (abso 715, for Capacity). Moving on every hit cost about 5 us a read in a 16,384-row Map. A cold collisions call on typescript-rs went from 11.5-13.2 s to 4.3 s (CPU 12.8-15.0 s to 6.1 s), same answer hash a985733e12a5. On abso's table, sync 6.9-8.0 s to 3.8-4.1 s and cold open 1.3-2.0 s to 0.7-0.8 s, all hashes equal. New test in state-cache; state-cache 7/7, engine 296/296, project 375/376 (1 skipped); Biome 2.5.15 clean.

of the draft ·perf·

Row cache: a hit moves its row to the back only when the row is in the older half (abso 715, for Capacity). Moving on every hit cost about 5 us a read in a 16,384-row Map.

A cold collisions call on typescript-rs went from 11.5-13.2 s to 4.3 s (CPU 12.8-15.0 s to 6.1 s), same answer hash a985733e12a5. On abso's table, sync 6.9-8.0 s to 3.8-4.1 s and cold open 1.3-2.0 s to 0.7-0.8 s, all hashes equal. New test in state-cache; state-cache 7/7, engine 296/296, project 375/376 (1 skipped); Biome 2.5.15 clean.

Version 0 · Files at version 1

2 files, 37 lines added, 3 removed.

--- a/src/engine/state-source.ts
+++ b/src/engine/state-source.ts
@@ -18,7 +18,7 @@
1818 * pages, immutable scalars and serialized objects preserve the copy contract. Discard it after an outer host
1919 * rollback or any mutation made through another storage instance.
2020 */
21−type CachedValue = { bytes: number } & (
21+type CachedValue = { bytes: number; at?: number } & (
2222 | { kind: "scalar"; value: string | number | boolean | null | undefined }
2323 | { kind: "numbers"; value: readonly number[] }
2424 | { kind: "json"; json: string }
@@ -30,6 +30,8 @@
3030 readonly #maxBytes: number;
3131 readonly #maxEntries: number;
3232 #bytes = 0;
33+ /** Counts every row put at the back of the cache (abso - Performance, 715): a row's `at` says how far forward it is. */
34+ #tick = 0;
3335 /**
3436 * The keys written or removed in each open transaction, innermost last (abso Capacity). A rollback drops only those:
3537 * every projection scratch scope ends in a rollback, and dropping the whole cache there read the same rows from
@@ -77,8 +79,14 @@
7779 const cacheKey = JSON.stringify([collection, key]);
7880 const hit = this.#cache.get(cacheKey);
7981 if (hit) {
80− this.#cache.delete(cacheKey);
81− this.#cache.set(cacheKey, hit);
82+ // Back to the end only from the older half (abso - Performance, 715): moving on every hit cost about 5 us a
83+ // read in a 16,384-row Map, most of a cold collisions call on typescript-rs. A row in the newer half is
84+ // evicted after every row in the older half anyway.
85+ if (this.#tick - (hit.at ?? 0) > this.#cache.size / 2) {
86+ this.#cache.delete(cacheKey);
87+ this.#cache.set(cacheKey, hit);
88+ hit.at = ++this.#tick;
89+ }
8290 if (hit.kind === "scalar") return hit.value;
8391 if (hit.kind === "numbers") return hit.value.slice();
8492 return (JSON.parse(hit.json) as { value?: unknown }).value;
@@ -118,6 +126,7 @@
118126 if (entry.bytes <= this.#maxEntryBytes && this.#maxEntries > 0) {
119127 while (this.#bytes + entry.bytes > this.#maxBytes || this.#cache.size >= this.#maxEntries)
120128 this.#drop(this.#cache.keys().next().value!);
129+ entry.at = ++this.#tick;
121130 this.#cache.set(cacheKey, entry);
122131 this.#bytes += entry.bytes;
123132 }
--- a/src/engine/test/state-cache.test.ts
+++ b/src/engine/test/state-cache.test.ts
@@ -163,3 +163,28 @@
163163 source.db.close();
164164 }
165165});
167+test("a hit moves a row to the back only from the older half, and eviction still takes the least recently moved row first (abso - Performance, 715)", () => {
168+ const source = new SqlState();
169+ try {
170+ const cache = new CachedStateStorage(source, 1_000_000, 4);
171+ for (const key of ["a", "b", "c", "d", "e", "f"]) source.write("rows", key, key.toUpperCase());
172+ for (const key of ["a", "b", "c", "d"]) assert.equal(cache.read("rows", key), key.toUpperCase());
173+ // a is the oldest of four: read again, it goes to the back. d is the newest: it stays where it is.
174+ assert.equal(cache.read("rows", "a"), "A");
175+ assert.equal(cache.read("rows", "d"), "D");
176+ const reads = source.reads;
177+ // A fifth row pushes out b, the oldest now that a moved.
178+ assert.equal(cache.read("rows", "e"), "E");
179+ assert.equal(source.reads, reads + 1);
180+ assert.equal(cache.read("rows", "a"), "A");
181+ assert.equal(cache.read("rows", "c"), "C");
182+ assert.equal(cache.read("rows", "d"), "D");
183+ assert.equal(source.reads, reads + 1);
184+ assert.equal(cache.read("rows", "b"), "B");
185+ assert.equal(source.reads, reads + 2);
186+ assert.equal(cache.retainedEntries, 4);
187+ } finally {
188+ source.db.close();
189+ }
190+});