@isograph/pico 0.0.0-main-5f205c00
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/LICENSE-MIT +7 -0
- package/README.md +152 -0
- package/dist/index.cjs +1154 -0
- package/dist/index.d.cts +176 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +176 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1139 -0
- package/dist/index.mjs.map +1 -0
- package/docs/contracts.md +45 -0
- package/docs/correctness.md +233 -0
- package/docs/design.md +97 -0
- package/docs/disposing-nodes.md +171 -0
- package/docs/lifetimes.md +25 -0
- package/docs/performance.md +85 -0
- package/package.json +49 -0
- package/src/batch/batch.ts +133 -0
- package/src/batch/flush.ts +30 -0
- package/src/batch/notifications.ts +39 -0
- package/src/disposal/age-drain.ts +124 -0
- package/src/disposal/cleanup.ts +171 -0
- package/src/disposal/older-than-updates.ts +132 -0
- package/src/disposal/schedule.ts +78 -0
- package/src/disposal/take.ts +52 -0
- package/src/disposal/update-counter.ts +9 -0
- package/src/graph/edges.ts +167 -0
- package/src/graph/marking.ts +310 -0
- package/src/graph/nodes.ts +196 -0
- package/src/graph/read.ts +150 -0
- package/src/graph/recompute.ts +127 -0
- package/src/graph/refresh.ts +232 -0
- package/src/index.ts +18 -0
- package/src/retention/retain.ts +133 -0
- package/src/state.ts +39 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { MutableDerivedNode } from '../graph/nodes';
|
|
2
|
+
|
|
3
|
+
/** Called once for each callback node whose recomputation changed its value. */
|
|
4
|
+
export function recordChangedNode(node: MutableDerivedNode<unknown>): void {
|
|
5
|
+
changedNodes[changedCount] = node;
|
|
6
|
+
changedCount += 1;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Nodes whose callbacks are due after the current flush. Each changed node
|
|
11
|
+
* becomes `CLEAN` during refresh, so a later queue entry for it cannot add it
|
|
12
|
+
* again. Callbacks finish before another flush can reuse this array.
|
|
13
|
+
*/
|
|
14
|
+
const changedNodes: Array<MutableDerivedNode<unknown> | undefined> = [];
|
|
15
|
+
|
|
16
|
+
let changedCount = 0;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Calls the `onChange` of each retain of each node whose refresh changed its
|
|
20
|
+
* value, in the order of each node's first refresh that changed it.
|
|
21
|
+
*/
|
|
22
|
+
export function notifyRetainsOfChangedNodes(): void {
|
|
23
|
+
for (let index = 0; index < changedCount; index += 1) {
|
|
24
|
+
const node = changedNodes[index]!;
|
|
25
|
+
// The array survives the flush. Release its reference before notifying.
|
|
26
|
+
changedNodes[index] = undefined;
|
|
27
|
+
// Contract: no computation or `onChange` retains or releases, so the
|
|
28
|
+
// retain list is the one the node had when the refresh changed its value.
|
|
29
|
+
const value = node.value;
|
|
30
|
+
let retain = node.firstRetain;
|
|
31
|
+
while (retain !== undefined) {
|
|
32
|
+
if (retain.onChange !== undefined) {
|
|
33
|
+
retain.onChange(value);
|
|
34
|
+
}
|
|
35
|
+
retain = retain.nextRetain;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
changedCount = 0;
|
|
39
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UNCOMPUTED,
|
|
3
|
+
SCAN_SEEN,
|
|
4
|
+
DISPOSE_FROM_CLEANUP_QUEUE,
|
|
5
|
+
NO_FLAGS,
|
|
6
|
+
} from '../state';
|
|
7
|
+
import { cleanupQueue, disposeNode } from './cleanup';
|
|
8
|
+
import type { MutableDerivedNode } from '../graph/nodes';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Disposes unneeded nodes whose newest queue entry is older than the age
|
|
12
|
+
* boundary, then any dependencies those disposals leave unneeded. Returns
|
|
13
|
+
* the number of remaining entries, excluding the boundary marker.
|
|
14
|
+
*
|
|
15
|
+
* Each boundary in positions must still refer to the same group of surviving
|
|
16
|
+
* entries after compaction. The backward scan counts entries after each
|
|
17
|
+
* boundary. The final pass converts those counts back into queue indices.
|
|
18
|
+
* All SCAN_SEEN bits are clear on entry and again before disposal begins.
|
|
19
|
+
*/
|
|
20
|
+
export function disposeEligibleCleanupEntries(
|
|
21
|
+
eligibleEnd: number,
|
|
22
|
+
positions: Array<number>,
|
|
23
|
+
count: number,
|
|
24
|
+
marker: MutableDerivedNode<unknown>,
|
|
25
|
+
outOfTime: (() => boolean) | undefined,
|
|
26
|
+
): number {
|
|
27
|
+
const queue = cleanupQueue;
|
|
28
|
+
const originalLength = queue.length;
|
|
29
|
+
// Pack the newest entry for each unneeded node into the tail of the
|
|
30
|
+
// original queue. A backward scan sees the newest duplicate first.
|
|
31
|
+
let firstKept = originalLength;
|
|
32
|
+
let eligibleCount = 0;
|
|
33
|
+
let boundaryIndex = count - 1;
|
|
34
|
+
for (let index = originalLength - 1; index >= 0; index -= 1) {
|
|
35
|
+
while (boundaryIndex >= 0 && (positions[boundaryIndex] as number) > index) {
|
|
36
|
+
positions[boundaryIndex] = originalLength - firstKept;
|
|
37
|
+
boundaryIndex -= 1;
|
|
38
|
+
}
|
|
39
|
+
const node = queue[index] as MutableDerivedNode<unknown>;
|
|
40
|
+
if (
|
|
41
|
+
node.firstRetain !== undefined ||
|
|
42
|
+
node.firstDependent !== undefined ||
|
|
43
|
+
node.status === UNCOMPUTED ||
|
|
44
|
+
(node.traversalFlags & SCAN_SEEN) !== NO_FLAGS
|
|
45
|
+
) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
node.traversalFlags |= SCAN_SEEN;
|
|
49
|
+
firstKept -= 1;
|
|
50
|
+
queue[firstKept] = node;
|
|
51
|
+
if (index < eligibleEnd) {
|
|
52
|
+
eligibleCount += 1;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (let index = firstKept; index < originalLength; index += 1) {
|
|
56
|
+
(queue[index] as MutableDerivedNode<unknown>).traversalFlags &= ~SCAN_SEEN;
|
|
57
|
+
}
|
|
58
|
+
// Oldest first, so a dependency's entry is judged before the disposal of a
|
|
59
|
+
// newer dependent can leave it unneeded. The first disposal runs whatever
|
|
60
|
+
// the budget, so each budgeted drain that has an old entry disposes one.
|
|
61
|
+
const firstYoung = firstKept + eligibleCount;
|
|
62
|
+
let nextEligible = firstKept;
|
|
63
|
+
let budgetExpired = false;
|
|
64
|
+
for (; nextEligible < firstYoung; nextEligible += 1) {
|
|
65
|
+
if (nextEligible > firstKept && outOfTime !== undefined && outOfTime()) {
|
|
66
|
+
budgetExpired = true;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
disposeNode(
|
|
70
|
+
queue[nextEligible] as MutableDerivedNode<unknown>,
|
|
71
|
+
DISPOSE_FROM_CLEANUP_QUEUE,
|
|
72
|
+
undefined,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
const disposedCount = nextEligible - firstKept;
|
|
76
|
+
// Disposing a node can leave its inputs unneeded. disposeNode appends
|
|
77
|
+
// them after the original queue. Follow that growing list in this drain.
|
|
78
|
+
let nextCascade = originalLength;
|
|
79
|
+
if (!budgetExpired) {
|
|
80
|
+
for (; nextCascade < queue.length; nextCascade += 1) {
|
|
81
|
+
const node = queue[nextCascade] as MutableDerivedNode<unknown>;
|
|
82
|
+
if (
|
|
83
|
+
node.firstRetain === undefined &&
|
|
84
|
+
node.firstDependent === undefined &&
|
|
85
|
+
node.status !== UNCOMPUTED
|
|
86
|
+
) {
|
|
87
|
+
if (outOfTime !== undefined && outOfTime()) {
|
|
88
|
+
budgetExpired = true;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
disposeNode(node, DISPOSE_FROM_CLEANUP_QUEUE, undefined);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Keep unfinished cascade entries first so the next drain can dispose
|
|
96
|
+
// them at any age. Old entries still waiting and then young entries follow.
|
|
97
|
+
const pendingCascade = budgetExpired ? queue.slice(nextCascade) : undefined;
|
|
98
|
+
const pendingCount = pendingCascade === undefined ? 0 : pendingCascade.length;
|
|
99
|
+
const keptCount = originalLength - firstKept;
|
|
100
|
+
const remainingCount = keptCount - disposedCount;
|
|
101
|
+
queue.copyWithin(pendingCount, firstKept + disposedCount, originalLength);
|
|
102
|
+
if (pendingCascade !== undefined) {
|
|
103
|
+
for (let index = 0; index < pendingCount; index += 1) {
|
|
104
|
+
queue[index] = pendingCascade[index] as MutableDerivedNode<unknown>;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
queue.length = pendingCount + remainingCount;
|
|
108
|
+
for (let index = 0; index < count; index += 1) {
|
|
109
|
+
positions[index] =
|
|
110
|
+
pendingCount + keptCount - (positions[index] as number) - disposedCount;
|
|
111
|
+
}
|
|
112
|
+
if (pendingCount + remainingCount > 0) {
|
|
113
|
+
queue.push(marker);
|
|
114
|
+
}
|
|
115
|
+
drainReachedTimeLimit = budgetExpired;
|
|
116
|
+
pendingCascadeCount = pendingCount;
|
|
117
|
+
return pendingCount + remainingCount;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** True if the last drain skipped eligible work because its time budget expired. */
|
|
121
|
+
export let drainReachedTimeLimit = false;
|
|
122
|
+
|
|
123
|
+
/** Nodes left at the front of the queue for disposal regardless of age. */
|
|
124
|
+
export let pendingCascadeCount = 0;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UNCOMPUTED,
|
|
3
|
+
CLEAN,
|
|
4
|
+
SCHEDULE_NOT_PENDING,
|
|
5
|
+
SCHEDULE_PENDING,
|
|
6
|
+
DISPOSE_FROM_CLEANUP_QUEUE,
|
|
7
|
+
DISPOSE_MARKED,
|
|
8
|
+
} from '../state';
|
|
9
|
+
import {
|
|
10
|
+
unlinkRemainingSourceDependencies,
|
|
11
|
+
removeEdgeFromDerivedDependents,
|
|
12
|
+
} from '../graph/edges';
|
|
13
|
+
import {
|
|
14
|
+
clearMarkedUnneededNodes,
|
|
15
|
+
markedUnneeded,
|
|
16
|
+
markedUnneededCount,
|
|
17
|
+
} from '../graph/marking';
|
|
18
|
+
import type { MutableDerivedNode } from '../graph/nodes';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Pops one entry off the cleanup queue and disposes its node if the node is
|
|
22
|
+
* still unneeded and not `UNCOMPUTED`. Disposing a node puts each of its
|
|
23
|
+
* derived dependencies that becomes unneeded on the cleanup queue. Returns
|
|
24
|
+
* whether the cleanup queue is non-empty.
|
|
25
|
+
*
|
|
26
|
+
* Contract: called outside computations, batches, and `onChange` callbacks.
|
|
27
|
+
*/
|
|
28
|
+
export function disposeNext(): boolean {
|
|
29
|
+
const node = cleanupQueue.pop();
|
|
30
|
+
if (
|
|
31
|
+
node !== undefined &&
|
|
32
|
+
node.firstRetain === undefined &&
|
|
33
|
+
node.firstDependent === undefined &&
|
|
34
|
+
node.status !== UNCOMPUTED
|
|
35
|
+
) {
|
|
36
|
+
disposeNode(node, DISPOSE_FROM_CLEANUP_QUEUE, undefined);
|
|
37
|
+
}
|
|
38
|
+
if (cleanupQueue.length > 0) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
scheduleStatus = SCHEDULE_NOT_PENDING;
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The same node can be queued more than once and can become needed again
|
|
47
|
+
* before disposal. Each queue entry must be checked when it is drained.
|
|
48
|
+
* An unneeded node with a cached value belongs here or on the current write's
|
|
49
|
+
* marked-unneeded list. A tracked read can briefly hold it between pulling
|
|
50
|
+
* its value and linking its new dependent.
|
|
51
|
+
*/
|
|
52
|
+
export const cleanupQueue: Array<MutableDerivedNode<unknown>> = [];
|
|
53
|
+
|
|
54
|
+
/** A pending request runs at the end of the outermost operation. */
|
|
55
|
+
export let scheduleStatus: ScheduleStatus = SCHEDULE_NOT_PENDING;
|
|
56
|
+
|
|
57
|
+
type ScheduleStatus = typeof SCHEDULE_NOT_PENDING | typeof SCHEDULE_PENDING;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* During a drain, push dependencies directly to the cleanup queue so the
|
|
61
|
+
* drain cannot schedule itself again. Queue callers pass no stack and can
|
|
62
|
+
* ignore the return value. Only DISPOSE_MARKED can create a stack to return:
|
|
63
|
+
* it collects newly unneeded dirty dependencies for disposal in the same pass.
|
|
64
|
+
* Clean dependencies go on the cleanup queue.
|
|
65
|
+
*/
|
|
66
|
+
export function disposeNode(
|
|
67
|
+
node: MutableDerivedNode<unknown>,
|
|
68
|
+
kind: DisposalKind,
|
|
69
|
+
initialStack: Array<MutableDerivedNode<unknown>> | undefined,
|
|
70
|
+
): Array<MutableDerivedNode<unknown>> | undefined {
|
|
71
|
+
let stack = initialStack;
|
|
72
|
+
const firstSource = node.firstSourceDependency;
|
|
73
|
+
if (firstSource !== undefined) {
|
|
74
|
+
unlinkRemainingSourceDependencies(firstSource);
|
|
75
|
+
}
|
|
76
|
+
let edge = node.firstDerivedDependency;
|
|
77
|
+
while (edge !== undefined) {
|
|
78
|
+
removeEdgeFromDerivedDependents(edge);
|
|
79
|
+
const dependency = edge.dependency;
|
|
80
|
+
if (
|
|
81
|
+
dependency.firstDependent === undefined &&
|
|
82
|
+
dependency.firstRetain === undefined
|
|
83
|
+
) {
|
|
84
|
+
if (kind === DISPOSE_MARKED) {
|
|
85
|
+
if (dependency.status === CLEAN) {
|
|
86
|
+
addToCleanupQueue(dependency);
|
|
87
|
+
} else {
|
|
88
|
+
(stack ??= []).push(dependency);
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
cleanupQueue.push(dependency);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
edge = edge.nextDependencyEdge;
|
|
95
|
+
}
|
|
96
|
+
node.firstSourceDependency = undefined;
|
|
97
|
+
node.lastSourceDependency = undefined;
|
|
98
|
+
node.firstDerivedDependency = undefined;
|
|
99
|
+
node.lastDerivedDependency = undefined;
|
|
100
|
+
node.firstKnownMarkedDependentEdge = undefined;
|
|
101
|
+
node.status = UNCOMPUTED;
|
|
102
|
+
node.value = undefined;
|
|
103
|
+
return stack;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
type DisposalKind = typeof DISPOSE_FROM_CLEANUP_QUEUE | typeof DISPOSE_MARKED;
|
|
107
|
+
|
|
108
|
+
export function addToCleanupQueue(node: MutableDerivedNode<unknown>): void {
|
|
109
|
+
if (cleanupQueue.length === 0) {
|
|
110
|
+
scheduleStatus = SCHEDULE_PENDING;
|
|
111
|
+
}
|
|
112
|
+
cleanupQueue.push(node);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Disposes every unneeded node on the cleanup queue, and each node that a
|
|
117
|
+
* disposal leaves unneeded.
|
|
118
|
+
*
|
|
119
|
+
* Guarantee: after `disposeNext` returns false, and after `disposeAll`
|
|
120
|
+
* returns, the cleanup queue is empty and no scheduler call is pending.
|
|
121
|
+
*
|
|
122
|
+
* Contract: as `disposeNext`.
|
|
123
|
+
*/
|
|
124
|
+
export function disposeAll(): void {
|
|
125
|
+
while (true) {
|
|
126
|
+
const node = cleanupQueue.pop();
|
|
127
|
+
if (node === undefined) {
|
|
128
|
+
scheduleStatus = SCHEDULE_NOT_PENDING;
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (
|
|
132
|
+
node.firstRetain === undefined &&
|
|
133
|
+
node.firstDependent === undefined &&
|
|
134
|
+
node.status !== UNCOMPUTED
|
|
135
|
+
) {
|
|
136
|
+
disposeNode(node, DISPOSE_FROM_CLEANUP_QUEUE, undefined);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Internal invariant: a dependency that a disposal here leaves unneeded is
|
|
143
|
+
* disposed too if it is not `CLEAN`, because the same write marked it. If it
|
|
144
|
+
* is `CLEAN`, it goes on the cleanup queue with its value.
|
|
145
|
+
*/
|
|
146
|
+
export function disposeMarkedUnneededNodes(): void {
|
|
147
|
+
let stack: Array<MutableDerivedNode<unknown>> | undefined;
|
|
148
|
+
for (let index = 0; index < markedUnneededCount; index += 1) {
|
|
149
|
+
let node: MutableDerivedNode<unknown> | undefined = markedUnneeded[index];
|
|
150
|
+
while (node !== undefined) {
|
|
151
|
+
if (
|
|
152
|
+
node.firstRetain === undefined &&
|
|
153
|
+
node.firstDependent === undefined &&
|
|
154
|
+
node.status !== UNCOMPUTED &&
|
|
155
|
+
node.status !== CLEAN
|
|
156
|
+
) {
|
|
157
|
+
stack = disposeNode(node, DISPOSE_MARKED, stack);
|
|
158
|
+
}
|
|
159
|
+
node = stack?.pop();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
clearMarkedUnneededNodes();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function requestCleanupScheduling(): void {
|
|
166
|
+
scheduleStatus = SCHEDULE_PENDING;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function clearPendingCleanupRequest(): void {
|
|
170
|
+
scheduleStatus = SCHEDULE_NOT_PENDING;
|
|
171
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import {
|
|
2
|
+
pendingCascadeCount,
|
|
3
|
+
disposeEligibleCleanupEntries,
|
|
4
|
+
} from './age-drain';
|
|
5
|
+
import {
|
|
6
|
+
cleanupQueue,
|
|
7
|
+
clearPendingCleanupRequest,
|
|
8
|
+
requestCleanupScheduling,
|
|
9
|
+
} from './cleanup';
|
|
10
|
+
import { createDerivedNode, type MutableDerivedNode } from '../graph/nodes';
|
|
11
|
+
import { updateCount } from './update-counter';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Disposes unneeded nodes whose newest cleanup entry is at least minAge
|
|
15
|
+
* updates old. Age starts at the first drain that sees an entry. A write
|
|
16
|
+
* or batch commit advances the update count. Repeated drains do not.
|
|
17
|
+
* Disposing a node can leave its inputs unneeded; these are eligible immediately.
|
|
18
|
+
*
|
|
19
|
+
* An optional idle deadline or performance.now() timestamp limits disposal.
|
|
20
|
+
* The queue scan always finishes, and the first eligible disposal always
|
|
21
|
+
* runs. Unfinished entries keep their ages for the next drain. If another
|
|
22
|
+
* disposal strategy removed the boundary marker between drains, the
|
|
23
|
+
* remaining entries start aging again at the current update count.
|
|
24
|
+
*
|
|
25
|
+
* Returns the number of entries still queued. When entries remain, request
|
|
26
|
+
* another scheduler call at the end of the next outermost operation.
|
|
27
|
+
* Call outside computations, batches, and callbacks, with an integer minAge.
|
|
28
|
+
*/
|
|
29
|
+
export function disposeOlderThanUpdates<
|
|
30
|
+
TDeadline extends { timeRemaining(): number },
|
|
31
|
+
>(minAge: number, budget?: TDeadline | number): number {
|
|
32
|
+
const length = cleanupQueue.length;
|
|
33
|
+
const now = updateCount;
|
|
34
|
+
let count = boundaryCount;
|
|
35
|
+
if (count > 0 && cleanupQueue[positions[count - 1] as number] !== marker) {
|
|
36
|
+
count = 0;
|
|
37
|
+
}
|
|
38
|
+
let eligibleEnd = minAge <= 0 ? length : 0;
|
|
39
|
+
if (minAge > 0) {
|
|
40
|
+
for (let index = count - 1; index >= 0; index -= 1) {
|
|
41
|
+
if (now - (updates[index] as number) >= minAge) {
|
|
42
|
+
eligibleEnd = positions[index] as number;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
marker ??= createDerivedNode(() => undefined) as MutableDerivedNode<unknown>;
|
|
48
|
+
const outOfTime =
|
|
49
|
+
budget === undefined
|
|
50
|
+
? undefined
|
|
51
|
+
: typeof budget === 'number'
|
|
52
|
+
? () => performance.now() >= budget
|
|
53
|
+
: () => budget.timeRemaining() <= 0;
|
|
54
|
+
const queued = disposeEligibleCleanupEntries(
|
|
55
|
+
eligibleEnd,
|
|
56
|
+
positions,
|
|
57
|
+
count,
|
|
58
|
+
marker,
|
|
59
|
+
outOfTime,
|
|
60
|
+
);
|
|
61
|
+
// The cascade entries that the drain did not reach form the first segment,
|
|
62
|
+
// whose count -Infinity makes it old enough for every `minAge`.
|
|
63
|
+
const front = pendingCascadeCount;
|
|
64
|
+
let next = 0;
|
|
65
|
+
for (let index = 0; index < count; index += 1) {
|
|
66
|
+
const position = positions[index] as number;
|
|
67
|
+
if (position > front) {
|
|
68
|
+
positions[next] = position;
|
|
69
|
+
updates[next] = updates[index] as number;
|
|
70
|
+
next += 1;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (front > 0) {
|
|
74
|
+
for (let index = next; index > 0; index -= 1) {
|
|
75
|
+
positions[index] = positions[index - 1] as number;
|
|
76
|
+
updates[index] = updates[index - 1] as number;
|
|
77
|
+
}
|
|
78
|
+
positions[0] = front;
|
|
79
|
+
updates[0] = -Infinity;
|
|
80
|
+
next += 1;
|
|
81
|
+
}
|
|
82
|
+
if (queued === 0) {
|
|
83
|
+
boundaryCount = 0;
|
|
84
|
+
clearPendingCleanupRequest();
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
if (next > 0 && positions[next - 1] === queued) {
|
|
88
|
+
// Every queued entry is below an existing boundary.
|
|
89
|
+
boundaryCount = next;
|
|
90
|
+
} else if (next > 0 && updates[next - 1] === now) {
|
|
91
|
+
// A drain with no update since the previous boundary extends that segment.
|
|
92
|
+
positions[next - 1] = queued;
|
|
93
|
+
boundaryCount = next;
|
|
94
|
+
} else {
|
|
95
|
+
positions[next] = queued;
|
|
96
|
+
updates[next] = now;
|
|
97
|
+
boundaryCount = next + 1;
|
|
98
|
+
}
|
|
99
|
+
requestCleanupScheduling();
|
|
100
|
+
return queued;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/*
|
|
104
|
+
* A drain is one call of `disposeOlderThanUpdates`. An update is a
|
|
105
|
+
* `writeSource` or a batch commit (batch.ts,
|
|
106
|
+
* `countUpdateAndScheduleCleanupIfIdle`). The update count of an entry is the value of `updateCount` at
|
|
107
|
+
* the first drain after the entry was queued, and its age is the current
|
|
108
|
+
* `updateCount` minus that. An entry queued before some updates that end
|
|
109
|
+
* before that drain is judged younger than it is by their number, never older.
|
|
110
|
+
*
|
|
111
|
+
* Internal invariant: `positions[0]` to `positions[boundaryCount - 1]` are
|
|
112
|
+
* positive and non-decreasing, and `updates[0]` to `updates[boundaryCount - 1]`
|
|
113
|
+
* are increasing. When `boundaryCount > 0` and the queue holds `marker` at index
|
|
114
|
+
* `positions[boundaryCount - 1]`, every entry below `positions[i]` was queued
|
|
115
|
+
* before a drain at which `updateCount` was at most `updates[i]`, and the
|
|
116
|
+
* entries above the marker were queued after the last drain. Entries are
|
|
117
|
+
* appended only at the end, and `disposeNext`, `disposeAll`, and `takeUnneeded`
|
|
118
|
+
* remove entries only from the end, so no entry below the marker moves while
|
|
119
|
+
* the marker stays.
|
|
120
|
+
*/
|
|
121
|
+
const positions: Array<number> = [];
|
|
122
|
+
|
|
123
|
+
const updates: Array<number> = [];
|
|
124
|
+
|
|
125
|
+
let boundaryCount = 0;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* An `UNCOMPUTED` node that a drain puts at its boundary. A disposal skips an
|
|
129
|
+
* `UNCOMPUTED` node, and nothing else queues this node, so it stays at its
|
|
130
|
+
* index until a call removes it together with every entry above it.
|
|
131
|
+
*/
|
|
132
|
+
let marker: MutableDerivedNode<unknown> | undefined;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { SCHEDULE_PENDING, DEFAULT_IDLE, DEFAULT_SCHEDULED } from '../state';
|
|
2
|
+
import {
|
|
3
|
+
cleanupQueue,
|
|
4
|
+
clearPendingCleanupRequest,
|
|
5
|
+
scheduleStatus,
|
|
6
|
+
} from './cleanup';
|
|
7
|
+
import { drainReachedTimeLimit } from './age-drain';
|
|
8
|
+
import { disposeOlderThanUpdates } from './older-than-updates';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* `scheduler.schedule` is called with no arguments at the end of an outermost
|
|
12
|
+
* operation after the cleanup queue went from empty to non-empty, or after a
|
|
13
|
+
* `disposeOlderThanUpdates` that left entries. A `writeSource` to a source that
|
|
14
|
+
* has no dependents, or that no computation read since the source was created
|
|
15
|
+
* or last written, returns before that call. `schedule` may call `disposeAll`
|
|
16
|
+
* synchronously. The default calls `disposeOlderThanUpdates(2)` once per call
|
|
17
|
+
* of `schedule`, with the time of a `requestIdleCallback` callback where that
|
|
18
|
+
* exists and of a 5 ms `setTimeout` task elsewhere, and requests another
|
|
19
|
+
* callback or task only when that drain ran out of time before an old entry.
|
|
20
|
+
* Assigning `scheduler.schedule` replaces the default.
|
|
21
|
+
*
|
|
22
|
+
* Contract: after a call, the scheduler calls `disposeNext` until it returns
|
|
23
|
+
* false, calls `disposeAll`, or calls `disposeOlderThanUpdates` once.
|
|
24
|
+
*/
|
|
25
|
+
export const scheduler: { schedule: () => void } = {
|
|
26
|
+
schedule: scheduleCleanupDuringIdleTime,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function scheduleCleanupDuringIdleTime(): void {
|
|
30
|
+
if (defaultScheduleStatus === DEFAULT_SCHEDULED) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
defaultScheduleStatus = DEFAULT_SCHEDULED;
|
|
34
|
+
if (typeof requestIdleCallback === 'function') {
|
|
35
|
+
requestIdleCallback(disposeOldNodesWithinTimeBudget);
|
|
36
|
+
} else {
|
|
37
|
+
setTimeout(disposeOldNodesWithinTimeBudget, 0);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* One drain with the idle period's time, or 5 ms in a `setTimeout` task,
|
|
43
|
+
* which passes no deadline. A drain disposes at least one old entry before it
|
|
44
|
+
* checks the time, so each callback that a cut-off requests makes progress. A
|
|
45
|
+
* drain that leaves only young entries requests nothing: the next outermost
|
|
46
|
+
* operation calls `schedule` again.
|
|
47
|
+
*/
|
|
48
|
+
function disposeOldNodesWithinTimeBudget(deadline?: IdleDeadline): void {
|
|
49
|
+
defaultScheduleStatus = DEFAULT_IDLE;
|
|
50
|
+
if (cleanupQueue.length === 0) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
// The returned count includes young nodes. Only a drain that set drainReachedTimeLimit
|
|
54
|
+
// left eligible work for another idle callback.
|
|
55
|
+
disposeOlderThanUpdates(
|
|
56
|
+
DEFAULT_MIN_AGE,
|
|
57
|
+
deadline ?? performance.now() + TIMEOUT_DISPOSAL_MS,
|
|
58
|
+
);
|
|
59
|
+
if (drainReachedTimeLimit) {
|
|
60
|
+
clearPendingCleanupRequest();
|
|
61
|
+
scheduleCleanupDuringIdleTime();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const DEFAULT_MIN_AGE = 2;
|
|
66
|
+
|
|
67
|
+
const TIMEOUT_DISPOSAL_MS = 5;
|
|
68
|
+
|
|
69
|
+
let defaultScheduleStatus: DefaultScheduleStatus = DEFAULT_IDLE;
|
|
70
|
+
|
|
71
|
+
type DefaultScheduleStatus = typeof DEFAULT_IDLE | typeof DEFAULT_SCHEDULED;
|
|
72
|
+
|
|
73
|
+
export function scheduleCleanupIfRequested(): void {
|
|
74
|
+
if (scheduleStatus === SCHEDULE_PENDING) {
|
|
75
|
+
clearPendingCleanupRequest();
|
|
76
|
+
scheduler.schedule();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { UNCOMPUTED, DISPOSE_FROM_CLEANUP_QUEUE } from '../state';
|
|
2
|
+
import {
|
|
3
|
+
cleanupQueue,
|
|
4
|
+
clearPendingCleanupRequest,
|
|
5
|
+
disposeNode,
|
|
6
|
+
requestCleanupScheduling,
|
|
7
|
+
} from './cleanup';
|
|
8
|
+
import type { DerivedNode, MutableDerivedNode } from '../graph/nodes';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Empties the cleanup queue and returns the node of each entry whose node is
|
|
12
|
+
* still unneeded and not `UNCOMPUTED`, oldest entry first. A node with several
|
|
13
|
+
* entries appears once per entry. pico-js no longer holds a taken node for
|
|
14
|
+
* disposal: it queues the node again only when the node becomes unneeded again
|
|
15
|
+
* after it is needed, or on a top-level read while it is unneeded.
|
|
16
|
+
*
|
|
17
|
+
* Contract: called outside computations, batches, and `onChange` callbacks.
|
|
18
|
+
*/
|
|
19
|
+
export function takeUnneeded(): ReadonlyArray<DerivedNode<unknown>> {
|
|
20
|
+
const taken = cleanupQueue.filter(isUnneededComputedNode);
|
|
21
|
+
cleanupQueue.length = 0;
|
|
22
|
+
clearPendingCleanupRequest();
|
|
23
|
+
return taken;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function isUnneededComputedNode(node: MutableDerivedNode<unknown>): boolean {
|
|
27
|
+
return (
|
|
28
|
+
node.firstRetain === undefined &&
|
|
29
|
+
node.firstDependent === undefined &&
|
|
30
|
+
node.status !== UNCOMPUTED
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Disposes `node` if it is unneeded and not `UNCOMPUTED`, and returns whether
|
|
36
|
+
* it did. The dependencies that the disposal leaves unneeded go on the cleanup
|
|
37
|
+
* queue.
|
|
38
|
+
*
|
|
39
|
+
* Contract: called outside computations, batches, and `onChange` callbacks.
|
|
40
|
+
*/
|
|
41
|
+
export function disposeIfUnneeded(node: DerivedNode<unknown>): boolean {
|
|
42
|
+
const mutableNode = node as MutableDerivedNode<unknown>;
|
|
43
|
+
if (!isUnneededComputedNode(mutableNode)) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
const wasEmpty = cleanupQueue.length === 0;
|
|
47
|
+
disposeNode(mutableNode, DISPOSE_FROM_CLEANUP_QUEUE, undefined);
|
|
48
|
+
if (wasEmpty && cleanupQueue.length > 0) {
|
|
49
|
+
requestCleanupScheduling();
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|