Skip to content

Commit d94d5c3

Browse files
ryansolidclaude
andauthored
feat(signals,solid): pay-for-use tree-shaking phase 2 (#2883) (#2889)
Optimistic write engine detaches from the core floor: override writes, lane routing/suspension, stashed-optimistic reads, transition-completion blockage and optimistic-node resolution move to core/optimistic.ts behind 14 nullable GlobalQueue hooks, installed by verdict.ts (companions are optimistic nodes) and at first createOptimistic/createOptimisticStore call. Core call sites guard on state only the engine creates; the A17 override-is-the-value read return stays inline. Differential smoke is byte-identical baseline vs hooked. createLoadingBoundary's hydration-resume machinery installs through the existing enableHydration() seam (same dispatcher pattern as the other hydrated overrides), so CSR bundles shake it; hydrate() opt-in costs +43 min bytes. Measured (esbuild, minify, mangle-props ^_, gzip -9): - core floor from src: 20,404/8,248 -> 18,921/7,683 min/gz - plain-store subset: 33,740/13,039 -> 32,230/12,445 - minimal app from published dist: 29,081/11,461 -> 27,570/10,866 - CSR app using <Loading>: -2,469 min / -899 gz (controlled fixture) - full flat bundle: +889 min hook tax (chunked prod dist unaffected) Tree-shake guard extended: optimistic.ts must be absent from floor and plain-store fixtures, present with createOptimistic; floor ceiling tightened to 19.5 KB. Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 1dc0b45 commit d94d5c3

11 files changed

Lines changed: 498 additions & 258 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@solidjs/signals": patch
3+
"solid-js": patch
4+
---
5+
6+
Pay-for-use tree-shaking, phase 2 (#2883). The optimistic write engine (override writes, lane routing/suspension, stashed-optimistic reads, transition-completion blockage, optimistic-node resolution) moves out of `core.ts`/`scheduler.ts`/`lanes.ts` into a new internal `optimistic` module behind fourteen nullable `GlobalQueue` hooks, installed by the verdict layer and at first `createOptimistic`/`createOptimisticStore` call. Every core call site is gated on state only the engine can create (an `_overrideValue` slot, a live lane, a non-empty optimistic batch), and the A17 override-is-the-value read path stays inline in core. On the `solid-js` side, `createLoadingBoundary`'s hydration-resume machinery (boundary triggers, resume scheduling, asset-failure reporting, snapshot capture) now installs through the existing `enableHydration()` seam, so client-only apps stop shipping it.
7+
8+
Measured (esbuild, minify, `_`-prop mangling, gzip -9): core floor 8.2 → 7.7 KB gzip; plain-store subset 13.0 → 12.4 KB; minimal app from published dist 11.5 → 10.9 KB; a CSR app using `<Loading>` drops a further ~0.9 KB gzip; opting into `hydrate()` costs +43 min bytes. Cumulative with phase 1, the minimal-app floor is down ~14% and the signals floor ~13.5% with no behavioral change — differential smoke runs are byte-identical and the full Tier-A suite passes unchanged.

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

Lines changed: 40 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,14 @@ import {
4242
markHeap,
4343
markNode
4444
} from "./heap.js";
45-
import {
46-
findLane,
47-
getOrCreateLane,
48-
hasActiveOverride,
49-
resolveLane,
50-
resolveTransition,
51-
type OptimisticLane
52-
} from "./lanes.js";
45+
import { type OptimisticLane } from "./lanes.js";
5346
import { clearSignals, DEV, emitDiagnostic } from "./dev.js";
54-
import { devTrackHeldPending, devTrackOptimistic } from "./invariants.js";
47+
import { devTrackHeldPending } from "./invariants.js";
5548
import { cleanup, disposeChildren, getNextChildId, markDisposal } from "./owner.js";
5649
import {
5750
activeAffectsMarks,
5851
activeTransition,
5952
armReaskClear,
60-
assignOrMergeLane,
6153
clock,
6254
dirtyQueue,
6355
globalQueue,
@@ -67,7 +59,6 @@ import {
6759
queuePendingNode,
6860
runInTransition,
6961
schedule,
70-
shouldReadStashedOptimisticValue,
7162
zombieQueue
7263
} from "./scheduler.js";
7364
import type { Computed, FirewallSignal, Link, NodeOptions, Owner, Root, Signal } from "./types.js";
@@ -220,25 +211,20 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
220211
strictRead = false;
221212
}
222213
tracking = true;
214+
// Lane posture lives with the engine: OPTIMISTIC_DIRTY is only ever set by
215+
// engine-driven paths, and _optimisticNodes is only pushed by
216+
// _optimisticWrite, so the hook is installed whenever either gate holds.
223217
if (isOptimisticDirty) {
224-
const lane = resolveLane(el);
218+
const lane = GlobalQueue._recomputeLane!(el, true);
225219
if (lane) currentOptimisticLane = lane;
226220
} else if (activeTransition && !create && activeTransition._optimisticNodes.length) {
227221
// Lane adoption: parent-deeper-than-owned-child can run before its OPT-dirty
228222
// child propagates. Walk deps once and inherit the OPT lane so this node
229223
// recomputes under the right posture and propagates correctly.
230-
for (let d: Link | null = el._deps; d; d = d._nextDep) {
231-
const dep = d._dep as Computed<any>;
232-
if (dep._flags & REACTIVE_OPTIMISTIC_DIRTY) {
233-
const depLane = resolveLane(dep);
234-
if (depLane) {
235-
isOptimisticDirty = true;
236-
currentOptimisticLane = depLane;
237-
el._flags |= REACTIVE_OPTIMISTIC_DIRTY;
238-
assignOrMergeLane(el as any, depLane);
239-
break;
240-
}
241-
}
224+
const lane = GlobalQueue._recomputeLane!(el, false);
225+
if (lane) {
226+
isOptimisticDirty = true;
227+
currentOptimisticLane = lane;
242228
}
243229
}
244230
const isStaleEffect = isEffect && isEffect !== EFFECT_USER;
@@ -262,25 +248,12 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
262248
if (!inFlightChanged && !isAsyncResult) el._inFlight = null;
263249
}
264250
clearStatus(el, create);
265-
if (el._optimisticLane) {
266-
const resolvedLane = resolveLane(el);
267-
if (resolvedLane) {
268-
resolvedLane._pendingAsync.delete(el);
269-
GlobalQueue._updatePendingSignal !== null &&
270-
GlobalQueue._updatePendingSignal(resolvedLane._source);
271-
}
272-
}
251+
// _optimisticLane is only ever assigned by engine paths.
252+
if (el._optimisticLane) GlobalQueue._laneAsyncSettled!(el);
273253
} catch (e) {
274254
// Track pending async in the lane (not the lane's source — it creates the lane
275255
// but doesn't belong to it). Set lane BEFORE notifyStatus for downstream propagation.
276-
if (e instanceof NotReadyError && currentOptimisticLane) {
277-
const lane = findLane(currentOptimisticLane);
278-
if (lane._source !== el) {
279-
lane._pendingAsync.add(el);
280-
el._optimisticLane = lane;
281-
GlobalQueue._updatePendingSignal !== null && GlobalQueue._updatePendingSignal(lane._source);
282-
}
283-
}
256+
if (e instanceof NotReadyError && currentOptimisticLane) GlobalQueue._laneAsyncPending!(el);
284257
let reaskChanged = false;
285258
if (e instanceof NotReadyError) {
286259
el._blocked = true;
@@ -838,17 +811,10 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
838811
});
839812
console.warn(message);
840813
}
841-
if (currentOptimisticLane) {
842-
// Per-lane suspension: only throw if in same lane as pending async
843-
// AND the node doesn't have an active override (overrides are the visible value,
844-
// downstream in the lane should read the override, not throw)
845-
const pendingLane = (owner as any)._optimisticLane;
846-
const lane = findLane(currentOptimisticLane);
847-
if (pendingLane && findLane(pendingLane) === lane && !hasActiveOverride(owner)) {
848-
if (!tracking && el !== c) link(el, c as Computed<any>);
849-
throw owner._error;
850-
}
851-
} else {
814+
// Per-lane suspension lives with the engine (a non-null lane implies it
815+
// is installed): under a lane, only same-lane pending async without an
816+
// active override throws.
817+
if (currentOptimisticLane === null || GlobalQueue._laneSuspends!(owner)) {
852818
if (!tracking && el !== c) link(el, c as Computed<any>);
853819
throw owner._error;
854820
}
@@ -898,7 +864,9 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
898864
}
899865

