@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.
Files changed (71) hide show
  1. package/README.md +455 -26
  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/bake.d.ts +8 -3
  7. package/dist/bake.js +5 -2
  8. package/dist/boundaries.d.ts +59 -0
  9. package/dist/boundaries.js +127 -0
  10. package/dist/certified.d.ts +66 -0
  11. package/dist/certified.js +192 -0
  12. package/dist/certify.d.ts +98 -0
  13. package/dist/certify.js +2628 -0
  14. package/dist/compiler.d.ts +37 -1
  15. package/dist/compiler.js +116 -1
  16. package/dist/context.d.ts +47 -0
  17. package/dist/context.js +139 -0
  18. package/dist/coordinator.d.ts +1 -1
  19. package/dist/coordinator.js +54 -5
  20. package/dist/extract.d.ts +114 -1
  21. package/dist/extract.js +1011 -242
  22. package/dist/fold.d.ts +32 -0
  23. package/dist/fold.js +105 -0
  24. package/dist/frames.d.ts +20 -2
  25. package/dist/frames.js +205 -10
  26. package/dist/inline.d.ts +182 -0
  27. package/dist/inline.js +766 -0
  28. package/dist/iteration-admission.d.ts +185 -0
  29. package/dist/iteration-admission.js +201 -0
  30. package/dist/iteration-layout.d.ts +135 -0
  31. package/dist/iteration-layout.js +169 -0
  32. package/dist/iteration-owner.d.ts +45 -0
  33. package/dist/iteration-owner.js +223 -0
  34. package/dist/iteration-worker.d.ts +1 -0
  35. package/dist/iteration-worker.js +216 -0
  36. package/dist/iteration.d.ts +329 -0
  37. package/dist/iteration.js +1210 -0
  38. package/dist/lease.d.ts +45 -0
  39. package/dist/lease.js +274 -0
  40. package/dist/managed.d.ts +78 -0
  41. package/dist/managed.js +448 -27
  42. package/dist/memo.d.ts +99 -0
  43. package/dist/memo.js +250 -0
  44. package/dist/node.d.ts +61 -5
  45. package/dist/node.js +421 -23
  46. package/dist/ownership-redirect.d.ts +30 -0
  47. package/dist/ownership-redirect.js +179 -0
  48. package/dist/ownership-slices.d.ts +44 -0
  49. package/dist/ownership-slices.js +72 -0
  50. package/dist/ownership.d.ts +161 -0
  51. package/dist/ownership.js +6568 -0
  52. package/dist/ranges.d.ts +78 -0
  53. package/dist/ranges.js +263 -0
  54. package/dist/read-scan.d.ts +18 -0
  55. package/dist/read-scan.js +94 -0
  56. package/dist/read-windows.d.ts +103 -0
  57. package/dist/read-windows.js +244 -0
  58. package/dist/router-worker.d.ts +1 -0
  59. package/dist/router-worker.js +4 -0
  60. package/dist/router.d.ts +203 -0
  61. package/dist/router.js +667 -0
  62. package/dist/runs.d.ts +44 -0
  63. package/dist/runs.js +182 -0
  64. package/dist/slices-worker.d.ts +1 -0
  65. package/dist/slices-worker.js +38 -0
  66. package/dist/slices.d.ts +126 -0
  67. package/dist/slices.js +441 -0
  68. package/dist/tasks.d.ts +14 -4
  69. package/dist/tasks.js +23 -9
  70. package/dist/transaction.d.ts +10 -0
  71. package/package.json +44 -4
