@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,74 @@
|
|
|
1
|
+
import type { TransactionResult } from '../execution/transaction.ts';
|
|
2
|
+
export interface MemoStats {
|
|
3
|
+
lookups: number;
|
|
4
|
+
hits: number;
|
|
5
|
+
misses: number;
|
|
6
|
+
/** Bytes hashed by lookups, summed over each candidate entry. */
|
|
7
|
+
hashedBytes: number;
|
|
8
|
+
/** Estimated cost of every entry recorded; see `RegionMemo` for the estimate. */
|
|
9
|
+
recordedBytes: number;
|
|
10
|
+
/** Estimated cost of the entries currently held. */
|
|
11
|
+
storedBytes: number;
|
|
12
|
+
recorded: number;
|
|
13
|
+
evictions: number;
|
|
14
|
+
/** Recorded execution time of every hit, summed. An estimate of lane time avoided. */
|
|
15
|
+
savedMs: number;
|
|
16
|
+
}
|
|
17
|
+
export interface MemoOptions {
|
|
18
|
+
/** Budget for stored entries, estimated in bytes. Default 8 MiB. */
|
|
19
|
+
bytes?: number;
|
|
20
|
+
/** Entries kept per key. Default 4. */
|
|
21
|
+
entries?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface MemoProbe {
|
|
24
|
+
observations: number;
|
|
25
|
+
distinct: number;
|
|
26
|
+
repeats: number;
|
|
27
|
+
/** Repeats divided by observations; an upper bound on the hit rate of a memo with no limits. */
|
|
28
|
+
repeatRate: number;
|
|
29
|
+
}
|
|
30
|
+
export interface MemoHit {
|
|
31
|
+
result: TransactionResult;
|
|
32
|
+
ms: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Recorded region results, reused while the bytes a region read are unchanged. The module string
|
|
36
|
+
* passed to `keyOf` must identify the module bytes and their instrumentation; a memo shared across
|
|
37
|
+
* processes or restarts keys on a content hash. A region must be deterministic in its read bytes
|
|
38
|
+
* and arguments, so it may not read a clock, randomness, a host import or a mutable global.
|
|
39
|
+
* `record` refuses traps, exhausted fuel, inconclusive results and reads outside shipped ranges.
|
|
40
|
+
*/
|
|
41
|
+
export declare class RegionMemo {
|
|
42
|
+
#private;
|
|
43
|
+
constructor(options?: MemoOptions);
|
|
44
|
+
/**
|
|
45
|
+
* The key for a call in a module, or undefined when an argument is not an exact integer. The
|
|
46
|
+
* module string is the host's identity for the tracked module; see the contract above.
|
|
47
|
+
*/
|
|
48
|
+
static keyOf(module: string, name: string, args: readonly unknown[]): string | undefined;
|
|
49
|
+
get stats(): MemoStats;
|
|
50
|
+
/**
|
|
51
|
+
* Returns the recorded result whose read bytes equal the current bytes, or undefined. With
|
|
52
|
+
* `runs`, the write set comes as `writeRuns` and `writeData`, which the owner commits as
|
|
53
|
+
* intervals; otherwise as `[address, value]` tuples.
|
|
54
|
+
*/
|
|
55
|
+
lookup(key: string, memory: Uint8Array, options?: {
|
|
56
|
+
runs?: boolean;
|
|
57
|
+
}): Promise<MemoHit | undefined>;
|
|
58
|
+
/**
|
|
59
|
+
* Records a clean result of an execution that started from `memory`. Returns false when the
|
|
60
|
+
* result is refused or already held. Throws on a read or write that the memory cannot hold.
|
|
61
|
+
*
|
|
62
|
+
* The cost estimate counts 8 bytes per read address and 40 per write (a two-element array and
|
|
63
|
+
* its slot), plus the hashed span table and the 32-byte digest.
|
|
64
|
+
*/
|
|
65
|
+
record(key: string, memory: Uint8Array, result: TransactionResult, ms?: number): Promise<boolean>;
|
|
66
|
+
/** Drops every entry and resets the counters. */
|
|
67
|
+
clear(): void;
|
|
68
|
+
/** Counts how often (key, read bytes) pairs repeat. Nothing is recorded. */
|
|
69
|
+
static probe(observations: Iterable<{
|
|
70
|
+
key: string;
|
|
71
|
+
memory: Uint8Array;
|
|
72
|
+
reads: readonly number[];
|
|
73
|
+
}>): Promise<MemoProbe>;
|
|
74
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { writeRunsOf } from "../execution/runs.js";
|
|
2
|
+
import { FootprintCache, RangeSet } from "./ranges.js";
|
|
3
|
+
async function digest(memory, spans) {
|
|
4
|
+
let total = 0;
|
|
5
|
+
for (let i = 1; i < spans.length; i += 2)
|
|
6
|
+
total += spans[i];
|
|
7
|
+
const buffer = new Uint8Array(total);
|
|
8
|
+
let offset = 0;
|
|
9
|
+
for (let i = 0; i < spans.length; i += 2) {
|
|
10
|
+
buffer.set(memory.subarray(spans[i], spans[i] + spans[i + 1]), offset);
|
|
11
|
+
offset += spans[i + 1];
|
|
12
|
+
}
|
|
13
|
+
const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', buffer));
|
|
14
|
+
return {
|
|
15
|
+
digest: Array.from(hash, (byte) => byte.toString(16).padStart(2, '0')).join(''),
|
|
16
|
+
bytes: total
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function spanArray(reads) {
|
|
20
|
+
return Uint32Array.from(RangeSet.fromAddresses(reads).spans().flat());
|
|
21
|
+
}
|
|
22
|
+
// a run-encoded result (`reads` empty, `readRuns`) is expanded so the record holds one shape
|
|
23
|
+
function addressForm(result) {
|
|
24
|
+
if (!result.readRuns && !result.writeRuns)
|
|
25
|
+
return result;
|
|
26
|
+
const reads = [...result.reads];
|
|
27
|
+
for (let i = 0; i < (result.readRuns?.length ?? 0); i += 2)
|
|
28
|
+
for (let k = 0; k < result.readRuns[i + 1]; k++)
|
|
29
|
+
reads.push(result.readRuns[i] + k);
|
|
30
|
+
const writes = [...result.writes];
|
|
31
|
+
let at = 0;
|
|
32
|
+
for (let i = 0; i < (result.writeRuns?.length ?? 0); i += 2)
|
|
33
|
+
for (let k = 0; k < result.writeRuns[i + 1]; k++)
|
|
34
|
+
writes.push([result.writeRuns[i] + k, result.writeData[at++]]);
|
|
35
|
+
return {
|
|
36
|
+
...result,
|
|
37
|
+
reads,
|
|
38
|
+
writes,
|
|
39
|
+
readRuns: undefined,
|
|
40
|
+
writeRuns: undefined,
|
|
41
|
+
writeData: undefined
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Recorded region results, reused while the bytes a region read are unchanged. The module string
|
|
46
|
+
* passed to `keyOf` must identify the module bytes and their instrumentation; a memo shared across
|
|
47
|
+
* processes or restarts keys on a content hash. A region must be deterministic in its read bytes
|
|
48
|
+
* and arguments, so it may not read a clock, randomness, a host import or a mutable global.
|
|
49
|
+
* `record` refuses traps, exhausted fuel, inconclusive results and reads outside shipped ranges.
|
|
50
|
+
*/
|
|
51
|
+
export class RegionMemo {
|
|
52
|
+
#budget;
|
|
53
|
+
#perKey;
|
|
54
|
+
#byKey = new Map();
|
|
55
|
+
// every entry, least recently used first
|
|
56
|
+
#lru = new Set();
|
|
57
|
+
#stats = emptyStats();
|
|
58
|
+
constructor(options = {}) {
|
|
59
|
+
this.#budget = options.bytes ?? 8 * 1048576;
|
|
60
|
+
this.#perKey = options.entries ?? 4;
|
|
61
|
+
if (!Number.isSafeInteger(this.#budget) || this.#budget < 1)
|
|
62
|
+
throw new RangeError('invalid memo byte budget');
|
|
63
|
+
if (!Number.isSafeInteger(this.#perKey) || this.#perKey < 1)
|
|
64
|
+
throw new RangeError('invalid memo entry limit');
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The key for a call in a module, or undefined when an argument is not an exact integer. The
|
|
68
|
+
* module string is the host's identity for the tracked module; see the contract above.
|
|
69
|
+
*/
|
|
70
|
+
static keyOf(module, name, args) {
|
|
71
|
+
for (const value of args) {
|
|
72
|
+
if (typeof value === 'bigint')
|
|
73
|
+
continue;
|
|
74
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || Object.is(value, -0))
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return JSON.stringify([module, FootprintCache.key(name, args)]);
|
|
78
|
+
}
|
|
79
|
+
get stats() {
|
|
80
|
+
return { ...this.#stats };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Returns the recorded result whose read bytes equal the current bytes, or undefined. With
|
|
84
|
+
* `runs`, the write set comes as `writeRuns` and `writeData`, which the owner commits as
|
|
85
|
+
* intervals; otherwise as `[address, value]` tuples.
|
|
86
|
+
*/
|
|
87
|
+
async lookup(key, memory, options = {}) {
|
|
88
|
+
this.#stats.lookups++;
|
|
89
|
+
const list = this.#byKey.get(key);
|
|
90
|
+
if (list)
|
|
91
|
+
for (const entry of [...list].reverse()) {
|
|
92
|
+
if (entry.memoryBytes !== memory.byteLength)
|
|
93
|
+
continue;
|
|
94
|
+
const now = await digest(memory, entry.spans);
|
|
95
|
+
this.#stats.hashedBytes += now.bytes;
|
|
96
|
+
// an entry removed while the digest ran must not be touched back into the LRU
|
|
97
|
+
if (!this.#lru.has(entry) || now.digest !== entry.digest)
|
|
98
|
+
continue;
|
|
99
|
+
this.#touch(entry);
|
|
100
|
+
this.#stats.hits++;
|
|
101
|
+
this.#stats.savedMs += entry.ms;
|
|
102
|
+
if (options.runs)
|
|
103
|
+
return {
|
|
104
|
+
result: {
|
|
105
|
+
value: entry.value,
|
|
106
|
+
reads: entry.reads.slice(),
|
|
107
|
+
writes: [],
|
|
108
|
+
writeRuns: entry.runs.slice(),
|
|
109
|
+
writeData: entry.runData.slice()
|
|
110
|
+
},
|
|
111
|
+
ms: entry.ms
|
|
112
|
+
};
|
|
113
|
+
entry.writes ??= Array.from(entry.writeAddresses, (address, i) => Object.freeze([address, entry.writeValues[i]]));
|
|
114
|
+
return {
|
|
115
|
+
result: {
|
|
116
|
+
value: entry.value,
|
|
117
|
+
reads: entry.reads.slice(),
|
|
118
|
+
// the tuples are frozen, so the copy cannot change the entry
|
|
119
|
+
writes: entry.writes.slice()
|
|
120
|
+
},
|
|
121
|
+
ms: entry.ms
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
this.#stats.misses++;
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Records a clean result of an execution that started from `memory`. Returns false when the
|
|
129
|
+
* result is refused or already held. Throws on a read or write that the memory cannot hold.
|
|
130
|
+
*
|
|
131
|
+
* The cost estimate counts 8 bytes per read address and 40 per write (a two-element array and
|
|
132
|
+
* its slot), plus the hashed span table and the 32-byte digest.
|
|
133
|
+
*/
|
|
134
|
+
async record(key, memory, result, ms = 0) {
|
|
135
|
+
result = addressForm(result);
|
|
136
|
+
if (result.trap !== undefined ||
|
|
137
|
+
result.exhausted ||
|
|
138
|
+
result.inconclusive ||
|
|
139
|
+
result.escapes?.length ||
|
|
140
|
+
!Number.isFinite(ms) ||
|
|
141
|
+
ms < 0)
|
|
142
|
+
return false;
|
|
143
|
+
for (const address of result.reads)
|
|
144
|
+
if (!Number.isSafeInteger(address) || address < 0 || address >= memory.byteLength)
|
|
145
|
+
throw new RangeError('memo read outside memory');
|
|
146
|
+
for (const [address, value] of result.writes)
|
|
147
|
+
if (!Number.isSafeInteger(address) ||
|
|
148
|
+
address < 0 ||
|
|
149
|
+
address >= memory.byteLength ||
|
|
150
|
+
!Number.isInteger(value) ||
|
|
151
|
+
value < 0 ||
|
|
152
|
+
value > 255)
|
|
153
|
+
throw new RangeError('memo write outside memory or not a byte');
|
|
154
|
+
const spans = spanArray(result.reads);
|
|
155
|
+
const seen = await digest(memory, spans);
|
|
156
|
+
const memoryBytes = memory.byteLength;
|
|
157
|
+
const list = this.#byKey.get(key) ?? [];
|
|
158
|
+
if (list.some((entry) => entry.memoryBytes === memoryBytes && entry.digest === seen.digest))
|
|
159
|
+
return false;
|
|
160
|
+
const reads = [...result.reads];
|
|
161
|
+
const writeAddresses = Uint32Array.from(result.writes, ([address]) => address);
|
|
162
|
+
const writeValues = Uint8Array.from(result.writes, ([, value]) => value);
|
|
163
|
+
const packed = writeRunsOf(result.writes);
|
|
164
|
+
const cost = spans.byteLength +
|
|
165
|
+
reads.length * 8 +
|
|
166
|
+
writeAddresses.length * 40 +
|
|
167
|
+
packed.runs.byteLength +
|
|
168
|
+
packed.data.byteLength +
|
|
169
|
+
32;
|
|
170
|
+
if (cost > this.#budget)
|
|
171
|
+
return false;
|
|
172
|
+
const entry = {
|
|
173
|
+
key,
|
|
174
|
+
memoryBytes,
|
|
175
|
+
spans,
|
|
176
|
+
digest: seen.digest,
|
|
177
|
+
reads,
|
|
178
|
+
writeAddresses,
|
|
179
|
+
writeValues,
|
|
180
|
+
runs: packed.runs,
|
|
181
|
+
runData: packed.data,
|
|
182
|
+
value: result.value,
|
|
183
|
+
ms,
|
|
184
|
+
cost
|
|
185
|
+
};
|
|
186
|
+
list.push(entry);
|
|
187
|
+
this.#byKey.set(key, list);
|
|
188
|
+
this.#lru.add(entry);
|
|
189
|
+
this.#stats.recorded++;
|
|
190
|
+
this.#stats.recordedBytes += cost;
|
|
191
|
+
this.#stats.storedBytes += cost;
|
|
192
|
+
while (list.length > this.#perKey)
|
|
193
|
+
this.#evict(list[0]);
|
|
194
|
+
while (this.#stats.storedBytes > this.#budget)
|
|
195
|
+
this.#evict(this.#lru.values().next().value);
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
/** Drops every entry and resets the counters. */
|
|
199
|
+
clear() {
|
|
200
|
+
this.#byKey.clear();
|
|
201
|
+
this.#lru.clear();
|
|
202
|
+
this.#stats = emptyStats();
|
|
203
|
+
}
|
|
204
|
+
/** Counts how often (key, read bytes) pairs repeat. Nothing is recorded. */
|
|
205
|
+
static async probe(observations) {
|
|
206
|
+
const counts = new Map();
|
|
207
|
+
let observed = 0;
|
|
208
|
+
for (const { key, memory, reads } of observations) {
|
|
209
|
+
const spans = spanArray(reads);
|
|
210
|
+
const seen = await digest(memory, spans);
|
|
211
|
+
const id = `${key}|${memory.byteLength}|${Array.from(spans).join(',')}|${seen.digest}`;
|
|
212
|
+
counts.set(id, (counts.get(id) ?? 0) + 1);
|
|
213
|
+
observed++;
|
|
214
|
+
}
|
|
215
|
+
let repeats = 0;
|
|
216
|
+
for (const count of counts.values())
|
|
217
|
+
repeats += count - 1;
|
|
218
|
+
return {
|
|
219
|
+
observations: observed,
|
|
220
|
+
distinct: counts.size,
|
|
221
|
+
repeats,
|
|
222
|
+
repeatRate: observed ? repeats / observed : 0
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
#touch(entry) {
|
|
226
|
+
this.#lru.delete(entry);
|
|
227
|
+
this.#lru.add(entry);
|
|
228
|
+
const list = this.#byKey.get(entry.key);
|
|
229
|
+
list.splice(list.indexOf(entry), 1);
|
|
230
|
+
list.push(entry);
|
|
231
|
+
}
|
|
232
|
+
#evict(entry) {
|
|
233
|
+
this.#lru.delete(entry);
|
|
234
|
+
const list = this.#byKey.get(entry.key);
|
|
235
|
+
if (list) {
|
|
236
|
+
list.splice(list.indexOf(entry), 1);
|
|
237
|
+
if (!list.length)
|
|
238
|
+
this.#byKey.delete(entry.key);
|
|
239
|
+
}
|
|
240
|
+
this.#stats.storedBytes -= entry.cost;
|
|
241
|
+
this.#stats.evictions++;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function emptyStats() {
|
|
245
|
+
return {
|
|
246
|
+
lookups: 0,
|
|
247
|
+
hits: 0,
|
|
248
|
+
misses: 0,
|
|
249
|
+
hashedBytes: 0,
|
|
250
|
+
recordedBytes: 0,
|
|
251
|
+
storedBytes: 0,
|
|
252
|
+
recorded: 0,
|
|
253
|
+
evictions: 0,
|
|
254
|
+
savedMs: 0
|
|
255
|
+
};
|
|
256
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** Page size for rounding learned footprints. */
|
|
2
|
+
export declare const rangePage = 4096;
|
|
3
|
+
/** Most ranges one frame carries; the header is capped at 64 KiB. */
|
|
4
|
+
export declare const maximumRanges = 1024;
|
|
5
|
+
/** Wire form of shipped ranges: omitted bytes are unknown, not zero. */
|
|
6
|
+
export interface RangesHeader {
|
|
7
|
+
memoryBytes: number;
|
|
8
|
+
/** Flat start, length pairs. */
|
|
9
|
+
spans: number[];
|
|
10
|
+
}
|
|
11
|
+
/** A sorted set of non-overlapping, non-touching byte ranges. Operations return new sets. */
|
|
12
|
+
export declare class RangeSet {
|
|
13
|
+
#private;
|
|
14
|
+
static readonly empty: RangeSet;
|
|
15
|
+
private constructor();
|
|
16
|
+
/** Builds a set from start, length pairs in any order; overlapping and touching pairs merge. */
|
|
17
|
+
static of(spans: Iterable<readonly [start: number, length: number]>): RangeSet;
|
|
18
|
+
/** Builds a set from byte addresses, as a tracked read set lists them. */
|
|
19
|
+
static fromAddresses(addresses: ArrayLike<number>): RangeSet;
|
|
20
|
+
/** Builds a set from a canonical flat run list, which a frame decoder has already checked. */
|
|
21
|
+
static fromRuns(runs: ArrayLike<number>): RangeSet;
|
|
22
|
+
/** The set as a flat run list, start then length, in ascending order. */
|
|
23
|
+
runs(): Uint32Array;
|
|
24
|
+
/** Number of ranges. */
|
|
25
|
+
get count(): number;
|
|
26
|
+
/** Total bytes covered. */
|
|
27
|
+
get length(): number;
|
|
28
|
+
/** Start, length pairs in ascending order. */
|
|
29
|
+
spans(): [number, number][];
|
|
30
|
+
/** True when every byte of `[start, start + length)` is in the set. */
|
|
31
|
+
contains(start: number, length?: number): boolean;
|
|
32
|
+
union(other: RangeSet): RangeSet;
|
|
33
|
+
/** Bytes of this set that `other` does not cover. */
|
|
34
|
+
subtract(other: RangeSet): RangeSet;
|
|
35
|
+
/** Merges ranges whose gap is at most `gap` bytes; the gap bytes join the set. */
|
|
36
|
+
coalesce(gap: number): RangeSet;
|
|
37
|
+
/** Widens every range to whole pages, never past `limit`. */
|
|
38
|
+
round(page: number, limit: number): RangeSet;
|
|
39
|
+
/** Drops bytes at or past `limit`. */
|
|
40
|
+
clip(limit: number): RangeSet;
|
|
41
|
+
}
|
|
42
|
+
/** Bytes of a tracked read set that fall outside what was shipped. */
|
|
43
|
+
export declare function escapes(reads: ArrayLike<number>, shipped: RangeSet): RangeSet;
|
|
44
|
+
/** Escapes of a decoded result, from its read runs when it has them and its addresses otherwise. */
|
|
45
|
+
export declare function resultEscapes(result: {
|
|
46
|
+
reads: ArrayLike<number>;
|
|
47
|
+
readRuns?: ArrayLike<number>;
|
|
48
|
+
}, shipped: RangeSet): RangeSet;
|
|
49
|
+
/** Learned footprints keyed by export and arguments, bounded, least recently used out first. */
|
|
50
|
+
export declare class FootprintCache {
|
|
51
|
+
#private;
|
|
52
|
+
constructor(maximumEntries?: number);
|
|
53
|
+
static key(name: string, args: readonly (number | bigint)[]): string;
|
|
54
|
+
get size(): number;
|
|
55
|
+
get(key: string): RangeSet | undefined;
|
|
56
|
+
/** Adds ranges to a key's footprint. */
|
|
57
|
+
learn(key: string, added: RangeSet): void;
|
|
58
|
+
clear(): void;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Builds a ranges frame: the wanted ranges are clipped to memory, coalesced until they fit the
|
|
62
|
+
* range limit, and copied out of `memory`. The payload ends with one pad byte so a lane that
|
|
63
|
+
* predates ranges rejects it as a snapshot instead of reading it as one. Returns the set that
|
|
64
|
+
* was shipped, which may be wider than the one asked for.
|
|
65
|
+
*/
|
|
66
|
+
export declare function encodeRangesFrame(header: object, memory: Uint8Array, wanted: RangeSet, maximum?: number): {
|
|
67
|
+
frame: Uint8Array;
|
|
68
|
+
shipped: RangeSet;
|
|
69
|
+
};
|
|
70
|
+
/** Checks a ranges header against its payload before any write and returns the shipped set. */
|
|
71
|
+
export declare function decodeRanges(ranges: RangesHeader | undefined, payload: Uint8Array): {
|
|
72
|
+
memoryBytes: number;
|
|
73
|
+
shipped: RangeSet;
|
|
74
|
+
};
|
|
75
|
+
/** Copies a validated ranges payload into `target`; bytes outside the ranges are untouched. */
|
|
76
|
+
export declare function restoreRanges(target: Uint8Array, payload: Uint8Array, shipped: RangeSet): void;
|
|
77
|
+
/** Zeroes every range of `set` in `target`. */
|
|
78
|
+
export declare function clearRanges(target: Uint8Array, set: RangeSet): void;
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { encodeFrame } from "../remote/frames.js";
|
|
2
|
+
/** Page size for rounding learned footprints. */
|
|
3
|
+
export const rangePage = 4096;
|
|
4
|
+
/** Most ranges one frame carries; the header is capped at 64 KiB. */
|
|
5
|
+
export const maximumRanges = 1024;
|
|
6
|
+
/** A sorted set of non-overlapping, non-touching byte ranges. Operations return new sets. */
|
|
7
|
+
export class RangeSet {
|
|
8
|
+
static empty = new RangeSet([]);
|
|
9
|
+
// flat start, end pairs; end is exclusive
|
|
10
|
+
#bounds;
|
|
11
|
+
constructor(bounds) {
|
|
12
|
+
this.#bounds = bounds;
|
|
13
|
+
}
|
|
14
|
+
/** Builds a set from start, length pairs in any order; overlapping and touching pairs merge. */
|
|
15
|
+
static of(spans) {
|
|
16
|
+
const pairs = [];
|
|
17
|
+
for (const [start, length] of spans) {
|
|
18
|
+
if (!Number.isSafeInteger(start) ||
|
|
19
|
+
!Number.isSafeInteger(length) ||
|
|
20
|
+
start < 0 ||
|
|
21
|
+
length < 0)
|
|
22
|
+
throw new RangeError('invalid range');
|
|
23
|
+
if (length)
|
|
24
|
+
pairs.push([start, start + length]);
|
|
25
|
+
}
|
|
26
|
+
pairs.sort((a, b) => a[0] - b[0]);
|
|
27
|
+
const bounds = [];
|
|
28
|
+
for (const [start, end] of pairs) {
|
|
29
|
+
if (bounds.length && start <= bounds[bounds.length - 1])
|
|
30
|
+
bounds[bounds.length - 1] = Math.max(bounds[bounds.length - 1], end);
|
|
31
|
+
else
|
|
32
|
+
bounds.push(start, end);
|
|
33
|
+
}
|
|
34
|
+
return new RangeSet(bounds);
|
|
35
|
+
}
|
|
36
|
+
/** Builds a set from byte addresses, as a tracked read set lists them. */
|
|
37
|
+
static fromAddresses(addresses) {
|
|
38
|
+
const sorted = Uint32Array.from(addresses).sort();
|
|
39
|
+
const bounds = [];
|
|
40
|
+
for (const address of sorted) {
|
|
41
|
+
if (bounds.length && address <= bounds[bounds.length - 1]) {
|
|
42
|
+
if (address === bounds[bounds.length - 1])
|
|
43
|
+
bounds[bounds.length - 1] = address + 1;
|
|
44
|
+
}
|
|
45
|
+
else
|
|
46
|
+
bounds.push(address, address + 1);
|
|
47
|
+
}
|
|
48
|
+
return new RangeSet(bounds);
|
|
49
|
+
}
|
|
50
|
+
/** Builds a set from a canonical flat run list, which a frame decoder has already checked. */
|
|
51
|
+
static fromRuns(runs) {
|
|
52
|
+
const bounds = new Array(runs.length);
|
|
53
|
+
for (let i = 0; i < runs.length; i += 2) {
|
|
54
|
+
bounds[i] = runs[i];
|
|
55
|
+
bounds[i + 1] = runs[i] + runs[i + 1];
|
|
56
|
+
}
|
|
57
|
+
return new RangeSet(bounds);
|
|
58
|
+
}
|
|
59
|
+
/** The set as a flat run list, start then length, in ascending order. */
|
|
60
|
+
runs() {
|
|
61
|
+
const out = new Uint32Array(this.#bounds.length);
|
|
62
|
+
for (let i = 0; i < out.length; i += 2) {
|
|
63
|
+
out[i] = this.#bounds[i];
|
|
64
|
+
out[i + 1] = this.#bounds[i + 1] - this.#bounds[i];
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
/** Number of ranges. */
|
|
69
|
+
get count() {
|
|
70
|
+
return this.#bounds.length / 2;
|
|
71
|
+
}
|
|
72
|
+
/** Total bytes covered. */
|
|
73
|
+
get length() {
|
|
74
|
+
let total = 0;
|
|
75
|
+
for (let i = 0; i < this.#bounds.length; i += 2)
|
|
76
|
+
total += this.#bounds[i + 1] - this.#bounds[i];
|
|
77
|
+
return total;
|
|
78
|
+
}
|
|
79
|
+
/** Start, length pairs in ascending order. */
|
|
80
|
+
spans() {
|
|
81
|
+
const out = [];
|
|
82
|
+
for (let i = 0; i < this.#bounds.length; i += 2)
|
|
83
|
+
out.push([this.#bounds[i], this.#bounds[i + 1] - this.#bounds[i]]);
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
/** True when every byte of `[start, start + length)` is in the set. */
|
|
87
|
+
contains(start, length = 1) {
|
|
88
|
+
if (length <= 0)
|
|
89
|
+
return true;
|
|
90
|
+
let low = 0, high = this.count - 1;
|
|
91
|
+
while (low <= high) {
|
|
92
|
+
const mid = (low + high) >> 1;
|
|
93
|
+
if (this.#bounds[mid * 2] > start)
|
|
94
|
+
high = mid - 1;
|
|
95
|
+
else if (this.#bounds[mid * 2 + 1] <= start)
|
|
96
|
+
low = mid + 1;
|
|
97
|
+
else
|
|
98
|
+
return start + length <= this.#bounds[mid * 2 + 1];
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
union(other) {
|
|
103
|
+
return RangeSet.of([...this.spans(), ...other.spans()]);
|
|
104
|
+
}
|
|
105
|
+
/** Bytes of this set that `other` does not cover. */
|
|
106
|
+
subtract(other) {
|
|
107
|
+
const bounds = [];
|
|
108
|
+
const cut = other.#bounds;
|
|
109
|
+
let j = 0;
|
|
110
|
+
for (let i = 0; i < this.#bounds.length; i += 2) {
|
|
111
|
+
let start = this.#bounds[i];
|
|
112
|
+
const end = this.#bounds[i + 1];
|
|
113
|
+
while (j < cut.length && cut[j + 1] <= start)
|
|
114
|
+
j += 2;
|
|
115
|
+
for (let k = j; k < cut.length && cut[k] < end; k += 2) {
|
|
116
|
+
if (cut[k] > start)
|
|
117
|
+
bounds.push(start, cut[k]);
|
|
118
|
+
start = Math.max(start, cut[k + 1]);
|
|
119
|
+
}
|
|
120
|
+
if (start < end)
|
|
121
|
+
bounds.push(start, end);
|
|
122
|
+
}
|
|
123
|
+
return new RangeSet(bounds);
|
|
124
|
+
}
|
|
125
|
+
/** Merges ranges whose gap is at most `gap` bytes; the gap bytes join the set. */
|
|
126
|
+
coalesce(gap) {
|
|
127
|
+
const bounds = [];
|
|
128
|
+
for (let i = 0; i < this.#bounds.length; i += 2) {
|
|
129
|
+
if (bounds.length && this.#bounds[i] - bounds[bounds.length - 1] <= gap)
|
|
130
|
+
bounds[bounds.length - 1] = this.#bounds[i + 1];
|
|
131
|
+
else
|
|
132
|
+
bounds.push(this.#bounds[i], this.#bounds[i + 1]);
|
|
133
|
+
}
|
|
134
|
+
return new RangeSet(bounds);
|
|
135
|
+
}
|
|
136
|
+
/** Widens every range to whole pages, never past `limit`. */
|
|
137
|
+
round(page, limit) {
|
|
138
|
+
const spans = [];
|
|
139
|
+
for (let i = 0; i < this.#bounds.length; i += 2) {
|
|
140
|
+
const start = Math.floor(this.#bounds[i] / page) * page;
|
|
141
|
+
const end = Math.min(Math.ceil(this.#bounds[i + 1] / page) * page, limit);
|
|
142
|
+
if (start < end)
|
|
143
|
+
spans.push([start, end - start]);
|
|
144
|
+
}
|
|
145
|
+
return RangeSet.of(spans);
|
|
146
|
+
}
|
|
147
|
+
/** Drops bytes at or past `limit`. */
|
|
148
|
+
clip(limit) {
|
|
149
|
+
return this.subtract(RangeSet.of([[limit, Number.MAX_SAFE_INTEGER - limit]]));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** Bytes of a tracked read set that fall outside what was shipped. */
|
|
153
|
+
export function escapes(reads, shipped) {
|
|
154
|
+
return RangeSet.fromAddresses(reads).subtract(shipped);
|
|
155
|
+
}
|
|
156
|
+
/** Escapes of a decoded result, from its read runs when it has them and its addresses otherwise. */
|
|
157
|
+
export function resultEscapes(result, shipped) {
|
|
158
|
+
return result.readRuns
|
|
159
|
+
? RangeSet.fromRuns(result.readRuns).subtract(shipped)
|
|
160
|
+
: escapes(result.reads, shipped);
|
|
161
|
+
}
|
|
162
|
+
/** Learned footprints keyed by export and arguments, bounded, least recently used out first. */
|
|
163
|
+
export class FootprintCache {
|
|
164
|
+
#entries = new Map();
|
|
165
|
+
#maximum;
|
|
166
|
+
constructor(maximumEntries = 1024) {
|
|
167
|
+
if (!Number.isSafeInteger(maximumEntries) || maximumEntries < 1)
|
|
168
|
+
throw new RangeError('invalid footprint cache size');
|
|
169
|
+
this.#maximum = maximumEntries;
|
|
170
|
+
}
|
|
171
|
+
static key(name, args) {
|
|
172
|
+
return JSON.stringify([
|
|
173
|
+
name,
|
|
174
|
+
...args.map((value) => (typeof value === 'bigint' ? `${value}n` : value))
|
|
175
|
+
]);
|
|
176
|
+
}
|
|
177
|
+
get size() {
|
|
178
|
+
return this.#entries.size;
|
|
179
|
+
}
|
|
180
|
+
get(key) {
|
|
181
|
+
const found = this.#entries.get(key);
|
|
182
|
+
if (found) {
|
|
183
|
+
this.#entries.delete(key);
|
|
184
|
+
this.#entries.set(key, found);
|
|
185
|
+
}
|
|
186
|
+
return found;
|
|
187
|
+
}
|
|
188
|
+
/** Adds ranges to a key's footprint. */
|
|
189
|
+
learn(key, added) {
|
|
190
|
+
const merged = (this.#entries.get(key) ?? RangeSet.empty).union(added);
|
|
191
|
+
this.#entries.delete(key);
|
|
192
|
+
this.#entries.set(key, merged);
|
|
193
|
+
if (this.#entries.size > this.#maximum)
|
|
194
|
+
this.#entries.delete(this.#entries.keys().next().value);
|
|
195
|
+
}
|
|
196
|
+
clear() {
|
|
197
|
+
this.#entries.clear();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Builds a ranges frame: the wanted ranges are clipped to memory, coalesced until they fit the
|
|
202
|
+
* range limit, and copied out of `memory`. The payload ends with one pad byte so a lane that
|
|
203
|
+
* predates ranges rejects it as a snapshot instead of reading it as one. Returns the set that
|
|
204
|
+
* was shipped, which may be wider than the one asked for.
|
|
205
|
+
*/
|
|
206
|
+
export function encodeRangesFrame(header, memory, wanted, maximum) {
|
|
207
|
+
if (memory.length % 65536)
|
|
208
|
+
throw new RangeError('invalid memory page size');
|
|
209
|
+
let shipped = wanted.clip(memory.length);
|
|
210
|
+
for (let gap = rangePage; shipped.count > maximumRanges; gap *= 2)
|
|
211
|
+
shipped = shipped.coalesce(gap);
|
|
212
|
+
const spans = shipped.spans();
|
|
213
|
+
const payload = new Uint8Array(shipped.length + 1);
|
|
214
|
+
let offset = 0;
|
|
215
|
+
for (const [start, length] of spans) {
|
|
216
|
+
payload.set(memory.subarray(start, start + length), offset);
|
|
217
|
+
offset += length;
|
|
218
|
+
}
|
|
219
|
+
const ranges = { memoryBytes: memory.length, spans: spans.flat() };
|
|
220
|
+
return { frame: encodeFrame({ ...header, ranges }, payload, maximum), shipped };
|
|
221
|
+
}
|
|
222
|
+
/** Checks a ranges header against its payload before any write and returns the shipped set. */
|
|
223
|
+
export function decodeRanges(ranges, payload) {
|
|
224
|
+
if (!ranges ||
|
|
225
|
+
!Number.isSafeInteger(ranges.memoryBytes) ||
|
|
226
|
+
ranges.memoryBytes < 0 ||
|
|
227
|
+
ranges.memoryBytes % 65536 ||
|
|
228
|
+
!Array.isArray(ranges.spans) ||
|
|
229
|
+
ranges.spans.length % 2 ||
|
|
230
|
+
ranges.spans.length > maximumRanges * 2)
|
|
231
|
+
throw new TypeError('invalid ranges');
|
|
232
|
+
let previous = 0, total = 0;
|
|
233
|
+
for (let i = 0; i < ranges.spans.length; i += 2) {
|
|
234
|
+
const start = ranges.spans[i], length = ranges.spans[i + 1];
|
|
235
|
+
if (!Number.isSafeInteger(start) ||
|
|
236
|
+
!Number.isSafeInteger(length) ||
|
|
237
|
+
length < 1 ||
|
|
238
|
+
start < previous ||
|
|
239
|
+
start + length > ranges.memoryBytes)
|
|
240
|
+
throw new TypeError('invalid range');
|
|
241
|
+
previous = start + length;
|
|
242
|
+
total += length;
|
|
243
|
+
}
|
|
244
|
+
if (payload.length !== total + 1)
|
|
245
|
+
throw new TypeError('ranges payload length differs');
|
|
246
|
+
const pairs = [];
|
|
247
|
+
for (let i = 0; i < ranges.spans.length; i += 2)
|
|
248
|
+
pairs.push([ranges.spans[i], ranges.spans[i + 1]]);
|
|
249
|
+
return { memoryBytes: ranges.memoryBytes, shipped: RangeSet.of(pairs) };
|
|
250
|
+
}
|
|
251
|
+
/** Copies a validated ranges payload into `target`; bytes outside the ranges are untouched. */
|
|
252
|
+
export function restoreRanges(target, payload, shipped) {
|
|
253
|
+
let offset = 0;
|
|
254
|
+
for (const [start, length] of shipped.spans()) {
|
|
255
|
+
target.set(payload.subarray(offset, offset + length), start);
|
|
256
|
+
offset += length;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/** Zeroes every range of `set` in `target`. */
|
|
260
|
+
export function clearRanges(target, set) {
|
|
261
|
+
for (const [start, length] of set.spans())
|
|
262
|
+
target.fill(0, start, start + length);
|
|
263
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Words a scan visited and words it found non-zero, summed over the calls that were given it. */
|
|
2
|
+
export interface ReadScanStats {
|
|
3
|
+
words: number;
|
|
4
|
+
set: number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Reads a read bitmap into `[start, length]` runs of granules and zeroes the words it reads. With
|
|
8
|
+
* a summary only the blocks of 64 words whose byte is non-zero are read, and every summary byte is
|
|
9
|
+
* zeroed; the summary must be a superset of the non-zero blocks (see `lowerInline`). Without one
|
|
10
|
+
* every word is read. The summary length is a multiple of 4.
|
|
11
|
+
*/
|
|
12
|
+
export declare function scanReads(words: Uint32Array, summary?: Uint8Array, stats?: ReadScanStats): Int32Array;
|
|
13
|
+
/**
|
|
14
|
+
* Sets the summary bytes of the blocks that the address range `[low, low + bytes)` touches, in the
|
|
15
|
+
* summary of `mask + 1` bytes at `base`. A window the owner marks in the read bitmap itself is
|
|
16
|
+
* summarized the same way.
|
|
17
|
+
*/
|
|
18
|
+
export declare function markReadSummary(u8: Uint8Array, base: number, mask: number, low: number, bytes: number): void;
|