Skip to content

Commit d0b9c91

Browse files
ryansolidclaude
andauthored
feat(signals,solid): pay-for-use tree-shaking phase 3 — audited cleanups (#2883) (#2890)
* feat(signals,solid): pay-for-use tree-shaking phase 3 (#2883) Mechanical cleanups chosen by the phase-3 cost/benefit audit (delegation seam and snapshot seam rejected by design ruling / weight gate): signals: - dedupe effect re-enqueue block (4 copies) + zombie/dirty queue ternaries into enqueueSub/queueFor; hot write loop microbenched, no regression - _-prefix boundary/reveal internal method names so property mangling reaches them (verified internal-only) - trim prod error strings to diagnostic codes; dev keeps full sentences - stop stripping /*@__PURE__*/ from dist/prod: drop rollup-plugin-prettier from the prod tree (it deletes annotations), terser preserve_annotations in mangle-props.mjs, new scripts/check-pure.mjs fails the build if annotations vanish again solid-js client: - sharedConfig.getNextContextId installs from enableHydration(); defining it in the object literal retained context/error machinery in every CSR bundle (SharedConfig.getNextContextId now optional) - lazy()'s hydration-module lookup moves behind _lazyHydrationLookup, installed by enableHydration() - MockPromise: class static block defeated DCE in every client bundle; now a PURE-annotated factory packaging: unpkg/jsdelivr fields point at browser prod ESM (were CJS, browser-fatal on bare CDN URLs) Measured (esbuild, minify, mangle-props ^_, gzip -9): - minimal app: 27,570/10,870 -> 26,429/10,432 - CSR app (+Loading/lazy): 35,565/13,935 -> 32,067/12,601 (incl. DOMElements dedup propagating through rebuilt web runtime) - signals floor: 18,921/7,687 -> 18,539/7,515 - full bundle: 55,366/20,587 -> 54,587/20,315 (hook-tax clawback) Co-Authored-By: Claude Fable 5 <[email protected]> * feat(signals): close esbuild-shakeable gaps at the source level (#2883) Rollup literal-tracks never-written module state and folds the dead branches; esbuild cannot, so these paths shipped only to esbuild-class consumers. Restructured so both bundlers land in the same place, all Rollup-neutral by measurement: - external-source wiring (computed setup + untrack) moves behind GlobalQueue hooks whose installs mirror enableExternalSource()'s config liveness exactly (installed on enable, removed on reset) - onlyMarkPending moves from async.ts into affects.ts behind the activeAffectsMarks-gated hook (mark-transparency is affects-only) - the optimistic-store settle loop moves inside the store-side hook (_clearOptimisticStores takes the batch; scheduler keeps a size-guarded call) esbuild minimal app: -77 gz from these + -194 gz from the companion dom-expressions hydration-hook change (ryansolid/dom-expressions#544). Co-Authored-By: Claude Fable 5 <[email protected]> --------- Co-authored-by: Claude Fable 5 <[email protected]>
1 parent d94d5c3 commit d0b9c91

22 files changed

Lines changed: 317 additions & 204 deletions
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@solidjs/signals": patch
3+
"solid-js": patch
4+
---
5+
6+
Pay-for-use tree-shaking, phase 3 (#2883) — mechanical cleanups selected by cost/benefit audit. Signals: the effect re-enqueue block (four copies) and zombie/dirty queue selection dedupe into shared `enqueueSub`/`queueFor` helpers (hot-path microbenched, no regression); boundary/reveal internal method names are `_`-prefixed so property mangling reaches them; production error strings trim to their diagnostic codes (dev builds keep full sentences); and the prod dist build stops stripping `/*@__PURE__*/` annotations — rollup-plugin-prettier is off the prod tree, terser re-emits annotations, and a new `check-pure` build guard fails the build if they ever vanish again. solid-js client: `sharedConfig.getNextContextId` and `lazy()`'s hydration-module lookup install from `enableHydration()` instead of shipping in every CSR bundle, and MockPromise's class static block (which defeated dead-code elimination in every client bundle) becomes a PURE-annotated factory. CDN `unpkg`/`jsdelivr` fields now point at browser production ESM instead of CommonJS files.
7+
8+
A last-mile pass closes part of the esbuild-vs-Rollup shaking gap at the source level (Rollup's literal tracking folds never-written state that esbuild retains): the external-source wiring in computed setup and `untrack` moves behind hooks that mirror `enableExternalSource()`'s config liveness, the affects-only `onlyMarkPending` read-path helper moves into the affects module, and the optimistic-store settle loop moves inside its store-side hook. All are Rollup-neutral by measurement.
9+
10+
Measured: minimal app 10.9 → 10.3 KB gzip under esbuild and **9.8 KB gzip / 9.0 KB brotli under Vite** (Solid's default toolchain — under the 10 KB mark); CSR app with `<Loading>`/`lazy` 13.9 → 12.6 KB; signals floor 7.7 → 7.4 KB; and the full-featured bundle *shrinks* ~330 gzip bytes, recovering a third of the phase-1/2 hook indirection tax. Cumulative across all three phases the minimal app is down ~19% with all 1,941 tests across the affected packages passing unchanged.

‎packages/solid-signals/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
},
1616
"main": "./dist/node.cjs",
1717
"module": "./dist/prod/index.js",
18+
"unpkg": "./dist/prod/index.js",
19+
"jsdelivr": "./dist/prod/index.js",
1820
"types": "./dist/types/index.d.ts",
1921
"type": "module",
2022
"sideEffects": false,
@@ -40,7 +42,7 @@
4042
"scripts": {
4143
"build": "npm-run-all -nl build:* && pnpm types",
4244
"build:clean": "rimraf dist/dev dist/prod dist/node dist/dev.js dist/prod.js dist/node.cjs",
43-
"build:js": "rollup -c && node ./scripts/mangle-props.mjs dist/prod dist/node.cjs",
45+
"build:js": "rollup -c && node ./scripts/mangle-props.mjs dist/prod dist/node.cjs && node ./scripts/check-pure.mjs dist/prod",
4446
"types": "tsc -p tsconfig.build.json && node ../../scripts/sync-dual-types.mjs ./dist/types ./dist/types-cjs",
4547
"test": "vitest run",
4648
"test:watch": "vitest watch tests",

‎packages/solid-signals/rollup.config.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ export default [
6363
target: "esnext",
6464
moduleResolution: "bundler",
6565
verbatimModuleSyntax: true
66-
}),
67-
prettier({
68-
parser: "typescript"
6966
})
67+
// NO prettier on the prod tree: rollup-plugin-prettier strips
68+
// /*@__PURE__*/ annotations, silently disabling consumer-side DCE of
69+
// annotated initializers. The mangle-props post-pass beautifies this
70+
// output anyway (and must run terser with preserve_annotations).
7071
]
7172
},
7273
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Build guard: the prod dist tree must retain /*@__PURE__*\/ annotations.
4+
* Two stages have silently stripped them before — rollup-plugin-prettier
5+
* (removed from the prod config) and terser without preserve_annotations —
6+
* and losing them disables consumer-side DCE of annotated initializers
7+
* without failing anything (#2883 phase 3). Fails the build if the tree
8+
* contains none while src contains some.
9+
*
10+
* Usage: node scripts/check-pure.mjs <dist-dir>
11+
*/
12+
import { readdirSync, readFileSync, statSync } from "node:fs";
13+
import { join } from "node:path";
14+
15+
function count(dir) {
16+
if (statSync(dir).isFile()) return (readFileSync(dir, "utf8").match(/@__PURE__/g) || []).length;
17+
let n = 0;
18+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
19+
const path = join(dir, entry.name);
20+
if (entry.isDirectory()) n += count(path);
21+
else if (/\.(js|cjs|ts)$/.test(entry.name)) n += count(path);
22+
}
23+
return n;
24+
}
25+
26+
const [dist] = process.argv.slice(2);
27+
const inSrc = count("src");
28+
const inDist = count(dist);
29+
if (inSrc > 0 && inDist === 0) {
30+
console.error(
31+
`check-pure: src has ${inSrc} /*@__PURE__*/ annotation(s) but ${dist} has none — ` +
32+
`a build stage is stripping them (prettier on the prod config, or terser without ` +
33+
`format.preserve_annotations).`
34+
);
35+
process.exit(1);
36+
}
37+
console.log(`check-pure: ${inDist} annotation(s) present in ${dist} (src has ${inSrc})`);

‎packages/solid-signals/scripts/mangle-props.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ for (const dir of process.argv.slice(2)) {
4242
module: false,
4343
properties: { regex: /^_/ }
4444
},
45-
format: { beautify: true, comments: true }
45+
// preserve_annotations: terser consumes /*@__PURE__*/ during parse and
46+
// only re-emits it when asked — without this the prod tree loses the
47+
// annotations that consumer bundlers rely on for DCE (#2883 phase 3).
48+
format: { beautify: true, comments: true, preserve_annotations: true }
4649
});
4750
writeFileSync(file, result.code);
4851
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,23 @@ function releaseAffectsMarks(nodes: MarkedNode[]): void {
158158
nodes.length = 0;
159159
}
160160

