Skip to content

Commit 62745f6

Browse files
ryansolidclaude
andauthored
perf(size): shrink the floors to make room for #4535 (−177 B minified on hello world) (#4560)
* perf(size): share the dependent walks, dep clearing and event-walk hops The three async dependent walks (settle, errored settle, release) share one visited-set walker; clearDeps reuses trimStaleDeps; the delegated event walk shares its parent hop and owner-boundary test. Drops checks already implied by pendingSource, and escalate's prod-dead default. Cold paths only; no behavior change. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * perf(size): fold the trailing hold guard into one expression Same truth table as the if: a non-effect holds, an effect holds unless freshHook answers CAUGHT_REARMED; freshHook is still read once. About -1 B minified on the boundary rows. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * fix(signals): the shared settle walk keeps a diamond join's early return walkDependents skipped a node an earlier branch had walked before calling the settle callback, so a diamond's join no longer took the settle walk's early-return branch (a verdict reader's re-run, a staged error's mark) on its second arrival. The callback now sees every arrival with a `seen` flag; the release and errored walks ignore revisits as before. Pinned. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * perf(size): notifyStatus writes the status flags once; setPendingError picks one error notifyStatus's pending-source branch computed the same flags the general formula gives a pending status; both branches now share it, the order of writes otherwise unchanged (neither pending-source helper reads the flags). setPendingError chooses the given, current or a fresh NotReadyError in one expression — the same error lands in every case. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * perf(signals): settle allocates the lane queues only with the lane seam Without the lane engine installed the lanes pair stayed empty every flush; it is now created only when the seam runs, and the merge below reads it optionally. Same queues in the same order either way. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * perf(size): flatten shares its thunk walk flatten and flattenArray share isThunk/unwrap/skipped: the same checks in the same order. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * perf(size): one helper queues an effect's owed run commitPendingNode's born-held effect and notifyEffectStatus's errored user effect both mark the run owed and queue the bound runner; they now share queueEffectRun (the runner is the same function either way). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * perf(size): template and render each read a value once template's create reads the content's first child once; render calls the app once before choosing the document or the insert path (the error hook is set before it either way, as before). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * chore: changeset covers the added savings Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * test(signals): the diamond pin links #4562 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * revert: flatten keeps next's thunk walk Reverts fdb6140. Review measured it as worse on brotli (hello world -5 B, render+one -12, CSR -15, compiled CSR -33 without it, for +62 B minified), and flatten runs per row. flatten.ts is now identical to next. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 --------- Co-authored-by: Claude <[email protected]>
1 parent 404d06a commit 62745f6

9 files changed

Lines changed: 187 additions & 115 deletions

File tree

‎.changeset/floor-shared-walks.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@solidjs/signals": patch
3+
"@solidjs/web": patch
4+
---
5+
6+
Shrink the compiled and hand-written floors: the dependent walks, dependency clearing, status writes, effect-run queueing, flatten's thunk walk and the delegated-event parent hop share their code (no behavior change).

‎packages/signals/src/core/async.ts‎

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,13 @@ export function setPendingError(el: Computed<any>, source?: Computed<any>, error
118118
if (el._x !== null) el._x._error = null;
119119
return;
120120
}
121-
if (error instanceof NotReadyError && error.source === source) {
122-
ext(el)._error = error;
123-
return;
124-
}
125-
const current = el._x?._error;
126-
if (!(current instanceof NotReadyError) || current.source !== source) {
127-
ext(el)._error = new NotReadyError(source);
128-
}
121+
// The given error when it names this source, else the current one when it
122+
// does (rewritten unchanged), else a fresh one.
123+
const current = error instanceof NotReadyError && error.source === source ? error : el._x?._error;
124+
ext(el)._error =
125+
current instanceof NotReadyError && current.source === source
126+
? current
127+
: new NotReadyError(source);
129128
}
130129

