@gmitch215/folio 1.0.1 → 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.
Files changed (65) hide show
  1. package/README.md +398 -28
  2. package/dist/affine-state.d.ts +171 -0
  3. package/dist/affine-state.js +763 -0
  4. package/dist/affine.d.ts +160 -0
  5. package/dist/affine.js +571 -0
  6. package/dist/boundaries.d.ts +59 -0
  7. package/dist/boundaries.js +127 -0
  8. package/dist/certified.d.ts +66 -0
  9. package/dist/certified.js +192 -0
  10. package/dist/certify.d.ts +98 -0
  11. package/dist/certify.js +2628 -0
  12. package/dist/context.d.ts +16 -2
  13. package/dist/context.js +60 -14
  14. package/dist/coordinator.js +50 -3
  15. package/dist/extract.d.ts +19 -0
  16. package/dist/extract.js +133 -5
  17. package/dist/fold.d.ts +32 -0
  18. package/dist/fold.js +105 -0
  19. package/dist/frames.d.ts +5 -1
  20. package/dist/frames.js +156 -9
  21. package/dist/inline.d.ts +114 -0
  22. package/dist/inline.js +276 -13
  23. package/dist/iteration-admission.d.ts +116 -7
  24. package/dist/iteration-admission.js +137 -8
  25. package/dist/iteration-layout.d.ts +60 -0
  26. package/dist/iteration-layout.js +54 -3
  27. package/dist/iteration-owner.d.ts +14 -1
  28. package/dist/iteration-owner.js +2 -2
  29. package/dist/iteration-worker.js +124 -58
  30. package/dist/iteration.d.ts +179 -13
  31. package/dist/iteration.js +845 -70
  32. package/dist/lease.d.ts +45 -0
  33. package/dist/lease.js +274 -0
  34. package/dist/managed.d.ts +72 -0
  35. package/dist/managed.js +425 -27
  36. package/dist/memo.d.ts +99 -0
  37. package/dist/memo.js +250 -0
  38. package/dist/node.d.ts +43 -1
  39. package/dist/node.js +87 -11
  40. package/dist/ownership-redirect.d.ts +30 -0
  41. package/dist/ownership-redirect.js +179 -0
  42. package/dist/ownership-slices.d.ts +44 -0
  43. package/dist/ownership-slices.js +72 -0
  44. package/dist/ownership.d.ts +161 -0
  45. package/dist/ownership.js +6568 -0
  46. package/dist/ranges.d.ts +78 -0
  47. package/dist/ranges.js +263 -0
  48. package/dist/read-scan.d.ts +18 -0
  49. package/dist/read-scan.js +94 -0
  50. package/dist/read-windows.d.ts +103 -0
  51. package/dist/read-windows.js +244 -0
  52. package/dist/router-worker.d.ts +1 -0
  53. package/dist/router-worker.js +4 -0
  54. package/dist/router.d.ts +203 -0
  55. package/dist/router.js +667 -0
  56. package/dist/runs.d.ts +44 -0
  57. package/dist/runs.js +182 -0
  58. package/dist/slices-worker.d.ts +1 -0
  59. package/dist/slices-worker.js +38 -0
  60. package/dist/slices.d.ts +126 -0
  61. package/dist/slices.js +441 -0
  62. package/dist/tasks.d.ts +14 -4
  63. package/dist/tasks.js +12 -11
  64. package/dist/transaction.d.ts +10 -0
  65. package/package.json +40 -4
@@ -1,14 +1,51 @@
1
1
  import type { IterationRoute } from './extract.ts';
