Skip to content

Commit 5a0a5f2

Browse files
authored
Merge pull request #3515 from solidjs/refactor/shared-read-predicates
refactor(signals): shared read predicates — readerSeesCommitted, visibleOverride, one hasActiveOverride
2 parents 590ff3a + 449ef6e commit 5a0a5f2

7 files changed

Lines changed: 458 additions & 422 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
Consolidate value-selection predicates: `readerSeesCommitted` (the full committed-vs-staged arm read()'s slow tail used to inline) and `visibleOverride` / `hasActiveOverride` (one definition each, previously duplicated between the core, lanes, verdict channels and the store) — a zero-semantic-change refactor toward one implementation per rule (DESIGN-CONSOLIDATION, move 3b).

‎packages/signals/docs/RULES-INDEX.md‎

Lines changed: 357 additions & 368 deletions
Large diffs are not rendered by default.

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

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,50 @@ export function enterStagedRead(
16311631
globalQueue.initTransition(t);
16321632
}
16331633

1634+
/**
1635+
* Rule 1 (value selection), the full arm: does this reader see a STAGED
1636+
* node's COMMITTED value? One implementation of the rule the fast paths
1637+
* (readNodeFast, read's fast block) carry as their trivial ternary and that
1638+
* every slow site — read's tail, the store's backing selection, the lane and
1639+
* verdict arms — used to restate by hand (DESIGN-CONSOLIDATION, move 3b). In order:
1640+
* - no reader at all (an untracked read) — the committed frame;
1641+
* - a reader under an optimistic lane the engine says reads committed
1642+
* (laneReadsCommitted: another lane's hold, #3460);
1643+
* - nothing staged;
1644+
* - a children-forbidden reader (createTrackedEffect / onSettled: the frame,
1645+
* never the graph — A32);
1646+
* - a stale reader (render effect) of a FOREIGN transaction's staged write —
1647+
* committed, no entanglement (heldFromStale registers the replay; a node
1648+
* born held has no committed frame to fall back to, `noCommitted`);
1649+
* - A17 for HELD truth (#3164, CONFIG_HELD_TRUTH): staged confirming truth —
1650+
* fold-staged onto an armed family, or entangle-stolen by an awaited
1651+
* until() — is masked from ordinary readers until its transaction's
1652+
* reveal, the retaining transaction's own speculative recomputes included
1653+
* (partial override coverage would otherwise compose override + staged
1654+
* truth into a state no timeline contains). Authoritative readers
1655+
* (until()'s predicate) and latest() see the staged truth — the tunnel that
1656+
* keeps the hold deadlock-free.
1657+
* False means the reader derives from the staged value and enters its
1658+
* transaction (enterStagedRead, A29).
1659+
*/
1660+
export function readerSeesCommitted(
1661+
el: Signal<any> | Computed<any>,
1662+
c: Computed<any> | null,
1663+
owner: Signal<any> | Computed<any>,
1664+
noCommitted: boolean
1665+
): boolean {
1666+
return !!(
1667+
!c ||
1668+
(currentOptimisticLane !== null && GlobalQueue._laneReadsCommitted!(el, owner, c)) ||
1669+
el._pendingValue === NOT_PENDING ||
1670+
c._config & CONFIG_CHILDREN_FORBIDDEN ||
1671+
(stale && !noCommitted && heldFromStale(el, c)) ||
1672+
(el._config & CONFIG_HELD_TRUTH &&
1673+
!latestReadActive &&
1674+
!(c._config & CONFIG_AUTHORITATIVE_READ))
1675+
);
1676+
}
1677+
16341678
/** A28 — set when a node is staged (queuePendingNode) or a held node rewritten
16351679
* (stashHeldRewrite) OUTSIDE a flush; cleared when the next flush begins. The
16361680
* read sites test this one module boolean instead of `globalQueue._running`:
@@ -1685,6 +1729,22 @@ export function unflushedOverride(el: Signal<any> | Computed<any>): boolean {
16851729
// Companions are optimistic signals written by the engine (see unflushed).
16861730
return !globalQueue._running && el._x?._overrideTime === clock && !el._x?._parentSource;
16871731
}
1732+
/** Active optimistic override on an armed node (an armed slot idles at
1733+
* NOT_PENDING; undefined = unarmed plain node). The writer's own channels —
1734+
* the draft, `in`/keys inside the setter — compose on this regardless of
1735+
* flush state. */
1736+
export function hasActiveOverride(el: Signal<any> | Computed<any>): boolean {
1737+
const x = el._x;
1738+
return x !== null && x._overrideValue !== undefined && x._overrideValue !== NOT_PENDING;
1739+
}
1740+
/** The override a READER sees: installed, and carried by a flush (A28 (5) —
1741+
* an optimistic write is a write; until its flush no reader sees it). One
1742+
* implementation for read()'s override arm, the verdict channels
1743+
* (latestRead, computePendingState) and the store's selection
1744+
* (DESIGN-CONSOLIDATION, move 3b). */
1745+
export function visibleOverride(el: Signal<any> | Computed<any>): boolean {
1746+
return hasActiveOverride(el) && !unflushedOverride(el);
1747+
}
16881748
/** A derivation served the committed value because of an unflushed write
16891749
* (A28) must run again in the flush that carries it — the late-linker case
16901750
* (#3337's reason to defer the walk): it linked after the write walked. */
@@ -1933,7 +1993,7 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
19331993
nodeName: (owner as any)?._name
19341994
});
19351995