161+
/**
162+
* True when a node's pending status comes ONLY from affects() sentinels. A
163+
* mark is a promise of change, not an absence of value: reads of mark-pended
164+
* derived nodes stay value-transparent (verdicts report pending; the read
165+
* path must not suspend). Any real async source among the pending sources
166+
* keeps normal suspension semantics. Core's read path reaches this through
167+
* `GlobalQueue._onlyMarkPending`, gated on the `activeAffectsMarks` counter.
168+
*/
169+
function onlyMarkPending(el: Computed<any>): boolean {
170+
const sources = el._pendingSources;
171+
if (sources) {
172+
for (const s of sources) if (!s._affectsFor) return false;
173+
return true;
174+
}
175+
return !!el._pendingSource?._affectsFor;
176+
}
177+
161178
// Late installation (same pattern as `GlobalQueue._update`): the mark engine
162179
// lives with the feature so graphs that never declare a mark never ship it.
163180
// Each call site is gated by state only this module creates (`markedReads`
@@ -168,6 +185,7 @@ GlobalQueue._applyAffectsReads = applyAffectsReads;
168185
GlobalQueue._releaseAffectsMarks = releaseAffectsMarks;
169186
GlobalQueue._markAffects = markAffects;
170187
GlobalQueue._releaseAffectsMark = releaseAffectsMark;
188+
GlobalQueue._onlyMarkPending = onlyMarkPending;
171189