@@ -0,0 +1,185 @@
1
+ /** Why the rest of a loop ran serially after the pooled path was tried or refused. */
2
+ export type IterationStop = 'no-admission' | 'log-exhausted' | 'frame-carried dependence' | 'dependency' | 'stalled';
3
+ /** What the admission decision saw and chose for one owner call's loop. */
4
+ export interface IterationAdmission {
5
+ /** Time of iteration 0 on the owner thread, on a plain instance and the real frame. */
6
+ plainIterationMs: number;
7
+ /** Serial time of the iterations after iteration 0, from `plainIterationMs`. */
8
+ remainingSerialMs: number;
9
+ /** The least remaining serial time worth dispatching: `fixedMs * margin`. */
10
+ floorMs: number;
11
+ /** The decision before the first dispatch. A later stop is `IterationReport.stopped`. */
12
+ admitted: boolean;
13
+ /** Wall of the last batch, dispatch to the end of its replays. */
14
+ batchWallMs?: number;
15
+ /** The serial time of the iterations of that batch, from the online estimate. */
16
+ batchSerialMs?: number;
17
+ /** The latest estimate of serial time per iteration, from iteration 0, replays or workers. */
18
+ estimateMs?: number;
19
+ /** Set when a refused loop was admitted after timed serial iterations; the iteration index. */
20
+ readmittedAt?: number;
21
+ /** Admitted as a probe: under the floor, over `probeFraction` of it, one timed batch decides. */
22
+ probe?: boolean;
23
+ /** The probe batch did not beat the serial time of its iterations, and the dispatch ended. */
24
+ probeLost?: boolean;
25
+ /** One entry per batch, only under `admissionSeam.trace`. */
26
+ trace?: AdmissionStep[];
27
+ }
28
+ /** What the estimate and the batch rule saw after one batch. @internal */
29
+ export interface AdmissionStep {
30
+ first: number;
31
+ count: number;
32
+ windows: boolean;
33
+ trackedMeanMs: number;
34
+ kappa?: number;
35
+ estimateMs: number;
36
+ wallMs: number;
37
+ serialMs: number;
38
+ }
39
+ /** The constants of the decision. A re-calibration changes these and nothing else. */
40
+ export interface IterationAdmissionConstants {
41
+ /** Wall of the pooled path that does not shrink with the loop: dispatch, join, validation. */
42
+ fixedMs: number;
43
+ /** The pooled path has to beat the serial one by this factor, before and after each batch. */
44
+ margin: number;
45
+ }
46
+ /**
47
+ * Fitted on a Mac sweep of `scripts/research/iteration-admission.ts` and checked on the rig at widths
48
+ * 8 and 4; a re-calibration updates only these two numbers.
49
+ *
50
+ * `fixedMs` 2 is the smallest value at which no cell of the Mac sweep (18 cells, 4 KiB to 4 MiB
51
+ * by 8 to 128 iterations, width 8) dispatched and lost. The pooled wall minus the batch time
52
+ * measured 0.1 to 2.9 ms there with the input copy left out of the timing; the prototype's rows
53
+ * bracket 3 to 8 ms but include that copy. The loop is not worth dispatching when the serial time
54
+ * of its remaining iterations is below `fixedMs * margin`.
55
+ *
56
+ * `margin` 1.25 is the speedup a batch has to show over the serial time of its iterations for
57
+ * the loop to keep dispatching. A batch that misses it twice in a row ends the dispatch, and so
58
+ * does one batch whose wall passes `batchCollapse` times that serial time.
59
+ */
60
+ export declare const iterationAdmissionConstants: IterationAdmissionConstants;
61
+ /**
62
+ * With the second chance on, a loop whose remaining serial time is under the floor but over this
63
+ * fraction of it is admitted as a probe: its first timed batch has to beat the serial time of its
64
+ * iterations by the margin, or the dispatch ends there. The floor was fitted on a build with the
65
+ * full read scan and no read windows, so it is the least certain number in the decision.
66
+ */
67
+ export declare const probeFraction = 0.5;
68
+ export interface AdmitInput {
69
+ plainIterationMs: number;
70
+ trip: number;
71
+ width: number;
72
+ constants?: IterationAdmissionConstants;
73
+ secondChance?: boolean;
74
+ }
75
+ /**
76
+ * Decides before the first dispatch from the measured serial time of iteration 0, which has
77
+ * already run. Admits when at least two iterations are left for at least two workers and their
78
+ * serial time exceeds `fixedMs * margin`. Whether dispatch pays is then checked per batch.
79
+ */
80
+ export declare function admitIteration({ plainIterationMs: t0, trip, width, constants, secondChance }: AdmitInput): IterationAdmission;
81
+ /** True when a batch did not beat the serial time of its iterations by the margin. */
82
+ export declare function batchLoses(batchWallMs: number, batchSerialMs: number, constants?: IterationAdmissionConstants): boolean;
83
+ /** Batch wall over serial time past which one batch is a verdict, with no second batch to confirm. */
84
+ export declare const batchCollapse = 1.5;
85
+ /** True when a batch took clearly longer than the serial time of its iterations. */
86
+ export declare function batchCollapses(batchWallMs: number, batchSerialMs: number): boolean;
87
+ /**
88
+ * Test and sweep seam, module state and not an option. `scale` multiplies the measured time of
89
+ * iteration 0 before the first decision and `batchScale` the wall of each batch before the batch
90
+ * rule, and divides the bound of a batch; `force` admits any loop with an iteration left after
91
+ * iteration 0 and never stops on the batch rule or the bound. The structural stops
92
+ * (`log-exhausted`, `frame-carried dependence`, `dependency`) are not affected. `trace` records
93
+ * one entry per batch in the admission report. `gate` false keeps read windows armed whatever the
94
+ * counted batch says, so a sweep can compare the two.
95
+ * @internal
96
+ */
97
+ export declare const admissionSeam: {
98
+ scale: number;
99
+ batchScale: number;
100
+ force: boolean;
101
+ trace: boolean;
102
+ gate: boolean;
103
+ estimator: "min" | "median" | "mean" | "q75";
104
+ };
105
+ /**
106
+ * The serial estimate the probe holds after a block: the lower of the last two block means, the
107
+ * median of every block mean so far, or the mean of every probed iteration.
108
+ * @internal
109
+ */
110
+ export declare function probeEstimate(kind: 'min' | 'median' | 'mean' | 'q75', last: number, per: number, means: number[], mean: number): number;
111
+ /** `admitIteration` through the seam; what the host function calls. */
112
+ export declare function decideAdmission(plainIterationMs: number, trip: number, width: number, secondChance?: boolean): IterationAdmission;
113
+ /** The serial time per iteration as far as the run knows it, and the scale from tracked time. */
114
+ export interface IterationEstimate {
115
+ /** Serial milliseconds per iteration. */
116
+ estimateMs: number;
117
+ /** Serial time over tracked time, fixed by the first batch; undefined before it. */
118
+ kappa?: number;
119
+ /** The same scale for batches that run on read windows; fixed by the first of them. */
120
+ kappaWindows?: number;
121
+ /** The serial time both scales are fitted to: the estimate before the first batch. */
122
+ referenceMs?: number;
123
+ }
124
+ /**
125
+ * Updates the estimate from the worker-side times of the iterations a batch completed, with no
126
+ * host call per iteration. The first batch fixes `kappa` as the serial estimate over the least
127
+ * tracked time, at most 1 because a tracked iteration is never faster than a plain one. The least
128
+ * time, not the median, because the iteration that the estimate came from may be the smallest of
129
+ * a batch that is mostly large, and the median would then read its size as the tracking cost. The
130
+ * estimate is then the mean tracked time times `kappa`: a batch of iterations dearer than the
131
+ * earlier estimate raises it, and a batch of cheaper ones lowers it. When the first iteration was
132
+ * larger than every iteration of the batch, `kappa` is capped and the estimate is the tracked
133
+ * mean, an upper bound on the serial time. Times of zero or less are ignored. A batch on read
134
+ * windows is cheaper to track than the first, so it has its own scale, fitted the same way to the
135
+ * same reference the first time one runs; the unwindowed scale would read its time as serial time.
136
+ */
137
+ export declare function refineEstimate(before: IterationEstimate, trackedMs: readonly number[], windows?: boolean): IterationEstimate;
138
+ /**
139
+ * Whether `remaining` more iterations at `perIterationMs` each are worth dispatching, through the
140
+ * seam. The pool asks it with the online estimate before each batch after the first and after the
141
+ * timed serial blocks of a refused loop.
142
+ *
143
+ * With `last`, the wall and size of the last timed batch, a loop under the floor still goes on
144
+ * when the batches it has seen say so: the pooled cost of what is left is the batch wall times the
145
+ * batches that cover it, and that times the margin has to be under their serial time. The floor
146
+ * prices the wake-up and the learning batch, which a loop that is running has already paid.
147
+ */
148
+ export declare function decideRemaining(perIterationMs: number, remaining: number, width: number, last?: {
149
+ wallMs: number;
150
+ count: number;
151
+ }): boolean;
152
+ /** `batchLoses` through the seam; what the host function passes the pool. */
153
+ export declare function decideBatch(batchWallMs: number, batchSerialMs: number): boolean;
154
+ /** `batchCollapses` through the seam; what the host function passes the pool. */
155
+ export declare function decideCollapse(batchWallMs: number, batchSerialMs: number): boolean;
156
+ /** Speedup below which a batch cannot pay even with free dispatch, validation and joins. */
157
+ export declare const boundFloor = 1;
158
+ /**
159
+ * The best speedup a batch could reach: the serial time of the iterations it completed over the
160
+ * time `width` workers need for them at the tracked time they showed. Wake, join and validation
161
+ * only add to the wall, so a bound under one means the loop cannot win at this tracking cost.
162
+ */
163
+ export declare function batchBound(perIterationMs: number, trackedMs: readonly number[], width: number): number;
164
+ /**
165
+ * A loop whose first batch bounds the speedup above this has tracking cost to spare: realized
166
+ * speedups ran at about half the bound (aes-ctr 1.2 of 2.3, lz4-compress 1.2 of 2.4,
167
+ * aes-cbc-decrypt 1.2 of 2.0 on the Mac sweep), so such a loop goes on to full batches on its
168
+ * read windows. A loop under it gets one short batch on them first.
169
+ */
170
+ export declare const proofBound = 2;
171
+ /** True when the bound of a batch is under `boundFloor`, through the seam; what the host passes. */
172
+ export declare function decideBound(bound: number): boolean;
173
+ /**
174
+ * The wall and the serial time of the next batch of `next` iterations, from a batch that ran. Its
175
+ * wall minus the time its workers needed for the iterations it completed is the cost that does
176
+ * not shrink with the batch: wake, join and validation. The next batch pays that plus the
177
+ * workers' share of its own iterations. Validation grows with the batch, so the wall comes out
178
+ * low and a projected loss is a loss.
179
+ */
180
+ export declare function projectBatch(wallMs: number, trackedMs: readonly number[], perIterationMs: number, next: number, width: number): {
181
+ wallMs: number;
182
+ serialMs: number;
183
+ } | undefined;
184
+ /** True when a bound is low enough that the first batch on read windows should be a short one. */
185
+ export declare function decideProof(bound: number): boolean;
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Fitted on a Mac sweep of `scripts/research/iteration-admission.ts` and checked on the rig at widths
3
+ * 8 and 4; a re-calibration updates only these two numbers.
4
+ *
5
+ * `fixedMs` 2 is the smallest value at which no cell of the Mac sweep (18 cells, 4 KiB to 4 MiB
6
+ * by 8 to 128 iterations, width 8) dispatched and lost. The pooled wall minus the batch time
7
+ * measured 0.1 to 2.9 ms there with the input copy left out of the timing; the prototype's rows
8
+ * bracket 3 to 8 ms but include that copy. The loop is not worth dispatching when the serial time
9
+ * of its remaining iterations is below `fixedMs * margin`.
10
+ *
11
+ * `margin` 1.25 is the speedup a batch has to show over the serial time of its iterations for
12
+ * the loop to keep dispatching. A batch that misses it twice in a row ends the dispatch, and so
13
+ * does one batch whose wall passes `batchCollapse` times that serial time.
14
+ */
15
+ export const iterationAdmissionConstants = {
16
+ fixedMs: 2,
17
+ margin: 1.25
18
+ };
19
+ /**
20
+ * With the second chance on, a loop whose remaining serial time is under the floor but over this
21
+ * fraction of it is admitted as a probe: its first timed batch has to beat the serial time of its
22
+ * iterations by the margin, or the dispatch ends there. The floor was fitted on a build with the
23
+ * full read scan and no read windows, so it is the least certain number in the decision.
24
+ */
25
+ export const probeFraction = 0.5;
26
+ /**
27
+ * Decides before the first dispatch from the measured serial time of iteration 0, which has
28
+ * already run. Admits when at least two iterations are left for at least two workers and their
29
+ * serial time exceeds `fixedMs * margin`. Whether dispatch pays is then checked per batch.
30
+ */
31
+ export function admitIteration({ plainIterationMs: t0, trip, width, constants = iterationAdmissionConstants, secondChance = false }) {
32
+ const remaining = trip - 1;
33
+ const remainingSerialMs = remaining * t0;
34
+ const floorMs = constants.fixedMs * constants.margin;
35
+ const able = Number.isFinite(t0) && remaining >= 2 && width >= 2;
36
+ const admitted = able && remainingSerialMs > floorMs;
37
+ if (secondChance && able && !admitted && remainingSerialMs > floorMs * probeFraction)
38
+ return { plainIterationMs: t0, remainingSerialMs, floorMs, admitted: true, probe: true };
39
+ return { plainIterationMs: t0, remainingSerialMs, floorMs, admitted };
40
+ }
41
+ /** True when a batch did not beat the serial time of its iterations by the margin. */
42
+ export function batchLoses(batchWallMs, batchSerialMs, constants = iterationAdmissionConstants) {
43
+ return batchWallMs * constants.margin > batchSerialMs;
44
+ }
45
+ /** Batch wall over serial time past which one batch is a verdict, with no second batch to confirm. */
46
+ export const batchCollapse = 1.5;
47
+ /** True when a batch took clearly longer than the serial time of its iterations. */
48
+ export function batchCollapses(batchWallMs, batchSerialMs) {
49
+ return batchWallMs > batchSerialMs * batchCollapse;
50
+ }
51
+ /**
52
+ * Test and sweep seam, module state and not an option. `scale` multiplies the measured time of
53
+ * iteration 0 before the first decision and `batchScale` the wall of each batch before the batch
54
+ * rule, and divides the bound of a batch; `force` admits any loop with an iteration left after
55
+ * iteration 0 and never stops on the batch rule or the bound. The structural stops
56
+ * (`log-exhausted`, `frame-carried dependence`, `dependency`) are not affected. `trace` records
57
+ * one entry per batch in the admission report. `gate` false keeps read windows armed whatever the
58
+ * counted batch says, so a sweep can compare the two.
59
+ * @internal
60
+ */
61
+ export const admissionSeam = {
62
+ scale: 1,
63
+ batchScale: 1,
64
+ force: false,
65
+ trace: false,
66
+ gate: true,
67
+ estimator: 'median'
68
+ };
69
+ /**
70
+ * The serial estimate the probe holds after a block: the lower of the last two block means, the
71
+ * median of every block mean so far, or the mean of every probed iteration.
72
+ * @internal
73
+ */
74
+ export function probeEstimate(kind, last, per, means, mean) {
75
+ if (kind === 'mean')
76
+ return mean;
77
+ if (kind === 'min')
78
+ return Math.min(last, per);
79
+ const sorted = [...means].sort((x, y) => x - y);
80
+ if (kind === 'q75')
81
+ return sorted[Math.min(sorted.length - 1, Math.ceil(0.75 * sorted.length) - 1)];
82
+ const mid = sorted.length >> 1;
83
+ return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
84
+ }
85
+ /** `admitIteration` through the seam; what the host function calls. */
86
+ export function decideAdmission(plainIterationMs, trip, width, secondChance = false) {
87
+ const decision = admitIteration({
88
+ plainIterationMs: plainIterationMs * admissionSeam.scale,
89
+ trip,
90
+ width,
91
+ secondChance
92
+ });
93
+ if (admissionSeam.force && trip >= 2 && width >= 2)
94
+ decision.admitted = true;
95
+ return decision;
96
+ }
97
+ /**
98
+ * Updates the estimate from the worker-side times of the iterations a batch completed, with no
99
+ * host call per iteration. The first batch fixes `kappa` as the serial estimate over the least
100
+ * tracked time, at most 1 because a tracked iteration is never faster than a plain one. The least
101
+ * time, not the median, because the iteration that the estimate came from may be the smallest of
102
+ * a batch that is mostly large, and the median would then read its size as the tracking cost. The
103
+ * estimate is then the mean tracked time times `kappa`: a batch of iterations dearer than the
104
+ * earlier estimate raises it, and a batch of cheaper ones lowers it. When the first iteration was
105
+ * larger than every iteration of the batch, `kappa` is capped and the estimate is the tracked
106
+ * mean, an upper bound on the serial time. Times of zero or less are ignored. A batch on read
107
+ * windows is cheaper to track than the first, so it has its own scale, fitted the same way to the
108
+ * same reference the first time one runs; the unwindowed scale would read its time as serial time.
109
+ */
110
+ export function refineEstimate(before, trackedMs, windows = false) {
111
+ const ran = trackedMs.filter((ms) => ms > 0);
112
+ if (!ran.length)
113
+ return before;
114
+ const referenceMs = before.referenceMs ?? before.estimateMs;
115
+ const learned = windows ? before.kappaWindows : before.kappa;
116
+ const kappa = learned ?? Math.min(1, referenceMs / Math.min(...ran));
117
+ return {
118
+ kappa: windows ? before.kappa : kappa,
119
+ kappaWindows: windows ? kappa : before.kappaWindows,
120
+ referenceMs,
121
+ estimateMs: (kappa * ran.reduce((sum, ms) => sum + ms, 0)) / ran.length
122
+ };
123
+ }
124
+ /**
125
+ * Whether `remaining` more iterations at `perIterationMs` each are worth dispatching, through the
126
+ * seam. The pool asks it with the online estimate before each batch after the first and after the
127
+ * timed serial blocks of a refused loop.
128
+ *
129
+ * With `last`, the wall and size of the last timed batch, a loop under the floor still goes on
130
+ * when the batches it has seen say so: the pooled cost of what is left is the batch wall times the
131
+ * batches that cover it, and that times the margin has to be under their serial time. The floor
132
+ * prices the wake-up and the learning batch, which a loop that is running has already paid.
133
+ */
134
+ export function decideRemaining(perIterationMs, remaining, width, last) {
135
+ if (decideAdmission(perIterationMs, remaining + 1, width).admitted)
136
+ return true;
137
+ if (!last || remaining < 2 || width < 2 || !(last.count > 0))
138
+ return false;
139
+ const pooled = Math.ceil(remaining / last.count) * last.wallMs * admissionSeam.batchScale;
140
+ return remaining * perIterationMs > pooled * iterationAdmissionConstants.margin;
141
+ }
142
+ /** `batchLoses` through the seam; what the host function passes the pool. */
143
+ export function decideBatch(batchWallMs, batchSerialMs) {
144
+ if (admissionSeam.force)
145
+ return false;
146
+ return batchLoses(batchWallMs * admissionSeam.batchScale, batchSerialMs);
147
+ }
148
+ /** `batchCollapses` through the seam; what the host function passes the pool. */
149
+ export function decideCollapse(batchWallMs, batchSerialMs) {
150
+ if (admissionSeam.force)
151
+ return false;
152
+ return batchCollapses(batchWallMs * admissionSeam.batchScale, batchSerialMs);
153
+ }
154
+ /** Speedup below which a batch cannot pay even with free dispatch, validation and joins. */
155
+ export const boundFloor = 1;
156
+ /**
157
+ * The best speedup a batch could reach: the serial time of the iterations it completed over the
158
+ * time `width` workers need for them at the tracked time they showed. Wake, join and validation
159
+ * only add to the wall, so a bound under one means the loop cannot win at this tracking cost.
160
+ */
161
+ export function batchBound(perIterationMs, trackedMs, width) {
162
+ const ran = trackedMs.filter((ms) => ms > 0);
163
+ if (!ran.length || !(perIterationMs > 0) || width < 1)
164
+ return Infinity;
165
+ const mean = ran.reduce((sum, ms) => sum + ms, 0) / ran.length;
166
+ return (ran.length * perIterationMs) / (Math.ceil(ran.length / width) * mean);
167
+ }
168
+ /**
169
+ * A loop whose first batch bounds the speedup above this has tracking cost to spare: realized
170
+ * speedups ran at about half the bound (aes-ctr 1.2 of 2.3, lz4-compress 1.2 of 2.4,
171
+ * aes-cbc-decrypt 1.2 of 2.0 on the Mac sweep), so such a loop goes on to full batches on its
172
+ * read windows. A loop under it gets one short batch on them first.
173
+ */
174
+ export const proofBound = 2;
175
+ /** True when the bound of a batch is under `boundFloor`, through the seam; what the host passes. */
176
+ export function decideBound(bound) {
177
+ if (admissionSeam.force)
178
+ return false;
179
+ return bound / admissionSeam.batchScale < boundFloor;
180
+ }
181
+ /**
182
+ * The wall and the serial time of the next batch of `next` iterations, from a batch that ran. Its
183
+ * wall minus the time its workers needed for the iterations it completed is the cost that does
184
+ * not shrink with the batch: wake, join and validation. The next batch pays that plus the
185
+ * workers' share of its own iterations. Validation grows with the batch, so the wall comes out
186
+ * low and a projected loss is a loss.
187
+ */
188
+ export function projectBatch(wallMs, trackedMs, perIterationMs, next, width) {
189
+ const ran = trackedMs.filter((ms) => ms > 0);
190
+ if (!ran.length || !(perIterationMs > 0) || next < 1 || width < 1)
191
+ return undefined;
192
+ const mean = ran.reduce((sum, ms) => sum + ms, 0) / ran.length;
193
+ const fixed = Math.max(0, wallMs - Math.ceil(ran.length / width) * mean);
194
+ return { wallMs: Math.ceil(next / width) * mean + fixed, serialMs: next * perIterationMs };
195
+ }
196
+ /** True when a bound is low enough that the first batch on read windows should be a short one. */
197
+ export function decideProof(bound) {
198
+ if (admissionSeam.force)
199
+ return false;
200
+ return bound / admissionSeam.batchScale < proofBound;
201
+ }
@@ -0,0 +1,135 @@
1
+ import type { IterationLocal } from './extract.ts';
2
+ /** Granule sizes of the inline lowering the iteration executor runs. */
3
+ export declare const iterationGranules: {
4
+ readonly read: 64;
5
+ readonly write: 32;
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;
12
+ /** Bytes of one undo entry: granule address, stamp, then the prior bytes of the granule. */
13
+ export declare const iterationEntryBytes: number;
14
+ /** Sizes of the per-batch arena. Every field has a default. */
15
+ export interface IterationArenaOptions {
16
+ /** Iterations in flight at once, one slot, bitmap pair and undo log each. Default 32. */
17
+ batch?: number;
18
+ /** Undo entries one iteration may log, the budget in write granules. Default 2048. */
19
+ logCapacity?: number;
20
+ /** Bytes of one private stack slot, a multiple of 64. Default 32768. */
21
+ slotBytes?: number;
22
+ /** Size of the owner frame whose reads the executor tracks, for the per-slot shadow. Default 0. */
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;
34
+ }
35
+ /** Absolute addresses of the arena parts. Slot 0 sits above one unused guard slot. */
36
+ export interface IterationArena {
37
+ base: number;
38
+ bytes: number;
39
+ memoryBytes: number;
40
+ batch: number;
41
+ logCapacity: number;
42
+ slotBytes: number;
43
+ slots: number;
44
+ stamp: number;
45
+ logs: number;
46
+ logBytes: number;
47
+ rmaps: number;
48
+ rsize: number;
49
+ wmaps: number;
50
+ wsize: number;
51
+ /** Start of the frame shadows, one per slot, after the write bitmaps. */
52
+ frameShadow: number;
53
+ /** Bytes of one slot's frame shadow; 0 without a tracked frame. */
54
+ frameSlotBytes: number;
55
+ /** The tracked frame size. */
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;
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;
70
+ /**
71
+ * Shadow of one tracked frame: `4 * ceil(size / 32)` bytes of written bits, one per frame byte
72
+ * (the bits of frame granule `g`, 32 bytes, are the i32 at `4 * g`), 4 pad bytes, the exposed bits
73
+ * in the same layout, and 4 pad bytes.
74
+ */
75
+ export declare function iterationFrameShadow(frameBytes: number): {
76
+ exposed: number;
77
+ bytes: number;
78
+ };
79
+ /**
80
+ * Lays out slots, the stamp word, undo logs and bitmaps from `base`. The bitmaps cover the whole
81
+ * memory, arena included, so `bytes` depends on `memoryBytes` and not on `base`.
82
+ */
83
+ export declare function iterationArena(base: number, memoryBytes: number, options?: IterationArenaOptions): IterationArena;
84
+ /** Bytes the caller must keep free for a pool over a memory of `memoryBytes`. */
85
+ export declare const iterationArenaBytes: (memoryBytes: number, options?: IterationArenaOptions) => number;
86
+ /**
87
+ * Smallest memory of whole pages that holds the arena above `startBytes`. The bitmaps cover the
88
+ * whole memory, so the size is a fixed point: it grows until the arena it needs fits. Undefined
89
+ * when the result would pass 65,536 pages.
90
+ */
91
+ export declare function iterationFit(startBytes: number, options?: IterationArenaOptions): {
92
+ base: number;
93
+ pages: number;
94
+ bytes: number;
95
+ memoryBytes: number;
96
+ } | undefined;
97
+ /**
98
+ * Arguments of `iterate` for iteration `k`. A strided local is `entry + k * stride` modulo 2^32, or
99
+ * `entry + k * stride * entry[strideLocal]` when its step is held in a local, an invariant local
100
+ * keeps its entry value, and the frame local takes `frame`.
101
+ */
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
+ };