Avoid full-base membership copies during collision checks
of shared·codex-capacity-chair··Not checked
Version 141 · Files at version 142
3 files, 44 lines added, 6 removed.
--- a/worker/project/collisions.ts
+++ b/worker/project/collisions.ts
@@ -12,6 +12,7 @@
1414} from "../../src/engine/index.ts"; 15+import { differenceSet, StableOverlaySet } from "../../src/engine/state-source.ts"; 1516import { definedTwice } from "./definitions.ts"; 1617import type { AgentState, Collision, CollisionSide } from "./project-types.ts"; 1718import { SHARED, type Space } from "./spaces.ts"; @@ -235,7 +236,7 @@
235236 readonly spaces: ReadonlyMap<string, Space>; 236237 version(): number; 237238 spaceOf(agent: AgentState): Space; 238− agentSet(agent: AgentState): Set<AtomId>;
239+ agentSet(agent: AgentState): ReadonlySet<AtomId>; 239240 draft(agent: AgentState): AtomId[]; 240241 agentView(agent: AgentState): View; 241242 at(space: Space, version: number): ReadonlySet<AtomId>; @@ -305,11 +306,11 @@
305306export function openPairs(host: CollisionHost, agent: AgentState): Pair[] { 306307 const space = host.spaceOf(agent); 307308 const set = host.agentSet(agent); 308− const mine = new Set(agent.mine);
309+ const mine = new StableOverlaySet(agent.mine); 309310 const out: Pair[] = []; 310311 const draft = host.draft(agent); 311312 if (draft.length > 0) { 312− const seen = new Set([...host.at(space, agent.base)].filter((id) => !mine.has(id)));
313+ const seen = differenceSet(host.at(space, agent.base), mine); 313314 out.push(...pairsIn(host.graph, draft, seen, set)); 315316 if (space.name !== SHARED) { --- a/worker/project/project.ts
+++ b/worker/project/project.ts
@@ -22,6 +22,7 @@
2526} from "../../src/engine/state-source.ts"; 2627import { type AddDraftHost, addDraft as hostedAddDraft } from "./add-draft.ts"; 2728import { type AmendHost, amend as hostedAmend } from "./amend.ts"; @@ -1773,7 +1774,7 @@
17731774 spaces: this.#spaces, 17741775 version: () => this.version, 17751776 spaceOf: (agent) => this.#spaceOf(agent), 1776− agentSet: (agent) => this.#agentSet(agent),
1777+ agentSet: (agent) => unionOverlay(this.#at(this.#spaceOf(agent), agent.base), agent.mine), 17771778 draft: (agent) => this.#draft(agent), 17781779 agentView: (agent) => this.#agentView(agent), 17791780 at: (space, version) => this.#at(space, version), --- a/worker/project/test/collision-streaming.test.ts
+++ b/worker/project/test/collision-streaming.test.ts
@@ -1,9 +1,11 @@
11import assert from "node:assert/strict"; 22import { test } from "node:test"; 33import { buildBase, Graph } from "../../../src/engine/index.ts"; 4−import { CachedStateStorage } from "../../../src/engine/state-source.ts";
4+import { CachedStateStorage, StableOverlaySet, unionOverlay } from "../../../src/engine/state-source.ts"; 55import { SqlState } from "../../../src/engine/test/storage-helper.ts"; 6−import { pairsIn } from "../collisions.ts";
6+import { type CollisionHost, openPairs, pairsIn } from "../collisions.ts"; 7+import type { AgentState } from "../project-types.ts"; 8+import { newSpace } from "../spaces.ts"; 810test("persistent conflict candidates and resolver intersections stream in exact canonical order", (t) => { 911 const source = new SqlState(); @@ -67,6 +69,40 @@
6870 const selected = new Set(["a1", "a257", "a513"]); 6971 assert.deepEqual(pairsIn(reopened, [draft], selected, settled), []); 72+ const base = new StableOverlaySet(against); 73+ t.mock.method(base, "values", () => { 74+ throw Error("enumerating the complete base membership is forbidden"); 76+ const actor: AgentState = { 88+ const space = newSpace(""); 89+ const host: CollisionHost = { 91+ spaces: new Map([["", space]]), 93+ spaceOf: () => space, 94+ agentSet: (agent) => unionOverlay(base, agent.mine), 95+ draft: () => [draft], 99+ throw Error("open pairs must not build a View"); 103+ openPairs(host, actor).map((pair) => pair.theirs), 104+ [...against].filter((id) => id !== "a1"),