@gmitch215/folio 1.0.0 → 1.0.1
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 +78 -19
- package/dist/bake.d.ts +8 -3
- package/dist/bake.js +5 -2
- package/dist/compiler.d.ts +37 -1
- package/dist/compiler.js +116 -1
- package/dist/context.d.ts +33 -0
- package/dist/context.js +93 -0
- package/dist/coordinator.d.ts +1 -1
- package/dist/coordinator.js +4 -2
- package/dist/extract.d.ts +95 -1
- package/dist/extract.js +883 -242
- package/dist/frames.d.ts +15 -1
- package/dist/frames.js +49 -1
- package/dist/inline.d.ts +68 -0
- package/dist/inline.js +503 -0
- package/dist/iteration-admission.d.ts +76 -0
- package/dist/iteration-admission.js +72 -0
- package/dist/iteration-layout.d.ts +75 -0
- package/dist/iteration-layout.js +118 -0
- package/dist/iteration-owner.d.ts +32 -0
- package/dist/iteration-owner.js +223 -0
- package/dist/iteration-worker.d.ts +1 -0
- package/dist/iteration-worker.js +150 -0
- package/dist/iteration.d.ts +163 -0
- package/dist/iteration.js +435 -0
- package/dist/managed.d.ts +6 -0
- package/dist/managed.js +33 -10
- package/dist/node.d.ts +19 -5
- package/dist/node.js +344 -22
- package/dist/tasks.js +16 -3
- package/package.json +6 -2
package/dist/node.js
CHANGED
|
@@ -2,6 +2,8 @@ import { availableParallelism } from 'node:os';
|
|
|
2
2
|
import { isArrayBuffer, isSharedArrayBuffer, isUint8Array } from 'node:util/types';
|
|
3
3
|
import { Worker } from 'node:worker_threads';
|
|
4
4
|
import { Coordinator } from "./coordinator.js";
|
|
5
|
+
import { decideAdmission, decideBatch, decideCollapse } from "./iteration-admission.js";
|
|
6
|
+
import { iterationArguments, iterationFit } from "./iteration-layout.js";
|
|
5
7
|
import { memoryImports } from "./memory.js";
|
|
6
8
|
import { numberBits, restoreNumbers } from "./values.js";
|
|
7
9
|
/** Wraps an asynchronous host import for Wasm stack suspension. */
|
|
@@ -29,6 +31,87 @@ function isMemory(value) {
|
|
|
29
31
|
return false;
|
|
30
32
|
}
|
|
31
33
|
}
|
|
34
|
+
const PAGE = 65536;
|
|
35
|
+
function addCounts(total, part) {
|
|
36
|
+
if (!total)
|
|
37
|
+
return structuredClone(part);
|
|
38
|
+
for (const key of Object.keys(part)) {
|
|
39
|
+
if (key === 'stopped')
|
|
40
|
+
total.stopped ??= part.stopped;
|
|
41
|
+
else if (key === 'admission')
|
|
42
|
+
total.admission = part.admission ?? total.admission;
|
|
43
|
+
else if (key === 'workers')
|
|
44
|
+
total.workers = Math.max(total.workers, part.workers);
|
|
45
|
+
else if (key === 'conflicts') {
|
|
46
|
+
total.conflicts.raw += part.conflicts.raw;
|
|
47
|
+
total.conflicts.war += part.conflicts.war;
|
|
48
|
+
total.conflicts.waw += part.conflicts.waw;
|
|
49
|
+
total.conflicts.frame += part.conflicts.frame;
|
|
50
|
+
}
|
|
51
|
+
else
|
|
52
|
+
total[key] += part[key];
|
|
53
|
+
}
|
|
54
|
+
return total;
|
|
55
|
+
}
|
|
56
|
+
// true when the host supplies the memory import through `imports`, or it cannot be told safely
|
|
57
|
+
function suppliesMemory(imports, module, name) {
|
|
58
|
+
if (!imports || !(module in imports))
|
|
59
|
+
return false;
|
|
60
|
+
const descriptor = Object.getOwnPropertyDescriptor(imports, module);
|
|
61
|
+
if (!descriptor || !('value' in descriptor))
|
|
62
|
+
return true;
|
|
63
|
+
const value = descriptor.value;
|
|
64
|
+
return value !== null && ['object', 'function'].includes(typeof value) && name in value;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Chooses the iteration route for a program no older route covers, and builds its owner module and
|
|
68
|
+
* shared memory. A refusal names its reason; a program that is not a candidate returns nothing.
|
|
69
|
+
*/
|
|
70
|
+
async function selectIteration(program, options, size) {
|
|
71
|
+
const route = program.iterationRoute;
|
|
72
|
+
if (!route?.eligible ||
|
|
73
|
+
size < 2 ||
|
|
74
|
+
program.control ||
|
|
75
|
+
program.compiled ||
|
|
76
|
+
Object.keys(program.loops).length ||
|
|
77
|
+
Object.keys(program.native ?? {}).length)
|
|
78
|
+
return;
|
|
79
|
+
const refuse = (reason, detail) => ({ reason, detail });
|
|
80
|
+
const { iterationOwner } = await import("./iteration-owner.js");
|
|
81
|
+
const built = iterationOwner(program.original, route, program.stackRoute?.stack?.name);
|
|
82
|
+
if (!('owner' in built))
|
|
83
|
+
return built;
|
|
84
|
+
const owner = built.owner;
|
|
85
|
+
if (suppliesMemory(options.imports, owner.memory.module, owner.memory.name))
|
|
86
|
+
return refuse('no-arena', 'the memory import is supplied through imports');
|
|
87
|
+
if (options.memory && !isMemory(options.memory))
|
|
88
|
+
return;
|
|
89
|
+
if (options.memory && !isSharedArrayBuffer(options.memory.buffer))
|
|
90
|
+
return refuse('no-arena', 'the host memory is not shared');
|
|
91
|
+
const start = options.memory ? options.memory.buffer.byteLength : owner.memory.initial * PAGE;
|
|
92
|
+
const fit = iterationFit(start, { frameBytes: route.frame?.size });
|
|
93
|
+
if (!fit)
|
|
94
|
+
return refuse('no-arena', 'the arena does not fit in 65536 pages');
|
|
95
|
+
if (owner.memory.maximum !== undefined && fit.pages > owner.memory.maximum)
|
|
96
|
+
return refuse('no-arena', `the declared maximum of ${owner.memory.maximum} pages is below the ${fit.pages} the arena needs`);
|
|
97
|
+
let memory = options.memory;
|
|
98
|
+
try {
|
|
99
|
+
if (memory)
|
|
100
|
+
memory.grow(fit.pages - start / PAGE);
|
|
101
|
+
else
|
|
102
|
+
memory = new WebAssembly.Memory({
|
|
103
|
+
initial: fit.pages,
|
|
104
|
+
maximum: Math.max(owner.memory.maximum ?? fit.pages, fit.pages),
|
|
105
|
+
shared: true
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (!(error instanceof RangeError))
|
|
110
|
+
throw error;
|
|
111
|
+
return refuse('no-arena', `memory cannot hold an arena of ${fit.bytes} bytes`);
|
|
112
|
+
}
|
|
113
|
+
return { route, owner, memory, arenaBase: fit.base, size };
|
|
114
|
+
}
|
|
32
115
|
function throwTrap(result) {
|
|
33
116
|
if (!result.trap)
|
|
34
117
|
return;
|
|
@@ -43,13 +126,19 @@ export class Folio {
|
|
|
43
126
|
/** Active sharing, extraction and validation mode; a one-worker pool reports none and serial. */
|
|
44
127
|
get capabilities() {
|
|
45
128
|
return Object.freeze({
|
|
46
|
-
physicalSharing: this.#workers.length
|
|
47
|
-
|
|
48
|
-
? this.#control
|
|
49
|
-
? 'control-flow'
|
|
50
|
-
: 'counted-loops'
|
|
129
|
+
physicalSharing: this.#workers.length || this.#iteration
|
|
130
|
+
? 'same-process'
|
|
51
131
|
: 'none',
|
|
52
|
-
|
|
132
|
+
automaticRegions: this.#iteration
|
|
133
|
+
? 'iteration'
|
|
134
|
+
: this.#workers.length
|
|
135
|
+
? this.#control
|
|
136
|
+
? 'control-flow'
|
|
137
|
+
: 'counted-loops'
|
|
138
|
+
: 'none',
|
|
139
|
+
dependence: this.#workers.length || this.#iteration
|
|
140
|
+
? 'ordered-validation'
|
|
141
|
+
: 'serial'
|
|
53
142
|
});
|
|
54
143
|
}
|
|
55
144
|
/** The original native exports, for trusted host integration. Raw calls bypass tracking. */
|
|
@@ -93,6 +182,9 @@ export class Folio {
|
|
|
93
182
|
});
|
|
94
183
|
}
|
|
95
184
|
#memory;
|
|
185
|
+
#iteration;
|
|
186
|
+
#ownerStack;
|
|
187
|
+
#refusal;
|
|
96
188
|
#workerMemory;
|
|
97
189
|
#control;
|
|
98
190
|
#serial;
|
|
@@ -115,17 +207,27 @@ export class Folio {
|
|
|
115
207
|
#maxBytes;
|
|
116
208
|
#minimumWork;
|
|
117
209
|
#profile;
|
|
210
|
+
#adaptive;
|
|
211
|
+
#routing = new Map();
|
|
118
212
|
#nextId = 0;
|
|
119
213
|
#busy = false;
|
|
120
214
|
#closed = false;
|
|
121
215
|
#closing;
|
|
122
216
|
#failure;
|
|
123
|
-
constructor(program, options, size) {
|
|
217
|
+
constructor(program, options, size, choice) {
|
|
124
218
|
this.#program = program;
|
|
219
|
+
if (choice && 'owner' in choice)
|
|
220
|
+
this.#iteration = choice;
|
|
221
|
+
else if (choice) {
|
|
222
|
+
this.#refusal = choice;
|
|
223
|
+
this.#runtimeDiagnostics.push(`iteration route refused: ${choice.detail}`);
|
|
224
|
+
}
|
|
225
|
+
const iteration = this.#iteration;
|
|
125
226
|
this.#fuel = options.fuel ?? 100_000_000;
|
|
126
227
|
this.#maxBytes = options.maxTransactionBytes ?? 262_144;
|
|
127
228
|
this.#minimumWork = options.minimumParallelWork ?? 100_000;
|
|
128
229
|
this.#profile = options.profile ?? false;
|
|
230
|
+
this.#adaptive = options.adaptiveRouting ?? false;
|
|
129
231
|
if (!Number.isInteger(this.#fuel) || this.#fuel < 2 || this.#fuel > 2_147_483_647)
|
|
130
232
|
throw new RangeError('invalid fuel');
|
|
131
233
|
if (!Number.isInteger(this.#maxBytes) || this.#maxBytes < 1)
|
|
@@ -150,6 +252,10 @@ export class Folio {
|
|
|
150
252
|
else
|
|
151
253
|
this.#runtimeDiagnostics.push('control extraction retained native binding order: rewritten import sequence differs');
|
|
152
254
|
}
|
|
255
|
+
if (iteration) {
|
|
256
|
+
executable = new WebAssembly.Module(Uint8Array.from(iteration.owner.module));
|
|
257
|
+
this.#runtimeDiagnostics.push(`iteration route: ${iteration.route.diagnostics.join('; ')}`);
|
|
258
|
+
}
|
|
153
259
|
const imported = WebAssembly.Module.imports(native).filter((item) => item.kind === 'memory');
|
|
154
260
|
if (imported.length > 1 && options.memory)
|
|
155
261
|
throw new TypeError('bind multiple memories by name in imports');
|
|
@@ -161,6 +267,7 @@ export class Folio {
|
|
|
161
267
|
const supplied = options.imports ?? Object.create(null);
|
|
162
268
|
const automatic = new Map();
|
|
163
269
|
let controlBindings;
|
|
270
|
+
let iterationBindings;
|
|
164
271
|
// Resolve bindings when the engine asks for them, in its original order.
|
|
165
272
|
// Enumerating/spreading caller namespaces would invoke accessors early or twice.
|
|
166
273
|
const imports = new Proxy(Object.create(null), {
|
|
@@ -188,6 +295,10 @@ export class Folio {
|
|
|
188
295
|
}
|
|
189
296
|
return (controlBindings = bindings);
|
|
190
297
|
}
|
|
298
|
+
if (iteration && module === iteration.owner.namespace)
|
|
299
|
+
return (iterationBindings ??= {
|
|
300
|
+
iterate: (...values) => this.#iterate(values)
|
|
301
|
+
});
|
|
191
302
|
const namespace = Reflect.get(supplied, module, supplied);
|
|
192
303
|
const memories = imported.filter((item) => item.module === module);
|
|
193
304
|
if (!memories.length ||
|
|
@@ -201,10 +312,12 @@ export class Folio {
|
|
|
201
312
|
return namespace === undefined
|
|
202
313
|
? undefined
|
|
203
314
|
: Reflect.get(namespace, name, namespace);
|
|
204
|
-
let bound =
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
315
|
+
let bound = iteration
|
|
316
|
+
? iteration.memory
|
|
317
|
+
: (options.memory ??
|
|
318
|
+
(namespace === undefined
|
|
319
|
+
? undefined
|
|
320
|
+
: Reflect.get(namespace, name, namespace)));
|
|
208
321
|
if (bound === undefined) {
|
|
209
322
|
const key = JSON.stringify([module, name]);
|
|
210
323
|
bound = automatic.get(key);
|
|
@@ -259,6 +372,18 @@ export class Folio {
|
|
|
259
372
|
this.#controlled.set(entry, runtime.promising(this.#instance.exports[entry]));
|
|
260
373
|
}
|
|
261
374
|
}
|
|
375
|
+
else if (iteration) {
|
|
376
|
+
this.#exports = Object.create(null);
|
|
377
|
+
for (const item of WebAssembly.Module.exports(native)) {
|
|
378
|
+
Object.defineProperty(this.#exports, item.name, {
|
|
379
|
+
value: this.#instance.exports[item.name],
|
|
380
|
+
enumerable: true
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
Object.preventExtensions(this.#exports);
|
|
384
|
+
if (iteration.owner.stackExport)
|
|
385
|
+
this.#ownerStack = this.#instance.exports[iteration.owner.stackExport];
|
|
386
|
+
}
|
|
262
387
|
else
|
|
263
388
|
this.#exports = this.#instance.exports;
|
|
264
389
|
for (const [name, value] of Object.entries(this.#exports)) {
|
|
@@ -294,7 +419,7 @@ export class Folio {
|
|
|
294
419
|
const size = options.workers ?? Math.min(4, availableParallelism());
|
|
295
420
|
if (!Number.isInteger(size) || size < 1 || size > 64)
|
|
296
421
|
throw new RangeError('invalid worker count');
|
|
297
|
-
const pool = new Folio(program, options, size);
|
|
422
|
+
const pool = new Folio(program, options, size, await selectIteration(program, options, size));
|
|
298
423
|
const transactional = pool.#control?.transactional ?? program.transactional;
|
|
299
424
|
if (!transactional || size === 1)
|
|
300
425
|
return pool;
|
|
@@ -382,7 +507,7 @@ export class Folio {
|
|
|
382
507
|
const entry = this.#instance.exports[call.export];
|
|
383
508
|
if (typeof entry !== 'function')
|
|
384
509
|
throw new TypeError(`unknown function export: ${call.export}`);
|
|
385
|
-
const wrapped = this.#promising.get(call.export);
|
|
510
|
+
const wrapped = this.#promising.get(call.export) ?? this.#controlled.get(call.export);
|
|
386
511
|
if (!wrapped)
|
|
387
512
|
return { value: entry(...call.args) };
|
|
388
513
|
const pending = wrapped(...call.args);
|
|
@@ -457,13 +582,37 @@ export class Folio {
|
|
|
457
582
|
: undefined;
|
|
458
583
|
const numeric = args.every((arg) => typeof arg === 'number');
|
|
459
584
|
const control = this.#controlled.get(entry);
|
|
585
|
+
const routeKey = this.#adaptive &&
|
|
586
|
+
options.parallel !== 'off' &&
|
|
587
|
+
options.parallel !== 'speculate' &&
|
|
588
|
+
width >= 2 &&
|
|
589
|
+
(plan || control) &&
|
|
590
|
+
args.every((arg) => typeof arg === 'number' || typeof arg === 'bigint')
|
|
591
|
+
? JSON.stringify([
|
|
592
|
+
entry,
|
|
593
|
+
width,
|
|
594
|
+
this.#memory?.buffer.byteLength,
|
|
595
|
+
args.map((arg) => (typeof arg === 'bigint' ? `${arg}n` : arg))
|
|
596
|
+
])
|
|
597
|
+
: undefined;
|
|
598
|
+
const history = routeKey ? this.#routing.get(routeKey) : undefined;
|
|
599
|
+
if (history && history.remaining > 0) {
|
|
600
|
+
history.remaining--;
|
|
601
|
+
const report = await this.runAll([{ export: entry, args }], { parallel: 'off' });
|
|
602
|
+
report.reason = 'previous-conflicts';
|
|
603
|
+
return report;
|
|
604
|
+
}
|
|
460
605
|
if (!plan &&
|
|
461
606
|
control &&
|
|
462
607
|
width >= 2 &&
|
|
463
608
|
options.parallel !== 'off' &&
|
|
464
609
|
args.every((arg) => typeof arg === 'number' || typeof arg === 'bigint')) {
|
|
465
|
-
|
|
610
|
+
const report = await this.#runControlled(control, args, width);
|
|
611
|
+
this.#observeRouting(routeKey, report);
|
|
612
|
+
return report;
|
|
466
613
|
}
|
|
614
|
+
if (!plan && this.#iteration && options.parallel !== 'off')
|
|
615
|
+
return this.#runIteration(entry, args, options);
|
|
467
616
|
const count = numeric && plan && typeof args[plan.countParameter] === 'number'
|
|
468
617
|
? Number(args[plan.countParameter]) >>> 0
|
|
469
618
|
: 0;
|
|
@@ -475,13 +624,15 @@ export class Folio {
|
|
|
475
624
|
report.reason =
|
|
476
625
|
options.parallel === 'off'
|
|
477
626
|
? 'requested'
|
|
478
|
-
: !
|
|
479
|
-
?
|
|
480
|
-
: !
|
|
481
|
-
? '
|
|
482
|
-
: !
|
|
483
|
-
? '
|
|
484
|
-
:
|
|
627
|
+
: !plan && this.#refusal
|
|
628
|
+
? this.#refusal.reason
|
|
629
|
+
: !numeric
|
|
630
|
+
? 'opaque-input'
|
|
631
|
+
: !this.#workers.length
|
|
632
|
+
? 'unsupported-module'
|
|
633
|
+
: !plan
|
|
634
|
+
? 'no-parallel-plan'
|
|
635
|
+
: 'single-region';
|
|
485
636
|
return report;
|
|
486
637
|
}
|
|
487
638
|
if (options.parallel !== 'speculate' && work !== undefined && work < this.#minimumWork) {
|
|
@@ -509,8 +660,174 @@ export class Folio {
|
|
|
509
660
|
: await this.runAll(calls, options);
|
|
510
661
|
if (proved && !disjoint && options.parallel !== 'speculate' && !report.reason)
|
|
511
662
|
report.reason = 'guard-failed';
|
|
663
|
+
this.#observeRouting(routeKey, report);
|
|
512
664
|
return { ...report, values: [undefined] };
|
|
513
665
|
}
|
|
666
|
+
// the workers of the iteration route start at the first call that may dispatch
|
|
667
|
+
#iterationPool(width) {
|
|
668
|
+
const it = this.#iteration;
|
|
669
|
+
const opened = (it.opening ?? Promise.resolve()).then(async () => {
|
|
670
|
+
if (this.#closed || it.disabled || it.pool?.width === width)
|
|
671
|
+
return;
|
|
672
|
+
await it.pool?.close();
|
|
673
|
+
it.pool = undefined;
|
|
674
|
+
try {
|
|
675
|
+
const { IterationPool } = await import("./iteration.js");
|
|
676
|
+
it.pool = await IterationPool.open(it.route, it.memory, {
|
|
677
|
+
arenaBase: it.arenaBase,
|
|
678
|
+
width
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
catch (error) {
|
|
682
|
+
it.disabled = true;
|
|
683
|
+
this.#runtimeDiagnostics.push(`iteration workers unavailable: ${error instanceof Error ? error.message : String(error)}`);
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
it.opening = opened.catch(() => { });
|
|
687
|
+
return opened.then(() => it.pool);
|
|
688
|
+
}
|
|
689
|
+
async #runIteration(entry, args, options) {
|
|
690
|
+
const it = this.#iteration;
|
|
691
|
+
if (this.#closed)
|
|
692
|
+
throw new Error('folio is closed');
|
|
693
|
+
if (this.#failure)
|
|
694
|
+
throw this.#failure;
|
|
695
|
+
if (this.#busy)
|
|
696
|
+
throw new Error('memory already has an active run');
|
|
697
|
+
const limit = options.maximumWorkers ?? it.size;
|
|
698
|
+
if (options.maximumWorkers !== undefined &&
|
|
699
|
+
(!Number.isInteger(limit) || limit < 1 || limit > 64))
|
|
700
|
+
throw new RangeError('invalid maximumWorkers');
|
|
701
|
+
const width = Math.min(limit, it.size);
|
|
702
|
+
const call = [{ export: entry, args }];
|
|
703
|
+
if (width < 2) {
|
|
704
|
+
const report = await this.runAll(call, { parallel: 'off' });
|
|
705
|
+
report.reason = 'single-region';
|
|
706
|
+
return report;
|
|
707
|
+
}
|
|
708
|
+
const route = it.route;
|
|
709
|
+
const parameter = route.trip?.parameter;
|
|
710
|
+
const small = entry === route.owner &&
|
|
711
|
+
parameter !== undefined &&
|
|
712
|
+
typeof args[parameter] === 'number' &&
|
|
713
|
+
Number(args[parameter]) >>> 0 < 2;
|
|
714
|
+
const pool = small ? undefined : await this.#iterationPool(width);
|
|
715
|
+
if (this.#closed)
|
|
716
|
+
throw new Error('folio is closed');
|
|
717
|
+
if (this.#busy)
|
|
718
|
+
throw new Error('memory already has an active run');
|
|
719
|
+
const run = { pool };
|
|
720
|
+
if (small)
|
|
721
|
+
run.reason = 'no-admission';
|
|
722
|
+
else if (!pool)
|
|
723
|
+
run.reason = 'workers-unavailable';
|
|
724
|
+
it.run = run;
|
|
725
|
+
let report;
|
|
726
|
+
try {
|
|
727
|
+
report = await this.runAll(call, { parallel: 'off' });
|
|
728
|
+
}
|
|
729
|
+
finally {
|
|
730
|
+
it.run = undefined;
|
|
731
|
+
}
|
|
732
|
+
const counts = run.iteration;
|
|
733
|
+
delete report.reason;
|
|
734
|
+
if (counts)
|
|
735
|
+
report.iteration = counts;
|
|
736
|
+
if (counts && counts.regions > 0) {
|
|
737
|
+
report.mode = 'speculative';
|
|
738
|
+
report.extraction = 'iteration';
|
|
739
|
+
report.memoryMode = 'shared';
|
|
740
|
+
report.workers = counts.workers;
|
|
741
|
+
report.regions = counts.regions;
|
|
742
|
+
report.replayed = counts.replayed;
|
|
743
|
+
report.fallbacks = counts.fallbacks;
|
|
744
|
+
if (report.profile) {
|
|
745
|
+
report.profile.validationMs = counts.validationMs;
|
|
746
|
+
report.profile.replayMs = counts.replayMs;
|
|
747
|
+
}
|
|
748
|
+
if (run.reason)
|
|
749
|
+
report.reason = run.reason;
|
|
750
|
+
else if (counts.replayed > 0)
|
|
751
|
+
report.reason = 'dependency';
|
|
752
|
+
}
|
|
753
|
+
else
|
|
754
|
+
report.reason = run.reason ?? 'no-parallel-plan';
|
|
755
|
+
return report;
|
|
756
|
+
}
|
|
757
|
+
// the host function the owner's loop calls: 1 when every iteration ran, 0 to run the loop itself
|
|
758
|
+
#iterate(values) {
|
|
759
|
+
const it = this.#iteration;
|
|
760
|
+
const run = it.run;
|
|
761
|
+
if (!run?.pool)
|
|
762
|
+
return 0;
|
|
763
|
+
const decline = (reason) => {
|
|
764
|
+
run.reason ??= reason;
|
|
765
|
+
return 0;
|
|
766
|
+
};
|
|
767
|
+
const route = it.route;
|
|
768
|
+
const pool = run.pool;
|
|
769
|
+
const entry = {};
|
|
770
|
+
it.owner.locals.forEach((local, index) => (entry[local] = values[index]));
|
|
771
|
+
const trip = Number(entry[route.trip.local]) >>> 0;
|
|
772
|
+
if (trip === 0)
|
|
773
|
+
return decline('trip-zero');
|
|
774
|
+
if (trip < 2)
|
|
775
|
+
return decline('no-admission');
|
|
776
|
+
if (it.memory.buffer.byteLength !== pool.arena.memoryBytes)
|
|
777
|
+
return decline('no-arena');
|
|
778
|
+
const frameBase = route.frame ? Number(entry[route.frame.local]) >>> 0 : 0;
|
|
779
|
+
const plain = (it.plain ??= this.#plain(it));
|
|
780
|
+
const replay = (k) => {
|
|
781
|
+
if (plain.stack && this.#ownerStack)
|
|
782
|
+
plain.stack.value = this.#ownerStack.value;
|
|
783
|
+
plain.iterate(...iterationArguments(route.locals, entry, k, frameBase));
|
|
784
|
+
};
|
|
785
|
+
try {
|
|
786
|
+
pool.run({
|
|
787
|
+
trip,
|
|
788
|
+
frameBase,
|
|
789
|
+
entry,
|
|
790
|
+
replay,
|
|
791
|
+
admit: (ms) => decideAdmission(ms, trip, pool.width),
|
|
792
|
+
loses: decideBatch,
|
|
793
|
+
collapses: decideCollapse
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
finally {
|
|
797
|
+
run.iteration = addCounts(run.iteration, pool.report);
|
|
798
|
+
}
|
|
799
|
+
if (pool.report.stopped)
|
|
800
|
+
run.reason ??= pool.report.stopped;
|
|
801
|
+
return 1;
|
|
802
|
+
}
|
|
803
|
+
// a plain instance of the worker module on the shared memory; replay runs iterations on it
|
|
804
|
+
#plain(it) {
|
|
805
|
+
const route = it.route;
|
|
806
|
+
const instance = new WebAssembly.Instance(new WebAssembly.Module(Uint8Array.from(route.module)), {
|
|
807
|
+
env: { memory: it.memory }
|
|
808
|
+
});
|
|
809
|
+
return {
|
|
810
|
+
stack: route.stackExport
|
|
811
|
+
? instance.exports[route.stackExport]
|
|
812
|
+
: undefined,
|
|
813
|
+
iterate: instance.exports[route.iterate]
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
#observeRouting(key, report) {
|
|
817
|
+
if (!key)
|
|
818
|
+
return;
|
|
819
|
+
const expensive = report.mode === 'speculative' &&
|
|
820
|
+
(report.fallbacks > 0 ||
|
|
821
|
+
(report.regions > 1 && report.replayed / (report.regions - 1) >= 0.5));
|
|
822
|
+
if (!expensive) {
|
|
823
|
+
this.#routing.delete(key);
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
const failures = Math.min(2, (this.#routing.get(key)?.failures ?? 0) + 1);
|
|
827
|
+
if (!this.#routing.has(key) && this.#routing.size >= 256)
|
|
828
|
+
this.#routing.delete(this.#routing.keys().next().value);
|
|
829
|
+
this.#routing.set(key, { failures, remaining: failures >= 2 ? 16 : 0 });
|
|
830
|
+
}
|
|
514
831
|
async #runControlled(entry, args, width) {
|
|
515
832
|
if (this.#closed)
|
|
516
833
|
throw new Error('folio is closed');
|
|
@@ -820,7 +1137,12 @@ export class Folio {
|
|
|
820
1137
|
this.#pending.clear();
|
|
821
1138
|
this.#closing = Promise.all([
|
|
822
1139
|
...this.#workers.map((worker) => worker.terminate()),
|
|
823
|
-
...Array.from(this.#nativePending, (pending) => pending.catch(() => { }))
|
|
1140
|
+
...Array.from(this.#nativePending, (pending) => pending.catch(() => { })),
|
|
1141
|
+
...(this.#iteration
|
|
1142
|
+
? [
|
|
1143
|
+
(this.#iteration.opening ?? Promise.resolve()).then(() => this.#iteration.pool?.close())
|
|
1144
|
+
]
|
|
1145
|
+
: [])
|
|
824
1146
|
]).then(() => { });
|
|
825
1147
|
return this.#closing;
|
|
826
1148
|
}
|
package/dist/tasks.js
CHANGED
|
@@ -135,9 +135,22 @@ export class TaskPool {
|
|
|
135
135
|
const seq = this.#seq++;
|
|
136
136
|
const id = `${worker}:${seq}`;
|
|
137
137
|
const submitMs = now();
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
// postMessage shares a SharedArrayBuffer instead of copying it
|
|
139
|
+
const posted = typeof input !== 'string' && input.buffer instanceof SharedArrayBuffer
|
|
140
|
+
? Uint8Array.from(input)
|
|
141
|
+
: input;
|
|
142
|
+
this.#owner[worker].port.postMessage({
|
|
143
|
+
id,
|
|
144
|
+
name,
|
|
145
|
+
input: posted,
|
|
146
|
+
submitMs
|
|
147
|
+
});
|
|
148
|
+
const sampled = this.#audit && seq % this.#audit.every === 0;
|
|
149
|
+
const audited = sampled
|
|
150
|
+
? typeof input === 'string' || posted !== input
|
|
151
|
+
? posted
|
|
152
|
+
: Uint8Array.from(input)
|
|
153
|
+
: undefined;
|
|
141
154
|
this.#pending.set(id, {
|
|
142
155
|
id,
|
|
143
156
|
worker,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gmitch215/folio",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Shared WASM memory and ordered speculative execution across isolates",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"webassembly",
|
|
@@ -58,6 +58,10 @@
|
|
|
58
58
|
"types": "./dist/tasks.d.ts",
|
|
59
59
|
"import": "./dist/tasks.js"
|
|
60
60
|
},
|
|
61
|
+
"./context": {
|
|
62
|
+
"types": "./dist/context.d.ts",
|
|
63
|
+
"import": "./dist/context.js"
|
|
64
|
+
},
|
|
61
65
|
"./package.json": "./package.json"
|
|
62
66
|
},
|
|
63
67
|
"files": [
|
|
@@ -73,7 +77,7 @@
|
|
|
73
77
|
"build:c": "bun scripts/build/fixtures.ts",
|
|
74
78
|
"build:prepared": "bun scripts/build/prepare.ts",
|
|
75
79
|
"build:experiments": "node experiments/managed/prepare.ts && node experiments/browser/prepare.ts && node experiments/browser/extraction/prepare.ts && node experiments/consumer/prepare.ts kernel-control",
|
|
76
|
-
"typecheck": "bun run build:c && bun run build:experiments && tsc --noEmit -p tsconfig.json && tsc --noEmit -p experiments/cloud/tsconfig.json && tsc --noEmit -p experiments/managed/tsconfig.json && tsc --noEmit -p experiments/browser/tsconfig.json && tsc --noEmit -p experiments/consumer/tsconfig.json",
|
|
80
|
+
"typecheck": "bun run build:c && bun run build:experiments && tsc --noEmit -p tsconfig.json && tsc --noEmit -p experiments/cloud/tsconfig.json && tsc --noEmit -p experiments/managed/tsconfig.json && tsc --noEmit -p experiments/browser/tsconfig.json && tsc --noEmit -p experiments/consumer/tsconfig.json && tsc --noEmit -p experiments/rpc/tsconfig.json",
|
|
77
81
|
"typecheck:php": "node experiments/consumer/php/prepare.ts && tsc --noEmit -p experiments/consumer/php/tsconfig.json",
|
|
78
82
|
"typecheck:bootimage": "node experiments/bootimage/prepare.ts && tsc --noEmit -p experiments/bootimage/tsconfig.json",
|
|
79
83
|
"test": "vitest run --project unit",
|