131130
export function forEachDependent(
@@ -135,6 +134,25 @@ export function forEachDependent(
135134
for (let s = el._subs; s !== null; s = s._nextSub) fn(s._sub, s);
136135
}
137136

137+
/** Each dependent reachable from `el`, depth first. `fn` sees every arrival,
138+
* `seen` when an earlier branch already walked the node (a diamond); the
139+
* node's dependents are walked once, on its first arrival unless `fn`
140+
* returns false (then it stays unmarked: a later branch may walk it). */
141+
function walkDependents(
142+
el: Computed<any>,
143+
fn: (node: Computed<any>, seen: boolean) => unknown
144+
): void {
145+
const visited = new NewSet<Computed<any>>();
146+
const visit = (node: Computed<any>) => {
147+
const seen = visited.has(node);
148+
if (fn(node, seen) !== false && !seen) {
149+
visited.add(node);
150+
forEachDependent(node, visit);
151+
}
152+
};
153+
forEachDependent(el, visit);
154+
}
155+
138156
// Queue a node to re-run on the next flush (used both when a pending source
139157
// settles and when an `isPending` observer must re-evaluate after a real error):
140158
// shared scheduling helper in heap.ts (tracked effects bypass the heap).
@@ -161,14 +179,9 @@ export function releaseIfSettledUnobserved(node: Computed<any>): void {
161179
// Collect-then-release so unobserved() never unlinks under the walk.
162180
export function releaseSettledDependents(el: Computed<any>): void {
163181
let candidates: Computed<any>[] | undefined;
164-
const visited = new NewSet<Computed<any>>();
165-
const visit = (node: Computed<any>) => {
166-
if (visited.has(node)) return;
167-
visited.add(node);
168-
if (!node._subs && node._config & CONFIG_AUTO_DISPOSE) (candidates ??= []).push(node);
169-
forEachDependent(node, visit);
170-
};
171-
forEachDependent(el, visit);
182+
walkDependents(el, (node, seen) => {
183+
if (!seen && !node._subs && node._config & CONFIG_AUTO_DISPOSE) (candidates ??= []).push(node);
184+
});
172185
if (candidates) for (const node of candidates) releaseIfSettledUnobserved(node);
173186
}
174187

@@ -256,17 +269,12 @@ function stageMark(node: Computed<any>, el: Computed<any>): void {
256269

257270
export function settleErroredDependents(el: Computed<any>, error: any): void {
258271
let scheduled = false;
259-
const visited = new NewSet<Computed<any>>();
260-
const visit = (node: Computed<any>) => {
261-
if (visited.has(node)) return;
262-
visited.add(node);
263-
if (node._x?._error === error) {
272+
walkDependents(el, (node, seen) => {
273+
if (!seen && node._x?._error === error) {
264274
enqueueSub(node);
265275
scheduled = true;
266276
}
267-
forEachDependent(node, visit);
268-
};
269-
forEachDependent(el, visit);
277+
});
270278
if (scheduled) schedule();
271279
}
272280

@@ -334,10 +342,12 @@ export function settlePendingSource(el: Computed<any>, source: Computed<any> = e
334342
removePendingSource(el, source);
335343
let scheduled = false;
336344
let released: Computed<any>[] | undefined;
337-
const visited = new NewSet<Computed<any>>();
338-
const settle = (node: Computed<any>) => {
345+
walkDependents(el, (node, seen) => {
339346
if (
340-
visited.has(node) ||
347+
// A node an earlier branch settled (a diamond) takes only this early
348+
// return's verdict re-run / staged-error mark, as the walk did before
349+
// it was shared.
350+
seen ||
341351
// A conditional dropped this source, but another dependency can still
342352
// carry it. Only retire pending state inherited through the recovered
343353
// branch. Deliberately NOT marked visited on this early return: the
@@ -355,11 +365,11 @@ export function settlePendingSource(el: Computed<any>, source: Computed<any> = e
355365
// D-41: a derivation of a staged error carries it (`stageMark`).
356366
return (
357367
holdWorkHook &&
358-
(node._config & CONFIG_VERDICT
359-
? enqueueSub(node)
360-
: el._config & CONFIG_STAGED_ERROR && stageMark(node, el))
368+
(node._config & CONFIG_VERDICT
369+
? enqueueSub(node)
370+
: el._config & CONFIG_STAGED_ERROR && stageMark(node, el)),
371+
false
361372
);
362-
visited.add(node);
363373
node._time = clock;
364374
const remaining = node._x?._pendingSources?.values().next().value;
365375
// STATUS_ERROR + pending sources only coexist via an errored loading
@@ -394,10 +404,7 @@ export function settlePendingSource(el: Computed<any>, source: Computed<any> = e
394404
// lists this walk is still iterating.
395405
if (!node._subs && node._config & CONFIG_AUTO_DISPOSE) (released ??= []).push(node);
396406
}
397-
forEachDependent(node, settle);
398-
};
399-
400-
forEachDependent(el, settle);
407+
});
401408

402409
// Release before the flush schedule below: unobserved() pulls the node back
403410
// out of the heap, so the enqueueSub above never recomputes a released node.
@@ -990,34 +997,27 @@ export function notifyStatus(el: Computed<any>, status: number, error?: any): vo
990997
const pendingSource = status === STATUS_PENDING && error instanceof NotReadyError && error.source;
991998
// (CARVE 2: the lane assignment went with the engine.)
992999

993-
if (status === STATUS_PENDING && pendingSource) {
1000+
if (pendingSource) {
9941001
// D-41: a re-ask of a shown error keeps it the display (`_shownError`).
9951002
if (el._statusFlags & STATUS_ERROR) el._x!._shownError = el._x!._error;
9961003
addPendingSource(el, pendingSource);
997-
// A re-ask of an error that answered the first read: still no value.
998-
el._statusFlags =
999-
STATUS_PENDING |
1000-
(el._statusFlags & STATUS_NO_VALUE
1004+
} else clearPendingSources(el);
1005+
// An error answering a node with no value marks it so; a re-ask of it
1006+
// (pending, from a source or not) is uninitialized again — still no
1007+
// value; a write (STATUS_NONE) gives it one.
1008+
el._statusFlags =
1009+
status |
1010+
(status === STATUS_ERROR
1011+
? el._statusFlags & (STATUS_UNINITIALIZED | STATUS_NO_VALUE)
1012+
? STATUS_NO_VALUE
1013+
: 0
1014+
: status === STATUS_PENDING && el._statusFlags & STATUS_NO_VALUE
10011015
? STATUS_UNINITIALIZED
10021016
: el._statusFlags & STATUS_UNINITIALIZED);
1003-
// Preserve the current source on this propagation so readers park on
1004-
// the distinct pending source.
1005-
setPendingError(el, pendingSource, error);
1006-
} else {
1007-
clearPendingSources(el);
1008-
// An error answering a node with no value marks it so; a re-ask of it
1009-
// (pending) is uninitialized again; a write (STATUS_NONE) gives it one.
1010-
el._statusFlags =
1011-
status |
1012-
(status === STATUS_ERROR
1013-
? el._statusFlags & (STATUS_UNINITIALIZED | STATUS_NO_VALUE)
1014-
? STATUS_NO_VALUE
1015-
: 0
1016-
: status === STATUS_PENDING && el._statusFlags & STATUS_NO_VALUE
1017-
? STATUS_UNINITIALIZED
1018-
: el._statusFlags & STATUS_UNINITIALIZED);
1019-
ext(el)._error = error;
1020-
}
1017+
// A propagated pending keeps its current source so readers park on the
1018+
// distinct pending source.
1019+
if (pendingSource) setPendingError(el, pendingSource, error);
1020+
else ext(el)._error = error;
10211021

10221022
// Lanes: a written guess is an optimistic boundary (A17 — "the optimistic
10231023
// future value until we know otherwise"). Its own source in flight is its
@@ -1098,7 +1098,7 @@ export function propagateStatus(el: Computed<any>, status: number, error: any):
10981098
// frame still passes through — its pass is the frame's (#3372, compiled
10991099
// shape: the unmount it stages makes the observer a zombie whose say is
11001100
// moot for this transaction, A15 #3463).
1101-
if (status === STATUS_PENDING && pendingSource && sub._x?._pendingSources?.has(pendingSource)) {
1101+
if (pendingSource && sub._x?._pendingSources?.has(pendingSource)) {
11021102
if (
11031103
passLane === null &&
11041104
(sub._config & (CONFIG_HELD | CONFIG_OVERRIDE)) === CONFIG_HELD &&
@@ -1109,7 +1109,7 @@ export function propagateStatus(el: Computed<any>, status: number, error: any):
11091109
return;
11101110
}
11111111
if (
1112-
(status === STATUS_PENDING && pendingSource) ||
1112+
pendingSource ||
11131113
(status !== STATUS_PENDING && (sub._x?._error !== error || sub._x?._pendingSources))
11141114
) {
11151115
// The marked subscriber is queued for the commit sweep so its status

‎packages/signals/src/core/core.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
986986
// through such a reader (`blockedBy`, #4542).
987987
if (create && lane === null && joined && el._statusFlags & STATUS_PENDING) {
988988
const t = flushTransaction ?? passTx;
989-
if (t && !t._lane && (!isEffect || freshHook?.(el) !== CAUGHT_REARMED)) holdNode(el, t);
989+
t && !t._lane && ((isEffect && freshHook?.(el) === CAUGHT_REARMED) || holdNode(el, t));
990990
}
991991
// Dependencies are the committed frame's until it is replaced (A30, #3410):
992992
// a pass that staged its value leaves the previous pass's tail linked for

‎packages/signals/src/core/effect.ts‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
_hitUnhandledAsync,
3636
GlobalQueue,
3737
globalQueue,
38+
queueEffectRun,
3839
haltReactivity,
3940
haltReactivityAt,
4041
resetUnhandledAsync,
@@ -121,10 +122,7 @@ function notifyEffectStatus(this: Effect<any>): void {
121122
// and the runner dispatches on them, so a recovery before the effect
122123
// phase takes the success arm instead. (Re-read: the boundary
123124
// notify above can run a redraw.)
124-
if (this._statusFlags & STATUS_ERROR) {
125-
this._modified = true;
126-
globalQueue.enqueue(this._type, (this._boundRunEffect ??= runEffect.bind(null, this)));
127-
}
125+
if (this._statusFlags & STATUS_ERROR) queueEffectRun(this);
128126
return;
129127
}
130128
// Dev locates the halt at the node that threw: the StatusError's
@@ -134,7 +132,7 @@ function notifyEffectStatus(this: Effect<any>): void {
134132
escalate(
135133
this,
136134
unwrapStatusError(actualError),
137-
__DEV__ && actualError instanceof StatusError ? actualError.source : this
135+
__DEV__ && actualError instanceof StatusError ? actualError.source : undefined
138136
);
139137
} else if (this._type === EFFECT_RENDER) {
140138
globalQueue.notify(this, STATUS_PENDING | STATUS_ERROR, actualStatus, actualError);
@@ -166,10 +164,11 @@ function notifyEffectStatus(this: Effect<any>): void {
166164
}
167165

168166
/** An effect's error no boundary takes halts the system (boundary-or-halt):
169-
* halted with `cause` and rethrown; dev locates it at `subject`. */
170-
function escalate(node: Effect<any>, cause: unknown, subject: DiagnosticSubject = node): void {
167+
* halted with `cause` and rethrown; dev locates it at `subject` (default:
168+
* the effect). */
169+
function escalate(node: Effect<any>, cause: unknown, subject?: DiagnosticSubject): void {
171170
if (!globalQueue.notify(node, STATUS_ERROR, STATUS_ERROR)) {
172-
__DEV__ ? haltReactivityAt(cause, subject) : haltReactivity(cause);
171+
__DEV__ ? haltReactivityAt(cause, subject ?? node) : haltReactivity(cause);
173172
throw cause;
174173
}
175174
}

‎packages/signals/src/core/graph.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,13 @@ export function trimStaleDeps(el: Computed<any>): void {
7474
// Shared by unobserved() and the disposeChildren child loop. The truthy guard
7575
// (not `!== null`) matters: plain Owners in a child chain have no _deps field,
7676
// and skipping early also avoids adding one (hidden-class churn) via the
77-
// null-out below.
77+
// null-out below. With `_depsTail` cleared, `trimStaleDeps` unlinks the whole
78+
// list and nulls `_deps`.
7879
export function clearDeps(el: Computed<unknown>): void {
79-
let dep = el._deps;
80-
if (!dep) return;
81-
do {
82-
dep = unlinkSubs(dep);
83-
} while (dep !== null);
84-
el._deps = null;
85-
el._depsTail = null;
80+
if (el._deps) {
81+
el._depsTail = null;
82+
trimStaleDeps(el);
83+
}
8684
}
8785

8886
/**

‎packages/signals/src/core/owner.ts‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,17 @@ export function disposeChildren(node: Owner, self: boolean = false, zombie?: boo
9999
// cleanups never run and it stays subscribed, rerunning in a torn-down
100100
// tree (#3561). Death only (`self`): a re-pass leaves the frame reacting
101101
// until the commit.
102-
if (
103-
self &&
104-
!zombie &&
105-
node._x !== null &&
106-
(node._x._pendingFirstChild !== null || node._x._pendingDisposal !== null)
107-
)
108-
disposeChildren(node, false, true);
109102
if (self) {
103+
if (
104+
!zombie &&
105+
node._x !== null &&
106+
(node._x._pendingFirstChild !== null || node._x._pendingDisposal !== null)
107+
)
108+
disposeChildren(node, false, true);
110109
(node as any)._flags = flags | REACTIVE_DISPOSED;
111110
if (__OBSERVE__ && node._parent === null && (node as Root)._root) unregisterRoot(node);
111+
if (__DEV__) clearSignals(node);
112112
}
113-
if (self && __DEV__) clearSignals(node);
114113
if (self && (node as any)._fn) {
115114
if ((node as Computed<unknown>)._x !== null) (node as Computed<unknown>)._x!._inFlight = null;
116115
// A death that can unblock the future (L2, `futureBlocked`): a held

‎packages/signals/src/core/scheduler.ts‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,12 +1068,12 @@ export class GlobalQueue implements IQueue {
10681068
// This flush's own runs, the lanes' reveals, and the landings' held runs
10691069
// are collected apart and ordered below.
10701070
const own = this._queues;
1071-
const lanes: [QueueCallback[], QueueCallback[]] = [[], []];
1071+
let lanes: [QueueCallback[], QueueCallback[]] | null = null;
10721072
// The passes that read this frame's stagings as the screen read held
10731073
// writes if it parked: the lane seam re-derives them on the committed
10741074
// world (a lane's runs wait this round).
10751075
if (laneSeamsHook) {
1076-
this._queues = lanes;
1076+
this._queues = lanes = [[], []];
10771077
laneSeamsHook(t);
10781078
if (stagedReaders.length) stagedReaders.length = 0;
10791079
}
@@ -1121,8 +1121,8 @@ export class GlobalQueue implements IQueue {
11211121
// same array, not a copy; `run` takes it whole). Every other seam
11221122
// builds the ordered queue above.
11231123
this._queues[i] =
1124-
parked || lanes[i].length || this._queues[i].length
1125-
? lanes[i].concat(parked ? [] : own[i], this._queues[i])
1124+
parked || lanes?.[i].length || this._queues[i].length
1125+
? (lanes ? lanes[i] : []).concat(parked ? [] : own[i], this._queues[i])
11261126
: own[i];
11271127
}
11281128
if (deferredZombies.length !== 0) {
@@ -1403,6 +1403,16 @@ export function laneDirty(node: Signal<any> | Computed<any>, l: Transaction): vo
14031403
if (t?._lane ? sameLane(t, l) : (c as any)._type) c._flags |= REACTIVE_LANE_DIRTY;
14041404
}
14051405
}
1406+
1407+
/** An effect owes a run: marked, and its bound runner queued in its phase. */
1408+
export function queueEffectRun(node: any): void {
1409+
node._modified = true;
1410+
globalQueue.enqueue(
1411+
node._type,
1412+
(node._boundRunEffect ??= GlobalQueue._runEffect.bind(null, node))
1413+
);
1414+
}
1415+
14061416
/** One lane, or two of one link group (#3335): one reveal unit. */
14071417
export function sameLane(a: Transaction, b: Transaction): boolean {
14081418
return a === b || (a._links !== null && a._links === b._links);
@@ -1461,13 +1471,7 @@ export function commitPendingNode(n: Signal<any>): void {
14611471
if (c._x != null) ((c._x._reask = false), (c._x._flushed = NOT_PENDING));
14621472
// An effect with a staged value was born held (L2): its first run is
14631473
// this commit's, not its creation's (A29) — queue it now.
1464-
if ((n as any)._type && (n as any)._type !== EFFECT_TRACKED) {
1465-
(n as any)._modified = true;
1466-
globalQueue.enqueue(
1467-
(n as any)._type,
1468-
((n as any)._boundRunEffect ??= GlobalQueue._runEffect.bind(null, c as Computed<unknown>))
1469-
);
1470-
}
1474+
if ((n as any)._type && (n as any)._type !== EFFECT_TRACKED) queueEffectRun(n);
14711475
}
14721476
// The committed value is the first observable answer for a loading-window
14731477
// node — the window closes here, not at compute time (#2990). Unconditional

0 commit comments

Comments
 (0)