Avoid JSON decoding for bounded numeric cache pages and scalar metadata
of shared·codex-capacity-chair··Not checked
Version 135 · Files at version 136
2 files, 87 lines added, 11 removed.
--- a/src/engine/state-source.ts
+++ b/src/engine/state-source.ts
@@ -14,13 +14,19 @@
17− * Operation-local read cache with fixed payload and entry limits. Serialized
18− * values preserve the storage copy contract. Discard it after an outer host
17+ * Operation-local read cache with fixed payload and entry limits. Private numeric 18+ * 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. 21+type CachedValue = { bytes: number } & ( 22+ | { kind: "scalar"; value: string | number | boolean | null | undefined } 23+ | { kind: "numbers"; value: readonly number[] } 24+ | { kind: "json"; json: string } 2127export class CachedStateStorage implements GraphStateStorage { 2228 readonly #source: GraphStateStorage; 23− readonly #cache = new Map<string, { json: string; bytes: number }>();
29+ readonly #cache = new Map<string, CachedValue>(); 2430 readonly #maxBytes: number; 2531 readonly #maxEntries: number; @@ -54,18 +60,40 @@
5561 this.#cache.delete(cacheKey); 5662 this.#cache.set(cacheKey, hit); 63+ if (hit.kind === "scalar") return hit.value; 64+ if (hit.kind === "numbers") return hit.value.slice(); 5765 return (JSON.parse(hit.json) as { value?: unknown }).value; 5967 const value = this.#source.read(collection, key); 60− const json = JSON.stringify({ value });
61− // Charge two bytes per UTF-16 unit and a conservative fixed entry cost.
62− // This bounds retained payload; it is not an isolate heap measurement.
63− const bytes = 2 * (json.length + cacheKey.length) + 128;
64− if (bytes <= this.#maxBytes && this.#maxEntries > 0) {
65− while (this.#bytes + bytes > this.#maxBytes || this.#cache.size >= this.#maxEntries)
68+ const overhead = 2 * cacheKey.length + 160; 69+ let entry: CachedValue; 71+ value === undefined || 73+ typeof value === "string" || 74+ typeof value === "boolean" || 75+ (typeof value === "number" && Number.isFinite(value)) 77+ entry = { kind: "scalar", value, bytes: overhead + (typeof value === "string" ? 2 * value.length : 16) }; 79+ Array.isArray(value) && 80+ value.length <= 256 && 81+ Object.keys(value).length === value.length && 82+ value.every(Number.isSafeInteger) 84+ // Store one private, bounded numeric page. Public reads still receive a 85+ // fresh copy; neither the source result nor a reader can mutate the cache. 86+ entry = { kind: "numbers", value: value.slice(), bytes: overhead + 32 + value.length * 8 }; 88+ const json = JSON.stringify({ value }); 89+ entry = { kind: "json", json, bytes: overhead + 2 * json.length }; 91+ // Payload accounting is bounded, not a measurement of actual isolate heap. 92+ if (entry.bytes <= this.#maxBytes && this.#maxEntries > 0) { 93+ while (this.#bytes + entry.bytes > this.#maxBytes || this.#cache.size >= this.#maxEntries) 6694 this.#drop(this.#cache.keys().next().value!); 67− this.#cache.set(cacheKey, { json, bytes });
68− this.#bytes += bytes;
95+ this.#cache.set(cacheKey, entry); 96+ this.#bytes += entry.bytes; --- a/src/engine/test/state-cache.test.ts
+++ b/src/engine/test/state-cache.test.ts
@@ -3,6 +3,54 @@
33import { CachedStateStorage } from "../state-source.ts"; 44import { SqlState } from "./storage-helper.ts"; 6+test("numeric cache pages are detached on first and repeated reads, and rollback invalidates them", () => { 7+ const source = new SqlState(); 9+ const cache = new CachedStateStorage(source, 4096, 4); 10+ cache.write("numbers", "page", [1, 2, 3]); 11+ (cache.read("numbers", "page") as number[])[0] = 99; 12+ assert.deepEqual(cache.read("numbers", "page"), [1, 2, 3]); 13+ (cache.read("numbers", "page") as number[]).push(4); 14+ assert.deepEqual(cache.read("numbers", "page"), [1, 2, 3]); 17+ cache.transaction(() => { 18+ cache.write("numbers", "page", [8]); 19+ assert.deepEqual(cache.read("numbers", "page"), [8]); 20+ throw new Error("rollback numeric page"); 22+ /rollback numeric page/, 24+ assert.equal(cache.retainedBytes, 0); 25+ assert.deepEqual(cache.read("numbers", "page"), [1, 2, 3]); 26+ cache.remove("numbers", "page"); 27+ assert.equal(cache.read("numbers", "page"), undefined); 33+test("cached scalar metadata and bounded numeric pages need no JSON decode on hits", (t) => { 34+ const source = new SqlState(); 36+ const cache = new CachedStateStorage(source, 8192, 8); 37+ const values = [undefined, null, true, "metadata", 42, Array.from({ length: 256 }, (_, n) => n)]; 38+ for (let i = 0; i < values.length; i++) { 39+ cache.write("rows", String(i), values[i]); 40+ cache.read("rows", String(i)); 42+ const reads = source.reads; 43+ t.mock.method(JSON, "parse", () => { 44+ throw new Error("cache hit unexpectedly decoded JSON"); 46+ for (let i = 0; i < values.length; i++) assert.deepEqual(cache.read("rows", String(i)), values[i]); 47+ assert.equal(source.reads, reads); 48+ assert.ok(cache.retainedBytes <= 8192); 654test("bounded writer cache copies values, invalidates writes and clears after rollback", () => { 755 const source = new SqlState(); 856 const cache = new CachedStateStorage(source, 1024, 3);