1936-
if (el._x?._overrideValue !== undefined && el._x?._overrideValue !== NOT_PENDING) {
1996+
if (hasActiveOverride(el)) {
19371997
// A17: the override IS the value for every reader — except an authoritative
19381998
// reader (until()'s predicate carries CONFIG_AUTHORITATIVE_READ): it must
19391999
// observe independently-arriving truth, and serving it the caller's own
@@ -1997,26 +2057,9 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
19972057
if (pendingCheckActive) GlobalQueue._recordFresh!(el, u);
19982058
return u as T;
19992059
}
2000-
const value =
2001-
!c ||
2002-
(currentOptimisticLane !== null &&
2003-
GlobalQueue._laneReadsCommitted!(el, owner, c as Computed<any>)) ||
2004-
el._pendingValue === NOT_PENDING ||
2005-
c._config & CONFIG_CHILDREN_FORBIDDEN ||
2006-
(stale && !noCommitted && heldFromStale(el, c as Computed<any>)) ||
2007-
// A17 for HELD truth (#3164, see CONFIG_HELD_TRUTH): staged confirming
2008-
// truth — fold-staged onto an armed family, or entangle-stolen by an
2009-
// awaited until() — is masked from ordinary readers until its
2010-
// transaction's reveal; the retaining transaction's own speculative
2011-
// recomputes included (partial override coverage would otherwise
2012-
// compose override + staged truth into a state no timeline contains).
2013-
// Authoritative readers (until()'s predicate) and latest() see the
2014-
// staged truth — the tunnel that keeps the hold deadlock-free.
2015-
(el._config & CONFIG_HELD_TRUTH &&
2016-
!latestReadActive &&
2017-
!((c as Computed<any>)._config & CONFIG_AUTHORITATIVE_READ))
2018-
? el._value
2019-
: (enterStagedRead(el), el._pendingValue as T);
2060+
const value = readerSeesCommitted(el, c as Computed<any> | null, owner, noCommitted)
2061+
? el._value
2062+
: (enterStagedRead(el), el._pendingValue as T);
20202063
// Record that this isPending() probe observed the fresh pending value, so
20212064
// the probe doesn't pair "pending" with the new value (#2831).
20222065
if (pendingCheckActive) GlobalQueue._recordFresh!(el, value);

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
NOT_PENDING,
55
REACTIVE_DISPOSED
66
} from "./constants.js";
7-
import { currentOptimisticLane, ext } from "./core.js";
7+
import { currentOptimisticLane, ext, hasActiveOverride } from "./core.js";
8+
export { hasActiveOverride };
89
import { enqueueSub } from "./heap.js";
910
import {
1011
activeTransition,
@@ -201,14 +202,6 @@ export function resolveTransition(el: Signal<any> | Computed<any>): Transition |
201202
return resolveLane(el)?._transition ?? el._transition;
202203
}
203204