900866
if (el._overrideValue !== undefined && el._overrideValue !== NOT_PENDING) {
901-
if (c && stale && shouldReadStashedOptimisticValue(el as Signal<any>)) return el._value as T;
867+
// An active override means the engine is installed (A17: the override IS
868+
// the value for every reader — that check itself stays right here).
869+
if (c && stale && GlobalQueue._readStashed!(el as Signal<any>)) return el._value as T;
902870
return el._overrideValue as T;
903871
}
904872

@@ -907,35 +875,25 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
907875
// manual writes use the firewall's manual-write flag to opt into this path.
908876
// Async drivers are not under an optimistic lane and so bypass this, reading
909877
// _pendingValue for correct fetching. The sub is recorded for replay at commit
910-
// so it re-runs with the new committed view.
878+
// so it re-runs with the new committed view. (Gate details live with the
879+
// engine — a non-null lane implies it is installed.)
911880
if (
912-
activeTransition !== null &&
913881
currentOptimisticLane !== null &&
914-
!latestReadActive &&
915-
el._pendingValue !== NOT_PENDING &&
916-
(owner === el || !!((owner as Computed<unknown>)._flags & REACTIVE_MANUAL_WRITE)) &&
917-
!(el as Partial<Computed<unknown>>)._fn &&
918-
c
882+
activeTransition !== null &&
883+
c !== null &&
884+
GlobalQueue._gatedRead!(el as Signal<any>, owner, c as Computed<any>)
919885
) {
920-
activeTransition._gatedSubs.add(c as Computed<any>);
921886
return el._value as T;
922887
}
923888

