Bound repeated indexed view membership lookups
of shared·codex-capacity-chair··Not checked
Version 115 · Files at version 116
2 files, 38 lines added, 1 removed.
--- a/src/engine/test/view.test.ts
+++ b/src/engine/test/view.test.ts
@@ -8,6 +8,21 @@
88import { lines, Repo } from "./helpers.ts"; 1010describe("View", () => { 11+ it("reuses indexed membership across a long run of lines without a history-sized memo", () => { 12+ const repo = Repo.init({ "long.txt": "line\n".repeat(1000) }); 13+ class CountingSet extends StableOverlaySet<AtomId> { 15+ override has(id: AtomId): boolean { 17+ return super.has(id); 20+ const set = new CountingSet(["a0"]); 21+ const view = new View(repo.graph, set); 23+ assert.equal(view.text("long.txt"), "line\n".repeat(1000)); 24+ assert.ok(set.calls <= 2, `membership was queried ${set.calls} times for one atom`); 1126 it("immutable indexed membership avoids a history-sized bitmap and remains a snapshot", () => { 1227 class RangeMembership extends StableSet<AtomId> { 1328 readonly retainedBytes = 64; @@ -41,6 +56,17 @@
4156 assert.equal(first.set.has("a80"), false); 4257 assert.throws(() => new View(repo.graph, new StableOverlaySet<AtomId>(["a1"])), /not closed/); 59+ it("membership memo collisions preserve both included and excluded historical atoms", () => { 60+ const repo = Repo.init({ "f.txt": "0\n" }); 61+ for (let i = 1; i <= 257; i++) repo.edit({ edits: [{ file: "f.txt", old: `${i - 1}\n`, new: `${i}\n` }] }); 62+ const old = new View(repo.graph, new StableOverlaySet<AtomId>(["a0"])); 63+ assert.equal(old.text("f.txt"), "0\n"); 65+ old.sequence("f.txt").map((line) => line.text), 68+ assert.equal(repo.view().text("f.txt"), "257\n"); 4470 it("keeps deleted lines as tombstones in the full sequence", () => { 4571 const repo = Repo.init({ "f.txt": lines("one", "two", "three") }); 4672 const del = repo.edit({ edits: [{ file: "f.txt", old: "two\n", new: "" }] }); --- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -95,6 +95,9 @@
9595 /** The set, made from #in the first time it's asked for: a view keeps a byte per atom, not a copy of the set (#46). */ 9696 #set: AtomSet | undefined; 9797 readonly #stable: StableSet<AtomId> | undefined; 98+ /** Fixed-size memo for immutable indexed membership, independent of history size. */ 99+ #membershipSeq = new Float64Array(256).fill(-1); 100+ #membershipValue = new Uint8Array(256); 98101 #files = new Map<string, FileState>(); 99102 /** Every file the set wrote in, sorted; worked out the first time it's asked for. */ 100103 #knownList: string[] | undefined; @@ -165,7 +168,14 @@
166169 /** Whether the atom with this seq is in the set. */ 167170 #has(seq: number): boolean { 168− if (this.#stable) return this.#stable.has(this.graph.idOfSeq(seq));
172+ const slot = seq % this.#membershipSeq.length; 173+ if (this.#membershipSeq[slot] === seq) return this.#membershipValue[slot] === 1; 174+ const present = this.#stable.has(this.graph.idOfSeq(seq)); 175+ this.#membershipSeq[slot] = seq; 176+ this.#membershipValue[slot] = present ? 1 : 0; 169179 return seq < this.#in.length && this.#in[seq] === 1; @@ -498,6 +508,7 @@
499509 weight(): number { 500510 let n = this.#in.byteLength; 511+ n += 2 * ARRAY_HEADER_BYTES + this.#membershipSeq.byteLength + this.#membershipValue.byteLength; 501512 n += ARRAY_HEADER_BYTES + REFERENCE_BYTES * this.#movers.length; 502513 n += MAP_HEADER_BYTES; // #files exists from construction. 503514 n += MAP_HEADER_BYTES; // #fileOf exists from construction.