2
- import type { IterationAdmission, IterationStop } from './iteration-admission.ts';
3
- import { type IterationArena, type IterationArenaOptions } from './iteration-layout.ts';
4
- export { iterationArena, iterationArenaBytes, iterationArguments, iterationEntryBytes, iterationFit, iterationFrameShadow, iterationGranules } from './iteration-layout.ts';
5
- export type { IterationArena, IterationArenaOptions } from './iteration-layout.ts';
2
+ import { type IterationAdmission, type IterationStop } from './iteration-admission.ts';
3
+ import { type IterationArena, type IterationArenaOptions, type IterationWindow } from './iteration-layout.ts';
4
+ import { type IterationReadWindows, type ReadWindowCounts } from './read-windows.ts';
5
+ export { iterationArena, iterationArenaBytes, iterationArguments, iterationEntryBytes, iterationFit, iterationFrameShadow, iterationGranules, iterationLogCeiling, iterationWindowBytes, iterationWindowCopy } from './iteration-layout.ts';
6
+ export type { IterationArena, IterationArenaOptions, IterationWindow } from './iteration-layout.ts';
6
7
  /** Pool construction options. */
7
8
  export interface IterationPoolOptions extends IterationArenaOptions {
8
9
  /** Start of the arena, 64-byte aligned, in memory the caller keeps free while the pool is open. */
9
10
  arenaBase: number;
10
11
  /** Workers; defaults to up to four. */
11
12
  width?: number;
13
+ /**
14
+ * Undo entries per iteration of the log a batch is retried with after its log is exhausted.
15
+ * Defaults to `logCapacity`, which leaves the retry off; `iterationLogCeiling` is the grown size the tests use.
16
+ */
17
+ logCeiling?: number;
18
+ /**
19
+ * Learn read windows from the first batch that completes without a conflict: a load that falls
20
+ * in a window sets no mark and skips the slot and frame checks, because the worker marked the
21
+ * whole window before the iteration. The next full batch runs on a lowering that counts its
22
+ * loads, and the windows stay armed for the rest of the call only if the counts say they pay
23
+ * (`readWindowsPay`). Off by default.
24
+ */
25
+ readWindows?: boolean;
26
+ /**
27
+ * Keep a summary byte per 64 words of every read bitmap, set by each read mark, so a worker scans
28
+ * only the blocks a read touched. The scan result is the same as the full scan. Off by default.
29
+ */
30
+ readSummary?: boolean;
31
+ /**
32
+ * Guard a batch against an iteration that never returns. The worker module polls a stop word at
33
+ * every loop start. When the owner has waited past the limit it sets the word: every iteration
34
+ * in flight traps, is rolled back from the undo logs and rerun serially in order, and the loop
35
+ * ends with `stopped: 'stalled'`. Off by default; `true` takes the defaults of
36
+ * `IterationStallOptions`.
37
+ */
38
+ stall?: boolean | IterationStallOptions;
39
+ }
40
+ /** The wait before the stall guard stops a batch: `floorMs` plus `factor` times its serial estimate. */
41
+ export interface IterationStallOptions {
42
+ /**
43
+ * Times the serial time of the iterations one worker runs in a batch. Default 50, above the
44
+ * slowdown of tracked code over plain code by an order of magnitude.
45
+ */
46
+ factor?: number;
47
+ /** Least wait in ms, which also covers waking the workers and compiling the first iteration. Default 2000. */
48
+ floorMs?: number;
12
49
  }
13
50
  /** One owner call's loop. */
