Skip to content

Commit b7d5c81

Browse files
committed
perf(signals): keep pending-source membership reads inline on hot paths (#4541)
The shared hasPendingSource helper put a call on recompute's per-pass wasPendingSource read (and propagateStatus' per-dependent check); the mount-rows reference bench regressed with it. Inline reads are back; the cold-path clearInFlight offset keeps core under its base size. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent b6ac8ab commit b7d5c81

3 files changed

Lines changed: 7 additions & 13 deletions

File tree

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,8 @@ import type { Computed, Link, Owner } from "./types.js";
6464
// created dual state whose migration invariant was easy to break: a third
6565
// overlapping source landed beside the Set and removePendingSource refused
6666
// to clear it, stranding the Set members' pending forever (#2893).
67-
export function hasPendingSource(el: Computed<any>, source: any): boolean | undefined {
68-
return el._x?._pendingSources?.has(source);
69-
}
70-
7167
export function addPendingSource(el: Computed<any>, source: Computed<any>): boolean {
72-
if (hasPendingSource(el, source)) return false;
68+
if (el._x?._pendingSources?.has(source)) return false;
7369
(ext(el)._pendingSources ??= new NewSet()).add(source);
7470
return true;
7571
}
@@ -101,7 +97,7 @@ function clearPendingSources(el: Computed<any>): void {
10197
export function retryReaches(el: Computed<any>, source: any): boolean {
10298
for (let d = el._deps; d; d = d._nextDep) {
10399
const dep = d._dep as Computed<any>;
104-
if (dep === source || hasPendingSource(dep, source)) return true;
100+
if (dep === source || dep._x?._pendingSources?.has(source)) return true;
105101
}
106102
return false;
107103
}
@@ -1108,7 +1104,7 @@ export function propagateStatus(el: Computed<any>, status: number, error: any):
11081104
// frame still passes through — its pass is the frame's (#3372, compiled
11091105
// shape: the unmount it stages makes the observer a zombie whose say is
11101106
// moot for this transaction, A15 #3463).
1111-
if (status === STATUS_PENDING && pendingSource && hasPendingSource(sub, pendingSource)) {
1107+
if (status === STATUS_PENDING && pendingSource && sub._x?._pendingSources?.has(pendingSource)) {
11121108
if (
11131109
passLane === null &&
11141110
(sub._config & (CONFIG_HELD | CONFIG_OVERRIDE)) === CONFIG_HELD &&

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
clearInFlight,
44
clearStatus,
55
handleAsync,
6-
hasPendingSource,
76
notifyStatus,
87
parkLoadingWindow,
98
releaseFlightTeardown,
@@ -536,7 +535,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
536535
// settles synchronously, those dependents settle HERE — asyncWrite's
537536
// settlePendingSource walk never runs for a landing that was preempted
538537
// (#3181).
539-
const wasPendingSource = hasPendingSource(el, el);
538+
const wasPendingSource = el._x?._pendingSources?.has(el);
540539
// D-41: this pass's error is staged, not published (`stageError`).
541540
let stagedErr = false;
542541
// A19 exc. 2: a `refresh()` re-asks the question already answered — quiet
@@ -679,7 +678,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
679678
// readers in between re-derive clean (#4541).
680679
if ((notReady || stagedErr) && outgoingPendingSources)
681680
for (const source of outgoingPendingSources)
682-
if (source !== el && (!hasPendingSource(el, source) || !retryReaches(el, source)))
681+
if (source !== el && (!el._x?._pendingSources?.has(source) || !retryReaches(el, source)))
683682
settlePendingSource(el, source);
684683
}
685684
} finally {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ import { bornInto, creatorReads, ext, stagedRead, tracking } from "./core.js";
6565
import { NotReadyError, type StatusError } from "./error.js";
6666
import {
6767
handleAsync,
68-
hasPendingSource,
6968
isThenable,
7069
notifyStatus,
7170
releaseFlightTeardown,
@@ -600,7 +599,7 @@ function dissolveLane(l: Transaction, into: Transaction | null, except?: Signal<
600599
slot === NOT_PENDING &&
601600
((n as Computed<any>)._statusFlags & (STATUS_UNINITIALIZED | STATUS_PENDING)) ===
602601
(STATUS_UNINITIALIZED | STATUS_PENDING) &&
603-
(x._inFlight || loading.some(q => hasPendingSource(n as Computed<any>, q)))
602+
(x._inFlight || loading.some(q => x._pendingSources?.has(q)))
604603
? loading.push(n as Computed<any>)
605604
: (n._pendingValue = latest);
606605
commitPendingNode(n);
@@ -1139,7 +1138,7 @@ const laneSeams = (t: Transaction | null): void => {
11391138
for (let s = r._subs, n; s; s = s._nextSub)
11401139
if (
11411140
isThenable((n = s._sub)._x?._inFlight) &&
1142-
hasPendingSource(n, n) &&
1141+
n._x!._pendingSources?.has(n) &&
11431142
n._config & CONFIG_HELD &&
11441143
txOf(n) === t
11451144
)

0 commit comments

Comments
 (0)