924889
// In optimistic lane context, return _value for optimistic/lane-assigned signals
925890
// and for regular signals in stale mode (render effects). Non-stale readers (user
926891
// effects) see _pendingValue so that latest() and direct reads stay consistent.
927-
// Exception: resolved projection store properties (firewall, owner !== el) whose
928-
// STATUS_PENDING has been cleared always return _pendingValue.
929-
// The latest() shadow computed (`c._parentSource === el`) always wants the
930-
// in-flight value: it may recompute under a stale/lane context inherited from
931-
// whichever flush ran it, and must not cache the committed value there (#2829).
892+
// (The lane-context clause lives with the engine.)
932893
const value =
933894
!c ||
934895
(currentOptimisticLane !== null &&
935-
(el._overrideValue !== undefined ||
936-
(el as any)._optimisticLane ||
937-
(owner === el && stale && (c as Computed<any>)._parentSource !== el) ||
938-
!!(owner._statusFlags & STATUS_PENDING))) ||
896+
GlobalQueue._laneReadsCommitted!(el, owner, c as Computed<any>)) ||
939897
el._pendingValue === NOT_PENDING ||
940898
(stale && el._transition && activeTransition !== el._transition)
941899
? el._value
@@ -980,15 +938,14 @@ export function setSignal<T>(el: Signal<T> | Computed<T>, v: T | ((prev: T) => T
980938
if (el._transition && activeTransition !== el._transition)
981939
globalQueue.initTransition(el._transition);
982940

983-
const isOptimistic = el._overrideValue !== undefined && !projectionWriteActive;
984-
const hasOverride = el._overrideValue !== undefined && el._overrideValue !== NOT_PENDING;
985-
const currentValue = isOptimistic
986-
? hasOverride
987-
? (el._overrideValue as T)
988-
: el._value
989-
: el._pendingValue === NOT_PENDING
990-
? el._value
991-
: (el._pendingValue as T);
941+
// The optimistic write path lives with the engine: only optimisticSignal /
942+
// optimisticComputed callers and optimistic store nodes carry an
943+
// _overrideValue slot, and every module that creates one installs the
944+
// engine first.
945+
if (el._overrideValue !== undefined && !projectionWriteActive)
946+
return GlobalQueue._optimisticWrite!(el, v);
947+
948+
const currentValue = el._pendingValue === NOT_PENDING ? el._value : (el._pendingValue as T);
992949

993950
if (typeof v === "function") v = (v as (prev: T) => T)(currentValue);
994951

@@ -998,39 +955,16 @@ export function setSignal<T>(el: Signal<T> | Computed<T>, v: T | ((prev: T) => T
998955
!!((el as Computed<T>)._statusFlags & STATUS_UNINITIALIZED) ||
999956
!el._equals ||
1000957
!el._equals(currentValue, v);
1001-
if (!valueChanged) {
1002-
// Same-value write with an active override still entangles the current
1003-
// action's transition — the hold must outlast all overlapping actions.
1004-
if (isOptimistic && hasOverride) {
1005-
const transition = resolveTransition(el as any);
1006-
if (transition && activeTransition !== transition) globalQueue.initTransition(transition);
1007-
}
1008-
return v;
1009-
}
1010-
1011-
if (isOptimistic) {
1012-
const firstOverride = el._overrideValue === NOT_PENDING;
1013-
if (!firstOverride) globalQueue.initTransition(resolveTransition(el as any));
1014-
// No revert target is stashed: while the override is active every reader
1015-
// sees it (A17), so authoritative arrivals commit silently into _value and
1016-
// reverting is just dropping the override — _value is already correct.
1017-
if (firstOverride) globalQueue._optimisticNodes.push(el);
958+
if (!valueChanged) return v;
1018959

1019-
const lane = getOrCreateLane(el);
1020-
el._optimisticLane = lane;
1021-
1022-
el._overrideValue = v;
1023-
if (__DEV__) devTrackOptimistic(el);
1024-
} else {
1025-
if (el._pendingValue === NOT_PENDING) queuePendingNode(el);
1026-
el._pendingValue = v;
1027-
if (__DEV__) devTrackHeldPending(el);
1028-
}
960+
if (el._pendingValue === NOT_PENDING) queuePendingNode(el);
961+
el._pendingValue = v;
962+
if (__DEV__) devTrackHeldPending(el);
1029963

1030964
GlobalQueue._syncCompanions !== null && GlobalQueue._syncCompanions(el, v);
1031965

1032966
el._time = clock;
1033-
insertSubs(el, isOptimistic);
967+
insertSubs(el);
1034968
schedule();
1035969
return v;
1036970
}

‎packages/solid-signals/src/core/index.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ export {
6161
flush,
6262
Queue,
6363
GlobalQueue,
64-
trackOptimisticStore,
6564
enforceLoadingBoundary,
6665
resetErrorHalt,
6766
type IQueue,

0 commit comments

Comments
 (0)