Draft collisions without copying shared's atoms per pair (disk-first 2; abso Capacity). Version 229 of shared· capacity· Oct 15 (2026-10-15 08:53 UTC) · Not checked
On typescript-rs, with one opened writer, collisions ran past 5 minutes with 2.5 million row reads; replay takes 5 s.
Each pair of agents built a new set of all of shared's atoms plus both drafts, and each agent's view of shared-now did the same. On a stored writer that read every one of shared's atoms, per pair, and the view then proved the copy closed by walking it. The pair's set is now a lookup over shared's own atoms and the two drafts (same membership as before). The views are built over shared's view's own closed set plus the drafts, which closes to the same atoms and walks only the drafts. View.lines and buildLines use the texts the packed layout already holds, instead of reading each line's text again. The definition rule takes a line's atom and place from its id, and collisions use graph.lineAtom; graph.line read each line's text as well. On typescript-rs adopted fresh, cold: collisions finishes in 71 s with 299,000 reads, matching replay. Still slow: most of what's left is laying the same files out again for each agent's view.
Named tests: every engine test (288 of 288), changed-definition, collision-streaming, definition-code, definitions, draft-reads-stored, same-line, spelling-clash, review-fixes, membership-pages, stored-closure, packed-crossings, project-writer-storage, writer-adoption, moves, version-files, where-many-agents (67 of 67); tsc and biome clean.
Version 228 · Files at version 229
3 files, 50 lines added, 15 removed.
--- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -859,15 +859,17 @@
859 859 /** Atom-building line handles; text is loaded only when requested. */ 860 860 buildLines(path: string): readonly VisibleLine[] { 861 861 const graph = this.graph; 862 − const vis = this.#file(path).vis;
862 + // Texts the packed layout already holds are not read again (abso Capacity). 863 + const { vis, visText } = this.#file(path); 863 864 const lines: VisibleLine[] = new Array(vis.length); 864 865 for (let i = 0; i < vis.length; i++) { 865 866 const n = vis[i]!; 867 + const packed = visText?.[i]; 867 869 id: graph.lineIdOf(n), 868 870 textChunks: () => graph.textChunks(n), 870 − return graph.textOf(n);
872 + return packed ?? graph.textOf(n); 872 874 atom: graph.idOfSeq(graph.writerSeqOf(n)), @@ -879,11 +881,15 @@
879 881 lines(path: string): readonly VisibleLine[] { 880 882 if (this.#linesCache?.path === path) return this.#linesCache.lines; 881 883 const graph = this.graph; 882 − const vis = this.#file(path).vis;
884 + const { vis, visText } = this.#file(path); 883 885 const lines: VisibleLine[] = new Array(vis.length); 884 886 for (let i = 0; i < vis.length; i++) { 885 887 const n = vis[i]!; 886 − lines[i] = { id: graph.lineIdOf(n), text: graph.textOf(n), atom: graph.idOfSeq(graph.writerSeqOf(n)) };
889 + id: graph.lineIdOf(n), 890 + text: visText?.[i] ?? graph.textOf(n), 891 + atom: graph.idOfSeq(graph.writerSeqOf(n)), 888 894 this.#linesCache = { path, lines }; --- a/worker/project/definition-rule.ts
+++ b/worker/project/definition-rule.ts
@@ -67,9 +67,11 @@
68 68 #replacedNames(line: LineId, base: ReadView, runsOf: Map<AtomId, readonly RunEnd[]>): ReadonlySet<string> { 69 69 const g = this.#graph; 70 − const info = g.line(line);
71 − if (!info) return EMPTY_NAMES;
72 − const atom = g.atom(info.atom);
70 + // The line's atom and place in it from its id (abso Capacity): line() reads its text as well. 71 + if (g.lineN(line) < 0) return EMPTY_NAMES; 72 + const dot = line.indexOf("."); 73 + const atom = g.atom(line.slice(0, dot)); 74 + const k = Number(line.slice(dot + 1)); 73 75 // Where each of the atom's runs ends, worked out once per check (abso issue 158): reading a run's 74 76 // lines can mean reading its text back from the log, so not once per line of a big change. 75 77 let runs = runsOf.get(atom.id); @@ -78,7 +80,7 @@
78 80 runs = atom.inserts.map((run) => ({ end: (k += run.lines.length), parent: run.parent })); 79 81 runsOf.set(atom.id, runs); 81 − const parent = runs.find((r) => info.k < r.end)?.parent;
83 + const parent = runs.find((r) => k < r.end)?.parent; 82 84 if (parent === undefined || parent.startsWith("^") || !g.deletersOf(parent).includes(atom.id)) return EMPTY_NAMES; 83 85 const key = `${atom.id} ${parent}`; 84 86 let names = this.#replacedCache.get(key); @@ -156,7 +158,7 @@
156 158 // Who changed it: newer writers of the new definition's lines, or newer deleters of the old one's. 157 159 const by = new Set<AtomId>(); 158 160 for (const d of after) 159 − for (const line of d.lines) if (newer.has(this.#graph.line(line)!.atom)) by.add(this.#graph.line(line)!.atom);
161 + for (const line of d.lines) if (newer.has(this.#graph.lineAtom(line)!)) by.add(this.#graph.lineAtom(line)!); 160 162 for (const d of before) 161 163 for (const line of d.lines) for (const x of this.#graph.deletersOf(line)) if (newer.has(x)) by.add(x); 162 164 const first = [...by].sort((p, q) => this.#graph.atom(p).seq - this.#graph.atom(q).seq)[0]; --- a/worker/project/draft-collisions.ts
+++ b/worker/project/draft-collisions.ts
@@ -2,6 +2,7 @@
2 2 // is the project in memory. 4 4 import { type AtomId, type Graph, parseKey, type ReadView } from "../../src/engine/index.ts"; 5 + import { StableSet, unionOverlay } from "../../src/engine/state-source.ts"; 5 6 import { claimantsAmong, describeSide, fileOfKey, pairsFrom } from "./collisions.ts"; 6 7 import type { DefinitionRule } from "./definition-rule.ts"; 7 8 import { type Definition, definedTwice } from "./definitions.ts"; @@ -37,6 +38,29 @@
37 38 * top of a file (refused once the second syncs onto the first's work). Exact for shared at `version`; as shared moves on, so can 38 39 * the answer, as a real publish's would. `file` or `folder` narrows it. 42 + * A set and some drafts' atoms, by lookup (abso Capacity): copying shared's atoms into a new set read every one of them 43 + * from disk on a stored writer, once per pair of agents. 45 + class WithDrafts extends StableSet<AtomId> { 46 + readonly retainedBytes: number; 47 + readonly #base: ReadonlySet<AtomId>; 48 + readonly #drafts: ReadonlySet<AtomId>; 49 + constructor(base: ReadonlySet<AtomId>, drafts: Iterable<AtomId>) { 52 + this.#drafts = new Set(drafts); 53 + this.retainedBytes = 64 + this.#drafts.size * 32; 55 + has(atom: AtomId): boolean { 56 + return this.#drafts.has(atom) || this.#base.has(atom); 58 + *values(): SetIterator<AtomId> { 60 + for (const atom of this.#drafts) if (!this.#base.has(atom)) yield atom; 40 64 export function draftCollisions( 41 65 host: DraftCollisionHost, 42 66 options: { file?: string; folder?: string } = {}, @@ -71,11 +95,14 @@
71 95 if (!set) draftSets.set(agent, (set = new Set(host.draft(agent)))); 74 − // Shared now with each agent's draft on top, once per agent: the key of a view of a set lists every atom in it.
98 + // Shared now with each agent's draft on top, once per agent. Over shared's view's own set (abso Capacity): a view's set 99 + // is closed under dependencies anyway, so shared's closed set and the drafts lay out what shared's atoms and the drafts 100 + // do, and only the drafts' atoms are walked to close it. 101 + const withDraftsView = (...drafts: readonly AtomId[][]) => host.view(unionOverlay(sharedView.set, drafts.flat())); 75 102 const nowViews = new Map<Agent, ReadView>(); 76 103 const nowViewOf = (agent: Agent) => { 77 104 let view = nowViews.get(agent); 78 − if (!view) nowViews.set(agent, (view = host.view(new Set([...shared, ...host.draft(agent)]))));
105 + if (!view) nowViews.set(agent, (view = withDraftsView(host.draft(agent)))); 81 108 // Each agent's touched files once, not once per pair (abso Capacity). @@ -93,7 +120,7 @@
93 120 const draftA = host.draft(a); 94 121 const draftB = host.draft(b); 95 122 const newer = draftSet(a); 96 − const union = new Set([...shared, ...draftA, ...draftB]);
123 + const union = new WithDrafts(shared, [...draftA, ...draftB]); 97 124 const viewA = host.agentView(a); 98 125 const viewB = host.agentView(b); @@ -134,15 +161,15 @@
134 161 const mineB = new Set(draftB); 135 162 const filesA = touchedBy(a); 136 163 for (const file of [...touchedBy(b)].filter((f) => filesA.has(f)).toSorted()) { 137 − both ??= host.view(union);
164 + both ??= withDraftsView(draftA, draftB); 138 165 if (!both.hasFile(file)) continue; 139 166 for (const [name, defs] of definedTwice(both, file)) { 140 − const by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.line(l)!.atom)));
167 + const by = (mine: Set<AtomId>) => defs.find((d) => d.lines.some((l) => mine.has(host.graph.lineAtom(l)!))); 141 168 const dA = by(mineA); 142 169 const dB = by(mineB); 143 170 if (!dA || !dB || dA === dB) continue; 144 171 const atomsOf = (d: Definition, mine: Set<AtomId>) => 145 − d.lines.map((l) => host.graph.line(l)!.atom).filter((x) => mine.has(x));
172 + d.lines.map((l) => host.graph.lineAtom(l)!).filter((x) => mine.has(x)); 146 173 if (atomsOf(dA, mineA).some((x) => atomsOf(dB, mineB).some((y) => paired.has(`${x} ${y}`)))) continue; 148 175 kind: "duplicate",