@gmitch215/folio 1.0.1 → 1.2.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 +98 -460
- package/dist/boot.d.ts +1 -0
- package/dist/boot.js +1 -0
- package/dist/browser/index.d.ts +73 -0
- package/dist/browser/index.js +277 -0
- package/dist/browser.d.ts +1 -68
- package/dist/browser.js +1 -274
- package/dist/continuation.d.ts +1 -0
- package/dist/continuation.js +1 -0
- package/dist/{compiler.d.ts → execution/compiler.d.ts} +1 -1
- package/dist/{compiler.js → execution/compiler.js} +5 -12
- package/dist/{coordinator.d.ts → execution/coordinator.d.ts} +1 -1
- package/dist/{coordinator.js → execution/coordinator.js} +55 -8
- package/dist/{extract.d.ts → execution/extract.d.ts} +22 -13
- package/dist/{extract.js → execution/extract.js} +137 -10
- package/dist/execution/fold.d.ts +44 -0
- package/dist/execution/fold.js +105 -0
- package/dist/{guards.d.ts → execution/guards.d.ts} +2 -1
- package/dist/execution/runs.d.ts +44 -0
- package/dist/execution/runs.js +182 -0
- package/dist/{transaction.d.ts → execution/transaction.d.ts} +10 -0
- package/dist/execution/types.d.ts +221 -0
- package/dist/folds/affine-state.d.ts +142 -0
- package/dist/folds/affine-state.js +737 -0
- package/dist/folds/affine.d.ts +148 -0
- package/dist/folds/affine.js +562 -0
- package/dist/folds/boundaries.d.ts +48 -0
- package/dist/folds/boundaries.js +119 -0
- package/dist/folds/certified.d.ts +55 -0
- package/dist/folds/certified.js +178 -0
- package/dist/folds/certify.d.ts +87 -0
- package/dist/folds/certify.js +2611 -0
- package/dist/footprints/memo.d.ts +74 -0
- package/dist/footprints/memo.js +256 -0
- package/dist/footprints/ranges.d.ts +78 -0
- package/dist/footprints/ranges.js +263 -0
- package/dist/footprints/read-scan.d.ts +18 -0
- package/dist/footprints/read-scan.js +94 -0
- package/dist/footprints/read-windows.d.ts +77 -0
- package/dist/footprints/read-windows.js +245 -0
- package/dist/index.d.ts +16 -4
- package/dist/index.js +14 -2
- package/dist/iteration/admission.d.ts +149 -0
- package/dist/iteration/admission.js +201 -0
- package/dist/iteration/index.d.ts +239 -0
- package/dist/iteration/index.js +1210 -0
- package/dist/iteration/inline.d.ts +182 -0
- package/dist/{inline.js → iteration/inline.js} +277 -21
- package/dist/{iteration-owner.d.ts → iteration/owner.d.ts} +16 -3
- package/dist/{iteration-owner.js → iteration/owner.js} +6 -5
- package/dist/iteration/worker.js +216 -0
- package/dist/{managed.d.ts → managed/index.d.ts} +75 -3
- package/dist/managed/index.js +817 -0
- package/dist/managed/outbox.d.ts +128 -0
- package/dist/managed/outbox.js +156 -0
- package/dist/managed/pool.d.ts +221 -0
- package/dist/managed/pool.js +310 -0
- package/dist/managed/tasks.d.ts +154 -0
- package/dist/managed/tasks.js +183 -0
- package/dist/{bake.d.ts → memory/bake.d.ts} +12 -6
- package/dist/{bake.js → memory/bake.js} +9 -7
- package/dist/memory/continuation.d.ts +141 -0
- package/dist/memory/continuation.js +495 -0
- package/dist/memory/elastic-peer.d.ts +20 -0
- package/dist/memory/elastic-peer.js +39 -0
- package/dist/memory/elastic-sql.d.ts +11 -0
- package/dist/memory/elastic-sql.js +35 -0
- package/dist/memory/elastic.d.ts +130 -0
- package/dist/memory/elastic.js +475 -0
- package/dist/memory/growth-governor.d.ts +16 -0
- package/dist/memory/growth-governor.js +42 -0
- package/dist/memory/imports.d.ts +10 -0
- package/dist/memory/imports.js +89 -0
- package/dist/{iteration-layout.d.ts → memory/iteration-layout.d.ts} +61 -1
- package/dist/{iteration-layout.js → memory/iteration-layout.js} +54 -3
- package/dist/memory/lease.d.ts +45 -0
- package/dist/memory/lease.js +274 -0
- package/dist/{pages.d.ts → memory/pages.d.ts} +3 -3
- package/dist/memory.d.ts +5 -10
- package/dist/memory.js +5 -89
- package/dist/{context.d.ts → node/context.d.ts} +17 -3
- package/dist/{context.js → node/context.js} +60 -14
- package/dist/{host.d.ts → node/host.d.ts} +1 -1
- package/dist/{host.js → node/host.js} +2 -2
- package/dist/{node.d.ts → node/index.d.ts} +46 -49
- package/dist/{node.js → node/index.js} +101 -26
- package/dist/{tasks.d.ts → node/tasks.d.ts} +16 -17
- package/dist/{tasks.js → node/tasks.js} +12 -11
- package/dist/node/worker.d.ts +1 -0
- package/dist/{worker.js → node/worker.js} +2 -2
- package/dist/ownership/index.d.ts +161 -0
- package/dist/ownership/index.js +6565 -0
- 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/{frames.d.ts → remote/frames.d.ts} +6 -2
- package/dist/remote/frames.js +306 -0
- package/dist/{remote.d.ts → remote/index.d.ts} +7 -2
- package/dist/{remote.js → remote/index.js} +7 -2
- package/dist/routing/index.d.ts +203 -0
- package/dist/routing/index.js +671 -0
- package/dist/routing/worker.d.ts +1 -0
- package/dist/routing/worker.js +4 -0
- package/dist/slices/index.d.ts +126 -0
- package/dist/slices/index.js +441 -0
- package/dist/slices/worker.d.ts +1 -0
- package/dist/slices/worker.js +38 -0
- package/dist/workers.d.ts +5 -0
- package/dist/workers.js +5 -0
- package/package.json +32 -46
- package/dist/frames.js +0 -159
- package/dist/inline.d.ts +0 -68
- package/dist/iteration-admission.d.ts +0 -76
- package/dist/iteration-admission.js +0 -72
- package/dist/iteration-worker.js +0 -150
- package/dist/iteration.d.ts +0 -163
- package/dist/iteration.js +0 -435
- package/dist/managed.js +0 -421
- /package/dist/{browser-worker.d.ts → browser/worker.d.ts} +0 -0
- /package/dist/{browser-worker.js → browser/worker.js} +0 -0
- /package/dist/{guards.js → execution/guards.js} +0 -0
- /package/dist/{import-order.d.ts → execution/import-order.d.ts} +0 -0
- /package/dist/{import-order.js → execution/import-order.js} +0 -0
- /package/dist/{transaction.js → execution/transaction.js} +0 -0
- /package/dist/{iteration-worker.d.ts → execution/types.js} +0 -0
- /package/dist/{worker.d.ts → iteration/worker.d.ts} +0 -0
- /package/dist/{pages.js → memory/pages.js} +0 -0
- /package/dist/{values.d.ts → remote/values.d.ts} +0 -0
- /package/dist/{values.js → remote/values.js} +0 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/** A synchronous store for chunks. `read` copies a chunk into `into` and returns its version, or undefined when absent. */
|
|
2
|
+
export interface SyncHolder {
|
|
3
|
+
read(index: number, into: Uint8Array): number | undefined;
|
|
4
|
+
write(index: number, version: number, bytes: Uint8Array): void;
|
|
5
|
+
}
|
|
6
|
+
/** The asynchronous form of `SyncHolder`. */
|
|
7
|
+
export interface AsyncHolder {
|
|
8
|
+
read(index: number, into: Uint8Array): Promise<number | undefined>;
|
|
9
|
+
write(index: number, version: number, bytes: Uint8Array): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export interface ElasticOptions {
|
|
12
|
+
/** Logical size in bytes; a whole number of chunks. */
|
|
13
|
+
byteLength: number;
|
|
14
|
+
/** A power of two from 4 KiB to 8 MiB. */
|
|
15
|
+
chunkBytes: number;
|
|
16
|
+
/** Resident window in chunks, at least 1. */
|
|
17
|
+
frames: number;
|
|
18
|
+
}
|
|
19
|
+
export interface ElasticStats {
|
|
20
|
+
faults: number;
|
|
21
|
+
zeroFills: number;
|
|
22
|
+
writeBacks: number;
|
|
23
|
+
evictions: number;
|
|
24
|
+
bytesIn: number;
|
|
25
|
+
bytesOut: number;
|
|
26
|
+
staleRefusals: number;
|
|
27
|
+
hits: number;
|
|
28
|
+
}
|
|
29
|
+
/** A chunk whose holder copy is missing or at a version other than the table's. */
|
|
30
|
+
export declare class StaleChunkError extends Error {
|
|
31
|
+
readonly chunk: number;
|
|
32
|
+
readonly expected: number;
|
|
33
|
+
readonly found: number | undefined;
|
|
34
|
+
constructor(chunk: number, expected: number, found: number | undefined);
|
|
35
|
+
}
|
|
36
|
+
declare class Window {
|
|
37
|
+
readonly byteLength: number;
|
|
38
|
+
readonly chunkBytes: number;
|
|
39
|
+
readonly chunkCount: number;
|
|
40
|
+
protected readonly buffer: ArrayBuffer;
|
|
41
|
+
protected readonly frames: Uint8Array[];
|
|
42
|
+
protected readonly chunkOf: Int32Array;
|
|
43
|
+
protected readonly frameOf: Int32Array;
|
|
44
|
+
protected readonly dirty: Uint8Array;
|
|
45
|
+
protected readonly pins: Uint32Array;
|
|
46
|
+
protected readonly recency: Float64Array;
|
|
47
|
+
protected readonly versions: Uint32Array;
|
|
48
|
+
protected readonly counts: ElasticStats;
|
|
49
|
+
private clock;
|
|
50
|
+
constructor(options: ElasticOptions);
|
|
51
|
+
stats(): ElasticStats;
|
|
52
|
+
snapshotTable(): Uint32Array;
|
|
53
|
+
protected replaceTable(table: Uint32Array): void;
|
|
54
|
+
protected check(offset: number, length: number): void;
|
|
55
|
+
protected viewChunk(offset: number, length: number): number;
|
|
56
|
+
protected sliceFrame(f: number, offset: number, length: number): Uint8Array;
|
|
57
|
+
protected frameOfView(view: Uint8Array): number;
|
|
58
|
+
protected pinFrame(f: number): void;
|
|
59
|
+
protected unpinView(view: Uint8Array, dirty: boolean): void;
|
|
60
|
+
protected markDirty(offset: number, length: number): void;
|
|
61
|
+
protected victim(): number;
|
|
62
|
+
protected freeFrame(): number;
|
|
63
|
+
protected windowFull(index: number): RangeError;
|
|
64
|
+
protected map(f: number, index: number): void;
|
|
65
|
+
protected unmap(f: number): void;
|
|
66
|
+
protected use(f: number): void;
|
|
67
|
+
protected needsHolder(f: number, index: number): boolean;
|
|
68
|
+
protected checkStored(index: number, found: number | undefined): void;
|
|
69
|
+
protected nextVersion(index: number): number;
|
|
70
|
+
protected committed(f: number, index: number, version: number): void;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A region of `byteLength` bytes held through a window of `frames` chunks in one buffer.
|
|
74
|
+
*
|
|
75
|
+
* The version table lives in the instance. After a restart, restore it with `restoreTable` from a
|
|
76
|
+
* copy the owner persisted, or every chunk reads as never written.
|
|
77
|
+
*/
|
|
78
|
+
export declare class ElasticRegion extends Window {
|
|
79
|
+
private readonly holder;
|
|
80
|
+
constructor(options: ElasticOptions & {
|
|
81
|
+
holder: SyncHolder;
|
|
82
|
+
});
|
|
83
|
+
/** The same region over an asynchronous holder. */
|
|
84
|
+
static async(options: ElasticOptions & {
|
|
85
|
+
holder: AsyncHolder;
|
|
86
|
+
}): AsyncElasticRegion;
|
|
87
|
+
/** A view inside one chunk, valid until the next call that can fault; `pin` keeps it longer. */
|
|
88
|
+
view(offset: number, length: number): Uint8Array;
|
|
89
|
+
pin(offset: number, length: number): Uint8Array;
|
|
90
|
+
unpin(view: Uint8Array, dirty: boolean): void;
|
|
91
|
+
read(offset: number, into: Uint8Array): void;
|
|
92
|
+
write(offset: number, bytes: Uint8Array): void;
|
|
93
|
+
touch(offset: number, length: number): void;
|
|
94
|
+
copyIntoMemory(memory: WebAssembly.Memory, pointer: number, offset: number, length: number): void;
|
|
95
|
+
copyFromMemory(memory: WebAssembly.Memory, pointer: number, offset: number, length: number): void;
|
|
96
|
+
flush(): void;
|
|
97
|
+
restoreTable(table: Uint32Array): void;
|
|
98
|
+
private fault;
|
|
99
|
+
private evict;
|
|
100
|
+
private writeBack;
|
|
101
|
+
}
|
|
102
|
+
/** The async form of `ElasticRegion`. Operations run one at a time; `prefetch` starts loads in the background. */
|
|
103
|
+
export declare class AsyncElasticRegion extends Window {
|
|
104
|
+
private readonly holder;
|
|
105
|
+
private readonly inflight;
|
|
106
|
+
private queue;
|
|
107
|
+
constructor(options: ElasticOptions & {
|
|
108
|
+
holder: AsyncHolder;
|
|
109
|
+
});
|
|
110
|
+
view(offset: number, length: number): Promise<Uint8Array>;
|
|
111
|
+
pin(offset: number, length: number): Promise<Uint8Array>;
|
|
112
|
+
unpin(view: Uint8Array, dirty: boolean): Promise<void>;
|
|
113
|
+
read(offset: number, into: Uint8Array): Promise<void>;
|
|
114
|
+
write(offset: number, bytes: Uint8Array): Promise<void>;
|
|
115
|
+
touch(offset: number, length: number): Promise<void>;
|
|
116
|
+
copyIntoMemory(memory: WebAssembly.Memory, pointer: number, offset: number, length: number): Promise<void>;
|
|
117
|
+
copyFromMemory(memory: WebAssembly.Memory, pointer: number, offset: number, length: number): Promise<void>;
|
|
118
|
+
flush(): Promise<void>;
|
|
119
|
+
restoreTable(table: Uint32Array): Promise<void>;
|
|
120
|
+
prefetch(offset: number, length: number): void;
|
|
121
|
+
settled(): Promise<void>;
|
|
122
|
+
private run;
|
|
123
|
+
private readInto;
|
|
124
|
+
private writeFrom;
|
|
125
|
+
private fault;
|
|
126
|
+
private loadAsync;
|
|
127
|
+
private evict;
|
|
128
|
+
private writeBack;
|
|
129
|
+
}
|
|
130
|
+
export {};
|
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
/** A chunk whose holder copy is missing or at a version other than the table's. */
|
|
2
|
+
export class StaleChunkError extends Error {
|
|
3
|
+
chunk;
|
|
4
|
+
expected;
|
|
5
|
+
found;
|
|
6
|
+
constructor(chunk, expected, found) {
|
|
7
|
+
super(found === undefined
|
|
8
|
+
? `chunk ${chunk} is missing from its holder; the table expects version ${expected}`
|
|
9
|
+
: `chunk ${chunk} is stale; the table expects version ${expected}, the holder has ${found}`);
|
|
10
|
+
this.name = 'StaleChunkError';
|
|
11
|
+
this.chunk = chunk;
|
|
12
|
+
this.expected = expected;
|
|
13
|
+
this.found = found;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const minChunkBytes = 4096;
|
|
17
|
+
const maxChunkBytes = 8 * 1024 * 1024;
|
|
18
|
+
// frame bookkeeping and the version table; the sync and async regions differ only in how they await the holder
|
|
19
|
+
class Window {
|
|
20
|
+
byteLength;
|
|
21
|
+
chunkBytes;
|
|
22
|
+
chunkCount;
|
|
23
|
+
buffer;
|
|
24
|
+
frames;
|
|
25
|
+
// chunk held by each frame, -1 when free
|
|
26
|
+
chunkOf;
|
|
27
|
+
// frame holding each chunk, -1 when not resident
|
|
28
|
+
frameOf;
|
|
29
|
+
dirty;
|
|
30
|
+
pins;
|
|
31
|
+
recency;
|
|
32
|
+
versions;
|
|
33
|
+
counts = {
|
|
34
|
+
faults: 0,
|
|
35
|
+
zeroFills: 0,
|
|
36
|
+
writeBacks: 0,
|
|
37
|
+
evictions: 0,
|
|
38
|
+
bytesIn: 0,
|
|
39
|
+
bytesOut: 0,
|
|
40
|
+
staleRefusals: 0,
|
|
41
|
+
hits: 0
|
|
42
|
+
};
|
|
43
|
+
clock = 0;
|
|
44
|
+
constructor(options) {
|
|
45
|
+
const { byteLength, chunkBytes, frames } = options;
|
|
46
|
+
if (!Number.isInteger(chunkBytes) ||
|
|
47
|
+
chunkBytes < minChunkBytes ||
|
|
48
|
+
chunkBytes > maxChunkBytes ||
|
|
49
|
+
(chunkBytes & (chunkBytes - 1)) !== 0)
|
|
50
|
+
throw new RangeError('chunkBytes must be a power of two from 4 KiB to 8 MiB');
|
|
51
|
+
if (!Number.isInteger(byteLength) ||
|
|
52
|
+
byteLength < chunkBytes ||
|
|
53
|
+
byteLength % chunkBytes !== 0)
|
|
54
|
+
throw new RangeError('byteLength must be a positive whole number of chunks');
|
|
55
|
+
if (!Number.isInteger(frames) || frames < 1)
|
|
56
|
+
throw new RangeError('frames must be a positive integer');
|
|
57
|
+
this.byteLength = byteLength;
|
|
58
|
+
this.chunkBytes = chunkBytes;
|
|
59
|
+
this.chunkCount = byteLength / chunkBytes;
|
|
60
|
+
this.buffer = new ArrayBuffer(frames * chunkBytes);
|
|
61
|
+
this.frames = Array.from({ length: frames }, (_, f) => new Uint8Array(this.buffer, f * chunkBytes, chunkBytes));
|
|
62
|
+
this.chunkOf = new Int32Array(frames).fill(-1);
|
|
63
|
+
this.frameOf = new Int32Array(this.chunkCount).fill(-1);
|
|
64
|
+
this.dirty = new Uint8Array(frames);
|
|
65
|
+
this.pins = new Uint32Array(frames);
|
|
66
|
+
this.recency = new Float64Array(frames);
|
|
67
|
+
this.versions = new Uint32Array(this.chunkCount);
|
|
68
|
+
}
|
|
69
|
+
stats() {
|
|
70
|
+
return { ...this.counts };
|
|
71
|
+
}
|
|
72
|
+
snapshotTable() {
|
|
73
|
+
return this.versions.slice();
|
|
74
|
+
}
|
|
75
|
+
replaceTable(table) {
|
|
76
|
+
if (table.length !== this.chunkCount)
|
|
77
|
+
throw new RangeError(`table has ${table.length} entries; the region has ${this.chunkCount} chunks`);
|
|
78
|
+
for (let f = 0; f < this.frames.length; f++)
|
|
79
|
+
if (this.dirty[f] || this.pins[f])
|
|
80
|
+
throw new Error('restoreTable needs no dirty or pinned frame; flush and unpin first');
|
|
81
|
+
for (let f = 0; f < this.frames.length; f++)
|
|
82
|
+
this.unmap(f);
|
|
83
|
+
this.versions.set(table);
|
|
84
|
+
}
|
|
85
|
+
check(offset, length) {
|
|
86
|
+
if (!Number.isInteger(offset) ||
|
|
87
|
+
!Number.isInteger(length) ||
|
|
88
|
+
offset < 0 ||
|
|
89
|
+
length < 0 ||
|
|
90
|
+
offset + length > this.byteLength)
|
|
91
|
+
throw new RangeError(`range ${offset}+${length} is outside the ${this.byteLength}-byte region`);
|
|
92
|
+
}
|
|
93
|
+
// validates a view and returns its chunk
|
|
94
|
+
viewChunk(offset, length) {
|
|
95
|
+
this.check(offset, length);
|
|
96
|
+
const index = Math.floor(offset / this.chunkBytes);
|
|
97
|
+
if (length < 1 || Math.floor((offset + length - 1) / this.chunkBytes) !== index)
|
|
98
|
+
throw new RangeError('a view covers 1 or more bytes inside one chunk');
|
|
99
|
+
return index;
|
|
100
|
+
}
|
|
101
|
+
sliceFrame(f, offset, length) {
|
|
102
|
+
const within = offset % this.chunkBytes;
|
|
103
|
+
return this.frames[f].subarray(within, within + length);
|
|
104
|
+
}
|
|
105
|
+
frameOfView(view) {
|
|
106
|
+
if (view.buffer !== this.buffer)
|
|
107
|
+
throw new RangeError('view is not from this region');
|
|
108
|
+
return Math.floor(view.byteOffset / this.chunkBytes);
|
|
109
|
+
}
|
|
110
|
+
pinFrame(f) {
|
|
111
|
+
this.pins[f] += 1;
|
|
112
|
+
}
|
|
113
|
+
unpinView(view, dirty) {
|
|
114
|
+
const f = this.frameOfView(view);
|
|
115
|
+
if (this.pins[f] === 0)
|
|
116
|
+
throw new Error('view is not pinned');
|
|
117
|
+
this.pins[f] -= 1;
|
|
118
|
+
if (dirty)
|
|
119
|
+
this.dirty[f] = 1;
|
|
120
|
+
}
|
|
121
|
+
markDirty(offset, length) {
|
|
122
|
+
this.check(offset, length);
|
|
123
|
+
for (let at = offset; at < offset + length;) {
|
|
124
|
+
const index = Math.floor(at / this.chunkBytes);
|
|
125
|
+
const f = this.frameOf[index];
|
|
126
|
+
if (f < 0)
|
|
127
|
+
throw new RangeError(`chunk ${index} is not resident; pin its view to keep it`);
|
|
128
|
+
this.dirty[f] = 1;
|
|
129
|
+
at = (index + 1) * this.chunkBytes;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
victim() {
|
|
133
|
+
let best = -1;
|
|
134
|
+
for (let f = 0; f < this.frames.length; f++) {
|
|
135
|
+
if (this.chunkOf[f] < 0 && this.pins[f] === 0)
|
|
136
|
+
return f;
|
|
137
|
+
if (this.pins[f] === 0 && (best < 0 || this.recency[f] < this.recency[best]))
|
|
138
|
+
best = f;
|
|
139
|
+
}
|
|
140
|
+
return best;
|
|
141
|
+
}
|
|
142
|
+
freeFrame() {
|
|
143
|
+
for (let f = 0; f < this.frames.length; f++)
|
|
144
|
+
if (this.chunkOf[f] < 0 && this.pins[f] === 0)
|
|
145
|
+
return f;
|
|
146
|
+
return -1;
|
|
147
|
+
}
|
|
148
|
+
windowFull(index) {
|
|
149
|
+
return new RangeError(`every frame of the ${this.frames.length}-frame window is pinned; unpin one before chunk ${index} loads`);
|
|
150
|
+
}
|
|
151
|
+
map(f, index) {
|
|
152
|
+
this.chunkOf[f] = index;
|
|
153
|
+
this.frameOf[index] = f;
|
|
154
|
+
this.dirty[f] = 0;
|
|
155
|
+
this.use(f);
|
|
156
|
+
}
|
|
157
|
+
unmap(f) {
|
|
158
|
+
const index = this.chunkOf[f];
|
|
159
|
+
if (index >= 0)
|
|
160
|
+
this.frameOf[index] = -1;
|
|
161
|
+
this.chunkOf[f] = -1;
|
|
162
|
+
this.dirty[f] = 0;
|
|
163
|
+
}
|
|
164
|
+
use(f) {
|
|
165
|
+
this.recency[f] = ++this.clock;
|
|
166
|
+
}
|
|
167
|
+
// a never-written chunk is zero-filled; returns true when the holder must supply the bytes
|
|
168
|
+
needsHolder(f, index) {
|
|
169
|
+
if (this.versions[index] !== 0)
|
|
170
|
+
return true;
|
|
171
|
+
this.frames[f].fill(0);
|
|
172
|
+
this.counts.zeroFills++;
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
checkStored(index, found) {
|
|
176
|
+
const expected = this.versions[index];
|
|
177
|
+
if (found !== undefined)
|
|
178
|
+
this.counts.bytesIn += this.chunkBytes;
|
|
179
|
+
if (found === expected)
|
|
180
|
+
return;
|
|
181
|
+
this.counts.staleRefusals++;
|
|
182
|
+
throw new StaleChunkError(index, expected, found);
|
|
183
|
+
}
|
|
184
|
+
// the version a write-back stamps; version 0 is reserved for never-written
|
|
185
|
+
nextVersion(index) {
|
|
186
|
+
const version = this.versions[index] + 1;
|
|
187
|
+
if (version > 0xffffffff)
|
|
188
|
+
throw new RangeError(`chunk ${index} has no version left`);
|
|
189
|
+
return version;
|
|
190
|
+
}
|
|
191
|
+
committed(f, index, version) {
|
|
192
|
+
this.versions[index] = version;
|
|
193
|
+
this.dirty[f] = 0;
|
|
194
|
+
this.counts.writeBacks++;
|
|
195
|
+
this.counts.bytesOut += this.chunkBytes;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* A region of `byteLength` bytes held through a window of `frames` chunks in one buffer.
|
|
200
|
+
*
|
|
201
|
+
* The version table lives in the instance. After a restart, restore it with `restoreTable` from a
|
|
202
|
+
* copy the owner persisted, or every chunk reads as never written.
|
|
203
|
+
*/
|
|
204
|
+
export class ElasticRegion extends Window {
|
|
205
|
+
holder;
|
|
206
|
+
constructor(options) {
|
|
207
|
+
super(options);
|
|
208
|
+
this.holder = options.holder;
|
|
209
|
+
}
|
|
210
|
+
/** The same region over an asynchronous holder. */
|
|
211
|
+
static async(options) {
|
|
212
|
+
return new AsyncElasticRegion(options);
|
|
213
|
+
}
|
|
214
|
+
/** A view inside one chunk, valid until the next call that can fault; `pin` keeps it longer. */
|
|
215
|
+
view(offset, length) {
|
|
216
|
+
const f = this.fault(this.viewChunk(offset, length));
|
|
217
|
+
return this.sliceFrame(f, offset, length);
|
|
218
|
+
}
|
|
219
|
+
pin(offset, length) {
|
|
220
|
+
const f = this.fault(this.viewChunk(offset, length));
|
|
221
|
+
this.pinFrame(f);
|
|
222
|
+
return this.sliceFrame(f, offset, length);
|
|
223
|
+
}
|
|
224
|
+
unpin(view, dirty) {
|
|
225
|
+
this.unpinView(view, dirty);
|
|
226
|
+
}
|
|
227
|
+
read(offset, into) {
|
|
228
|
+
this.check(offset, into.length);
|
|
229
|
+
for (let done = 0; done < into.length;) {
|
|
230
|
+
const at = offset + done;
|
|
231
|
+
const within = at % this.chunkBytes;
|
|
232
|
+
const n = Math.min(this.chunkBytes - within, into.length - done);
|
|
233
|
+
const f = this.fault(Math.floor(at / this.chunkBytes));
|
|
234
|
+
into.set(this.frames[f].subarray(within, within + n), done);
|
|
235
|
+
done += n;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
write(offset, bytes) {
|
|
239
|
+
this.check(offset, bytes.length);
|
|
240
|
+
for (let done = 0; done < bytes.length;) {
|
|
241
|
+
const at = offset + done;
|
|
242
|
+
const within = at % this.chunkBytes;
|
|
243
|
+
const n = Math.min(this.chunkBytes - within, bytes.length - done);
|
|
244
|
+
const f = this.fault(Math.floor(at / this.chunkBytes));
|
|
245
|
+
this.frames[f].set(bytes.subarray(done, done + n), within);
|
|
246
|
+
this.dirty[f] = 1;
|
|
247
|
+
done += n;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
touch(offset, length) {
|
|
251
|
+
this.markDirty(offset, length);
|
|
252
|
+
}
|
|
253
|
+
copyIntoMemory(memory, pointer, offset, length) {
|
|
254
|
+
this.read(offset, new Uint8Array(memory.buffer, pointer, length));
|
|
255
|
+
}
|
|
256
|
+
copyFromMemory(memory, pointer, offset, length) {
|
|
257
|
+
this.write(offset, new Uint8Array(memory.buffer, pointer, length));
|
|
258
|
+
}
|
|
259
|
+
flush() {
|
|
260
|
+
for (let f = 0; f < this.frames.length; f++)
|
|
261
|
+
if (this.dirty[f])
|
|
262
|
+
this.writeBack(f);
|
|
263
|
+
}
|
|
264
|
+
restoreTable(table) {
|
|
265
|
+
this.replaceTable(table);
|
|
266
|
+
}
|
|
267
|
+
fault(index) {
|
|
268
|
+
const known = this.frameOf[index];
|
|
269
|
+
if (known >= 0) {
|
|
270
|
+
this.counts.hits++;
|
|
271
|
+
this.use(known);
|
|
272
|
+
return known;
|
|
273
|
+
}
|
|
274
|
+
const f = this.victim();
|
|
275
|
+
if (f < 0)
|
|
276
|
+
throw this.windowFull(index);
|
|
277
|
+
this.counts.faults++;
|
|
278
|
+
if (this.chunkOf[f] >= 0)
|
|
279
|
+
this.evict(f);
|
|
280
|
+
this.map(f, index);
|
|
281
|
+
try {
|
|
282
|
+
if (this.needsHolder(f, index))
|
|
283
|
+
this.checkStored(index, this.holder.read(index, this.frames[f]));
|
|
284
|
+
}
|
|
285
|
+
catch (error) {
|
|
286
|
+
this.unmap(f);
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
return f;
|
|
290
|
+
}
|
|
291
|
+
evict(f) {
|
|
292
|
+
if (this.dirty[f])
|
|
293
|
+
this.writeBack(f);
|
|
294
|
+
this.counts.evictions++;
|
|
295
|
+
this.unmap(f);
|
|
296
|
+
}
|
|
297
|
+
writeBack(f) {
|
|
298
|
+
const index = this.chunkOf[f];
|
|
299
|
+
const version = this.nextVersion(index);
|
|
300
|
+
this.holder.write(index, version, this.frames[f]);
|
|
301
|
+
this.committed(f, index, version);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/** The async form of `ElasticRegion`. Operations run one at a time; `prefetch` starts loads in the background. */
|
|
305
|
+
export class AsyncElasticRegion extends Window {
|
|
306
|
+
holder;
|
|
307
|
+
inflight = new Map();
|
|
308
|
+
queue = Promise.resolve();
|
|
309
|
+
constructor(options) {
|
|
310
|
+
super(options);
|
|
311
|
+
this.holder = options.holder;
|
|
312
|
+
}
|
|
313
|
+
view(offset, length) {
|
|
314
|
+
return this.run(async () => {
|
|
315
|
+
const f = await this.fault(this.viewChunk(offset, length));
|
|
316
|
+
return this.sliceFrame(f, offset, length);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
pin(offset, length) {
|
|
320
|
+
return this.run(async () => {
|
|
321
|
+
const f = await this.fault(this.viewChunk(offset, length));
|
|
322
|
+
this.pinFrame(f);
|
|
323
|
+
return this.sliceFrame(f, offset, length);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
unpin(view, dirty) {
|
|
327
|
+
return this.run(async () => this.unpinView(view, dirty));
|
|
328
|
+
}
|
|
329
|
+
read(offset, into) {
|
|
330
|
+
return this.run(() => this.readInto(offset, into));
|
|
331
|
+
}
|
|
332
|
+
write(offset, bytes) {
|
|
333
|
+
return this.run(() => this.writeFrom(offset, bytes));
|
|
334
|
+
}
|
|
335
|
+
touch(offset, length) {
|
|
336
|
+
return this.run(async () => this.markDirty(offset, length));
|
|
337
|
+
}
|
|
338
|
+
copyIntoMemory(memory, pointer, offset, length) {
|
|
339
|
+
return this.run(() => this.readInto(offset, new Uint8Array(memory.buffer, pointer, length)));
|
|
340
|
+
}
|
|
341
|
+
copyFromMemory(memory, pointer, offset, length) {
|
|
342
|
+
return this.run(() => this.writeFrom(offset, new Uint8Array(memory.buffer, pointer, length)));
|
|
343
|
+
}
|
|
344
|
+
flush() {
|
|
345
|
+
return this.run(async () => {
|
|
346
|
+
for (let f = 0; f < this.frames.length; f++)
|
|
347
|
+
if (this.dirty[f])
|
|
348
|
+
await this.writeBack(f);
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
restoreTable(table) {
|
|
352
|
+
return this.run(async () => this.replaceTable(table));
|
|
353
|
+
}
|
|
354
|
+
// starts loads for the missing chunks that fit in free frames; never evicts and never awaits
|
|
355
|
+
prefetch(offset, length) {
|
|
356
|
+
this.check(offset, length);
|
|
357
|
+
if (length === 0)
|
|
358
|
+
return;
|
|
359
|
+
const last = Math.floor((offset + length - 1) / this.chunkBytes);
|
|
360
|
+
for (let index = Math.floor(offset / this.chunkBytes); index <= last; index++) {
|
|
361
|
+
if (this.frameOf[index] >= 0)
|
|
362
|
+
continue;
|
|
363
|
+
const f = this.freeFrame();
|
|
364
|
+
if (f < 0)
|
|
365
|
+
break;
|
|
366
|
+
this.map(f, index);
|
|
367
|
+
this.pinFrame(f);
|
|
368
|
+
this.counts.faults++;
|
|
369
|
+
const load = this.loadAsync(f, index)
|
|
370
|
+
.catch((error) => {
|
|
371
|
+
this.unmap(f);
|
|
372
|
+
throw error;
|
|
373
|
+
})
|
|
374
|
+
.finally(() => {
|
|
375
|
+
this.pins[f] -= 1;
|
|
376
|
+
if (this.inflight.get(index) === load)
|
|
377
|
+
this.inflight.delete(index);
|
|
378
|
+
});
|
|
379
|
+
load.catch(() => undefined);
|
|
380
|
+
this.inflight.set(index, load);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
// waits for the loads in flight; rejects with the first failure among them
|
|
384
|
+
async settled() {
|
|
385
|
+
const results = await Promise.allSettled([...this.inflight.values()]);
|
|
386
|
+
const failed = results.find((result) => result.status === 'rejected');
|
|
387
|
+
if (failed?.status === 'rejected')
|
|
388
|
+
throw failed.reason;
|
|
389
|
+
}
|
|
390
|
+
run(op) {
|
|
391
|
+
const next = this.queue.then(op);
|
|
392
|
+
this.queue = next.catch(() => undefined);
|
|
393
|
+
return next;
|
|
394
|
+
}
|
|
395
|
+
async readInto(offset, into) {
|
|
396
|
+
this.check(offset, into.length);
|
|
397
|
+
for (let done = 0; done < into.length;) {
|
|
398
|
+
const at = offset + done;
|
|
399
|
+
const within = at % this.chunkBytes;
|
|
400
|
+
const n = Math.min(this.chunkBytes - within, into.length - done);
|
|
401
|
+
const f = await this.fault(Math.floor(at / this.chunkBytes));
|
|
402
|
+
into.set(this.frames[f].subarray(within, within + n), done);
|
|
403
|
+
done += n;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
async writeFrom(offset, bytes) {
|
|
407
|
+
this.check(offset, bytes.length);
|
|
408
|
+
for (let done = 0; done < bytes.length;) {
|
|
409
|
+
const at = offset + done;
|
|
410
|
+
const within = at % this.chunkBytes;
|
|
411
|
+
const n = Math.min(this.chunkBytes - within, bytes.length - done);
|
|
412
|
+
const f = await this.fault(Math.floor(at / this.chunkBytes));
|
|
413
|
+
this.frames[f].set(bytes.subarray(done, done + n), within);
|
|
414
|
+
this.dirty[f] = 1;
|
|
415
|
+
done += n;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
async fault(index) {
|
|
419
|
+
const pending = this.inflight.get(index);
|
|
420
|
+
if (pending) {
|
|
421
|
+
await pending;
|
|
422
|
+
this.counts.hits++;
|
|
423
|
+
return this.frameOf[index];
|
|
424
|
+
}
|
|
425
|
+
const known = this.frameOf[index];
|
|
426
|
+
if (known >= 0) {
|
|
427
|
+
this.counts.hits++;
|
|
428
|
+
this.use(known);
|
|
429
|
+
return known;
|
|
430
|
+
}
|
|
431
|
+
let f = this.victim();
|
|
432
|
+
while (f < 0 && this.inflight.size > 0) {
|
|
433
|
+
await Promise.allSettled([...this.inflight.values()]);
|
|
434
|
+
f = this.victim();
|
|
435
|
+
}
|
|
436
|
+
if (f < 0)
|
|
437
|
+
throw this.windowFull(index);
|
|
438
|
+
this.counts.faults++;
|
|
439
|
+
// pinned across the awaits so a prefetch cannot take the frame between eviction and mapping
|
|
440
|
+
this.pinFrame(f);
|
|
441
|
+
try {
|
|
442
|
+
if (this.chunkOf[f] >= 0)
|
|
443
|
+
await this.evict(f);
|
|
444
|
+
this.map(f, index);
|
|
445
|
+
try {
|
|
446
|
+
await this.loadAsync(f, index);
|
|
447
|
+
}
|
|
448
|
+
catch (error) {
|
|
449
|
+
this.unmap(f);
|
|
450
|
+
throw error;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
finally {
|
|
454
|
+
this.pins[f] -= 1;
|
|
455
|
+
}
|
|
456
|
+
return f;
|
|
457
|
+
}
|
|
458
|
+
async loadAsync(f, index) {
|
|
459
|
+
if (!this.needsHolder(f, index))
|
|
460
|
+
return;
|
|
461
|
+
this.checkStored(index, await this.holder.read(index, this.frames[f]));
|
|
462
|
+
}
|
|
463
|
+
async evict(f) {
|
|
464
|
+
if (this.dirty[f])
|
|
465
|
+
await this.writeBack(f);
|
|
466
|
+
this.counts.evictions++;
|
|
467
|
+
this.unmap(f);
|
|
468
|
+
}
|
|
469
|
+
async writeBack(f) {
|
|
470
|
+
const index = this.chunkOf[f];
|
|
471
|
+
const version = this.nextVersion(index);
|
|
472
|
+
await this.holder.write(index, version, this.frames[f]);
|
|
473
|
+
this.committed(f, index, version);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface GrowthGovernorOptions {
|
|
2
|
+
capBytes: number;
|
|
3
|
+
onRefuse?: (info: {
|
|
4
|
+
fromBytes: number;
|
|
5
|
+
toBytes: number;
|
|
6
|
+
}) => void;
|
|
7
|
+
}
|
|
8
|
+
export interface GrowthGovernor {
|
|
9
|
+
refused: number;
|
|
10
|
+
restore(): void;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Refuses any memory.grow whose final size would exceed capBytes. Governors stack: a later one
|
|
14
|
+
* calls the one it replaced, so both caps hold, and restore() must run in reverse install order.
|
|
15
|
+
*/
|
|
16
|
+
export declare function governGrowth(memory: WebAssembly.Memory, options: GrowthGovernorOptions): GrowthGovernor;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const PAGE_BYTES = 65536;
|
|
2
|
+
/**
|
|
3
|
+
* Refuses any memory.grow whose final size would exceed capBytes. Governors stack: a later one
|
|
4
|
+
* calls the one it replaced, so both caps hold, and restore() must run in reverse install order.
|
|
5
|
+
*/
|
|
6
|
+
export function governGrowth(memory, options) {
|
|
7
|
+
const { capBytes, onRefuse } = options;
|
|
8
|
+
if (!Number.isInteger(capBytes) || capBytes <= 0) {
|
|
9
|
+
throw new RangeError(`capBytes must be a finite positive integer, got ${capBytes}`);
|
|
10
|
+
}
|
|
11
|
+
const inner = memory.grow;
|
|
12
|
+
const previous = Object.getOwnPropertyDescriptor(memory, 'grow');
|
|
13
|
+
let installed = true;
|
|
14
|
+
const governor = {
|
|
15
|
+
refused: 0,
|
|
16
|
+
restore() {
|
|
17
|
+
if (!installed)
|
|
18
|
+
return;
|
|
19
|
+
if (Object.getOwnPropertyDescriptor(memory, 'grow')?.value !== grow) {
|
|
20
|
+
throw new Error('growth governors must be restored in reverse install order');
|
|
21
|
+
}
|
|
22
|
+
if (previous)
|
|
23
|
+
Object.defineProperty(memory, 'grow', previous);
|
|
24
|
+
else
|
|
25
|
+
delete memory.grow;
|
|
26
|
+
installed = false;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
function grow(pages) {
|
|
30
|
+
const fromBytes = memory.buffer.byteLength;
|
|
31
|
+
// emscripten passes fractional pages; native grow truncates them, so the target must too
|
|
32
|
+
const toBytes = fromBytes + Math.trunc(pages) * PAGE_BYTES;
|
|
33
|
+
if (toBytes > capBytes) {
|
|
34
|
+
governor.refused += 1;
|
|
35
|
+
onRefuse?.({ fromBytes, toBytes });
|
|
36
|
+
throw new RangeError(`memory.grow to ${toBytes} bytes exceeds the ${capBytes} byte cap`);
|
|
37
|
+
}
|
|
38
|
+
return inner.call(memory, pages);
|
|
39
|
+
}
|
|
40
|
+
Object.defineProperty(memory, 'grow', { value: grow, configurable: true, writable: true });
|
|
41
|
+
return governor;
|
|
42
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface MemoryImport {
|
|
2
|
+
module: string;
|
|
3
|
+
name: string;
|
|
4
|
+
initial: number;
|
|
5
|
+
maximum?: number;
|
|
6
|
+
shared: boolean;
|
|
7
|
+
memory64: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** Reads imported memory limits without changing the module or compiling its functions. */
|
|
10
|
+
export declare function memoryImports(bytes: Uint8Array): MemoryImport[];
|