@gmitch215/folio 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +455 -26
- package/dist/affine-state.d.ts +171 -0
- package/dist/affine-state.js +763 -0
- package/dist/affine.d.ts +160 -0
- package/dist/affine.js +571 -0
- package/dist/bake.d.ts +8 -3
- package/dist/bake.js +5 -2
- package/dist/boundaries.d.ts +59 -0
- package/dist/boundaries.js +127 -0
- package/dist/certified.d.ts +66 -0
- package/dist/certified.js +192 -0
- package/dist/certify.d.ts +98 -0
- package/dist/certify.js +2628 -0
- package/dist/compiler.d.ts +37 -1
- package/dist/compiler.js +116 -1
- package/dist/context.d.ts +47 -0
- package/dist/context.js +139 -0
- package/dist/coordinator.d.ts +1 -1
- package/dist/coordinator.js +54 -5
- package/dist/extract.d.ts +114 -1
- package/dist/extract.js +1011 -242
- package/dist/fold.d.ts +32 -0
- package/dist/fold.js +105 -0
- package/dist/frames.d.ts +20 -2
- package/dist/frames.js +205 -10
- package/dist/inline.d.ts +182 -0
- package/dist/inline.js +766 -0
- package/dist/iteration-admission.d.ts +185 -0
- package/dist/iteration-admission.js +201 -0
- package/dist/iteration-layout.d.ts +135 -0
- package/dist/iteration-layout.js +169 -0
- package/dist/iteration-owner.d.ts +45 -0
- package/dist/iteration-owner.js +223 -0
- package/dist/iteration-worker.d.ts +1 -0
- package/dist/iteration-worker.js +216 -0
- package/dist/iteration.d.ts +329 -0
- package/dist/iteration.js +1210 -0
- package/dist/lease.d.ts +45 -0
- package/dist/lease.js +274 -0
- package/dist/managed.d.ts +78 -0
- package/dist/managed.js +448 -27
- package/dist/memo.d.ts +99 -0
- package/dist/memo.js +250 -0
- package/dist/node.d.ts +61 -5
- package/dist/node.js +421 -23
- package/dist/ownership-redirect.d.ts +30 -0
- package/dist/ownership-redirect.js +179 -0
- package/dist/ownership-slices.d.ts +44 -0
- package/dist/ownership-slices.js +72 -0
- package/dist/ownership.d.ts +161 -0
- package/dist/ownership.js +6568 -0
- package/dist/ranges.d.ts +78 -0
- package/dist/ranges.js +263 -0
- package/dist/read-scan.d.ts +18 -0
- package/dist/read-scan.js +94 -0
- package/dist/read-windows.d.ts +103 -0
- package/dist/read-windows.js +244 -0
- package/dist/router-worker.d.ts +1 -0
- package/dist/router-worker.js +4 -0
- package/dist/router.d.ts +203 -0
- package/dist/router.js +667 -0
- package/dist/runs.d.ts +44 -0
- package/dist/runs.js +182 -0
- package/dist/slices-worker.d.ts +1 -0
- package/dist/slices-worker.js +38 -0
- package/dist/slices.d.ts +126 -0
- package/dist/slices.js +441 -0
- package/dist/tasks.d.ts +14 -4
- package/dist/tasks.js +23 -9
- package/dist/transaction.d.ts +10 -0
- package/package.json +44 -4
package/dist/ranges.d.ts
ADDED
|
@@ -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;
|
package/dist/ranges.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { encodeFrame } from "./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;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { iterationSummaryShift, iterationSummaryWords } from "./iteration-layout.js";
|
|
2
|
+
// one scan at a time per thread; the run being built lives here so the word loop stays flat
|
|
3
|
+
let start = -1;
|
|
4
|
+
let at = 0;
|
|
5
|
+
let runs = [];
|
|
6
|
+
let visited = 0;
|
|
7
|
+
let found = 0;
|
|
8
|
+
function visit(words, from, to) {
|
|
9
|
+
if (start >= 0 && at !== from) {
|
|
10
|
+
runs.push(start, at * 32 - start);
|
|
11
|
+
start = -1;
|
|
12
|
+
}
|
|
13
|
+
visited += to - from;
|
|
14
|
+
for (let k = from; k < to; k++) {
|
|
15
|
+
const w = words[k];
|
|
16
|
+
if (w === 0) {
|
|
17
|
+
if (start >= 0) {
|
|
18
|
+
runs.push(start, k * 32 - start);
|
|
19
|
+
start = -1;
|
|
20
|
+
}
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
words[k] = 0;
|
|
24
|
+
found++;
|
|
25
|
+
if (w === 0xffffffff) {
|
|
26
|
+
if (start < 0)
|
|
27
|
+
start = k * 32;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
for (let b = 0; b < 32; b++) {
|
|
31
|
+
if ((w >>> b) & 1) {
|
|
32
|
+
if (start < 0)
|
|
33
|
+
start = k * 32 + b;
|
|
34
|
+
}
|
|
35
|
+
else if (start >= 0) {
|
|
36
|
+
runs.push(start, k * 32 + b - start);
|
|
37
|
+
start = -1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
at = to;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Reads a read bitmap into `[start, length]` runs of granules and zeroes the words it reads. With
|
|
45
|
+
* a summary only the blocks of 64 words whose byte is non-zero are read, and every summary byte is
|
|
46
|
+
* zeroed; the summary must be a superset of the non-zero blocks (see `lowerInline`). Without one
|
|
47
|
+
* every word is read. The summary length is a multiple of 4.
|
|
48
|
+
*/
|
|
49
|
+
export function scanReads(words, summary, stats) {
|
|
50
|
+
start = -1;
|
|
51
|
+
at = 0;
|
|
52
|
+
runs = [];
|
|
53
|
+
visited = 0;
|
|
54
|
+
found = 0;
|
|
55
|
+
if (!summary)
|
|
56
|
+
visit(words, 0, words.length);
|
|
57
|
+
else {
|
|
58
|
+
const blocks = Math.ceil(words.length / iterationSummaryWords);
|
|
59
|
+
const wide = new Uint32Array(summary.buffer, summary.byteOffset, summary.length >> 2);
|
|
60
|
+
for (let i = 0; i < wide.length; i++) {
|
|
61
|
+
if (wide[i] === 0)
|
|
62
|
+
continue;
|
|
63
|
+
for (let b = 4 * i; b < 4 * i + 4; b++) {
|
|
64
|
+
if (!summary[b])
|
|
65
|
+
continue;
|
|
66
|
+
summary[b] = 0;
|
|
67
|
+
if (b < blocks)
|
|
68
|
+
visit(words, b * iterationSummaryWords, Math.min(words.length, (b + 1) * iterationSummaryWords));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (start >= 0)
|
|
73
|
+
runs.push(start, at * 32 - start);
|
|
74
|
+
if (stats) {
|
|
75
|
+
stats.words += visited;
|
|
76
|
+
stats.set += found;
|
|
77
|
+
}
|
|
78
|
+
const out = Int32Array.from(runs);
|
|
79
|
+
runs = [];
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Sets the summary bytes of the blocks that the address range `[low, low + bytes)` touches, in the
|
|
84
|
+
* summary of `mask + 1` bytes at `base`. A window the owner marks in the read bitmap itself is
|
|
85
|
+
* summarized the same way.
|
|
86
|
+
*/
|
|
87
|
+
export function markReadSummary(u8, base, mask, low, bytes) {
|
|
88
|
+
if (bytes <= 0)
|
|
89
|
+
return;
|
|
90
|
+
const first = Math.floor(low / 2 ** iterationSummaryShift);
|
|
91
|
+
const last = Math.floor((low + bytes - 1) / 2 ** iterationSummaryShift);
|
|
92
|
+
for (let b = first; b <= last; b++)
|
|
93
|
+
u8[base + (b & mask)] = 1;
|
|
94
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/** A byte range an iteration reads without per-access marks. */
|
|
2
|
+
export interface IterationReadWindow {
|
|
3
|
+
low: number;
|
|
4
|
+
bytes: number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The read windows of one loop. The strided window moves with the iteration index: iteration `k`
|
|
8
|
+
* reads from the granule boundary at or below `low + (k - index) * stride`. The fixed window is the same range in every
|
|
9
|
+
* iteration. Neither has to be right for a run to be correct: a load outside the window is tracked
|
|
10
|
+
* as usual, and a window only costs a false conflict when another iteration writes inside it.
|
|
11
|
+
*/
|
|
12
|
+
export interface IterationReadWindows {
|
|
13
|
+
strided?: {
|
|
14
|
+
index: number;
|
|
15
|
+
low: number;
|
|
16
|
+
stride: number;
|
|
17
|
+
bytes: number;
|
|
18
|
+
};
|
|
19
|
+
fixed?: IterationReadWindow;
|
|
20
|
+
}
|
|
21
|
+
/** What one iteration's outcome has to hold for learning. */
|
|
22
|
+
export interface ReadWindowSample {
|
|
23
|
+
index: number;
|
|
24
|
+
status: number;
|
|
25
|
+
/** sorted write granules (32 bytes) */
|
|
26
|
+
written: Int32Array;
|
|
27
|
+
/** [start, length] runs of read granules (64 bytes) */
|
|
28
|
+
reads: Int32Array;
|
|
29
|
+
}
|
|
30
|
+
/** Smallest window worth marking: four read granules for the strided one, two for the fixed one. */
|
|
31
|
+
export declare const readWindowMinimum: {
|
|
32
|
+
readonly strided: number;
|
|
33
|
+
readonly fixed: number;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Loads of at most 8 bytes of one run on the module with read windows, by where they landed:
|
|
37
|
+
* inside a window, tracked outside every window, or exempt (the slot's own callee stack).
|
|
38
|
+
*/
|
|
39
|
+
export interface ReadWindowCounts {
|
|
40
|
+
hits: number;
|
|
41
|
+
misses: number;
|
|
42
|
+
exempt: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Executed Wasm instructions of one load of at most 8 bytes, counted on the lowering with the slot
|
|
46
|
+
* checks and no frame shadow (one node per instruction). The windows go before the exempt check
|
|
47
|
+
* (`window` order) or after it (`exempt` order, the `exemptFirst` option). The frame shadow adds
|
|
48
|
+
* 10 to a tracked load and to a miss and nothing to a hit or an exempt load.
|
|
49
|
+
*/
|
|
50
|
+
export declare const readWindowInstructions: {
|
|
51
|
+
readonly tracked: 50;
|
|
52
|
+
readonly frame: 10;
|
|
53
|
+
readonly exempt: 15;
|
|
54
|
+
/** per window a load runs through without hitting it */
|
|
55
|
+
readonly guard: 6;
|
|
56
|
+
/** a hit in the first window, and in the second, by order */
|
|
57
|
+
readonly hit: {
|
|
58
|
+
readonly window: readonly [11, 17];
|
|
59
|
+
readonly exempt: readonly [21, 27];
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
/** What a run of the window module is expected to cost or save, in instructions. */
|
|
63
|
+
export interface ReadWindowVerdict {
|
|
64
|
+
/** Instructions the hits would not execute, against the lowering with no windows. */
|
|
65
|
+
saved: number;
|
|
66
|
+
/** Instructions the misses, the exempt loads and the marking of the windows add. */
|
|
67
|
+
spent: number;
|
|
68
|
+
/** The order of the two checks that costs less for these counts. */
|
|
69
|
+
order: 'window' | 'exempt';
|
|
70
|
+
pay: boolean;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Whether read windows pay for a loop, and in which order, from the counts of a batch on them. A
|
|
74
|
+
* hit replaces the mark and the slot checks of a tracked load; a miss executes the window guards on
|
|
75
|
+
* top of a tracked load; an exempt load executes them too when the windows come first; and the
|
|
76
|
+
* owner marks every window granule before each iteration. Windows are armed when the instructions
|
|
77
|
+
* saved exceed those spent by at least `margin` of the instructions the counted tracked loads
|
|
78
|
+
* cost. A window of the wrong size is still sound, so this decides cost only.
|
|
79
|
+
*/
|
|
80
|
+
export declare function readWindowsPay(counts: ReadWindowCounts, windows: IterationReadWindows, frame: boolean, iterations: number, margin?: number): ReadWindowVerdict;
|
|
81
|
+
/** Fraction of the counted tracked loads' instructions a window run must save to be armed. */
|
|
82
|
+
export declare const readWindowMargin = 0.05;
|
|
83
|
+
/** Windows of iteration `k` inside `[0, memoryBytes)`, at most two. */
|
|
84
|
+
export declare function readWindowsAt(windows: IterationReadWindows, k: number, memoryBytes: number): IterationReadWindow[];
|
|
85
|
+
/**
|
|
86
|
+
* Whether every window of iterations `first` to `first + count - 1` stays clear of the forbidden
|
|
87
|
+
* ranges (the arena, the grown logs), as `[low, end)` pairs. A load in a window skips the checks
|
|
88
|
+
* that would have found an access to those ranges.
|
|
89
|
+
*/
|
|
90
|
+
export declare function readWindowsClear(windows: IterationReadWindows, first: number, count: number, memoryBytes: number, forbidden: readonly (readonly [number, number])[]): boolean;
|
|
91
|
+
/** Sets the read bits of every granule a window touches in the bitmap that starts at `base`. */
|
|
92
|
+
export declare function markReadWindow(u8: Uint8Array, base: number, low: number, bytes: number): void;
|
|
93
|
+
/**
|
|
94
|
+
* Windows from the read and write sets of tracked iterations that completed without a conflict.
|
|
95
|
+
* The strided one follows the largest read run of each iteration when the runs line up on one
|
|
96
|
+
* stride; when their lengths differ by more than a granule the window is the shortest one; the fixed one is the largest run
|
|
97
|
+
* every iteration read that is not inside the strided one. A window that another iteration wrote
|
|
98
|
+
* into, in the sample, is dropped: its reads would conflict. Needs three completed iterations.
|
|
99
|
+
*/
|
|
100
|
+
export declare function learnReadWindows(samples: readonly ReadWindowSample[]): {
|
|
101
|
+
windows?: IterationReadWindows;
|
|
102
|
+
reason?: string;
|
|
103
|
+
};
|