Skip to content

Commit d1000a8

Browse files
committed
perf(signals): one snapshot-serve helper for read() and the verdict window (#4570)
The verdict window's copy of read()'s snapshot serve cost the live pages 125-159 B minified. Both sites now call one helper, servedSnapshot, behind the same inline guard, so builds that never hydrate still fold both away. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent dd3c9f3 commit d1000a8

2 files changed

Lines changed: 43 additions & 33 deletions

File tree

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,26 @@ function servesStaged(c: Computed<any>, el: Signal<any> | Computed<any>, owner:
20552055
);
20562056
}
20572057

2058+
/**
2059+
* A reader in a snapshot scope (a hydration claim) is served the value the
2060+
* server rendered with: a client write's recompute still in flight does not
2061+
* pull the claim off the markup it adopts — the reader is marked stale and
2062+
* replays at the scope's release. NOT_PENDING when there is nothing to
2063+
* serve: no snapshot, or an errored node, which throws as for any reader.
2064+
* Shared by `read` and the verdict window (`latest`/`isPending`, #4570);
2065+
* callers check `snapshotCaptureActive` and the scope first, so builds that
2066+
* never hydrate fold both sites away.
2067+
*/
2068+
export function servedSnapshot(el: Signal<any> | Computed<any>, c: Computed<any>): unknown {
2069+
const sv = el._x?._snapshotValue;
2070+
if (sv === undefined || (el as Computed<any>)._statusFlags & STATUS_ERROR) return NOT_PENDING;
2071+
const snapshot = sv === NO_SNAPSHOT ? undefined : sv;
2072+
if ((el._pendingValue !== NOT_PENDING ? el._pendingValue : el._value) !== snapshot)
2073+
c._flags |= REACTIVE_SNAPSHOT_STALE;
2074+
if (__TEST__) shadowActual(SLOT_SNAPSHOT);
2075+
return snapshot;
2076+
}
2077+
20582078
export function read<T>(el: Signal<T> | Computed<T>): T {
20592079
// Shadow (S0): every read is run through `shadowRead`, which re-enters
20602080
// here once (the bypass) and compares the slot the table predicts with
@@ -2238,19 +2258,10 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
22382258
nodeName: (owner as any)?._name
22392259
});
22402260

2241-
// A reader in a snapshot scope (a hydration claim) is served the value the
2242-
// server rendered with: a client write's recompute still in flight does
2243-
// not pull the claim off the markup it adopts — the reader replays at the
2244-
// scope's release. An errored node throws below as for any reader.
2261+
// A reader in a snapshot scope (a hydration claim): `servedSnapshot`.
22452262
if (snapshotCaptureActive && c && (c as Computed<any>)._config & CONFIG_IN_SNAPSHOT_SCOPE) {
2246-
const sv = el._x?._snapshotValue;
2247-
if (sv !== undefined && !(owner._statusFlags & STATUS_ERROR)) {
2248-
const snapshot = sv === NO_SNAPSHOT ? undefined : sv;
2249-
const current = el._pendingValue !== NOT_PENDING ? el._pendingValue : el._value;
2250-
if (current !== snapshot) (c as Computed<any>)._flags |= REACTIVE_SNAPSHOT_STALE;
2251-
if (__TEST__) shadowActual(SLOT_SNAPSHOT);
2252-
return snapshot as T;
2253-
}
2263+
const v = servedSnapshot(el, c as Computed<any>);
2264+
if (v !== NOT_PENDING) return v as T;
22542265
}
22552266

22562267
// A stale reader of a held flight (rule 3, A15's reveal corollary) is served

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ import {
2222
CONFIG_VERDICT,
2323
EFFECT_RENDER,
2424
NOT_PENDING,
25-
NO_SNAPSHOT,
2625
REACTIVE_DISPOSED,
2726
REACTIVE_FRAME_READ,
2827
REACTIVE_JOINED,
2928
REACTIVE_LANE_READ,
3029
REACTIVE_PROBED,
3130
REACTIVE_REASK,
32-
REACTIVE_SNAPSHOT_STALE,
3331
REACTIVE_PROBE_UNANSWERED,
3432
REACTIVE_STAGED_READ,
3533
REACTIVE_WINDOW,
@@ -46,6 +44,7 @@ import {
4644
markLateLinker,
4745
prepareComputed,
4846
pullComputed,
47+
servedSnapshot,
4948
setVerdict,
5049
snapshotCaptureActive,
5150
spectating,
@@ -80,7 +79,6 @@ import {
8079
SLOT_DV,
8180
SLOT_LV,
8281
SLOT_P,
83-
SLOT_SNAPSHOT,
8482
shadowActual,
8583
shadowExpect,
8684
shadowWindow
@@ -451,25 +449,26 @@ function verdictValue(el: Signal<any> | Computed<any>, c: Computed<any> | null):
451449
latestActive = prevLatest;
452450
probing = prevProbing;
453451
// A reader in a snapshot scope (a hydration claim) is served the value the
454-
// server rendered with, as `read` serves a plain one: `latest(a)` beside
455-
// `a()` in a claim shows the server's frame whole (F-1), not a client
456-
// write's flight the markup never had — which would put a boundary over
457-
// it in a fallback the server never rendered, claimed against nothing
458-
// (#4570). `isPending` answers for that frame: nothing in flight. The
459-
// reader replays at the scope's release.
460-
const sv =
461-
snapshotCaptureActive && c && c._config & CONFIG_IN_SNAPSHOT_SCOPE
462-
? el._x?._snapshotValue
463-
: undefined,
464-
served = sv !== undefined && !(owner._statusFlags & STATUS_ERROR);
452+
// server rendered with, as `read` serves a plain one (`servedSnapshot`):
453+
// `latest(a)` beside `a()` in a claim shows the server's frame whole (F-1),
454+
// not a client write's flight the markup never had — which would put a
455+
// boundary over it in a fallback the server never rendered, claimed
456+
// against nothing (#4570). `isPending` answers for that frame: nothing in
457+
// flight.
458+
const scoped = snapshotCaptureActive && c && c._config & CONFIG_IN_SNAPSHOT_SCOPE;
465459
// Shadow (S0): the window's decision point — the rows Vl*/Vp*.
466-
if (__TEST__) shadowExpect(c, el, tracking, latestActive ? 2 : 1, spectating, served);
467-
if (served) {
468-
const snapshot = sv === NO_SNAPSHOT ? undefined : sv;
469-
if ((el._pendingValue !== NOT_PENDING ? el._pendingValue : el._value) !== snapshot)
470-
c!._flags |= REACTIVE_SNAPSHOT_STALE;
471-
if (__TEST__) shadowActual(SLOT_SNAPSHOT);
472-
return snapshot;
460+
if (__TEST__)
461+
shadowExpect(
462+
c,
463+
el,
464+
tracking,
465+
latestActive ? 2 : 1,
466+
spectating,
467+
!!scoped && el._x?._snapshotValue !== undefined && !(owner._statusFlags & STATUS_ERROR)
468+
);
469+
if (scoped) {
470+
const v = servedSnapshot(el, c!);
471+
if (v !== NOT_PENDING) return v;
473472
}
474473
// A live `affects()` mark on the node or a dependency (affects.ts): not
475474
// final by declaration — the value itself is read as below. The verdict is

0 commit comments

Comments
 (0)