Cache successful integer writes within existing storage limits (#16)
of shared·codex-capacity-chair··Not checked
Version 144 · Files at version 145
3 files, 55 lines added, 3 removed.
--- a/src/engine/state-source.ts
+++ b/src/engine/state-source.ts
@@ -88,6 +88,11 @@
8888 const json = JSON.stringify({ value }); 8989 entry = { kind: "json", json, bytes: overhead + 2 * json.length }; 91+ this.#remember(cacheKey, entry); 94+ #remember(cacheKey: string, entry: CachedValue): void { 95+ this.#drop(cacheKey); 9196 // Payload accounting is bounded, not a measurement of actual isolate heap. 9297 if (entry.bytes <= this.#maxBytes && this.#maxEntries > 0) { 9398 while (this.#bytes + entry.bytes > this.#maxBytes || this.#cache.size >= this.#maxEntries) @@ -95,11 +100,15 @@
95100 this.#cache.set(cacheKey, entry); 96101 this.#bytes += entry.bytes; 98− return value;
100104 write(collection: string, key: string, value: unknown): void { 101− this.#drop(JSON.stringify([collection, key]));
105+ const cacheKey = JSON.stringify([collection, key]); 106+ this.#drop(cacheKey); 102107 this.#source.write(collection, key, value); 108+ // Allocation counters are immutable scalars. Keep successful writes within 109+ // the existing cache budget instead of immediately reading them back. 110+ if (typeof value === "number" && Number.isSafeInteger(value) && !Object.is(value, -0)) 111+ this.#remember(cacheKey, { kind: "scalar", value, bytes: 2 * cacheKey.length + 176 }); 104113 remove(collection: string, key: string): void { 105114 this.#drop(JSON.stringify([collection, key])); --- a/src/engine/test/projection-scratch.test.ts
+++ b/src/engine/test/projection-scratch.test.ts
@@ -32,11 +32,12 @@
3333 assert.deepEqual(result, { value: 99, data: new Int32Array([1, 2]) }); 3434 assert.throws(() => escaped!.write("x", "y", true), /ended/); 35+ assert.equal(storage.retainedEntries, 0, "scratch rollback invalidates every cached value"); 3536 assert.equal(storage.read("canonical", "before"), 1); 3637 storage.write("canonical", "after", 2); 3839 assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, 2); 39− assert.equal(storage.retainedEntries, 1);
40+ assert.equal(storage.retainedEntries, 2, "only the canonical read and successful counter write remain"); 4041 assert.equal(storage.read("canonical", "after"), 2); --- a/src/engine/test/state-source.test.ts
+++ b/src/engine/test/state-source.test.ts
@@ -5,6 +5,48 @@
55import { lines, Repo } from "./helpers.ts"; 66import { SqlState } from "./storage-helper.ts"; 8+test("counter writes avoid rereads while respecting failed writes, rollback and cache bounds", () => { 9+ const source = new SqlState(); 11+ const cache = new CachedStateStorage(source, 512, 2); 12+ const before = source.reads; 13+ for (let n = 0; n < 100; n++) { 14+ cache.write("counter", "next", n); 15+ assert.equal(cache.read("counter", "next"), n); 16+ assert.ok(cache.retainedBytes <= 512); 17+ assert.ok(cache.retainedEntries <= 2); 19+ assert.equal(source.reads, before); 20+ source.failWrite = source.writes + 1; 21+ assert.throws(() => cache.write("counter", "next", 100), /injected/); 22+ assert.equal(cache.read("counter", "next"), 99); 23+ source.failWrite = Infinity; 26+ cache.transaction(() => { 27+ cache.write("counter", "next", 101); 28+ assert.equal(cache.read("counter", "next"), 101); 29+ throw Error("counter rollback"); 33+ assert.equal(cache.retainedEntries, 0); 34+ assert.equal(cache.read("counter", "next"), 99); 35+ for (let n = 0; n < 20; n++) cache.write("counter", String(n), n); 36+ assert.ok(cache.retainedBytes <= 512); 37+ assert.ok(cache.retainedEntries <= 2); 38+ assert.equal(cache.read("counter", "0"), 0); 39+ const disabled = new CachedStateStorage(source, 0, 0); 40+ disabled.write("counter", "next", 102); 41+ assert.equal(disabled.retainedEntries, 0); 42+ assert.equal(disabled.read("counter", "next"), 102); 43+ cache.write("counter", "negative-zero", -0); 44+ assert.equal(Object.is(cache.read("counter", "negative-zero"), source.read("counter", "negative-zero")), true); 850test("current visibility stops at the first included deletion while history retains every deleter", () => { 951 class ObservedStorage extends SqlState {