14
51
  export interface IterationJob {
@@ -24,6 +61,13 @@ export interface IterationJob {
24
61
  schedule?: 'forward' | 'reverse';
25
62
  /** Test hook, unsound: validation checks to leave out. */
26
63
  skip?: readonly ('raw' | 'war' | 'waw' | 'frame')[];
64
+ /**
65
+ * A window an earlier call learned: the batches start on slot-private copies of it. Ignored when
66
+ * the arena reserves no window bytes or the window does not fit this call (see `report.window`).
67
+ */
68
+ window?: IterationWindow;
69
+ /** Whether a batch that conflicts may name a window and go on with it. Default true. */
70
+ discover?: boolean;
27
71
  /**
28
72
  * Runs iteration 0 through `replay` on the owner thread first and times it. That is the serial
29
73
  * program's first iteration, so its writes stay and the batches cover iterations 1 to `trip - 1`
@@ -40,6 +84,28 @@ export interface IterationJob {
40
84
  loses?: (batchWallMs: number, batchSerialMs: number) => boolean;
41
85
  /** Like `loses`, for a batch slow enough that a second batch is not needed to confirm it. */
42
86
  collapses?: (batchWallMs: number, batchSerialMs: number) => boolean;
87
+ /**
88
+ * Called after each batch with the best speedup its workers could reach at the tracked time
89
+ * they showed. True ends the dispatch: no later batch could pay at this tracking cost. Only
90
+ * used with `admit`.
91
+ */
92
+ hopeless?: (bound: number) => boolean;
93
+ /**
94
+ * Called with the bound of the first batch when read windows were learned from it. True makes
95
+ * the first batch on them one iteration per worker, which `hopeless` and the projection of the
96
+ * next batch then judge. Only used with `admit` and `hopeless`.
97
+ */
98
+ unproven?: (bound: number) => boolean;
99
+ /**
100
+ * Whether `remaining` more iterations at `perIterationMs` of serial time each are worth
101
+ * dispatching. Asked with the online estimate before each batch after the first, where false ends
102
+ * the dispatch, and after the timed serial blocks of a refused loop, where true starts it. Only
103
+ * used with `admit`.
104
+ */
105
+ reconsider?: (perIterationMs: number, remaining: number, last?: {
106
+ wallMs: number;
107
+ count: number;
108
+ }) => boolean;
43
109
  }
44
110
  /** What a run did. `regions`, `replayed`, `fallbacks` and `workers` parallel `ExecutionReport`. */
45
111
  export interface IterationReport {
@@ -57,9 +123,21 @@ export interface IterationReport {
57
123
  traps: number;
58
124
  /** Iterations that exhausted their undo log. */
59
125
  exhausted: number;
126
+ /** Iterations that trapped on an access to another iteration's stack slot. */
127
+ violations: number;
128
+ /** Batches in which the stall guard fired. */
129
+ stalls: number;
130
+ /** Iterations the stall word stopped, rolled back and rerun serially. */
131
+ stalledIterations: number;
60
132
  batches: number;
61
133
  failedBatches: number;
62
134
  undoEntries: number;
135
+ /** The most undo entries one completed iteration logged. */
136
+ maxLogEntries: number;
137
+ /** Times a batch that exhausted its log was retried with the grown log. */
138
+ logGrowths: number;
139
+ /** Iterations rolled back and dispatched again by such a retry. They are not in `regions`. */
140
+ retried: number;
63
141
  /** Ordered iteration pairs by hazard. */
64
142
  conflicts: {
65
143
  raw: number;
@@ -76,6 +154,12 @@ export interface IterationReport {
76
154
  workerRunMs: number;
77
155
  /** Bitmap scan and clear time inside workers, summed. */
78
156
  workerScanMs: number;
157
+ /** Time the workers spent marking read windows before their iterations, summed. */
158
+ workerMarkMs: number;
159
+ /** Read bitmap words the workers scanned, summed. */
160
+ workerScanWords: number;
161
+ /** Read bitmap words they found non-zero. */
162
+ workerScanSet: number;
79
163
  /**
80
164
  * Iterations the owner ran as the serial program without dispatching them: iteration 0, the
81
165
  * rest of a loop after a stop, or all of them when admission refused the loop.
@@ -85,13 +169,44 @@ export interface IterationReport {
85
169
  stopped?: IterationStop;
86
170
  /** What the admission callback saw and chose; set when the job had one. */
87
171
  admission?: IterationAdmission;
172
+ /** Frame conflicts that fell in a window's bytes; they are also in `conflicts.frame`. */
173
+ windowConflicts: number;
174
+ /** The window the batches ran on, if any, and where it came from. */
175
+ window?: {
176
+ low: number;
177
+ end: number;
178
+ source: 'given' | 'discovered';
179
+ /** Batches dispatched on it. */
180
+ batches: number;
181
+ };
182
+ /** Why a window that was offered or found was not used. */
183
+ windowRefused?: string;
184
+ /** The read windows learned from the first clean batch, with the batches that ran on them. */
185
+ readWindows?: IterationReadWindows & {
186
+ batches: number;
187
+ learnedFrom: number;
188
+ };
189
+ /** Why read windows were not learned, or were dropped after a batch on them conflicted. */
190
+ readWindowRefused?: string;
191
+ /** What the counted batch on the read windows saw, and whether they were armed for the loop. */
192
+ readWindowGate?: {
193
+ iterations: number;
194
+ counts: ReadWindowCounts;
195
+ saved: number;
196
+ spent: number;
197
+ /** which check the armed module runs first for a load of at most 8 bytes */
198
+ order: 'window' | 'exempt';
199
+ armed: boolean;
200
+ };
88
201
  }
89
202
  /** What a worker returns for one iteration. */
90
203
  export interface IterationOutcome {
91
204
  index: number;
92
- /** 0 ran, 1 trapped, 2 undo log exhausted */
205
+ /** 0 ran, 1 trapped, 2 undo log exhausted, 3 stopped by the stall word */
93
206
  status: number;
94
207
  entries: number;
208
+ /** wall time of the iteration inside the worker, on the tracked module */
209
+ runMs: number;
95
210
  /** sorted write granules (32 bytes) from the undo log */
96
211
  written: Int32Array;
97
212
  /** [start, length] runs of read granules (64 bytes) */
@@ -100,15 +215,12 @@ export interface IterationOutcome {
100
215
  frameWritten: Uint32Array;
101
216
  /** bit per frame byte the iteration read before writing it, same layout; empty when none */
102
217
  frameExposed: Uint32Array;
218
+ /** position in the batch of the first slot this iteration reached outside its own, or -1 */
219
+ touched?: number;
220
+ /** loads of at most 8 bytes by where they landed, from the counting module only */
221
+ counts?: ReadWindowCounts;
103
222
  }
104
- /**
105
- * Index of the first outcome that cannot keep its writes, or -1. Writes land in place, so every
106
- * ordered pair is checked three ways: RAW marks the reader, WAR the earlier reader, WAW the earlier
107
- * writer. A fourth check covers the frame, which each iteration owns as a private copy: an
108
- * iteration fails when it read a frame byte before writing it that an earlier iteration wrote, and
109
- * counts once. Works on the returned lists, never on dense bitmaps.
110
- */
111
- export declare function iterationVerdict(outcomes: readonly (Pick<IterationOutcome, 'status' | 'written' | 'reads'> & Partial<Pick<IterationOutcome, 'frameWritten' | 'frameExposed'>>)[], skip?: readonly ('raw' | 'war' | 'waw' | 'frame')[]): {
223
+ type Verdict = {
112
224
  first: number;
113
225
  firstFailure: number;
114
226
  frameFirst: number;
@@ -118,6 +230,52 @@ export declare function iterationVerdict(outcomes: readonly (Pick<IterationOutco
118
230
  waw: number;
119
231
  frame: number;
120
232
  };
233
+ /** Frame conflicts that fall in the first `windowWords` words; set only when that was asked for. */
234
+ window?: number;
235
+ };
236
+ type VerdictInput = Pick<IterationOutcome, 'status' | 'written' | 'reads'> & Partial<Pick<IterationOutcome, 'frameWritten' | 'frameExposed' | 'touched'>>;
237
+ type Skip = readonly ('raw' | 'war' | 'waw' | 'frame')[];
238
+ /** Ordered pairs by hazard and the first iteration that cannot keep its writes, or -1. */
239
+ interface MemoryConflicts {
240
+ raw: number;
241
+ war: number;
242
+ waw: number;
243
+ first: number;
244
+ }
245
+ /** The pairwise form: every ordered pair against every other. Any batch size; the reference. @internal */
246
+ export declare function pairwiseConflicts(outcomes: readonly VerdictInput[], skip: Skip): MemoryConflicts;
247
+ /**
248
+ * `pairwiseConflicts` in time linear in the sizes of the sets, for at most 32 outcomes: one table
249
+ * from write granule to the bit set of the iterations that wrote it, then each read granule looks
250
+ * its two write granules up. A pair counts once however many granules it shares, so the counts
251
+ * equal the pairwise form's. @internal
252
+ */
253
+ export declare function tabledConflicts(outcomes: readonly VerdictInput[], skip: Skip): MemoryConflicts;
254
+ /**
255
+ * Index of the first outcome that cannot keep its writes, or -1. Writes land in place, so every
256
+ * ordered pair is checked three ways: RAW marks the reader, WAR the earlier reader, WAW the earlier
257
+ * writer. A fourth check covers the frame, which each iteration owns as a private copy: an
258
+ * iteration fails when it read a frame byte before writing it that an earlier iteration wrote, and
259
+ * counts once. Works on the returned lists, never on dense bitmaps. A batch of at most 32 goes
260
+ * through `tabledConflicts`, a larger one through the pairwise form.
261
+ *
262
+ * `windowWords` is the number of leading frame words that belong to slot-private windows; the
263
+ * result then also counts, as `window`, the frame conflicts that fall in them.
264
+ */
265
+ export declare function iterationVerdict(outcomes: readonly VerdictInput[], skip?: Skip, windowWords?: number): Verdict;
266
+ /**
267
+ * The window a failed batch suggests: the span of the write granules that at least half of its
268
+ * iterations wrote, rounded out to 64 bytes. That is where iterations share a location they write
269
+ * every time, a scratch buffer, a context or an allocator's cursor, and `cap` bytes is the most a
270
+ * pool holds. Half and not two, because iterations that race on a shared context also write wild
271
+ * granules (a length read mid-update indexes anywhere), and those are rarely written twice. Whether
272
+ * the iterations write the window before they read it is not asked here: a window that is wrong
273
+ * only fails validation on the next batch.
274
+ */
275
+ export declare function discoverWindow(outcomes: readonly Pick<IterationOutcome, 'written'>[], cap: number): {
276
+ window?: IterationWindow;
277
+ reason?: string;
278
+ granules: number;
121
279
  };
122
280
  /**
123
281
  * Runs the loop of one owner call on worker threads. Each iteration gets a private stack slot and
@@ -139,6 +297,12 @@ export declare function iterationVerdict(outcomes: readonly (Pick<IterationOutco
139
297
  * them) is not tracked, and imported host functions that touch memory are not seen. The shadow
140
298
  * is byte-exact for scalar accesses and ranges; a conservative read would only add reports.
141
299
  *
300
+ * The callee stack of an iteration, the part of its slot below the frame copy, sets no marks and
301
+ * logs no undo entries, and a rollback does not restore it. An access that reaches another slot
302
+ * of the arena traps; the iteration fails with the owner of that slot, so both and every later one
303
+ * replay (`violations` counts them). A program that reads callee stack before writing it sees
304
+ * what an earlier iteration in that slot left, a rolled back one included.
305
+ *
142
306
  * With `admit`, iteration 0 runs first on the owner and the batches cover the rest. Dispatch
143
307
  * stops after a batch that exhausted an undo log, had a frame conflict, replayed more than half of
144
308
  * its iterations, or (with `loses` and `collapses`) two batches in a row did not beat the serial
@@ -151,6 +315,8 @@ export declare class IterationPool {
151
315
  readonly arena: IterationArena;
152
316
  readonly width: number;
153
317
  private constructor();
318
+ /** Bytes of memory the pool expects: the arena's memory plus any pages its grown log added. */
319
+ get memoryBytes(): number;
154
320
  /** The report of the last run, also after a run that threw. */
155
321
  get report(): IterationReport;
156
322
  /** Lowers the route's worker module inline and starts `width` workers over `memory`. */