Skip to content

Commit 61df506

Browse files
committed
perf(signals): keep an error boundary's recovery linear in its readers (#4526)
The #4526 fix asked `prune` on every reader clear, and `prune` walked every reader still waiting: a recovery of N readers was N²/2 steps (8000 render effects under <Errored>: ~820 ms vs ~40 ms). `catchStatus` now asks with `pass` 0, which ends the walk at the first reader kept by its status — one is left, so this clear is not the last — and leaves the readers behind it to a later prune. The seam, `ready` and the output's pass walk as before. A deterministic pin counts the Map iteration steps of a 1000-reader recovery (linear: ~N; quadratic: ~N²/2). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 1002293 commit 61df506

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎packages/signals/src/boundaries.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,12 @@ function catchStatus(
260260
// under its parked pass drops the rows that read the error — are gone,
261261
// and left for the seam, they revealed the stale content outside the
262262
// hold (#4526).
263-
if (collecting.size !== 0)
263+
if (collecting.size)
264264
for (let b: Boundary | null | undefined = boundaryOf(node); b; b = b._parent)
265265
if (
266266
!((b._readers.get(node)?._statusFlags as number) & (b._type | STATUS_PENDING)) &&
267267
b._readers.delete(node) &&
268-
prune(b, false) === 0
268+
!prune(b, 0)
269269
) {
270270
if (b._fallback) redraw(b);
271271
if (revealHooks && b._reveal !== null) revealHooks._changed(b._reveal);
@@ -343,8 +343,13 @@ function release(b: Boundary): void {
343343
* source. A reader pending again after the source that threw answered (it
344344
* re-asks through a memo that carried the error) has no new answer yet: the
345345
* error stays the display (D-39: a pending is not a value; async-gen 91501
346-
* #1831). */
347-
export function prune(b: Boundary, pass: boolean): number {
346+
* #1831).
347+
*
348+
* `pass` 0: a reader clearing asks whether it was the last (`catchStatus`),
349+
* once per clear. The first reader kept by its status ends the walk — one
350+
* is left — and the ones behind it wait for a later prune, so a recovery of
351+
* N readers walks each once, not N²/2 times (#4526). */
352+
export function prune(b: Boundary, pass?: boolean | 0): number {
348353
const mask = b._type | STATUS_PENDING;
349354
for (const [r, s] of b._readers) {
350355
if (!((r._flags | s._flags) & REACTIVE_DISPOSED)) {
@@ -355,8 +360,10 @@ export function prune(b: Boundary, pass: boolean): number {
355360
if (
356361
(r._statusFlags | s._statusFlags) & mask ||
357362
(mask & STATUS_ERROR && s._config & CONFIG_STAGED_ERROR)
358-
)
363+
) {
364+
if (pass === 0) break;
359365
continue;
366+
}
360367
if (
361368
(s !== b._tree || b._armed) &&
362369
(s._config & (CONFIG_HELD | CONFIG_OVERRIDE)) === CONFIG_HELD
@@ -392,7 +399,7 @@ export function ready(b: Boundary): boolean {
392399
b._tree._config & CONFIG_HELD &&
393400
(b._output === null || b._output._statusFlags & STATUS_UNINITIALIZED)
394401
) &&
395-
prune(b, false) === 0)
402+
prune(b) === 0)
396403
);
397404
}
398405

@@ -408,7 +415,7 @@ function boundarySeam(): void {
408415
}
409416
// A parked frame's boundary (zombie) keeps its state for its revival.
410417
if (flags & REACTIVE_ZOMBIE) continue;
411-
prune(b, false);
418+
prune(b);
412419
const o = b._output!;
413420
// A fallback staged into the very hold its readers landed in folds back
414421
// (D-16, U-9): the content is ready when that hold commits (D-15). Not

‎packages/signals/tests/errored-list-recovery-held-4526.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { expect, it } from "vitest";
1717
import {
1818
action,
1919
createErrorBoundary,
20+
createMemo,
2021
createOptimisticStore,
2122
createRenderEffect,
2223
createRoot,
@@ -106,3 +107,53 @@ it("#4526: an errored list's held recovery keeps the error until its frame revea
106107
expect(screen()).toBe("[#1][#2]");
107108
dispose();
108109
});
110+
111+
it("#4526: a recovery's reader clears walk the boundary's readers once, not once per clear", () => {
112+
// N render effects read one memo under the boundary; the memo errors and
113+
// recovers. Each effect's clear asks whether it was the last reader: that
114+
// must not re-walk the remaining readers (`prune`) every time — N²/2 steps.
115+
// Counted as Map iteration steps during the recovery flush (deterministic).
116+
const N = 1000;
117+
const [bad, setBad] = createSignal(false);
118+
let shown: unknown;
119+
const dispose = createRoot(d => {
120+
const m = createMemo(() => {
121+
if (bad()) throw new Error("x");
122+
return 1;
123+
});
124+
const view = createErrorBoundary(
125+
() => {
126+
for (let i = 0; i < N; i++) createRenderEffect(m, () => {});
127+
return "ok";
128+
},
129+
() => "err"
130+
);
131+
createRenderEffect(view, v => void (shown = v));
132+
return d;
133+
});
134+
flush();
135+
setBad(true);
136+
flush();
137+
expect(shown).toBe("err");
138+
const proto = Map.prototype as any;
139+
const iterate = proto[Symbol.iterator];
140+
let steps = 0;
141+
proto[Symbol.iterator] = function (this: Map<unknown, unknown>) {
142+
const it = iterate.call(this);
143+
return {
144+
next: () => (steps++, it.next()),
145+
[Symbol.iterator]() {
146+
return this;
147+
}
148+
};
149+
};
150+
try {
151+
setBad(false);
152+
flush();
153+
} finally {
154+
proto[Symbol.iterator] = iterate;
155+
}
156+
expect(shown).toBe("ok");
157+
expect(steps).toBeLessThan(4 * N);
158+
dispose();
159+
});

0 commit comments

Comments
 (0)