204-
/**
205-
* Check if a node has an active optimistic override.
206-
*/
207-
export function hasActiveOverride(el: Signal<any> | Computed<any>): boolean {
208-
const x = el._x;
209-
return x !== null && x._overrideValue !== undefined && x._overrideValue !== NOT_PENDING;
210-
}
211-
212205
/**
213206
* Assign or merge a lane onto a node. At convergence points (node already has
214207
* a different active lane), merge unless the node has an active override.

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
setSignal,
3838
unflushed,
3939
unflushedCompanions,
40-
unflushedOverride,
40+
visibleOverride,
4141
unflushedValue,
4242
setStrictRead,
4343
stale,
@@ -292,8 +292,7 @@ function computePendingState(el: Signal<any> | Computed<any>): boolean {
292292
if (
293293
el._config & CONFIG_OVERRIDE_SUPERSEDED &&
294294
el._pendingValue === NOT_PENDING &&
295-
hasActiveOverride(el) &&
296-
!unflushedOverride(el)
295+
visibleOverride(el)
297296
)
298297
return !el._equals || !el._equals(el._value as any, unwrapOverride(el._x?._overrideValue));
299298
// A28 (2): an unflushed write is not yet observable — the verdict answers
@@ -306,7 +305,7 @@ function computePendingState(el: Signal<any> | Computed<any>): boolean {
306305
// non-final"; an override is one (a node whose first landing was held
307306
// by a reveal it never got to commit, then superseded under its
308307
// override, read false here).
309-
if (hasActiveOverride(el) && !unflushedOverride(el))
308+
if (visibleOverride(el))
310309
return !el._equals || !el._equals(staged as any, unwrapOverride(el._x?._overrideValue));
311310
// A quiet re-ask's held landing still answers the same question: the
312311
// classification survives the landing (asyncWrite) and dies with the
@@ -503,9 +502,7 @@ function latestRead<T>(el: Signal<T> | Computed<T>): T {
503502
const prevPending = latestReadActive;
504503
setLatestReadActive(false);
505504
const visibleValue = (
506-
hasActiveOverride(el) && !unflushedOverride(el)
507-
? unwrapOverride(el._x?._overrideValue)
508-
: el._value
505+
visibleOverride(el) ? unwrapOverride(el._x?._overrideValue) : el._value
509506
) as T;
510507
// A28: an unflushed write is not the staged value latest() serves. The
511508
// shadow was written at the source's write to mirror it (A8) — consult it

‎packages/signals/src/store/next/store.ts‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import {
3636
isEqual,
3737
latestReadActive,
3838
stale,
39-
unflushedOverride,
39+
hasActiveOverride,
40+
visibleOverride,
4041
prepareComputed,
4142
read as readNode,
4243
READ_SLOW,
@@ -1757,18 +1758,7 @@ export function runAuthoritative<T>(fn: () => T): T {
17571758
}
17581759
}
17591760

1760-
/** Active optimistic override on an armed node (armed slot idles at
1761-
* NOT_PENDING; undefined = unarmed plain node). */
1762-
export function hasActiveOverride(node: Signal<any>): boolean {
1763-
return node._x?._overrideValue !== undefined && node._x?._overrideValue !== NOT_PENDING;
1764-
}
1765-
/** The override a READER sees: installed, and carried by a flush (A28 (5) —
1766-
* an optimistic write is a write; until its flush no reader sees it). The
1767-
* writer's own channels (the draft, `in`/keys inside the setter) compose on
1768-
* the installed override regardless — they use hasActiveOverride. */
1769-
export function visibleOverride(node: Signal<any>): boolean {
1770-
return hasActiveOverride(node) && !unflushedOverride(node);
1771-
}
1761+
export { hasActiveOverride, visibleOverride };
17721762

