tla-ts 0.0.3-dev.763cc71 → 0.0.3-dev.9c2dda8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +12 -12
- package/dist/run-cli.js +13 -13
- package/dist/transfer.cjs +10 -10
- package/dist/transfer.js +10 -10
- package/package.json +1 -1
- package/src/fingerprints.ts +46 -11
- package/src/lexer.ts +5 -1
- package/src/parallel-worker.ts +158 -221
- package/src/parallel.ts +58 -33
- package/src/symmetry.ts +14 -2
- package/src/values.ts +7 -4
package/package.json
CHANGED
package/src/fingerprints.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { getValueFingerprint, getValueHash, type Value } from "./values";
|
|
2
2
|
|
|
3
3
|
const EMPTY = 0;
|
|
4
|
-
const TOMBSTONE = 1;
|
|
5
4
|
const INITIAL_CAPACITY = 1 << 12;
|
|
6
5
|
|
|
7
6
|
interface FingerprintTable {
|
|
8
7
|
insert: (first: number, second: number) => boolean;
|
|
9
|
-
remove: (first: number, second: number) => void;
|
|
10
8
|
}
|
|
11
9
|
|
|
12
10
|
const mix = (hash: number) => {
|
|
@@ -26,15 +24,13 @@ export const writeStateFingerprint = (state: Value[], fingerprint: Int32Array) =
|
|
|
26
24
|
fingerprint[1] = mix(second);
|
|
27
25
|
};
|
|
28
26
|
|
|
29
|
-
export const getFingerprintOwner = (second: number, count: number) => (second >>> 0) % count;
|
|
30
|
-
|
|
31
27
|
export const createFingerprintTable = (): FingerprintTable => {
|
|
32
28
|
const table = { slots: new Int32Array(INITIAL_CAPACITY * 2), used: 0 };
|
|
33
29
|
const findSlot = (slots: Int32Array, first: number, second: number) => {
|
|
34
30
|
const mask = slots.length / 2 - 1;
|
|
35
31
|
for (let index = first & mask; ; index = (index + 1) & mask) {
|
|
36
32
|
const stored = slots[index * 2];
|
|
37
|
-
if (stored === EMPTY
|
|
33
|
+
if (stored === EMPTY) return index;
|
|
38
34
|
if (stored === first && slots[index * 2 + 1] === second) return index;
|
|
39
35
|
}
|
|
40
36
|
};
|
|
@@ -59,11 +55,50 @@ export const createFingerprintTable = (): FingerprintTable => {
|
|
|
59
55
|
table.used++;
|
|
60
56
|
return true;
|
|
61
57
|
},
|
|
62
|
-
remove: (first, second) => {
|
|
63
|
-
const slot = findSlot(table.slots, first, second);
|
|
64
|
-
if (table.slots[slot * 2] !== first) return;
|
|
65
|
-
table.slots[slot * 2] = EMPTY;
|
|
66
|
-
table.slots[slot * 2 + 1] = TOMBSTONE;
|
|
67
|
-
},
|
|
68
58
|
};
|
|
69
59
|
};
|
|
60
|
+
|
|
61
|
+
// Workers share one table of 64-bit fingerprints. The first word is never 0, so 0 marks an empty slot.
|
|
62
|
+
export interface SharedFingerprints {
|
|
63
|
+
slots: BigInt64Array;
|
|
64
|
+
used: Int32Array;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const INITIAL_SHARED_CAPACITY = 1 << 10;
|
|
68
|
+
|
|
69
|
+
const insertSharedKey = (slots: BigInt64Array, key: bigint) => {
|
|
70
|
+
const mask = slots.length - 1;
|
|
71
|
+
for (let index = Number(key >> 32n) & mask; ; index = (index + 1) & mask) {
|
|
72
|
+
const stored = Atomics.load(slots, index);
|
|
73
|
+
if (stored === key) return false;
|
|
74
|
+
if (stored !== 0n) continue;
|
|
75
|
+
const previous = Atomics.compareExchange(slots, index, 0n, key);
|
|
76
|
+
if (previous === 0n) return true;
|
|
77
|
+
if (previous === key) return false;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const createSharedFingerprints = (): SharedFingerprints => ({
|
|
82
|
+
slots: new BigInt64Array(new SharedArrayBuffer(INITIAL_SHARED_CAPACITY * 8)),
|
|
83
|
+
used: new Int32Array(new SharedArrayBuffer(4)),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
export const insertSharedFingerprint = (
|
|
87
|
+
table: SharedFingerprints,
|
|
88
|
+
first: number,
|
|
89
|
+
second: number,
|
|
90
|
+
) => {
|
|
91
|
+
const key = BigInt.asIntN(64, (BigInt(first) << 32n) | BigInt(second >>> 0));
|
|
92
|
+
if (!insertSharedKey(table.slots, key)) return false;
|
|
93
|
+
Atomics.add(table.used, 0, 1);
|
|
94
|
+
return true;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const isSharedFingerprintsFull = (table: SharedFingerprints) =>
|
|
98
|
+
Atomics.load(table.used, 0) * 2 >= table.slots.length;
|
|
99
|
+
|
|
100
|
+
export const growSharedFingerprints = (table: SharedFingerprints): SharedFingerprints => {
|
|
101
|
+
const slots = new BigInt64Array(new SharedArrayBuffer(table.slots.byteLength * 2));
|
|
102
|
+
for (const key of table.slots) if (key !== 0n) insertSharedKey(slots, key);
|
|
103
|
+
return { slots, used: table.used };
|
|
104
|
+
};
|
package/src/lexer.ts
CHANGED
|
@@ -345,6 +345,8 @@ export const tokenize = (source: string): Token[] => {
|
|
|
345
345
|
if (!headerMatch)
|
|
346
346
|
throw new TlaSyntaxError("Expected a module header such as ---- MODULE Name ----");
|
|
347
347
|
const tokens: Token[] = [];
|
|
348
|
+
// Each name is one string object, so looking a name up in a scope compares pointers.
|
|
349
|
+
const names = new Map<string, string>();
|
|
348
350
|
const prefixLines = source.slice(0, headerMatch.index).split("\n");
|
|
349
351
|
const cursor = {
|
|
350
352
|
index: headerMatch.index,
|
|
@@ -481,7 +483,9 @@ export const tokenize = (source: string): Token[] => {
|
|
|
481
483
|
pushToken("Keyword", word.slice(0, 3), 3);
|
|
482
484
|
continue;
|
|
483
485
|
}
|
|
484
|
-
|
|
486
|
+
const name = names.get(word) ?? word;
|
|
487
|
+
names.set(name, name);
|
|
488
|
+
pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", name, word.length);
|
|
485
489
|
continue;
|
|
486
490
|
}
|
|
487
491
|
const stepLength = matchStep(source, cursor.index);
|
package/src/parallel-worker.ts
CHANGED
|
@@ -1,66 +1,41 @@
|
|
|
1
1
|
import { createCanonicalizer } from "./canonical";
|
|
2
2
|
import { createSearch } from "./check";
|
|
3
3
|
import { createEnvironment } from "./environment";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
import {
|
|
5
|
+
createFingerprintTable,
|
|
6
|
+
insertSharedFingerprint,
|
|
7
|
+
isSharedFingerprintsFull,
|
|
8
|
+
writeStateFingerprint,
|
|
9
|
+
} from "./fingerprints";
|
|
10
|
+
import type {
|
|
11
|
+
CoordinatorMessage,
|
|
12
|
+
LevelMessage,
|
|
13
|
+
Port,
|
|
14
|
+
StartMessage,
|
|
15
|
+
WorkerMessage,
|
|
16
|
+
} from "./parallel";
|
|
17
|
+
import { getSuccessors, type Successor } from "./successors";
|
|
7
18
|
import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
|
|
8
19
|
import { OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
9
20
|
import { createStateDecoder, createStateEncoder, type EncodedStates } from "./transfer";
|
|
10
21
|
import { getRankedStringCount, setStringOrder, type Value } from "./values";
|
|
11
22
|
|
|
12
23
|
const WORKER_THREADS_MODULE = "node:worker_threads";
|
|
24
|
+
const LOCKED = -1;
|
|
13
25
|
|
|
14
|
-
interface
|
|
26
|
+
interface Expansion {
|
|
15
27
|
states: Value[][];
|
|
16
|
-
|
|
17
|
-
|
|
28
|
+
origin: number;
|
|
29
|
+
end: number;
|
|
30
|
+
successors: Successor[] | undefined;
|
|
31
|
+
index: number;
|
|
32
|
+
added: number;
|
|
33
|
+
generated: number;
|
|
34
|
+
outDegrees: number[];
|
|
35
|
+
next: Value[][];
|
|
18
36
|
fingerprintStates: Value[][];
|
|
19
37
|
}
|
|
20
38
|
|
|
21
|
-
interface PeerMessage {
|
|
22
|
-
phase: "query" | "answer" | "sizes" | "balance";
|
|
23
|
-
level: number;
|
|
24
|
-
from: number;
|
|
25
|
-
fingerprints?: Int32Array;
|
|
26
|
-
bits?: Uint8Array;
|
|
27
|
-
states?: EncodedStates;
|
|
28
|
-
unmarks?: Int32Array;
|
|
29
|
-
size?: number;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
interface Transfer {
|
|
33
|
-
from: number;
|
|
34
|
-
to: number;
|
|
35
|
-
count: number;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const getBalancePlan = (sizes: number[]) => {
|
|
39
|
-
const total = sizes.reduce((sum, size) => sum + size, 0);
|
|
40
|
-
const average = total / sizes.length;
|
|
41
|
-
if (Math.max(...sizes) - Math.min(...sizes) <= Math.max(64, average / 4)) return [];
|
|
42
|
-
const targets = sizes.map(
|
|
43
|
-
(_, index) => Math.floor(average) + (index < total % sizes.length ? 1 : 0),
|
|
44
|
-
);
|
|
45
|
-
const donors = sizes.flatMap((size, index) =>
|
|
46
|
-
size > targets[index] ? [{ index, amount: size - targets[index] }] : [],
|
|
47
|
-
);
|
|
48
|
-
const receivers = sizes.flatMap((size, index) =>
|
|
49
|
-
size < targets[index] ? [{ index, amount: targets[index] - size }] : [],
|
|
50
|
-
);
|
|
51
|
-
const transfers: Transfer[] = [];
|
|
52
|
-
for (const donor of donors) {
|
|
53
|
-
for (const receiver of receivers) {
|
|
54
|
-
const moved = Math.min(donor.amount, receiver.amount);
|
|
55
|
-
if (moved === 0) continue;
|
|
56
|
-
transfers.push({ from: donor.index, to: receiver.index, count: moved });
|
|
57
|
-
donor.amount -= moved;
|
|
58
|
-
receiver.amount -= moved;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return transfers;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
39
|
const getParentPort = async () => {
|
|
65
40
|
if (typeof (globalThis as { postMessage?: unknown }).postMessage === "function") {
|
|
66
41
|
return globalThis as unknown as Port;
|
|
@@ -71,33 +46,6 @@ const getParentPort = async () => {
|
|
|
71
46
|
return threads.parentPort as unknown as Port;
|
|
72
47
|
};
|
|
73
48
|
|
|
74
|
-
const createInbox = () => {
|
|
75
|
-
const messages = new Map<string, PeerMessage[]>();
|
|
76
|
-
const waiters = new Map<string, { count: number; resolve: (messages: PeerMessage[]) => void }>();
|
|
77
|
-
const getKey = (phase: PeerMessage["phase"], level: number) => `${phase}:${level}`;
|
|
78
|
-
const settle = (key: string) => {
|
|
79
|
-
const waiter = waiters.get(key);
|
|
80
|
-
const received = messages.get(key) ?? [];
|
|
81
|
-
if (!waiter || received.length < waiter.count) return;
|
|
82
|
-
waiters.delete(key);
|
|
83
|
-
messages.delete(key);
|
|
84
|
-
waiter.resolve(received);
|
|
85
|
-
};
|
|
86
|
-
return {
|
|
87
|
-
deliver: (message: PeerMessage) => {
|
|
88
|
-
const key = getKey(message.phase, message.level);
|
|
89
|
-
messages.set(key, [...(messages.get(key) ?? []), message]);
|
|
90
|
-
settle(key);
|
|
91
|
-
},
|
|
92
|
-
receive: (phase: PeerMessage["phase"], level: number, count: number) =>
|
|
93
|
-
new Promise<PeerMessage[]>((resolve) => {
|
|
94
|
-
const key = getKey(phase, level);
|
|
95
|
-
waiters.set(key, { count, resolve });
|
|
96
|
-
settle(key);
|
|
97
|
-
}),
|
|
98
|
-
};
|
|
99
|
-
};
|
|
100
|
-
|
|
101
49
|
const run = (start: StartMessage, parent: Port) => {
|
|
102
50
|
const modules = new Map(start.modules);
|
|
103
51
|
const loadModule = (name: string) => modules.get(name);
|
|
@@ -118,46 +66,73 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
118
66
|
const nextAction = search?.spec.next;
|
|
119
67
|
if (!search || nextAction === undefined)
|
|
120
68
|
return parent.postMessage({ type: "abort" } satisfies WorkerMessage);
|
|
121
|
-
const count = start.count;
|
|
122
69
|
const self = start.index;
|
|
123
70
|
const canonicalize = createCanonicalizer();
|
|
124
71
|
const decode = createStateDecoder(environment.variables.length);
|
|
125
|
-
const
|
|
126
|
-
const
|
|
72
|
+
const shared = { fingerprints: start.fingerprints };
|
|
73
|
+
const requests = start.requests;
|
|
74
|
+
const remaining = start.remaining;
|
|
127
75
|
// Under symmetry, a successor the worker generated before has the representative it had then,
|
|
128
76
|
// so the worker skips the search for one.
|
|
129
77
|
const generatedStates = search.isSymmetric ? createFingerprintTable() : undefined;
|
|
130
78
|
const fingerprint = new Int32Array(2);
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
peer.port.start?.();
|
|
79
|
+
const waiters = { resume: () => {}, loot: (_states: EncodedStates) => {} };
|
|
80
|
+
start.ports.forEach((port) => {
|
|
81
|
+
port?.addEventListener("message", (event) => waiters.loot(event.data as EncodedStates));
|
|
82
|
+
port?.start?.();
|
|
136
83
|
});
|
|
137
|
-
for (let index = 0; index < start.seedFingerprints.length; index += 2) {
|
|
138
|
-
table.insert(start.seedFingerprints[index], start.seedFingerprints[index + 1]);
|
|
139
|
-
}
|
|
140
84
|
const frontier = { states: decode(start.seed).map(canonicalize) };
|
|
141
|
-
const send = (index: number, message: PeerMessage, transfer: unknown[]) =>
|
|
142
|
-
start.ports[index]?.postMessage(message, transfer);
|
|
143
85
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
86
|
+
// A worker that runs out of states asks a peer for some by writing its index + 1 to the peer's
|
|
87
|
+
// request word. The peer sends half of the states it hasn't started, keeps expanding, and wakes
|
|
88
|
+
// the workers waiting to ask it next.
|
|
89
|
+
const answer = (expansion: Expansion, request: number) => {
|
|
90
|
+
const thief = Atomics.load(requests, self) - 1;
|
|
91
|
+
const given = Math.floor((expansion.end - expansion.origin - 1) / 2);
|
|
92
|
+
const encoder = createStateEncoder();
|
|
93
|
+
const states = expansion.states.slice(expansion.end - given, expansion.end);
|
|
94
|
+
const isEncoded = given > 0 && states.every(encoder.encode);
|
|
95
|
+
if (isEncoded) expansion.end -= given;
|
|
96
|
+
const loot = isEncoded ? encoder.finish() : createStateEncoder().finish();
|
|
97
|
+
start.ports[thief]?.postMessage(loot, [loot.words.buffer]);
|
|
98
|
+
Atomics.store(requests, self, request);
|
|
99
|
+
Atomics.notify(requests, self);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// A worker answers any request before it pauses or runs out of states, and takes no more until
|
|
103
|
+
// it resumes.
|
|
104
|
+
const lock = (expansion: Expansion) => {
|
|
105
|
+
Atomics.store(remaining, self, 0);
|
|
106
|
+
if (Atomics.compareExchange(requests, self, 0, LOCKED) === 0) return;
|
|
107
|
+
answer(expansion, LOCKED);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const unlock = (expansion: Expansion) => {
|
|
111
|
+
Atomics.store(remaining, self, expansion.end - expansion.origin);
|
|
112
|
+
Atomics.store(requests, self, 0);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// The worker pauses before a successor when the shared table needs to grow, and resumes there.
|
|
116
|
+
const expand = (expansion: Expansion) => {
|
|
117
|
+
for (; expansion.origin < expansion.end; expansion.origin++) {
|
|
118
|
+
const state = expansion.states[expansion.origin];
|
|
119
|
+
if (!expansion.successors) {
|
|
120
|
+
if (Atomics.load(requests, self) > 0) answer(expansion, 0);
|
|
121
|
+
Atomics.store(remaining, self, expansion.end - expansion.origin - 1);
|
|
122
|
+
expansion.successors = getSuccessors(nextAction, state, environment, search.spec.scope);
|
|
123
|
+
expansion.generated += expansion.successors.length;
|
|
124
|
+
if (expansion.successors.length === 0 && start.config.checkDeadlock) return undefined;
|
|
125
|
+
}
|
|
126
|
+
const successors = expansion.successors;
|
|
127
|
+
for (; expansion.index < successors.length; expansion.index++) {
|
|
128
|
+
if (isSharedFingerprintsFull(shared.fingerprints)) return "paused";
|
|
129
|
+
const successor = successors[expansion.index];
|
|
159
130
|
if (search.findViolatedActionProperty(state, successor.state)) return undefined;
|
|
160
|
-
if (!search.satisfiesActionConstraints(state, successor.state))
|
|
131
|
+
if (!search.satisfiesActionConstraints(state, successor.state)) {
|
|
132
|
+
// TLC checks the invariants of every successor it has not seen, even outside the model.
|
|
133
|
+
if (search.findViolatedInvariant(successor.state, state)) return undefined;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
161
136
|
if (generatedStates) {
|
|
162
137
|
writeStateFingerprint(successor.state, fingerprint);
|
|
163
138
|
if (!generatedStates.insert(fingerprint[0], fingerprint[1])) continue;
|
|
@@ -165,136 +140,93 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
165
140
|
canonicalize(successor.state);
|
|
166
141
|
const fingerprintState = search.getFingerprintState(successor.state);
|
|
167
142
|
writeStateFingerprint(fingerprintState, fingerprint);
|
|
168
|
-
|
|
169
|
-
if (
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
continue;
|
|
175
|
-
}
|
|
176
|
-
next.push(successor.state);
|
|
177
|
-
nextFingerprintStates.push(fingerprintState);
|
|
178
|
-
newStates[origin]++;
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
if (!known.insert(fingerprint[0], fingerprint[1])) continue;
|
|
182
|
-
pending[owner].states.push(successor.state);
|
|
183
|
-
pending[owner].origins.push(origin);
|
|
184
|
-
pending[owner].fingerprints.push(fingerprint[0], fingerprint[1]);
|
|
185
|
-
pending[owner].fingerprintStates.push(fingerprintState);
|
|
143
|
+
if (!insertSharedFingerprint(shared.fingerprints, fingerprint[0], fingerprint[1])) continue;
|
|
144
|
+
if (search.findViolatedInvariant(successor.state, state)) return undefined;
|
|
145
|
+
if (!search.satisfiesConstraints(successor.state, state)) continue;
|
|
146
|
+
expansion.next.push(successor.state);
|
|
147
|
+
expansion.fingerprintStates.push(fingerprintState);
|
|
148
|
+
expansion.added++;
|
|
186
149
|
}
|
|
150
|
+
expansion.outDegrees[Math.min(expansion.added, OUT_DEGREE_BUCKETS - 1)]++;
|
|
151
|
+
expansion.successors = undefined;
|
|
152
|
+
expansion.index = 0;
|
|
153
|
+
expansion.added = 0;
|
|
187
154
|
}
|
|
188
|
-
return
|
|
155
|
+
return "done";
|
|
189
156
|
};
|
|
190
157
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
if (search.findViolatedInvariant(state, parentState)) return undefined;
|
|
212
|
-
if (search.satisfiesConstraints(state, parentState)) {
|
|
213
|
-
next.push(state);
|
|
214
|
-
nextFingerprintStates.push(pending.fingerprintStates[index]);
|
|
215
|
-
newStates[pending.origins[index]]++;
|
|
158
|
+
// Each attempt asks the peer with the most states left. While another worker's request to that
|
|
159
|
+
// peer is open, the worker waits for the peer to answer it. The worker stops once no peer that
|
|
160
|
+
// takes requests has two states left.
|
|
161
|
+
const steal = async (expansion: Expansion) => {
|
|
162
|
+
const skipped = new Set<number>();
|
|
163
|
+
for (;;) {
|
|
164
|
+
const victim = start.ports.reduce<number | undefined>((best, port, index) => {
|
|
165
|
+
if (!port || skipped.has(index) || Atomics.load(remaining, index) < 2) return best;
|
|
166
|
+
return best === undefined || Atomics.load(remaining, index) > Atomics.load(remaining, best)
|
|
167
|
+
? index
|
|
168
|
+
: best;
|
|
169
|
+
}, undefined);
|
|
170
|
+
if (victim === undefined) return false;
|
|
171
|
+
const request = Atomics.compareExchange(requests, victim, 0, self + 1);
|
|
172
|
+
if (request === LOCKED) skipped.add(victim);
|
|
173
|
+
if (request > 0) Atomics.wait(requests, victim, request);
|
|
174
|
+
if (request !== 0) continue;
|
|
175
|
+
const loot = await new Promise<EncodedStates>((resolve) => (waiters.loot = resolve));
|
|
176
|
+
if (loot.count === 0) {
|
|
177
|
+
skipped.add(victim);
|
|
216
178
|
continue;
|
|
217
179
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
const removeAll = (fingerprints: Int32Array) => {
|
|
225
|
-
for (let index = 0; index < fingerprints.length; index += 2) {
|
|
226
|
-
table.remove(fingerprints[index], fingerprints[index + 1]);
|
|
180
|
+
expansion.states = decode(loot).map(canonicalize);
|
|
181
|
+
expansion.origin = 0;
|
|
182
|
+
expansion.end = expansion.states.length;
|
|
183
|
+
unlock(expansion);
|
|
184
|
+
return true;
|
|
227
185
|
}
|
|
228
186
|
};
|
|
229
187
|
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
if (transfers.length === 0) return true;
|
|
233
|
-
const outgoing = new Map(peers.map((peer) => [peer.index, createStateEncoder()]));
|
|
234
|
-
for (const transfer of transfers.filter((candidate) => candidate.from === self)) {
|
|
235
|
-
const encoder = outgoing.get(transfer.to);
|
|
236
|
-
const states = next.splice(next.length - transfer.count, transfer.count);
|
|
237
|
-
if (!encoder || !states.every(encoder.encode)) return false;
|
|
238
|
-
}
|
|
239
|
-
outgoing.forEach((encoder, index) => {
|
|
240
|
-
const states = encoder.finish();
|
|
241
|
-
send(index, { phase: "balance", level, from: self, states }, [states.words.buffer]);
|
|
242
|
-
});
|
|
243
|
-
const incoming = await inbox.receive("balance", level, peers.length);
|
|
244
|
-
incoming.forEach((message) => {
|
|
245
|
-
if (message.states) next.push(...decode(message.states).map(canonicalize));
|
|
246
|
-
});
|
|
247
|
-
return true;
|
|
248
|
-
};
|
|
188
|
+
const report = (message: WorkerMessage) =>
|
|
189
|
+
parent.postMessage(message, message.type === "level" ? [message.fingerprints.buffer] : []);
|
|
249
190
|
|
|
250
191
|
const runLevel = async (level: number): Promise<LevelMessage | undefined> => {
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
192
|
+
const expansion: Expansion = {
|
|
193
|
+
states: frontier.states,
|
|
194
|
+
origin: 0,
|
|
195
|
+
end: frontier.states.length,
|
|
196
|
+
successors: undefined,
|
|
197
|
+
index: 0,
|
|
198
|
+
added: 0,
|
|
199
|
+
generated: 0,
|
|
200
|
+
outDegrees: Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0),
|
|
201
|
+
next: [],
|
|
202
|
+
fingerprintStates: [],
|
|
203
|
+
};
|
|
204
|
+
for (;;) {
|
|
205
|
+
const status = expand(expansion);
|
|
206
|
+
if (status === undefined) return undefined;
|
|
207
|
+
lock(expansion);
|
|
208
|
+
if (status === "paused") {
|
|
209
|
+
const resumed = new Promise<void>((resolve) => (waiters.resume = resolve));
|
|
210
|
+
report({ type: "paused" });
|
|
211
|
+
await resumed;
|
|
212
|
+
unlock(expansion);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (!(await steal(expansion))) break;
|
|
275
216
|
}
|
|
276
|
-
const added = next.length;
|
|
277
|
-
const outDegrees = Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0);
|
|
278
|
-
expanded.newStates.forEach((degree) => outDegrees[Math.min(degree, OUT_DEGREE_BUCKETS - 1)]++);
|
|
279
|
-
const fingerprints = search.getStoredFingerprints(next, nextFingerprintStates);
|
|
280
|
-
const sizes = Array.from({ length: count }, () => 0);
|
|
281
|
-
sizes[self] = added;
|
|
282
|
-
peers.forEach((peer) =>
|
|
283
|
-
send(peer.index, { phase: "sizes", level, from: self, size: added }, []),
|
|
284
|
-
);
|
|
285
|
-
const received = await inbox.receive("sizes", level, peers.length * 2);
|
|
286
|
-
received.forEach((message) => {
|
|
287
|
-
if (message.unmarks) removeAll(message.unmarks);
|
|
288
|
-
else sizes[message.from] = message.size ?? 0;
|
|
289
|
-
});
|
|
290
217
|
if (getRankedStringCount() !== startupStrings) return undefined;
|
|
291
|
-
|
|
292
|
-
frontier.states
|
|
293
|
-
return {
|
|
218
|
+
frontier.states = expansion.next;
|
|
219
|
+
Atomics.store(remaining, self, frontier.states.length);
|
|
220
|
+
return {
|
|
221
|
+
type: "level",
|
|
222
|
+
level,
|
|
223
|
+
generated: expansion.generated,
|
|
224
|
+
added: expansion.next.length,
|
|
225
|
+
outDegrees: expansion.outDegrees,
|
|
226
|
+
fingerprints: search.getStoredFingerprints(expansion.next, expansion.fingerprintStates),
|
|
227
|
+
};
|
|
294
228
|
};
|
|
295
229
|
|
|
296
|
-
const report = (message: WorkerMessage) =>
|
|
297
|
-
parent.postMessage(message, message.type === "level" ? [message.fingerprints.buffer] : []);
|
|
298
230
|
const step = (level: number) => {
|
|
299
231
|
runLevel(level).then(
|
|
300
232
|
(message) => report(message ?? { type: "abort" }),
|
|
@@ -302,9 +234,14 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
302
234
|
);
|
|
303
235
|
};
|
|
304
236
|
parent.addEventListener("message", (event) => {
|
|
305
|
-
const message = event.data as
|
|
237
|
+
const message = event.data as CoordinatorMessage;
|
|
306
238
|
if (message.type === "level") step(message.level);
|
|
239
|
+
if (message.type === "resize") {
|
|
240
|
+
shared.fingerprints = message.fingerprints;
|
|
241
|
+
waiters.resume();
|
|
242
|
+
}
|
|
307
243
|
});
|
|
244
|
+
Atomics.store(remaining, self, frontier.states.length);
|
|
308
245
|
report({ type: "ready" });
|
|
309
246
|
};
|
|
310
247
|
|