Avoid full membership enumeration for indexed view cache keys
of shared·codex-capacity-chair··Not checked
Version 116 · Files at version 117
2 files, 36 lines added, 3 removed.
--- a/worker/project/test/view-cache.test.ts
+++ b/worker/project/test/view-cache.test.ts
@@ -1,10 +1,34 @@
11import assert from "node:assert/strict"; 22import { test } from "node:test"; 33import { buildBase, Graph, View } from "../../../src/engine/index.ts"; 4+import { StableOverlaySet } from "../../../src/engine/state-source.ts"; 5+import type { AtomId } from "../../../src/engine/types.ts"; 46import { ViewCache } from "../views.ts"; 68const VIEW_BUDGET = 24_000_000; 10+test("indexed view cache keys never enumerate membership and remain bounded by eviction", () => { 11+ class Counted extends StableOverlaySet<AtomId> { 13+ override values(): SetIterator<AtomId> { 15+ return super.values(); 18+ const graph = new Graph(); 19+ graph.add(buildBase({ "a.txt": "one\n" }, { author: "test", message: "base" })); 20+ const cache = new ViewCache(graph); 21+ const set = new Counted(["a0"]); 22+ const view = cache.view(set); 23+ assert.equal(set.iterations, 1, "only View closure validation enumerates membership"); 24+ assert.equal(cache.view(set), view); 25+ assert.equal(set.iterations, 1, "cache hit must not construct a full-history key"); 26+ assert.equal(view.text("a.txt"), "one\n"); 27+ for (let i = 0; i < 33; i++) cache.view(new Counted(["a0"])); 28+ assert.notEqual(cache.view(set), view, "indexed identities obey the same cache eviction limit"); 29+ assert.equal(set.iterations, 2); 832type WeightControls = { 933 setIncomingWeights(values: number[]): void; 1034 setWeight(view: View, weight: number): void; --- a/worker/project/views.ts
+++ b/worker/project/views.ts
@@ -1,7 +1,9 @@
11// The views a project lays out from sets of atoms, cached under a byte budget (#46, disk-first 1). Published 2−// versions never change, so their views are kept by name; any other set is kept by its exact contents.
2+// versions never change, so their views are kept by name. Immutable indexed sets use their identity; 3+// ordinary in-memory sets use their exact contents. 45import { type AtomId, type Graph, View } from "../../src/engine/index.ts"; 6+import { StableSet } from "../../src/engine/state-source.ts"; 79 * About how many bytes the cached views may hold together (#46). A laid-out @@ -13,7 +15,7 @@
1315/** Views by key, most recently used last, held under VIEW_BUDGET bytes and 32 views. */ 1416export class ViewCache { 1517 readonly #graph: Graph; 16− readonly #cache = new Map<string, View>();
18+ readonly #cache = new Map<string | StableSet<AtomId>, View>(); 1820 constructor(graph: Graph) { 1921 this.#graph = graph; @@ -34,8 +36,11 @@
37− /** A view of a set, cached by the set's exact contents. */
39+ /** Indexed snapshots use identity; ordinary sets use their exact contents. */ 3840 view(set: ReadonlySet<AtomId>): View { 41+ // Immutable indexed membership is already a stable identity. Building 42+ // a sorted history-sized key defeats its external-storage contract. 43+ if (set instanceof StableSet) return this.#get(set, () => set); 3944 const key = [...set] 4045 .map((id) => this.#graph.atom(id).seq) 4146 .sort((a, b) => a - b) @@ -45,6 +50,10 @@
4651 /** A view cached under `key`, made from `set()` when it isn't there. */ 4752 keyed(key: string, set: () => ReadonlySet<AtomId>): View { 53+ return this.#get(key, set); 56+ #get(key: string | StableSet<AtomId>, set: () => ReadonlySet<AtomId>): View { 4857 let view = this.#cache.get(key); 5059 // Most recently used last, so eviction takes the oldest.