--- a/worker/project/definition-rule.ts
+++ b/worker/project/definition-rule.ts
@@ -7,6 +7,7 @@
77import { type AtomId, type Graph, joinLines, type LineId, type ReadView } from "../../src/engine/index.ts"; 88import { type Origin, touchedFiles } from "./collisions.ts"; 99import { type Definition, definitionsIn, importsOf, usedNames } from "./definitions.ts"; 10+import { definitionsBytes, type KeptParts, usesBytes } from "./kept-parts.ts"; 1011import { codeOfLines, declaredIn, holdsNoCode } from "./names.ts"; 1112import { definitionText } from "./news.ts"; 1213import type { Collision } from "./project.ts"; @@ -109,6 +110,8 @@
109110 * in the text of the name's definitions (see definitions.ts), including one 110111 * that disappeared; a use that imports the name, or sits in a file that 111112 * defines it, only counts that definition. `nowView` may be a function, called only when the draft uses a name. 113+ * With `kept` (draft collisions, abso Capacity), the draft's uses and the newer work's definitions are kept between 114+ * calls under keys that name everything they depend on. 114117 draft: ReadonlySet<AtomId>, @@ -117,26 +120,21 @@
117120 nowView: ReadView | (() => ReadView), 118121 newer: ReadonlySet<AtomId>, 119122 spaceName: string, 123+ kept?: { parts: KeptParts; uses: string; changed: string }, 120124 ): DefinitionCollision[] { 121125 if (draft.size === 0 || newer.size === 0) return []; 122− let kept = this.#usesCache.get(draft);
123− if (!kept || kept.view !== view || kept.base !== baseView) {
124− kept = { view, base: baseView, uses: this.#usesOf(draft, view, baseView) };
125− this.#usesCache.set(draft, kept);
126− }
127− const uses = kept.uses;
127+ ? kept.parts.get(`uses ${kept.uses}`, () => this.#usesOf(draft, view, baseView), usesBytes) 128+ : this.#usesFor(draft, view, baseView); 128129 if (uses.size === 0) return []; 129− const now = typeof nowView === "function" ? nowView() : nowView;
130−
131− // A definition can only have changed in a file the newer work touched.
132− let changed = this.#changedCache.get(newer);
133− if (!changed || changed.base !== baseView || changed.now !== now) {
134− const touched = touchedFiles(this.#graph, newer, [baseView, now]);
135− const defs = { baseDefs: definitionsIn(baseView, touched), nowDefs: definitionsIn(now, touched) };
136− changed = { base: baseView, now, defs };
137− this.#changedCache.set(newer, changed);
138− }
139− const { baseDefs, nowDefs } = changed.defs;
130+ const nowOf = () => (typeof nowView === "function" ? nowView() : nowView); 131+ const { baseDefs, nowDefs } = kept 133+ `changed ${kept.changed}`, 134+ () => this.#changed(newer, baseView, nowOf()), 135+ (c) => definitionsBytes(c.baseDefs) + definitionsBytes(c.nowDefs), 137+ : this.#changedFor(newer, baseView, nowOf()); 140138 const out: DefinitionCollision[] = []; 141139 for (const [name, where] of [...uses].sort(([a], [b]) => (a < b ? -1 : 1))) { 142140 // Which definitions the uses mean: the file each use imports the name @@ -193,6 +191,32 @@
194+ /** The draft's uses, kept while it is asked about with the same views. */ 195+ #usesFor(draft: ReadonlySet<AtomId>, view: ReadView, baseView: ReadView): Uses { 196+ let kept = this.#usesCache.get(draft); 197+ if (!kept || kept.view !== view || kept.base !== baseView) { 198+ kept = { view, base: baseView, uses: this.#usesOf(draft, view, baseView) }; 199+ this.#usesCache.set(draft, kept); 204+ /** The newer work's definitions before and after, kept while it is asked about with the same views. */ 205+ #changedFor(newer: ReadonlySet<AtomId>, baseView: ReadView, now: ReadView): Changed { 206+ let changed = this.#changedCache.get(newer); 207+ if (!changed || changed.base !== baseView || changed.now !== now) { 208+ changed = { base: baseView, now, defs: this.#changed(newer, baseView, now) }; 209+ this.#changedCache.set(newer, changed); 211+ return changed.defs; 214+ /** Definitions in the files the newer work touched, before it and with it: only there can one have changed. */ 215+ #changed(newer: ReadonlySet<AtomId>, baseView: ReadView, now: ReadView): Changed { 216+ const touched = touchedFiles(this.#graph, newer, [baseView, now]); 217+ return { baseDefs: definitionsIn(baseView, touched), nowDefs: definitionsIn(now, touched) }; 196220 /** The names the draft's lines use and don't declare, less those the lines they replaced already used. */ 197221 #usesOf(draft: ReadonlySet<AtomId>, view: ReadView, baseView: ReadView): Uses { 198222 const uses = new Map<string, { file: string; index: number }[]>(); --- a/worker/project/draft-collisions.ts
+++ b/worker/project/draft-collisions.ts
@@ -6,6 +6,7 @@
66import { claimantsAmong, describeSide, fileOfKey, pairsFrom } from "./collisions.ts"; 77import type { DefinitionRule } from "./definition-rule.ts"; 88import { type Definition, definedTwice } from "./definitions.ts"; 9+import { definitionsBytes, type KeptParts } from "./kept-parts.ts"; 910import { pathKey } from "./pathkey.ts"; 1011import type { CollisionSide, DraftCollision } from "./project-types.ts"; 1112import { last, SHARED, type Space } from "./spaces.ts"; @@ -28,6 +29,10 @@
2829 /** Shared now with these agents' drafts on top. */ 2930 draftsView(agents: readonly Agent[]): ReadView; 3031 touchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>; 32+ /** What collisions keep between calls (kept-parts.ts), when the project keeps any. */ 33+ keptParts?(): KeptParts | undefined; 34+ /** A key naming everything a draft's part of the answer depends on: shared's version, its base and its atoms. */ 35+ draftKey(agent: Agent): string; @@ -99,6 +104,16 @@
99104 // Shared now with each agent's draft on top (project.ts, draftsView): kept between calls while shared and the 100105 // draft stay the same, and laid out as shared is for every file the draft doesn't touch (abso Capacity). 101106 const nowViewOf = (agent: Agent) => host.draftsView([agent]); 107+ // Each draft's uses and changed definitions, and each pair's names defined twice, kept between calls (kept-parts.ts, 108+ // abso Capacity): what this call doesn't use is dropped at its end. 109+ const parts = host.keptParts?.(); 110+ const keys = new Map<Agent, string>(); 111+ const keyOf = (agent: Agent) => { 112+ let key = keys.get(agent); 113+ if (key === undefined) keys.set(agent, (key = host.draftKey(agent))); 116+ parts?.startRound(); 102117 // Each agent's touched files once, not once per pair (abso Capacity). 103118 const touched = new Map<Agent, Set<string>>(); 104119 const touchedBy = (agent: Agent) => { @@ -154,10 +169,15 @@
154169 const mineA = new Set(draftA); 155170 const mineB = new Set(draftB); 156171 const filesA = touchedBy(a); 172+ const twiceIn = (file: string): ReadonlyMap<string, Definition[]> => { 173+ const make = () => { 174+ both ??= host.draftsView([a, b]); 175+ return both.hasFile(file) ? definedTwice(both, file) : new Map<string, Definition[]>(); 177+ return parts ? parts.get(`twice ${keyOf(a)} | ${keyOf(b)} ${file}`, make, definitionsBytes) : make(); 157179 for (const file of [...touchedBy(b)].filter((f) => filesA.has(f)).toSorted()) { 158− both ??= host.draftsView([a, b]);
159− if (!both.hasFile(file)) continue;
160− for (const [name, defs] of definedTwice(both, file)) {
180+ for (const [name, defs] of twiceIn(file)) { 161181 const by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.lineAtom(l)!))); 162182 const dA = by(mineA); 163183 const dB = by(mineB); @@ -177,7 +197,8 @@
178198 // Definitions: b's new lines use a name whose definition a's changes change. 179199 const nowView = () => nowViewOf(a); 180− for (const d of host.definitionRule().collisions(draftSet(b), viewB, sharedView, nowView, newer, SHARED)) {
200+ const kept = parts ? { parts, uses: keyOf(b), changed: keyOf(a) } : undefined; 201+ for (const d of host.definitionRule().collisions(draftSet(b), viewB, sharedView, nowView, newer, SHARED, kept)) { 181202 const c = d.collision; 183204 kind: "definition", @@ -189,6 +210,7 @@
192214 const fileKey = options.file !== undefined ? pathKey(options.file) : undefined; 193215 const folderParts = 194216 options.folder !== undefined ? options.folder.split("/").filter((s) => s !== "" && s !== ".") : undefined;