172190
/**
173191
* Declares that in-flight work will change the targeted data: the named

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ function isRevealController(slot: RevealSlot): slot is RevealController {
9999
}
100100

101101
function isSlotReady(slot: RevealSlot): boolean {
102-
return isRevealController(slot) ? slot.isReady() : slot._sources.size === 0 && !slot._pending;
102+
return isRevealController(slot) ? slot._isReady() : slot._sources.size === 0 && !slot._pending;
103103
}
104104

105105
function isSlotMinimallyReady(slot: RevealSlot): boolean {
106-
return isRevealController(slot) ? slot.isMinimallyReady() : isSlotReady(slot);
106+
return isRevealController(slot) ? slot._isMinimallyReady() : isSlotReady(slot);
107107
}
108108

109109
function setSlotState(
@@ -116,7 +116,7 @@ function setSlotState(
116116
setSignal(slot._collapsed, collapsed);
117117
if (isRevealController(slot)) {
118118
if (!disabled && slot._parentController === controller) slot._parentController = undefined;
119-
return slot.evaluate(disabled, collapsed);
119+
return slot._evaluate(disabled, collapsed);
120120
}
121121
if (!disabled && slot._revealController === controller && slot._initialized)
122122
slot._revealController = undefined;
@@ -148,7 +148,7 @@ export class RevealController {
148148
return true;
149149
}
150150

151-
isReady(): boolean {
151+
_isReady(): boolean {
152152
return this._forEachOwnedSlot(isSlotReady);
153153
}
154154

@@ -159,7 +159,7 @@ export class RevealController {
159159
* - `sequential`: the first owned slot is minimally ready (frontier can advance).
160160
* - `natural`: any owned slot is minimally ready.
161161
*/
162-
isMinimallyReady(): boolean {
162+
_isMinimallyReady(): boolean {
163163
const order = untrack(this._orderAccessor);
164164
if (order === "together") return this._forEachOwnedSlot(isSlotMinimallyReady);
165165
if (order === "natural") {
@@ -183,7 +183,7 @@ export class RevealController {
183183
return firstReady;
184184
}
185185

186-
register(slot: RevealSlot): void {
186+
_register(slot: RevealSlot): void {
187187
if (this._slots.includes(slot)) return;
188188
this._slots.push(slot);
189189
const order = untrack(this._orderAccessor);
@@ -192,16 +192,16 @@ export class RevealController {
192192
slot._collapsed,
193193
order === "sequential" ? !!untrack(this._collapsedAccessor) : false
194194
));
195-
untrack(() => this.evaluate());
195+
untrack(() => this._evaluate());
196196
}
197197

198-
unregister(slot: RevealSlot): void {
198+
_unregister(slot: RevealSlot): void {
199199
const index = this._slots.indexOf(slot);
200200
if (index >= 0) this._slots.splice(index, 1);
201-
untrack(() => this.evaluate());
201+
untrack(() => this._evaluate());
202202
}
203203

204-
evaluate(disabledOverride?: boolean, collapsedOverride?: boolean): void {
204+
_evaluate(disabledOverride?: boolean, collapsedOverride?: boolean): void {
205205
if (this._evaluating) return;
206206
this._evaluating = true;
207207
const wasReady = this._ready;
@@ -224,7 +224,7 @@ export class RevealController {
224224
if (isRevealController(slot)) {
225225
setSignal(slot._collapsed, false);
226226
setSignal(slot._disabled, false);
227-
slot.evaluate(false, false);
227+
slot._evaluate(false, false);
228228
} else {
229229
setSlotState(slot, this, !isSlotReady(slot), false);
230230
}
@@ -252,22 +252,22 @@ export class RevealController {
252252
if (isRevealController(slot)) {
253253
setSignal(slot._collapsed, false);
254254
setSignal(slot._disabled, false);
255-
slot.evaluate(false, false);
255+
slot._evaluate(false, false);
256256
} else {
257257
setSlotState(slot, this, true, false);
258258
}
259259
});
260260
}
261261
} finally {
262-
this._ready = this.isReady();
263-
this._minimallyReady = this.isMinimallyReady();
262+
this._ready = this._isReady();
263+
this._minimallyReady = this._isMinimallyReady();
264264
this._evaluating = false;
265265
}
266266
if (
267267
this._parentController &&
268268
(wasReady !== this._ready || wasMinReady !== this._minimallyReady)
269269
)
270-
this._parentController.evaluate();
270+
this._parentController._evaluate();
271271
}
272272
}
273273

@@ -337,7 +337,7 @@ export class CollectionQueue extends Queue {
337337
type &= ~this._collectionType;
338338
return type ? super.notify(node, type, flags, error) : true;
339339
}
340-
checkSources() {
340+
_checkSources() {
341341
for (const source of this._sources) {
342342
if (
343343
source._flags & REACTIVE_DISPOSED ||
@@ -368,7 +368,7 @@ export class CollectionQueue extends Queue {
368368
}
369369
}
370370
}
371-
if (_revealUsed) this._revealController?.evaluate();
371+
if (_revealUsed) this._revealController?._evaluate();
372372
}
373373
}
374374

@@ -413,8 +413,8 @@ function createCollectionBoundary<T>(
413413
_revealUsed && type === STATUS_PENDING ? getContext(RevealControllerContext) : null;
414414
if (controller) {
415415
queue._revealController = controller;
416-
controller.register(queue);
417-
cleanup(() => controller.unregister(queue));
416+
controller._register(queue);
417+
cleanup(() => controller._unregister(queue));
418418
}
419419
return accessor<T>(
420420
computed(
@@ -561,12 +561,12 @@ export function createRevealOrder<T>(
561561
computed(() => {
562562
order();
563563
collapsed();
564-
controller.evaluate();
564+
controller._evaluate();
565565
});
566566
if (parentController) {
567567
controller._parentController = parentController;
568-
parentController.register(controller);
569-
cleanup(() => parentController.unregister(controller));
568+
parentController._register(controller);
569+
cleanup(() => parentController._unregister(controller));
570570
}
571571
return value;
572572
});

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

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { devTrackHeldPending } from "./invariants.js";
1515
import { emitDiagnostic } from "./dev.js";
1616
import { NotReadyError, StatusError } from "./error.js";
1717
import { trimStaleDeps } from "./graph.js";
18-
import { insertIntoHeap } from "./heap.js";
18+
import { enqueueSub } from "./heap.js";
1919
import { hasActiveOverride, resolveLane, resolveTransition, type OptimisticLane } from "./lanes.js";
2020
import { cleanup } from "./owner.js";
2121
import {
@@ -84,22 +84,6 @@ export function setPendingError(el: Computed<any>, source?: Computed<any>, error
8484
}
8585
}
8686

87-
/**
88-
* True when a node's pending status comes ONLY from affects() sentinels. A
89-
* mark is a promise of change, not an absence of value: reads of mark-pended
90-
* derived nodes stay value-transparent (verdicts report pending; the read
91-
* path must not suspend). Any real async source among the pending sources
92-
* keeps normal suspension semantics.
93-
*/
94-
export function onlyMarkPending(el: Computed<any>): boolean {
95-
const sources = el._pendingSources;
96-
if (sources) {
97-
for (const s of sources) if (!s._affectsFor) return false;
98-
return true;
99-
}
100-
return !!el._pendingSource?._affectsFor;
101-
}
102-
10387
export function forEachDependent(
10488
el: Computed<any>,
10589
fn: (node: Computed<any>, link: Link) => void
@@ -116,20 +100,8 @@ export function forEachDependent(
116100
}
117101

118102
// Queue a node to re-run on the next flush (used both when a pending source
119-
// settles and when an `isPending` observer must re-evaluate after a real error).
120-
function enqueueForRerun(node: Computed<any>): void {
121-
if ((node as any)._type === EFFECT_TRACKED) {
122-
const tracked = node as any;
123-
if (!tracked._modified) {
124-
tracked._modified = true;
125-
tracked._queue.enqueue(EFFECT_USER, tracked._run);
126-
}
127-
} else {
128-
const queue = node._flags & REACTIVE_ZOMBIE ? zombieQueue : dirtyQueue;
129-
if (queue._min > node._height) queue._min = node._height;
130-
insertIntoHeap(node, queue);
131-
}
132-
}
103+
// settles and when an `isPending` observer must re-evaluate after a real error):
104+
// shared scheduling helper in heap.ts (tracked effects bypass the heap).
133105

134106
export function settlePendingSource(
135107
el: Computed<any>,
@@ -156,7 +128,7 @@ export function settlePendingSource(
156128
setPendingError(node);
157129
updateCompanions !== null && updateCompanions(node);
158130
if (node._blocked) {
159-
enqueueForRerun(node);
131+
enqueueSub(node);
160132
scheduled = true;
161133
}
162134
node._blocked = false;
@@ -510,7 +482,7 @@ export function notifyStatus(
510482
// read swallows those, and the async path must match. Re-run the observer
511483
// so `isPending` re-evaluates (to not-pending) instead of forwarding.
512484
if (link._pendingObserver && status !== STATUS_PENDING && !(error instanceof NotReadyError)) {
513-
enqueueForRerun(sub);
485+
enqueueSub(sub);
514486
schedule();
515487
return;
516488
}

0 commit comments

Comments
 (0)