17731763
/** The reading computation is until()'s authoritative-view predicate — same
17741764
* source of truth as core read()'s A17 carve-out (`context`, which persists
@@ -1806,7 +1796,7 @@ function nodeValue(node: Signal<any>, backing: any): any {
18061796
// only: staged pending values are authoritative, overrides are the
18071797
// caller's optimism.
18081798
const v =
1809-
!authoritativeServe() && hasActiveOverride(node) && !unflushedOverride(node)
1799+
!authoritativeServe() && visibleOverride(node)
18101800
? unwrapOverride(node._x?._overrideValue)
18111801
: node._pendingValue !== NOT_PENDING &&
18121802
(latestReadActive ||

‎scripts/size/.size-limit.js‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@ module.exports = [
539539
// Hydration claim-path trim (#3513, 2026-09-17): measured at 16,517 B against
540540
// current `next`'s 16,495 (+22). The fixed-shape `_snapshotValue` cleanup
541541
// replaces `delete` with assignment; the pure core floor shrinks by 1 B.
542+
// Shared read predicates (DESIGN-CONSOLIDATION move 3b step 1, 2026-09-17):
543+
// readerSeesCommitted / visibleOverride / one hasActiveOverride. Minified
544+
// signals: core +13 B, +createStore -38 B, full bundle -72 B; brotli on the
545+
// pure-signals fixtures -4 / -29 / -5 B. This scenario's esbuild bundle
546+
// measured at 16,509 B rebased over #3507, against `next`'s 16,517 (-8 B).
542547
limit: "16.55 KB",
543548
modifyEsbuildConfig
544549
},
@@ -925,7 +930,12 @@ module.exports = [
925930
// hydrating body that marks the snapshot root; a resume window records its
926931
// boundary owner and `sharedConfig.isClaiming` walks `_parent` to it;
927932
// @solidjs/web's isHydrating consults it. 0 B in the signals floor.
928-
limit: "20.05 KB",
933+
// Shared read predicates (DESIGN-CONSOLIDATION move 3b step 1, 2026-09-17):
934+
// readerSeesCommitted / visibleOverride / one hasActiveOverride. Minified
935+
// signals: core +13 B, +createStore -38 B, full bundle -72 B; brotli on the
936+
// pure-signals fixtures -4 / -29 / -5 B. This scenario's esbuild bundle
937+
// measured at 20,054 B rebased over #3507, against `next`'s 20,044 (+10 B).
938+
limit: "20.10 KB",
929939
modifyEsbuildConfig
930940
},
931941
{
@@ -1229,7 +1239,12 @@ module.exports = [
12291239
// 15,170 B before the #3496 rebase (+30 over its base); 0 B minified in
12301240
// the signals floor (24,578 flat). Combined with `_parent` mangling:
12311241
// 15,250 B; cap ratcheted to the measured output.
1232-
limit: "15.25 KB",
1242+
// Shared read predicates (DESIGN-CONSOLIDATION move 3b step 1, 2026-09-17):
1243+
// readerSeesCommitted / visibleOverride / one hasActiveOverride. Minified
1244+
// signals: core +13 B, +createStore -38 B, full bundle -72 B; brotli on the
1245+
// pure-signals fixtures -4 / -29 / -5 B. This scenario's esbuild bundle
1246+
// measured at 15,251 B rebased over #3507, against `next`'s 15,238 (+13 B).
1247+
limit: "15.30 KB",
12331248
modifyEsbuildConfig
12341249
},
12351250
{
@@ -1472,7 +1487,11 @@ module.exports = [
14721487
// consumer (enable, subscribe, formatRerun) and ships none of them. The
14731488
// formatters stay in the engine because the `log` option prints through
14741489
// them. Ratcheted down to pin the reduction.
1475-
limit: "27.25 KB",
1490+
// Shared read predicates (DESIGN-CONSOLIDATION move 3b step 1, 2026-09-17):
1491+
// measured at 27,256 B rebased over #3507, against `next`'s 27,196 (+60 B).
1492+
// The source change is in the signals core; this scenario's attribution
1493+
// modules only alter the compressor layout.
1494+
limit: "27.30 KB",
14761495
modifyEsbuildConfig: observeEsbuildConfig
14771496
},
14781497
{

0 commit comments

Comments
 (0)