@gmitch215/folio 1.0.0 → 1.1.0
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 +455 -26
- package/dist/affine-state.d.ts +171 -0
- package/dist/affine-state.js +763 -0
- package/dist/affine.d.ts +160 -0
- package/dist/affine.js +571 -0
- package/dist/bake.d.ts +8 -3
- package/dist/bake.js +5 -2
- package/dist/boundaries.d.ts +59 -0
- package/dist/boundaries.js +127 -0
- package/dist/certified.d.ts +66 -0
- package/dist/certified.js +192 -0
- package/dist/certify.d.ts +98 -0
- package/dist/certify.js +2628 -0
- package/dist/compiler.d.ts +37 -1
- package/dist/compiler.js +116 -1
- package/dist/context.d.ts +47 -0
- package/dist/context.js +139 -0
- package/dist/coordinator.d.ts +1 -1
- package/dist/coordinator.js +54 -5
- package/dist/extract.d.ts +114 -1
- package/dist/extract.js +1011 -242
- package/dist/fold.d.ts +32 -0
- package/dist/fold.js +105 -0
- package/dist/frames.d.ts +20 -2
- package/dist/frames.js +205 -10
- package/dist/inline.d.ts +182 -0
- package/dist/inline.js +766 -0
- package/dist/iteration-admission.d.ts +185 -0
- package/dist/iteration-admission.js +201 -0
- package/dist/iteration-layout.d.ts +135 -0
- package/dist/iteration-layout.js +169 -0
- package/dist/iteration-owner.d.ts +45 -0
- package/dist/iteration-owner.js +223 -0
- package/dist/iteration-worker.d.ts +1 -0
- package/dist/iteration-worker.js +216 -0
- package/dist/iteration.d.ts +329 -0
- package/dist/iteration.js +1210 -0
- package/dist/lease.d.ts +45 -0
- package/dist/lease.js +274 -0
- package/dist/managed.d.ts +78 -0
- package/dist/managed.js +448 -27
- package/dist/memo.d.ts +99 -0
- package/dist/memo.js +250 -0
- package/dist/node.d.ts +61 -5
- package/dist/node.js +421 -23
- package/dist/ownership-redirect.d.ts +30 -0
- package/dist/ownership-redirect.js +179 -0
- package/dist/ownership-slices.d.ts +44 -0
- package/dist/ownership-slices.js +72 -0
- package/dist/ownership.d.ts +161 -0
- package/dist/ownership.js +6568 -0
- package/dist/ranges.d.ts +78 -0
- package/dist/ranges.js +263 -0
- package/dist/read-scan.d.ts +18 -0
- package/dist/read-scan.js +94 -0
- package/dist/read-windows.d.ts +103 -0
- package/dist/read-windows.js +244 -0
- package/dist/router-worker.d.ts +1 -0
- package/dist/router-worker.js +4 -0
- package/dist/router.d.ts +203 -0
- package/dist/router.js +667 -0
- package/dist/runs.d.ts +44 -0
- package/dist/runs.js +182 -0
- package/dist/slices-worker.d.ts +1 -0
- package/dist/slices-worker.js +38 -0
- package/dist/slices.d.ts +126 -0
- package/dist/slices.js +441 -0
- package/dist/tasks.d.ts +14 -4
- package/dist/tasks.js +23 -9
- package/dist/transaction.d.ts +10 -0
- package/package.json +44 -4
package/dist/compiler.d.ts
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
|
-
import type { AdmissionOptions, ControlPlan } from './extract.ts';
|
|
1
|
+
import type { AdmissionOptions, ControlPlan, IterationRoute, StackRoute } from './extract.ts';
|
|
2
2
|
import type { NativePlan } from './guards.ts';
|
|
3
|
+
/** Offline exports used to checkpoint a private instance; function bodies remain unchanged. */
|
|
4
|
+
export interface PreparedContext {
|
|
5
|
+
/** The module as passed in. */
|
|
6
|
+
original: Uint8Array;
|
|
7
|
+
/** The module to instantiate: the original plus the added exports, or the original on refusal. */
|
|
8
|
+
bytes: Uint8Array;
|
|
9
|
+
/** Export names of every memory. */
|
|
10
|
+
memories: string[];
|
|
11
|
+
/** Export names of every mutable numeric global. */
|
|
12
|
+
globals: string[];
|
|
13
|
+
/** Export names of every table. */
|
|
14
|
+
tables: string[];
|
|
15
|
+
/** A refusal retains original bytes so the caller can recreate a context instead. */
|
|
16
|
+
reasons: string[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Exposes Wasm memories, mutable globals and tables for private-context reset. This adds exports
|
|
20
|
+
* only; start functions and code run normally. Unsupported metadata retains the original module.
|
|
21
|
+
* Host effects, passive segment drop state and objects behind references require recreation or
|
|
22
|
+
* a caller-owned reset. Modules using segment drop are refused for checkpoint reuse.
|
|
23
|
+
* @since 1.0.1
|
|
24
|
+
*/
|
|
25
|
+
export declare function prepareContext(input: Uint8Array): PreparedContext;
|
|
3
26
|
/** A counted loop the splitter can divide: the count parameter and the affine pointer steps. */
|
|
4
27
|
export interface LoopPlan {
|
|
5
28
|
/** Index of the parameter holding the iteration count. */
|
|
@@ -30,6 +53,19 @@ export interface PreparedProgram {
|
|
|
30
53
|
control?: ControlPlan;
|
|
31
54
|
/** Why control-flow extraction produced no plan; the counted-loop plans above are unaffected. */
|
|
32
55
|
controlReasons?: string[];
|
|
56
|
+
/**
|
|
57
|
+
* The stack-pointer route: the worker-side function set when one mutable i32 global is private
|
|
58
|
+
* and recursion is allowed. It ignores `reasons` and nothing consumes it yet.
|
|
59
|
+
*
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
stackRoute?: StackRoute;
|
|
63
|
+
/**
|
|
64
|
+
* One loop of independent calls into the stack route's worker set; inert like `stackRoute`.
|
|
65
|
+
*
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
iterationRoute?: IterationRoute;
|
|
33
69
|
}
|
|
34
70
|
/** Plan options: whether to optimize, and when a region is worth dispatching. */
|
|
35
71
|
export interface PrepareOptions extends AdmissionOptions {
|
package/dist/compiler.js
CHANGED
|
@@ -1,6 +1,119 @@
|
|
|
1
1
|
import binaryen from 'binaryen';
|
|
2
|
-
import { admission, extract } from "./extract.js";
|
|
2
|
+
import { admission, extract, iterationRoute, stackRoute } from "./extract.js";
|
|
3
3
|
import { nativePlan } from "./guards.js";
|
|
4
|
+
/**
|
|
5
|
+
* Exposes Wasm memories, mutable globals and tables for private-context reset. This adds exports
|
|
6
|
+
* only; start functions and code run normally. Unsupported metadata retains the original module.
|
|
7
|
+
* Host effects, passive segment drop state and objects behind references require recreation or
|
|
8
|
+
* a caller-owned reset. Modules using segment drop are refused for checkpoint reuse.
|
|
9
|
+
* @since 1.0.1
|
|
10
|
+
*/
|
|
11
|
+
export function prepareContext(input) {
|
|
12
|
+
const original = Uint8Array.from(input), native = new WebAssembly.Module(original);
|
|
13
|
+
const result = {
|
|
14
|
+
original,
|
|
15
|
+
bytes: original,
|
|
16
|
+
memories: [],
|
|
17
|
+
globals: [],
|
|
18
|
+
tables: [],
|
|
19
|
+
reasons: []
|
|
20
|
+
};
|
|
21
|
+
let mod;
|
|
22
|
+
try {
|
|
23
|
+
mod = binaryen.readBinary(original);
|
|
24
|
+
mod.setFeatures(binaryen.Features.All);
|
|
25
|
+
// passive segment and gc state cannot be restored through exported bindings
|
|
26
|
+
const text = mod.emitText();
|
|
27
|
+
if (/\((?:data|elem)\.drop(?:\s|\))/.test(text))
|
|
28
|
+
throw new TypeError('passive segment drop state requires recreation');
|
|
29
|
+
if (/\((?:struct|array)\./.test(text))
|
|
30
|
+
throw new TypeError('Wasm GC object state requires recreation');
|
|
31
|
+
const exports = new Set(WebAssembly.Module.exports(native).map((item) => item.name));
|
|
32
|
+
let prefix = '__folio_context_';
|
|
33
|
+
while ([...exports].some((name) => name.startsWith(prefix)))
|
|
34
|
+
prefix += '_';
|
|
35
|
+
const additions = [];
|
|
36
|
+
const leb = (value) => {
|
|
37
|
+
const out = [];
|
|
38
|
+
do {
|
|
39
|
+
const byte = value & 127;
|
|
40
|
+
value = Math.floor(value / 128);
|
|
41
|
+
out.push(byte | (value ? 128 : 0));
|
|
42
|
+
} while (value);
|
|
43
|
+
return out;
|
|
44
|
+
};
|
|
45
|
+
const add = (kind, index, names, label) => {
|
|
46
|
+
const name = `${prefix}${label}${index}`, encoded = new TextEncoder().encode(name);
|
|
47
|
+
additions.push(...leb(encoded.length), ...encoded, kind, ...leb(index));
|
|
48
|
+
names.push(name);
|
|
49
|
+
};
|
|
50
|
+
let cursor = 8, definedMemories = 0;
|
|
51
|
+
const unsigned = () => {
|
|
52
|
+
let value = 0, shift = 0, byte;
|
|
53
|
+
do {
|
|
54
|
+
byte = original[cursor++];
|
|
55
|
+
value += (byte & 127) * 2 ** shift;
|
|
56
|
+
shift += 7;
|
|
57
|
+
} while (byte & 128);
|
|
58
|
+
return value;
|
|
59
|
+
};
|
|
60
|
+
const sections = [];
|
|
61
|
+
while (cursor < original.length) {
|
|
62
|
+
const start = cursor, kind = original[cursor++], length = unsigned(), payload = cursor, end = cursor + length;
|
|
63
|
+
if (kind === 5)
|
|
64
|
+
definedMemories = unsigned();
|
|
65
|
+
sections.push({ kind, start, payload, end });
|
|
66
|
+
cursor = end;
|
|
67
|
+
}
|
|
68
|
+
const memoryCount = definedMemories +
|
|
69
|
+
WebAssembly.Module.imports(native).filter((item) => item.kind === 'memory').length;
|
|
70
|
+
for (let index = 0; index < memoryCount; index++)
|
|
71
|
+
add(2, index, result.memories, 'memory');
|
|
72
|
+
for (let index = 0; index < mod.getNumGlobals(); index++) {
|
|
73
|
+
const global = binaryen.getGlobalInfo(mod.getGlobalByIndex(index));
|
|
74
|
+
if (![binaryen.i32, binaryen.i64, binaryen.f32, binaryen.f64, binaryen.v128].includes(global.type))
|
|
75
|
+
throw new TypeError('reference global state requires recreation');
|
|
76
|
+
if (!global.mutable)
|
|
77
|
+
continue;
|
|
78
|
+
if (global.type === binaryen.v128)
|
|
79
|
+
throw new TypeError('v128 globals require recreation');
|
|
80
|
+
add(3, index, result.globals, 'global');
|
|
81
|
+
}
|
|
82
|
+
for (let index = 0; index < mod.getNumTables(); index++)
|
|
83
|
+
add(1, index, result.tables, 'table');
|
|
84
|
+
const prior = sections.find((section) => section.kind === 7);
|
|
85
|
+
let count = 0, records = new Uint8Array();
|
|
86
|
+
if (prior) {
|
|
87
|
+
cursor = prior.payload;
|
|
88
|
+
count = unsigned();
|
|
89
|
+
records = original.subarray(cursor, prior.end);
|
|
90
|
+
}
|
|
91
|
+
const addedCount = result.memories.length + result.globals.length + result.tables.length;
|
|
92
|
+
const payload = Uint8Array.from([...leb(count + addedCount), ...records, ...additions]);
|
|
93
|
+
const header = Uint8Array.from([7, ...leb(payload.length)]);
|
|
94
|
+
const position = prior?.start ??
|
|
95
|
+
sections.find((section) => [8, 9, 12, 10, 11].includes(section.kind))?.start ??
|
|
96
|
+
original.length;
|
|
97
|
+
const rest = prior?.end ?? position;
|
|
98
|
+
const bytes = new Uint8Array(position + header.length + payload.length + original.length - rest);
|
|
99
|
+
bytes.set(original.subarray(0, position));
|
|
100
|
+
bytes.set(header, position);
|
|
101
|
+
bytes.set(payload, position + header.length);
|
|
102
|
+
bytes.set(original.subarray(rest), position + header.length + payload.length);
|
|
103
|
+
new WebAssembly.Module(bytes);
|
|
104
|
+
result.bytes = bytes;
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
result.memories = [];
|
|
108
|
+
result.globals = [];
|
|
109
|
+
result.tables = [];
|
|
110
|
+
result.reasons.push(error instanceof Error ? error.message : String(error));
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
mod?.dispose();
|
|
114
|
+
}
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
4
117
|
const info = (expression) => {
|
|
5
118
|
const id = binaryen.getExpressionId(expression);
|
|
6
119
|
if (id === binaryen.UnreachableId || id === binaryen.NopId) {
|
|
@@ -500,6 +613,8 @@ export function prepare(bytes, options = {}) {
|
|
|
500
613
|
catch (error) {
|
|
501
614
|
prepared.reasons.push(`control-flow extraction unavailable: ${error instanceof Error ? error.message : String(error)}`);
|
|
502
615
|
}
|
|
616
|
+
prepared.stackRoute = stackRoute(prepared.original, options);
|
|
617
|
+
prepared.iterationRoute = iterationRoute(prepared.original, prepared.stackRoute);
|
|
503
618
|
}
|
|
504
619
|
return prepared;
|
|
505
620
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { PreparedContext } from './compiler.ts';
|
|
2
|
+
/** A changed memory or table size requires a newly instantiated context. */
|
|
3
|
+
export declare class ContextResetError extends Error {
|
|
4
|
+
/** Which binding changed size. */
|
|
5
|
+
readonly kind: 'memory-size' | 'table-size';
|
|
6
|
+
constructor(kind: 'memory-size' | 'table-size');
|
|
7
|
+
}
|
|
8
|
+
/** Memory traffic and state restored by a checkpoint operation. */
|
|
9
|
+
export interface ContextResetReport {
|
|
10
|
+
/** Bytes compared; zero for a full restore. */
|
|
11
|
+
checkedBytes: number;
|
|
12
|
+
/** Bytes written back to memory. */
|
|
13
|
+
copiedBytes: number;
|
|
14
|
+
/** 64 KiB pages written back. */
|
|
15
|
+
changedPages: number;
|
|
16
|
+
/** Globals whose value was restored. */
|
|
17
|
+
globals: number;
|
|
18
|
+
/** Table slots whose reference was restored. */
|
|
19
|
+
tableSlots: number;
|
|
20
|
+
/** Bytes inside `overwrite` ranges, neither compared nor restored. */
|
|
21
|
+
skippedBytes: number;
|
|
22
|
+
}
|
|
23
|
+
/** Bytes of one context memory that the caller writes completely before anything reads them. */
|
|
24
|
+
export interface ContextRange {
|
|
25
|
+
/** Index into the plan's memories. The default is 0. */
|
|
26
|
+
memory?: number;
|
|
27
|
+
offset: number;
|
|
28
|
+
length: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Captures private Wasm state after initialization. `prepareContext` exposes hidden bindings.
|
|
32
|
+
* Each restore checks current bytes, so different inputs may dirty different pages. Callers own
|
|
33
|
+
* exclusive access and reset host resources and objects behind externref values separately.
|
|
34
|
+
* Memory/table growth requires recreation because Wasm cannot shrink those bindings.
|
|
35
|
+
* @since 1.0.1
|
|
36
|
+
*/
|
|
37
|
+
export declare class InstanceCheckpoint {
|
|
38
|
+
#private;
|
|
39
|
+
constructor(instance: WebAssembly.Instance, plan: PreparedContext);
|
|
40
|
+
/**
|
|
41
|
+
* Restores all mutable bindings. `full` skips page comparison and copies every memory byte.
|
|
42
|
+
* Bytes inside `overwrite` are left as they are: the caller must write every one of them
|
|
43
|
+
* before the program reads it, so a task that rewrites its input region each time does not pay
|
|
44
|
+
* to compare and copy back the previous task's input.
|
|
45
|
+
*/
|
|
46
|
+
restore(strategy?: 'changed' | 'full', overwrite?: ContextRange[]): ContextResetReport;
|
|
47
|
+
}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
/** A changed memory or table size requires a newly instantiated context. */
|
|
3
|
+
export class ContextResetError extends Error {
|
|
4
|
+
/** Which binding changed size. */
|
|
5
|
+
kind;
|
|
6
|
+
constructor(kind) {
|
|
7
|
+
super(`context ${kind} changed; instantiate and initialize a new context`);
|
|
8
|
+
this.name = 'ContextResetError';
|
|
9
|
+
this.kind = kind;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Captures private Wasm state after initialization. `prepareContext` exposes hidden bindings.
|
|
14
|
+
* Each restore checks current bytes, so different inputs may dirty different pages. Callers own
|
|
15
|
+
* exclusive access and reset host resources and objects behind externref values separately.
|
|
16
|
+
* Memory/table growth requires recreation because Wasm cannot shrink those bindings.
|
|
17
|
+
* @since 1.0.1
|
|
18
|
+
*/
|
|
19
|
+
export class InstanceCheckpoint {
|
|
20
|
+
#memories;
|
|
21
|
+
#globals;
|
|
22
|
+
#tables;
|
|
23
|
+
constructor(instance, plan) {
|
|
24
|
+
if (plan.reasons.length)
|
|
25
|
+
throw new TypeError(`context requires recreation: ${plan.reasons.join('; ')}`);
|
|
26
|
+
this.#memories = plan.memories.map((name) => {
|
|
27
|
+
const memory = instance.exports[name];
|
|
28
|
+
if (!(memory instanceof WebAssembly.Memory))
|
|
29
|
+
throw new TypeError(`missing context memory ${name}`);
|
|
30
|
+
return { memory, bytes: Uint8Array.from(new Uint8Array(memory.buffer)) };
|
|
31
|
+
});
|
|
32
|
+
this.#globals = plan.globals.map((name) => {
|
|
33
|
+
const global = instance.exports[name];
|
|
34
|
+
if (!(global instanceof WebAssembly.Global))
|
|
35
|
+
throw new TypeError(`missing context global ${name}`);
|
|
36
|
+
return { global, value: global.value };
|
|
37
|
+
});
|
|
38
|
+
this.#tables = plan.tables.map((name) => {
|
|
39
|
+
const table = instance.exports[name];
|
|
40
|
+
if (!(table instanceof WebAssembly.Table))
|
|
41
|
+
throw new TypeError(`missing context table ${name}`);
|
|
42
|
+
return {
|
|
43
|
+
table,
|
|
44
|
+
slots: Array.from({ length: table.length }, (_, index) => table.get(index))
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Restores all mutable bindings. `full` skips page comparison and copies every memory byte.
|
|
50
|
+
* Bytes inside `overwrite` are left as they are: the caller must write every one of them
|
|
51
|
+
* before the program reads it, so a task that rewrites its input region each time does not pay
|
|
52
|
+
* to compare and copy back the previous task's input.
|
|
53
|
+
*/
|
|
54
|
+
restore(strategy = 'changed', overwrite = []) {
|
|
55
|
+
if (strategy !== 'changed' && strategy !== 'full')
|
|
56
|
+
throw new TypeError('invalid context reset strategy');
|
|
57
|
+
for (const { memory, bytes } of this.#memories)
|
|
58
|
+
if (memory.buffer.byteLength !== bytes.length)
|
|
59
|
+
throw new ContextResetError('memory-size');
|
|
60
|
+
for (const { table, slots } of this.#tables)
|
|
61
|
+
if (table.length !== slots.length)
|
|
62
|
+
throw new ContextResetError('table-size');
|
|
63
|
+
const skipped = this.#ranges(overwrite);
|
|
64
|
+
const report = {
|
|
65
|
+
checkedBytes: 0,
|
|
66
|
+
copiedBytes: 0,
|
|
67
|
+
changedPages: 0,
|
|
68
|
+
globals: 0,
|
|
69
|
+
tableSlots: 0,
|
|
70
|
+
skippedBytes: 0
|
|
71
|
+
};
|
|
72
|
+
this.#memories.forEach(({ memory, bytes }, index) => {
|
|
73
|
+
const target = new Uint8Array(memory.buffer);
|
|
74
|
+
let from = 0;
|
|
75
|
+
for (const [start, end] of [
|
|
76
|
+
...skipped[index],
|
|
77
|
+
[bytes.length, bytes.length]
|
|
78
|
+
]) {
|
|
79
|
+
for (let offset = from; offset < start; offset += 65536) {
|
|
80
|
+
const stop = Math.min(offset + 65536, start);
|
|
81
|
+
const source = bytes.subarray(offset, stop), current = target.subarray(offset, stop);
|
|
82
|
+
if (strategy === 'changed')
|
|
83
|
+
report.checkedBytes += current.length;
|
|
84
|
+
if (strategy === 'full' ||
|
|
85
|
+
!Buffer.from(current.buffer, current.byteOffset, current.length).equals(Buffer.from(source.buffer, source.byteOffset, source.length))) {
|
|
86
|
+
current.set(source);
|
|
87
|
+
report.copiedBytes += current.length;
|
|
88
|
+
report.changedPages++;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
report.skippedBytes += end - start;
|
|
92
|
+
from = end;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
for (const { global, value } of this.#globals) {
|
|
96
|
+
if (!Object.is(global.value, value)) {
|
|
97
|
+
global.value = value;
|
|
98
|
+
report.globals++;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
for (const { table, slots } of this.#tables)
|
|
102
|
+
for (let index = 0; index < slots.length; index++)
|
|
103
|
+
if (!Object.is(table.get(index), slots[index])) {
|
|
104
|
+
table.set(index, slots[index]);
|
|
105
|
+
report.tableSlots++;
|
|
106
|
+
}
|
|
107
|
+
return report;
|
|
108
|
+
}
|
|
109
|
+
/** Validates ranges and returns, per memory, sorted and merged [start, end) pairs. */
|
|
110
|
+
#ranges(overwrite) {
|
|
111
|
+
const merged = this.#memories.map(() => []);
|
|
112
|
+
for (const { memory = 0, offset, length } of overwrite) {
|
|
113
|
+
const size = this.#memories[memory]?.bytes.length;
|
|
114
|
+
if (size === undefined)
|
|
115
|
+
throw new RangeError('overwrite range names a missing memory');
|
|
116
|
+
if (!Number.isSafeInteger(offset) ||
|
|
117
|
+
!Number.isSafeInteger(length) ||
|
|
118
|
+
offset < 0 ||
|
|
119
|
+
length < 0)
|
|
120
|
+
throw new RangeError('overwrite range needs a non-negative integer offset and length');
|
|
121
|
+
if (offset + length > size)
|
|
122
|
+
throw new RangeError('overwrite range exceeds the memory');
|
|
123
|
+
if (length)
|
|
124
|
+
merged[memory].push([offset, offset + length]);
|
|
125
|
+
}
|
|
126
|
+
return merged.map((pairs) => {
|
|
127
|
+
pairs.sort((a, b) => a[0] - b[0]);
|
|
128
|
+
const out = [];
|
|
129
|
+
for (const [start, end] of pairs) {
|
|
130
|
+
const last = out[out.length - 1];
|
|
131
|
+
if (last && start <= last[1])
|
|
132
|
+
last[1] = Math.max(last[1], end);
|
|
133
|
+
else
|
|
134
|
+
out.push([start, end]);
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
package/dist/coordinator.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare class Coordinator {
|
|
|
17
17
|
pending?: {
|
|
18
18
|
value: unknown;
|
|
19
19
|
};
|
|
20
|
-
dispatch: (worker: number, calls: RegionCall[]) => Promise<TransactionResult[]>;
|
|
20
|
+
dispatch: (worker: number, calls: RegionCall[], generation: number) => Promise<TransactionResult[]>;
|
|
21
21
|
native: (call: RegionCall) => void;
|
|
22
22
|
});
|
|
23
23
|
enqueue(call: RegionCall): Promise<void> | undefined;
|
package/dist/coordinator.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DirtyRuns, runBytes } from "./runs.js";
|
|
1
2
|
function trap(result) {
|
|
2
3
|
if (!result.trap)
|
|
3
4
|
return;
|
|
@@ -20,6 +21,7 @@ export class Coordinator {
|
|
|
20
21
|
#pending;
|
|
21
22
|
#armed = false;
|
|
22
23
|
#polls = 0;
|
|
24
|
+
#generation = 0;
|
|
23
25
|
#flushing;
|
|
24
26
|
constructor(options) {
|
|
25
27
|
this.#pending = options.pending;
|
|
@@ -127,11 +129,13 @@ export class Coordinator {
|
|
|
127
129
|
const width = Math.min(this.#width, batch.length);
|
|
128
130
|
this.report.workers = Math.max(this.report.workers, width);
|
|
129
131
|
const lanes = Array.from({ length: width }, (_, lane) => batch.filter((_, index) => index % width === lane));
|
|
130
|
-
const
|
|
132
|
+
const generation = ++this.#generation;
|
|
133
|
+
const outcomes = await Promise.allSettled(lanes.map((calls, i) => this.#dispatch(i, calls, generation)));
|
|
131
134
|
const failure = outcomes.find((outcome) => outcome.status === 'rejected');
|
|
132
135
|
if (failure?.status === 'rejected')
|
|
133
136
|
throw failure.reason;
|
|
134
137
|
const dirty = new Set();
|
|
138
|
+
const dirtyRuns = new DirtyRuns();
|
|
135
139
|
const canonical = new Uint8Array(this.#memory.buffer);
|
|
136
140
|
const shared = new Uint8Array(this.#workerMemory.buffer);
|
|
137
141
|
const observe = (result) => {
|
|
@@ -139,8 +143,12 @@ export class Coordinator {
|
|
|
139
143
|
return;
|
|
140
144
|
this.#profile.workerWallMs.push(result.timing.wallMs);
|
|
141
145
|
this.#profile.workerCpuMs.push(result.timing.cpuMs);
|
|
142
|
-
this.#profile.readBytes += result.
|
|
143
|
-
|
|
146
|
+
this.#profile.readBytes += result.readRuns
|
|
147
|
+
? runBytes(result.readRuns)
|
|
148
|
+
: result.reads.length;
|
|
149
|
+
this.#profile.writeBytes += result.writeData
|
|
150
|
+
? result.writeData.length
|
|
151
|
+
: result.writes.length;
|
|
144
152
|
};
|
|
145
153
|
const commit = (result) => {
|
|
146
154
|
const start = this.#profile ? performance.now() : 0;
|
|
@@ -150,6 +158,28 @@ export class Coordinator {
|
|
|
150
158
|
shared[address] = value;
|
|
151
159
|
dirty.add(address);
|
|
152
160
|
}
|
|
161
|
+
if (result.writeRuns) {
|
|
162
|
+
let at = 0;
|
|
163
|
+
for (let i = 0; i < result.writeRuns.length; i += 2) {
|
|
164
|
+
const start = result.writeRuns[i], length = result.writeRuns[i + 1];
|
|
165
|
+
const data = result.writeData;
|
|
166
|
+
const mirror = this.#memory !== this.#workerMemory;
|
|
167
|
+
if (length > 16) {
|
|
168
|
+
const piece = data.subarray(at, at + length);
|
|
169
|
+
canonical.set(piece, start);
|
|
170
|
+
if (mirror)
|
|
171
|
+
shared.set(piece, start);
|
|
172
|
+
}
|
|
173
|
+
else
|
|
174
|
+
for (let k = 0; k < length; k++) {
|
|
175
|
+
canonical[start + k] = data[at + k];
|
|
176
|
+
if (mirror)
|
|
177
|
+
shared[start + k] = data[at + k];
|
|
178
|
+
}
|
|
179
|
+
dirtyRuns.add(start, length);
|
|
180
|
+
at += length;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
153
183
|
if (this.#profile)
|
|
154
184
|
this.#profile.commitMs += performance.now() - start;
|
|
155
185
|
};
|
|
@@ -158,7 +188,13 @@ export class Coordinator {
|
|
|
158
188
|
let result = outcome.value[Math.floor(i / width)];
|
|
159
189
|
observe(result);
|
|
160
190
|
const start = this.#profile ? performance.now() : 0;
|
|
161
|
-
|
|
191
|
+
// a batch's results share one encoding; both paths stay exact if they ever mix
|
|
192
|
+
const stored = dirtyRuns.count > 0;
|
|
193
|
+
const conflict = result.reads.some((address) => dirty.has(address) || (stored && dirtyRuns.has(address))) ||
|
|
194
|
+
(result.readRuns !== undefined &&
|
|
195
|
+
(dirtyRuns.overlaps(result.readRuns) ||
|
|
196
|
+
(dirty.size > 0 &&
|
|
197
|
+
[...dirty].some((address) => overlapsRuns(result.readRuns, address)))));
|
|
162
198
|
if (this.#profile)
|
|
163
199
|
this.#profile.validationMs += performance.now() - start;
|
|
164
200
|
if (result.exhausted || conflict) {
|
|
@@ -170,7 +206,7 @@ export class Coordinator {
|
|
|
170
206
|
}
|
|
171
207
|
if (!result.exhausted) {
|
|
172
208
|
const replayStart = this.#profile ? performance.now() : 0;
|
|
173
|
-
result = (await this.#dispatch(0, [batch[i]]))[0];
|
|
209
|
+
result = (await this.#dispatch(0, [batch[i]], ++this.#generation))[0];
|
|
174
210
|
observe(result);
|
|
175
211
|
if (this.#profile)
|
|
176
212
|
this.#profile.replayMs += performance.now() - replayStart;
|
|
@@ -187,3 +223,16 @@ export class Coordinator {
|
|
|
187
223
|
}
|
|
188
224
|
}
|
|
189
225
|
}
|
|
226
|
+
function overlapsRuns(runs, address) {
|
|
227
|
+
let low = 0, high = runs.length / 2 - 1;
|
|
228
|
+
while (low <= high) {
|
|
229
|
+
const mid = (low + high) >> 1;
|
|
230
|
+
if (runs[mid * 2] > address)
|
|
231
|
+
high = mid - 1;
|
|
232
|
+
else if (runs[mid * 2] + runs[mid * 2 + 1] <= address)
|
|
233
|
+
low = mid + 1;
|
|
234
|
+
else
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
return false;
|
|
238
|
+
}
|
package/dist/extract.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export interface AdmissionOptions {
|
|
|
8
8
|
/** Iterations assumed for a loop whose bound is neither a constant nor a parameter. */
|
|
9
9
|
assumedTripCount?: number;
|
|
10
10
|
}
|
|
11
|
-
/** Calibrated on Apple M2 Pro; see `results/
|
|
11
|
+
/** Calibrated on Apple M2 Pro; see `results/execution/admission/admission-calibration.summary.json`. */
|
|
12
12
|
export declare const DEFAULT_MINIMUM_REGION_WORK = 200000;
|
|
13
13
|
export declare const DEFAULT_ASSUMED_TRIP_COUNT = 1000;
|
|
14
14
|
export interface ControlPlan {
|
|
@@ -69,3 +69,116 @@ export declare function extract(original: Uint8Array, prepareTracked: (bytes: Ui
|
|
|
69
69
|
accesses: number;
|
|
70
70
|
}) => void;
|
|
71
71
|
}): ControlPlan | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* The stack route: functions a worker could run when it holds a private copy of one mutable i32
|
|
74
|
+
* global, the stack pointer, and calls may recurse. Nothing consumes it yet, and it never changes
|
|
75
|
+
* the control plan or the module-wide reasons.
|
|
76
|
+
*/
|
|
77
|
+
export interface StackRoute {
|
|
78
|
+
/** True when at least one function can run on a worker and no global is refused for the module. */
|
|
79
|
+
eligible: boolean;
|
|
80
|
+
/** The one admitted mutable global and its initial value, when a function reads it. */
|
|
81
|
+
stack?: {
|
|
82
|
+
name: string;
|
|
83
|
+
initial: number;
|
|
84
|
+
};
|
|
85
|
+
/** Immutable globals with a constant initializer that the set reads. */
|
|
86
|
+
constants: string[];
|
|
87
|
+
/** Delta globals of an ownership redirect that the set reads; the worker module exports them. */
|
|
88
|
+
redirect?: string[];
|
|
89
|
+
/** The worker set: closed under calls, in module order. */
|
|
90
|
+
functions: string[];
|
|
91
|
+
/** The members that sit on a call cycle. */
|
|
92
|
+
recursive: string[];
|
|
93
|
+
/** Tables a member calls through; every worker holds its own copy, built from the segments. */
|
|
94
|
+
tables: string[];
|
|
95
|
+
/** Static work per member; a recursive member counts one level of its own calls. */
|
|
96
|
+
work: Record<string, Work>;
|
|
97
|
+
/** Why the module is or is not eligible. */
|
|
98
|
+
diagnostics: string[];
|
|
99
|
+
}
|
|
100
|
+
/** Name prefix of the mutable delta globals an ownership redirect adds; the stack route sets them aside. */
|
|
101
|
+
export declare const redirectDelta = "__folio_od";
|
|
102
|
+
/** What an ownership redirect hands the iteration executor: windows with their delta globals and the copy-out. */
|
|
103
|
+
export interface OwnershipPlan {
|
|
104
|
+
/** each window `[low, end)` and the exported mutable global that holds `copy - low` */
|
|
105
|
+
windows: {
|
|
106
|
+
low: number;
|
|
107
|
+
end: number;
|
|
108
|
+
delta: string;
|
|
109
|
+
}[];
|
|
110
|
+
/** byte ranges the last record's copy supplies */
|
|
111
|
+
copyOut: [number, number][];
|
|
112
|
+
}
|
|
113
|
+
/** Computes the stack route of a module. A failure is reported in `diagnostics`, never thrown. */
|
|
114
|
+
export declare function stackRoute(original: Uint8Array, options?: AdmissionOptions): StackRoute;
|
|
115
|
+
/** One local of the owner that the schedule or the outlined body needs. */
|
|
116
|
+
export interface IterationLocal {
|
|
117
|
+
local: number;
|
|
118
|
+
/** `induction` is the trip counter; `stride` advances by a constant each iteration */
|
|
119
|
+
kind: 'induction' | 'stride' | 'invariant' | 'frame';
|
|
120
|
+
/** position in the `iterate` argument list; absent when the body never reads the local */
|
|
121
|
+
argument?: number;
|
|
122
|
+
/** where the value at loop entry comes from, when it is an owner parameter or a constant */
|
|
123
|
+
start?: {
|
|
124
|
+
parameter: number;
|
|
125
|
+
} | {
|
|
126
|
+
constant: number;
|
|
127
|
+
};
|
|
128
|
+
/** change per iteration, modulo 2^32; the value for iteration k is `start + k * stride` */
|
|
129
|
+
stride?: number;
|
|
130
|
+
/**
|
|
131
|
+
* a stride held in an owner local the loop never writes: the step is `stride * entry[strideLocal]`
|
|
132
|
+
* with `stride` 1 or -1, read at loop entry, and the value for iteration k is `start + k * step`
|
|
133
|
+
*/
|
|
134
|
+
strideLocal?: number;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The iteration route: one owner function whose single loop runs independent calls into the worker
|
|
138
|
+
* set. It reads the stack route and changes nothing; `Folio.open` consumes it when no older route
|
|
139
|
+
* covers the program. `module` holds the worker set, the stack global and the constants, with
|
|
140
|
+
* `iterate` doing one iteration of the body.
|
|
141
|
+
*
|
|
142
|
+
* The route proves a shape, not independence. A loop-carried dependence through the owner frame
|
|
143
|
+
* is caught at run time: the lowered worker module tracks the frame bytes each iteration reads
|
|
144
|
+
* before it writes them, and validation fails an iteration that read a byte an earlier iteration
|
|
145
|
+
* of its batch wrote. Reads of stale callee stack below the frame and imported host functions that
|
|
146
|
+
* touch memory are not tracked.
|
|
147
|
+
*/
|
|
148
|
+
export interface IterationRoute {
|
|
149
|
+
eligible: boolean;
|
|
150
|
+
owner?: string;
|
|
151
|
+
/** the loop label, when the owner names it */
|
|
152
|
+
loop?: string;
|
|
153
|
+
/** the loop runs the entry value of `local` times; an entry value of zero runs it 2^32 times */
|
|
154
|
+
trip?: {
|
|
155
|
+
local: number;
|
|
156
|
+
parameter?: number;
|
|
157
|
+
constant?: number;
|
|
158
|
+
};
|
|
159
|
+
/** every local the body reads or the schedule advances; `iterate` takes them in `argument` order */
|
|
160
|
+
locals: IterationLocal[];
|
|
161
|
+
/**
|
|
162
|
+
* the owner's frame: `local` holds `global - size` after the prologue and `iterate` takes it last.
|
|
163
|
+
* The image at [global - size, global) is copied into each slot at enqueue; a body that reads a
|
|
164
|
+
* frame byte an earlier iteration wrote is not detected here.
|
|
165
|
+
*/
|
|
166
|
+
frame?: {
|
|
167
|
+
global: string;
|
|
168
|
+
size: number;
|
|
169
|
+
local: number;
|
|
170
|
+
copy: true;
|
|
171
|
+
};
|
|
172
|
+
/** export names in `module` */
|
|
173
|
+
iterate?: string;
|
|
174
|
+
stackExport?: string;
|
|
175
|
+
module?: Uint8Array;
|
|
176
|
+
/** set when `module` carries an ownership redirect: the windows each slot keeps a private copy of */
|
|
177
|
+
ownership?: OwnershipPlan;
|
|
178
|
+
diagnostics: string[];
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Finds an owner whose single loop is a countdown over independent calls into the stack route's
|
|
182
|
+
* worker set and outlines one iteration of it. A refusal is a named diagnostic, never an exception.
|
|
183
|
+
*/
|
|
184
|
+
export declare function iterationRoute(original: Uint8Array, route: StackRoute): IterationRoute;
|