@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,89 @@
|
|
|
1
|
+
/** Reads imported memory limits without changing the module or compiling its functions. */
|
|
2
|
+
export function memoryImports(bytes) {
|
|
3
|
+
let offset = 8;
|
|
4
|
+
const byte = () => {
|
|
5
|
+
if (offset >= bytes.length)
|
|
6
|
+
throw new RangeError('truncated wasm metadata');
|
|
7
|
+
return bytes[offset++];
|
|
8
|
+
};
|
|
9
|
+
const unsigned = () => {
|
|
10
|
+
let value = 0n, shift = 0n;
|
|
11
|
+
for (let i = 0; i < 10; i++) {
|
|
12
|
+
const part = byte();
|
|
13
|
+
value |= BigInt(part & 127) << shift;
|
|
14
|
+
if (!(part & 128)) {
|
|
15
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER))
|
|
16
|
+
throw new RangeError('memory limit exceeds JS integer precision');
|
|
17
|
+
return Number(value);
|
|
18
|
+
}
|
|
19
|
+
shift += 7n;
|
|
20
|
+
}
|
|
21
|
+
throw new RangeError('invalid wasm integer');
|
|
22
|
+
};
|
|
23
|
+
const name = () => {
|
|
24
|
+
const length = unsigned();
|
|
25
|
+
const end = offset + length;
|
|
26
|
+
if (end > bytes.length)
|
|
27
|
+
throw new RangeError('truncated wasm name');
|
|
28
|
+
const value = new TextDecoder().decode(bytes.subarray(offset, end));
|
|
29
|
+
offset = end;
|
|
30
|
+
return value;
|
|
31
|
+
};
|
|
32
|
+
const reference = () => {
|
|
33
|
+
const type = byte();
|
|
34
|
+
if (type === 0x63 || type === 0x64) {
|
|
35
|
+
while (byte() & 128) { }
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
while (offset < bytes.length) {
|
|
39
|
+
const section = byte();
|
|
40
|
+
const length = unsigned();
|
|
41
|
+
const end = offset + length;
|
|
42
|
+
if (end > bytes.length)
|
|
43
|
+
throw new RangeError('truncated wasm section');
|
|
44
|
+
if (section !== 2) {
|
|
45
|
+
offset = end;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const count = unsigned();
|
|
49
|
+
const memories = [];
|
|
50
|
+
for (let i = 0; i < count; i++) {
|
|
51
|
+
const module = name(), base = name(), kind = byte();
|
|
52
|
+
if (kind === 0)
|
|
53
|
+
unsigned();
|
|
54
|
+
else if (kind === 1) {
|
|
55
|
+
reference();
|
|
56
|
+
const flags = unsigned();
|
|
57
|
+
unsigned();
|
|
58
|
+
if (flags & 1)
|
|
59
|
+
unsigned();
|
|
60
|
+
}
|
|
61
|
+
else if (kind === 2) {
|
|
62
|
+
const flags = unsigned();
|
|
63
|
+
if (flags & ~7)
|
|
64
|
+
throw new TypeError('unsupported memory-limit metadata');
|
|
65
|
+
const initial = unsigned(), maximum = flags & 1 ? unsigned() : undefined;
|
|
66
|
+
memories.push({
|
|
67
|
+
module,
|
|
68
|
+
name: base,
|
|
69
|
+
initial,
|
|
70
|
+
maximum,
|
|
71
|
+
shared: Boolean(flags & 2),
|
|
72
|
+
memory64: Boolean(flags & 4)
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
else if (kind === 3) {
|
|
76
|
+
reference();
|
|
77
|
+
byte();
|
|
78
|
+
}
|
|
79
|
+
else if (kind === 4) {
|
|
80
|
+
byte();
|
|
81
|
+
unsigned();
|
|
82
|
+
}
|
|
83
|
+
else
|
|
84
|
+
throw new TypeError('unsupported wasm import metadata');
|
|
85
|
+
}
|
|
86
|
+
return memories;
|
|
87
|
+
}
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import type { IterationLocal } from '
|
|
1
|
+
import type { IterationLocal } from '../execution/extract.ts';
|
|
2
2
|
/** Granule sizes of the inline lowering the iteration executor runs. */
|
|
3
3
|
export declare const iterationGranules: {
|
|
4
4
|
readonly read: 64;
|
|
5
5
|
readonly write: 32;
|
|
6
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* Undo entries per iteration of the grown log, 64 times the default capacity: 4 MiB of tracked
|
|
9
|
+
* writes. A batch whose log is exhausted is retried once at this size when the memory can grow.
|
|
10
|
+
*/
|
|
11
|
+
export declare const iterationLogCeiling = 131072;
|
|
7
12
|
/** Bytes of one undo entry: granule address, stamp, then the prior bytes of the granule. */
|
|
8
13
|
export declare const iterationEntryBytes: number;
|
|
9
14
|
/** Sizes of the per-batch arena. Every field has a default. */
|
|
@@ -16,6 +21,16 @@ export interface IterationArenaOptions {
|
|
|
16
21
|
slotBytes?: number;
|
|
17
22
|
/** Size of the owner frame whose reads the executor tracks, for the per-slot shadow. Default 0. */
|
|
18
23
|
frameBytes?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Bytes the slot-private windows of one loop may hold, a multiple of 64, which the frame shadows
|
|
26
|
+
* also cover. Default 0, which leaves windows off.
|
|
27
|
+
*/
|
|
28
|
+
windowBytes?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Reserve one summary byte per 64 words of every read bitmap, so the owner and the workers scan
|
|
31
|
+
* only the blocks a read touched. Default false.
|
|
32
|
+
*/
|
|
33
|
+
readSummary?: boolean;
|
|
19
34
|
}
|
|
20
35
|
/** Absolute addresses of the arena parts. Slot 0 sits above one unused guard slot. */
|
|
21
36
|
export interface IterationArena {
|
|
@@ -39,7 +54,19 @@ export interface IterationArena {
|
|
|
39
54
|
frameSlotBytes: number;
|
|
40
55
|
/** The tracked frame size. */
|
|
41
56
|
frameBytes: number;
|
|
57
|
+
/** The window bytes the shadows reserve room for. */
|
|
58
|
+
windowBytes: number;
|
|
59
|
+
/** Start of the read summaries, one per slot, after the frame shadows; 0 without a summary. */
|
|
60
|
+
smaps: number;
|
|
61
|
+
/** Bytes of one slot's read summary, a power of two of at least 64; 0 without a summary. */
|
|
62
|
+
ssize: number;
|
|
42
63
|
}
|
|
64
|
+
/** Words of a read bitmap one summary byte covers. */
|
|
65
|
+
export declare const iterationSummaryWords = 64;
|
|
66
|
+
/** Log2 of the bytes of address space one summary byte covers: 64 words of 32 granules. */
|
|
67
|
+
export declare const iterationSummaryShift: number;
|
|
68
|
+
/** Bytes of slot-private windows a Folio reserves room for in its arena. */
|
|
69
|
+
export declare const iterationWindowBytes = 24576;
|
|
43
70
|
/**
|
|
44
71
|
* Shadow of one tracked frame: `4 * ceil(size / 32)` bytes of written bits, one per frame byte
|
|
45
72
|
* (the bits of frame granule `g`, 32 bytes, are the i32 at `4 * g`), 4 pad bytes, the exposed bits
|
|
@@ -73,3 +100,36 @@ export declare function iterationFit(startBytes: number, options?: IterationAren
|
|
|
73
100
|
* keeps its entry value, and the frame local takes `frame`.
|
|
74
101
|
*/
|
|
75
102
|
export declare function iterationArguments(locals: readonly Pick<IterationLocal, 'local' | 'kind' | 'argument' | 'stride' | 'strideLocal'>[], entry: Readonly<Record<number, number | bigint>>, k: number, frame: number): (number | bigint)[];
|
|
103
|
+
/**
|
|
104
|
+
* Where one slot keeps the private copies of certified windows: each below the one before it, the
|
|
105
|
+
* first below the frame, each at its window's offset modulo 64. `low` is the lowest address used,
|
|
106
|
+
* where the slot's stack may start.
|
|
107
|
+
*/
|
|
108
|
+
export declare function iterationShadowCopies(top: number, frameSize: number, windows: readonly {
|
|
109
|
+
low: number;
|
|
110
|
+
end: number;
|
|
111
|
+
}[]): {
|
|
112
|
+
copies: {
|
|
113
|
+
copy: number;
|
|
114
|
+
delta: number;
|
|
115
|
+
}[];
|
|
116
|
+
low: number;
|
|
117
|
+
};
|
|
118
|
+
/** An address window the iterations of a loop run on slot-private copies of. */
|
|
119
|
+
export interface IterationWindow {
|
|
120
|
+
/** First byte, a multiple of 64. */
|
|
121
|
+
low: number;
|
|
122
|
+
/** One past the last byte, a multiple of 64. */
|
|
123
|
+
end: number;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Where one slot keeps its copy of a window: below the frame, so that the copy and the frame form
|
|
127
|
+
* one shadowed region from `copy` to the slot's top. The copy sits at the same offset modulo 64 as
|
|
128
|
+
* the window, so accesses keep their alignment. `frameOffset` is the frame's offset in the region.
|
|
129
|
+
*/
|
|
130
|
+
export declare function iterationWindowCopy(top: number, frameSize: number, window: IterationWindow): {
|
|
131
|
+
copy: number;
|
|
132
|
+
delta: number;
|
|
133
|
+
region: number;
|
|
134
|
+
frameOffset: number;
|
|
135
|
+
};
|
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
/** Granule sizes of the inline lowering the iteration executor runs. */
|
|
2
2
|
export const iterationGranules = { read: 64, write: 32 };
|
|
3
|
+
/**
|
|
4
|
+
* Undo entries per iteration of the grown log, 64 times the default capacity: 4 MiB of tracked
|
|
5
|
+
* writes. A batch whose log is exhausted is retried once at this size when the memory can grow.
|
|
6
|
+
*/
|
|
7
|
+
export const iterationLogCeiling = 131072;
|
|
3
8
|
/** Bytes of one undo entry: granule address, stamp, then the prior bytes of the granule. */
|
|
4
9
|
export const iterationEntryBytes = 8 + iterationGranules.write;
|
|
10
|
+
/** Words of a read bitmap one summary byte covers. */
|
|
11
|
+
export const iterationSummaryWords = 64;
|
|
12
|
+
/** Log2 of the bytes of address space one summary byte covers: 64 words of 32 granules. */
|
|
13
|
+
export const iterationSummaryShift = Math.log2(iterationGranules.read) + 11;
|
|
14
|
+
/** Bytes of slot-private windows a Folio reserves room for in its arena. */
|
|
15
|
+
export const iterationWindowBytes = 24576;
|
|
5
16
|
const align = (value, to) => Math.ceil(value / to) * to;
|
|
6
17
|
/**
|
|
7
18
|
* Shadow of one tracked frame: `4 * ceil(size / 32)` bytes of written bits, one per frame byte
|
|
@@ -24,8 +35,11 @@ export function iterationArena(base, memoryBytes, options = {}) {
|
|
|
24
35
|
const logCapacity = options.logCapacity ?? 2048;
|
|
25
36
|
const slotBytes = options.slotBytes ?? 32768;
|
|
26
37
|
const frameBytes = options.frameBytes ?? 0;
|
|
38
|
+
const windowBytes = options.windowBytes ?? 0;
|
|
27
39
|
if (!Number.isSafeInteger(frameBytes) || frameBytes < 0)
|
|
28
40
|
throw new RangeError('invalid frame');
|
|
41
|
+
if (!Number.isSafeInteger(windowBytes) || windowBytes < 0 || windowBytes % 64)
|
|
42
|
+
throw new RangeError('invalid window size');
|
|
29
43
|
if (!Number.isSafeInteger(base) || base < 0 || base % 64)
|
|
30
44
|
throw new RangeError('invalid base');
|
|
31
45
|
if (!Number.isSafeInteger(memoryBytes) || memoryBytes <= 0)
|
|
@@ -45,8 +59,15 @@ export function iterationArena(base, memoryBytes, options = {}) {
|
|
|
45
59
|
const wmaps = rmaps + batch * rsize;
|
|
46
60
|
const wsize = align(Math.ceil(memoryBytes / (iterationGranules.write * 8)), 64);
|
|
47
61
|
const frameShadow = wmaps + batch * wsize;
|
|
48
|
-
|
|
49
|
-
const
|
|
62
|
+
// a window copy sits below the frame, up to 63 bytes of padding apart, and both are shadowed
|
|
63
|
+
const frameSlotBytes = iterationFrameShadow(frameBytes + (windowBytes ? windowBytes + 64 : 0)).bytes;
|
|
64
|
+
const frameEnd = frameShadow + batch * frameSlotBytes;
|
|
65
|
+
let ssize = 0;
|
|
66
|
+
if (options.readSummary) {
|
|
67
|
+
const blocks = Math.ceil(rsize / 4 / iterationSummaryWords);
|
|
68
|
+
ssize = Math.max(64, 2 ** Math.ceil(Math.log2(blocks)));
|
|
69
|
+
}
|
|
70
|
+
const end = frameEnd + batch * ssize;
|
|
50
71
|
return {
|
|
51
72
|
base,
|
|
52
73
|
bytes: end - base,
|
|
@@ -64,7 +85,10 @@ export function iterationArena(base, memoryBytes, options = {}) {
|
|
|
64
85
|
wsize,
|
|
65
86
|
frameShadow,
|
|
66
87
|
frameSlotBytes,
|
|
67
|
-
frameBytes
|
|
88
|
+
frameBytes,
|
|
89
|
+
windowBytes,
|
|
90
|
+
smaps: ssize ? frameEnd : 0,
|
|
91
|
+
ssize
|
|
68
92
|
};
|
|
69
93
|
}
|
|
70
94
|
/** Bytes the caller must keep free for a pool over a memory of `memoryBytes`. */
|
|
@@ -116,3 +140,30 @@ export function iterationArguments(locals, entry, k, frame) {
|
|
|
116
140
|
}
|
|
117
141
|
return values;
|
|
118
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Where one slot keeps the private copies of certified windows: each below the one before it, the
|
|
145
|
+
* first below the frame, each at its window's offset modulo 64. `low` is the lowest address used,
|
|
146
|
+
* where the slot's stack may start.
|
|
147
|
+
*/
|
|
148
|
+
export function iterationShadowCopies(top, frameSize, windows) {
|
|
149
|
+
let at = top - frameSize;
|
|
150
|
+
const copies = windows.map((w) => {
|
|
151
|
+
const base = at - (w.end - w.low);
|
|
152
|
+
const copy = base - ((((base - w.low) % 64) + 64) % 64);
|
|
153
|
+
at = copy;
|
|
154
|
+
return { copy, delta: copy - w.low };
|
|
155
|
+
});
|
|
156
|
+
return { copies, low: at };
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Where one slot keeps its copy of a window: below the frame, so that the copy and the frame form
|
|
160
|
+
* one shadowed region from `copy` to the slot's top. The copy sits at the same offset modulo 64 as
|
|
161
|
+
* the window, so accesses keep their alignment. `frameOffset` is the frame's offset in the region.
|
|
162
|
+
*/
|
|
163
|
+
export function iterationWindowCopy(top, frameSize, window) {
|
|
164
|
+
const bytes = window.end - window.low;
|
|
165
|
+
const frame = top - frameSize;
|
|
166
|
+
const base = frame - bytes;
|
|
167
|
+
const copy = base - ((((base - window.low) % 64) + 64) % 64);
|
|
168
|
+
return { copy, delta: copy - window.low, region: top - copy, frameOffset: frame - copy };
|
|
169
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** byte size, page size, and one lowercase hex sha256 per page; the last page may be short. */
|
|
2
|
+
export interface LeaseManifest {
|
|
3
|
+
size: number;
|
|
4
|
+
pageSize: number;
|
|
5
|
+
hashes: string[];
|
|
6
|
+
}
|
|
7
|
+
/** peek found pages that are not resident. */
|
|
8
|
+
export declare class PageAbsent extends Error {
|
|
9
|
+
readonly pages: readonly number[];
|
|
10
|
+
constructor(pages: readonly number[]);
|
|
11
|
+
}
|
|
12
|
+
/** a page failed its manifest hash on every attempt; nothing was inserted. */
|
|
13
|
+
export declare class LeaseMismatch extends Error {
|
|
14
|
+
readonly page: number;
|
|
15
|
+
constructor(page: number);
|
|
16
|
+
}
|
|
17
|
+
export interface LeaseOptions {
|
|
18
|
+
manifest: LeaseManifest;
|
|
19
|
+
/** the remote read; returns fewer bytes than asked only at the end of the object. */
|
|
20
|
+
fetchRange(offset: number, length: number): Promise<Uint8Array>;
|
|
21
|
+
/** lru budget in bytes, raised to one page when smaller. default 4 MiB. */
|
|
22
|
+
residentBytes?: number;
|
|
23
|
+
/** refetches of a page whose bytes failed verification. default 1. */
|
|
24
|
+
retries?: number;
|
|
25
|
+
}
|
|
26
|
+
export interface LeaseStats {
|
|
27
|
+
hits: number;
|
|
28
|
+
misses: number;
|
|
29
|
+
fetches: number;
|
|
30
|
+
fetchedBytes: number;
|
|
31
|
+
mismatches: number;
|
|
32
|
+
evictions: number;
|
|
33
|
+
residentBytes: number;
|
|
34
|
+
}
|
|
35
|
+
/** paged read-through cache over a remote immutable object; every page is verified. */
|
|
36
|
+
export declare class Lease {
|
|
37
|
+
#private;
|
|
38
|
+
constructor(options: LeaseOptions);
|
|
39
|
+
get stats(): LeaseStats;
|
|
40
|
+
peek(offset: number, length: number): Uint8Array;
|
|
41
|
+
read(offset: number, length: number): Promise<Uint8Array>;
|
|
42
|
+
prefetch(offset: number, length: number): Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
/** producer side: pages of bytes and their hashes; the last page may be short. */
|
|
45
|
+
export declare function buildManifest(bytes: Uint8Array, pageSize: number): Promise<LeaseManifest>;
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/** peek found pages that are not resident. */
|
|
2
|
+
export class PageAbsent extends Error {
|
|
3
|
+
pages;
|
|
4
|
+
constructor(pages) {
|
|
5
|
+
super(`lease pages not resident: ${pages.join(', ')}`);
|
|
6
|
+
this.name = 'PageAbsent';
|
|
7
|
+
this.pages = pages;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/** a page failed its manifest hash on every attempt; nothing was inserted. */
|
|
11
|
+
export class LeaseMismatch extends Error {
|
|
12
|
+
page;
|
|
13
|
+
constructor(page) {
|
|
14
|
+
super(`lease page ${page} failed its hash`);
|
|
15
|
+
this.name = 'LeaseMismatch';
|
|
16
|
+
this.page = page;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** paged read-through cache over a remote immutable object; every page is verified. */
|
|
20
|
+
export class Lease {
|
|
21
|
+
#options;
|
|
22
|
+
#manifest;
|
|
23
|
+
#budget;
|
|
24
|
+
#retries;
|
|
25
|
+
// insertion order is recency order, oldest first
|
|
26
|
+
#resident = new Map();
|
|
27
|
+
#pins = new Map();
|
|
28
|
+
#inflight = new Map();
|
|
29
|
+
#waiting = [];
|
|
30
|
+
// a page another read is fetching counts as a miss
|
|
31
|
+
#stats = {
|
|
32
|
+
hits: 0,
|
|
33
|
+
misses: 0,
|
|
34
|
+
fetches: 0,
|
|
35
|
+
fetchedBytes: 0,
|
|
36
|
+
mismatches: 0,
|
|
37
|
+
evictions: 0,
|
|
38
|
+
residentBytes: 0
|
|
39
|
+
};
|
|
40
|
+
constructor(options) {
|
|
41
|
+
const { size, pageSize, hashes } = options.manifest;
|
|
42
|
+
if (!Number.isSafeInteger(size) ||
|
|
43
|
+
size < 0 ||
|
|
44
|
+
!Number.isSafeInteger(pageSize) ||
|
|
45
|
+
pageSize < 1 ||
|
|
46
|
+
hashes.length !== Math.ceil(size / pageSize)) {
|
|
47
|
+
throw new RangeError('lease manifest hashes do not cover its size');
|
|
48
|
+
}
|
|
49
|
+
this.#options = options;
|
|
50
|
+
this.#manifest = options.manifest;
|
|
51
|
+
this.#budget = Math.max(options.residentBytes ?? 4 * 2 ** 20, pageSize);
|
|
52
|
+
this.#retries = options.retries ?? 1;
|
|
53
|
+
}
|
|
54
|
+
get stats() {
|
|
55
|
+
return { ...this.#stats };
|
|
56
|
+
}
|
|
57
|
+
peek(offset, length) {
|
|
58
|
+
this.#check(offset, length);
|
|
59
|
+
if (length === 0)
|
|
60
|
+
return new Uint8Array(0);
|
|
61
|
+
const first = this.#pageOf(offset);
|
|
62
|
+
const last = this.#pageOf(offset + length - 1);
|
|
63
|
+
const pages = [];
|
|
64
|
+
const absent = [];
|
|
65
|
+
for (let page = first; page <= last; page++) {
|
|
66
|
+
const bytes = this.#resident.get(page);
|
|
67
|
+
if (bytes) {
|
|
68
|
+
this.#hit(page, bytes);
|
|
69
|
+
pages.push(bytes);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
this.#stats.misses++;
|
|
73
|
+
absent.push(page);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (absent.length)
|
|
77
|
+
throw new PageAbsent(absent);
|
|
78
|
+
return this.#assemble(offset, length, first, pages);
|
|
79
|
+
}
|
|
80
|
+
async read(offset, length) {
|
|
81
|
+
this.#check(offset, length);
|
|
82
|
+
if (length === 0)
|
|
83
|
+
return new Uint8Array(0);
|
|
84
|
+
const first = this.#pageOf(offset);
|
|
85
|
+
const pages = await this.#gather(first, this.#pageOf(offset + length - 1));
|
|
86
|
+
return this.#assemble(offset, length, first, pages);
|
|
87
|
+
}
|
|
88
|
+
async prefetch(offset, length) {
|
|
89
|
+
this.#check(offset, length);
|
|
90
|
+
if (length === 0)
|
|
91
|
+
return;
|
|
92
|
+
await this.#gather(this.#pageOf(offset), this.#pageOf(offset + length - 1));
|
|
93
|
+
}
|
|
94
|
+
// pins the range once the pages it newly pins fit beside the pinned ones, or it runs alone
|
|
95
|
+
async #turn(first, last) {
|
|
96
|
+
for (;;) {
|
|
97
|
+
let pinned = 0;
|
|
98
|
+
for (const page of this.#pins.keys())
|
|
99
|
+
pinned += this.#pageLength(page);
|
|
100
|
+
let need = 0;
|
|
101
|
+
for (let page = first; page <= last; page++)
|
|
102
|
+
if (!this.#pins.has(page))
|
|
103
|
+
need += this.#pageLength(page);
|
|
104
|
+
if (pinned === 0 || pinned + need <= this.#budget) {
|
|
105
|
+
this.#pin(first, last, 1);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
await new Promise((resolve) => this.#waiting.push(resolve));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// a pinned page is never evicted while a read still needs it
|
|
112
|
+
async #gather(first, last) {
|
|
113
|
+
await this.#turn(first, last);
|
|
114
|
+
try {
|
|
115
|
+
const parts = await Promise.all(this.#claim(first, last));
|
|
116
|
+
// digests finish in any order; recency follows page order so the tail of a range stays
|
|
117
|
+
for (let page = first; page <= last; page++) {
|
|
118
|
+
const bytes = this.#resident.get(page);
|
|
119
|
+
if (!bytes)
|
|
120
|
+
continue;
|
|
121
|
+
this.#resident.delete(page);
|
|
122
|
+
this.#resident.set(page, bytes);
|
|
123
|
+
}
|
|
124
|
+
return parts;
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
this.#pin(first, last, -1);
|
|
128
|
+
this.#evict();
|
|
129
|
+
for (const wake of this.#waiting.splice(0))
|
|
130
|
+
wake();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// starts one fetch per run of adjacent missing pages and returns a promise per page
|
|
134
|
+
#claim(first, last) {
|
|
135
|
+
const missing = [];
|
|
136
|
+
for (let page = first; page <= last; page++) {
|
|
137
|
+
const bytes = this.#resident.get(page);
|
|
138
|
+
if (bytes) {
|
|
139
|
+
this.#hit(page, bytes);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
this.#stats.misses++;
|
|
143
|
+
if (!this.#inflight.has(page))
|
|
144
|
+
missing.push(page);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
for (let i = 0; i < missing.length; i++) {
|
|
148
|
+
let end = i;
|
|
149
|
+
while (end + 1 < missing.length && missing[end + 1] === missing[end] + 1)
|
|
150
|
+
end++;
|
|
151
|
+
this.#fetch(missing[i], missing[end]);
|
|
152
|
+
i = end;
|
|
153
|
+
}
|
|
154
|
+
const parts = [];
|
|
155
|
+
for (let page = first; page <= last; page++) {
|
|
156
|
+
const bytes = this.#resident.get(page);
|
|
157
|
+
parts.push(bytes ? Promise.resolve(bytes) : this.#inflight.get(page));
|
|
158
|
+
}
|
|
159
|
+
return parts;
|
|
160
|
+
}
|
|
161
|
+
#fetch(first, last) {
|
|
162
|
+
const { pageSize, size } = this.#manifest;
|
|
163
|
+
const offset = first * pageSize;
|
|
164
|
+
const source = this.#transfer(offset, Math.min((last + 1) * pageSize, size) - offset);
|
|
165
|
+
for (let page = first; page <= last; page++) {
|
|
166
|
+
this.#inflight.set(page, this.#admit(page, first, source));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async #transfer(offset, length) {
|
|
170
|
+
this.#stats.fetches++;
|
|
171
|
+
const bytes = await this.#options.fetchRange(offset, length);
|
|
172
|
+
this.#stats.fetchedBytes += bytes.length;
|
|
173
|
+
return bytes;
|
|
174
|
+
}
|
|
175
|
+
// source holds bytes from page first onward; a retry replaces it with the one page
|
|
176
|
+
async #admit(page, first, source) {
|
|
177
|
+
const { pageSize } = this.#manifest;
|
|
178
|
+
const length = this.#pageLength(page);
|
|
179
|
+
try {
|
|
180
|
+
let origin = first * pageSize;
|
|
181
|
+
let bytes = await source;
|
|
182
|
+
for (let attempt = 0;; attempt++) {
|
|
183
|
+
const start = page * pageSize - origin;
|
|
184
|
+
const copy = bytes.slice(start, start + length);
|
|
185
|
+
if (copy.length === length &&
|
|
186
|
+
(await digest(copy)) === this.#manifest.hashes[page]) {
|
|
187
|
+
this.#insert(page, copy);
|
|
188
|
+
return copy;
|
|
189
|
+
}
|
|
190
|
+
this.#stats.mismatches++;
|
|
191
|
+
if (attempt >= this.#retries)
|
|
192
|
+
throw new LeaseMismatch(page);
|
|
193
|
+
origin = page * pageSize;
|
|
194
|
+
bytes = await this.#transfer(origin, length);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
finally {
|
|
198
|
+
this.#inflight.delete(page);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
#insert(page, bytes) {
|
|
202
|
+
this.#resident.set(page, bytes);
|
|
203
|
+
this.#stats.residentBytes += bytes.length;
|
|
204
|
+
this.#evict();
|
|
205
|
+
}
|
|
206
|
+
#hit(page, bytes) {
|
|
207
|
+
this.#stats.hits++;
|
|
208
|
+
this.#resident.delete(page);
|
|
209
|
+
this.#resident.set(page, bytes);
|
|
210
|
+
}
|
|
211
|
+
#evict() {
|
|
212
|
+
for (const [page, bytes] of this.#resident) {
|
|
213
|
+
if (this.#stats.residentBytes <= this.#budget)
|
|
214
|
+
return;
|
|
215
|
+
if (this.#pins.has(page))
|
|
216
|
+
continue;
|
|
217
|
+
this.#resident.delete(page);
|
|
218
|
+
this.#stats.residentBytes -= bytes.length;
|
|
219
|
+
this.#stats.evictions++;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
#pin(first, last, delta) {
|
|
223
|
+
for (let page = first; page <= last; page++) {
|
|
224
|
+
const count = (this.#pins.get(page) ?? 0) + delta;
|
|
225
|
+
if (count)
|
|
226
|
+
this.#pins.set(page, count);
|
|
227
|
+
else
|
|
228
|
+
this.#pins.delete(page);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
#assemble(offset, length, first, pages) {
|
|
232
|
+
const { pageSize } = this.#manifest;
|
|
233
|
+
const out = new Uint8Array(length);
|
|
234
|
+
pages.forEach((bytes, i) => {
|
|
235
|
+
const start = (first + i) * pageSize;
|
|
236
|
+
const from = Math.max(offset - start, 0);
|
|
237
|
+
const to = Math.min(offset + length - start, bytes.length);
|
|
238
|
+
out.set(bytes.subarray(from, to), start + from - offset);
|
|
239
|
+
});
|
|
240
|
+
return out;
|
|
241
|
+
}
|
|
242
|
+
#pageOf(byte) {
|
|
243
|
+
return Math.floor(byte / this.#manifest.pageSize);
|
|
244
|
+
}
|
|
245
|
+
#pageLength(page) {
|
|
246
|
+
const { pageSize, size } = this.#manifest;
|
|
247
|
+
return Math.min(pageSize, size - page * pageSize);
|
|
248
|
+
}
|
|
249
|
+
#check(offset, length) {
|
|
250
|
+
const { size } = this.#manifest;
|
|
251
|
+
if (!Number.isSafeInteger(offset) ||
|
|
252
|
+
!Number.isSafeInteger(length) ||
|
|
253
|
+
offset < 0 ||
|
|
254
|
+
length < 0 ||
|
|
255
|
+
offset + length > size) {
|
|
256
|
+
throw new RangeError(`lease range ${offset}+${length} is outside 0..${size}`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/** producer side: pages of bytes and their hashes; the last page may be short. */
|
|
261
|
+
export async function buildManifest(bytes, pageSize) {
|
|
262
|
+
if (!Number.isSafeInteger(pageSize) || pageSize < 1) {
|
|
263
|
+
throw new RangeError(`page size ${pageSize} is not a positive integer`);
|
|
264
|
+
}
|
|
265
|
+
const hashes = [];
|
|
266
|
+
for (let at = 0; at < bytes.length; at += pageSize) {
|
|
267
|
+
hashes.push(await digest(bytes.subarray(at, at + pageSize)));
|
|
268
|
+
}
|
|
269
|
+
return { size: bytes.length, pageSize, hashes };
|
|
270
|
+
}
|
|
271
|
+
async function digest(bytes) {
|
|
272
|
+
const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', bytes));
|
|
273
|
+
return Array.from(hash, (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
274
|
+
}
|
|
@@ -12,8 +12,8 @@ export interface PageVersion {
|
|
|
12
12
|
fence: number;
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
* Transport boundary for immutable pages. Reads return independent copies. Staging is idempotent
|
|
16
|
-
* identical versions and bytes; discarding fences that version against delayed stage requests.
|
|
15
|
+
* Transport boundary for immutable pages. Reads return independent copies. Staging is idempotent
|
|
16
|
+
* for identical versions and bytes; discarding fences that version against delayed stage requests.
|
|
17
17
|
* Implementations must authenticate the scope and enforce their own storage, request and deadline
|
|
18
18
|
* limits. A never-settling request retains its directory locks; expiry cannot cancel an RPC.
|
|
19
19
|
*/
|
|
@@ -34,7 +34,7 @@ export declare class MemoryPageOwner implements PageOwner {
|
|
|
34
34
|
stage(scope: PageScope, version: PageVersion, bytes: Uint8Array): Promise<void>;
|
|
35
35
|
discard(scope: PageScope, version: PageVersion): Promise<void>;
|
|
36
36
|
}
|
|
37
|
-
/** Bounds
|
|
37
|
+
/** Bounds cover retained metadata, resident pages and commit snapshots, not caller-owned copies. */
|
|
38
38
|
export interface PageDirectoryOptions {
|
|
39
39
|
/** Fresh incarnation for one trusted active directory; never reuse it after a restart. */
|
|
40
40
|
scope: PageScope;
|
package/dist/memory.d.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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[];
|
|
1
|
+
export * from './memory/elastic-peer.ts';
|
|
2
|
+
export * from './memory/elastic-sql.ts';
|
|
3
|
+
export * from './memory/elastic.ts';
|
|
4
|
+
export * from './memory/growth-governor.ts';
|
|
5
|
+
export * from './memory/pages.ts';
|