Skip to content

Commit 886850b

Browse files
ryansolidcursoragentClaude via Cursor
authored
feat(signals): GRAPH_GROWTH — the live graph counted at navigation settle (#3619)
* feat(signals): GRAPH_GROWTH — the live graph counted at each navigation's settle The leak class a heap snapshot finds, as a finding. At every navigation's settle the engine counts the live graph and emits a `graph` record (GraphEvent: at, owners, roots, route, navigation); when the count at the same route has climbed on `visits` consecutive visits (3) to `ratio`× the first (1.25), GRAPH_GROWTH names the route with the counts. The count is the whole graph's, so the first route to complete its climb reports and names the others seen; the verdict then resets for the graph. The plan assumed the core kept node counters; it does not — the fan-in/out numbers are per-walk locals. Rather than add a live counter (an increment at every creation and disposal, on the idle observe path), the observe core registers top-level roots weakly — `createOwner` with no parent adds a WeakRef to a Set reaped by a FinalizationRegistry, disposal removes it — and the engine walks the owner tree from them: nothing per node, a walk at navigation cadence, only while the check is on or something listens. `graphSize()` is exported from solid-js/attribution for a consumer's own schedule. Prod is byte-identical (the bodies fold to a return). Size: tier +165 B (the registry; cap 17.5 -> 17.60 KB), engine +632 B (cap 29.00 -> 29.65 KB). Idle tripwire unchanged. Co-Authored-By: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> * feat(signals): GRAPH_GROWTH measures the reactive graph, not only the owner tree The owner walk counted components and owned computations and missed the leak it named in its own message: an effect created with no owner, held alive only by the sources it reads. The walk now continues from the owner tree into the reactive graph — each computation's dependency links (`edges`) and the `signals` and computations they reach, and each reached node's subscriber list, which is where an ownerless computation is met. Iterative with a worklist; a subscriber chain can be thousands deep. `GraphSize` is `{ roots, owners, computations, signals, edges }`; `GraphEvent` carries it. GRAPH_GROWTH judges every series and the message names the shape by which one climbed — owners is an undisposed root, computations with owners flat is an ownerless effect, edges alone is a subscription per visit to something long-lived. A node the app's own graph never reaches is not counted, and the RFC says so. Measured: 10k owners (9k computations, 9k edges) walk in 0.6ms, 50k in 3.6ms; the Set of met nodes is most of it. Engine cap 29.65 -> 29.95 KB; tier 17.60 -> 17.65 KB (drift; the core is unchanged since the registry). Co-Authored-By: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> * chore(size): GRAPH_GROWTH moves the tier cap to 17.65 KB and the engine cap to 30.00 KB (rebased) Co-Authored-By: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> * chore(size): re-measure GRAPH_GROWTH at landing; engine cap 29.80 -> 30.70 KB 30,649 B against next's 29,725 on the rebased tree (+924 B); tier 17,618 under the 17.65 KB cap. Co-authored-by: Claude via Cursor <[email protected]> --------- Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Claude via Cursor <[email protected]>
1 parent c25d69f commit 886850b

11 files changed

Lines changed: 595 additions & 6 deletions

File tree

‎.changeset/graph-growth.md‎

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+
`GRAPH_GROWTH` (warn, perf): the live reactive graph — owners, computations, signals, edges — measured at each route's settle climbed on consecutive visits, and which measure climbed names the leak. The observe core registers top-level roots weakly; the engine walks the owner tree and the reactive graph it reaches at navigation settle (never a per-node counter), emits a `graph` attribution record (`GraphEvent`, `GraphSize`), and exports `graphSize()` from `solid-js/attribution`. `graphGrowth: { visits, ratio } | false` configures the check.

‎documentation/plans/responsiveness-findings-plan.md‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,23 @@ an engine `WeakMap`.
184184
copy on every tick fires once after the window; the same memo reading
185185
one property fires nothing.
186186

187-
### 7. Graph growth as a leak detector
187+
### 7. Graph growth as a leak detector — LANDED (with a correction)
188+
189+
The plan said the core "keeps node and edge counters"; it does not — the
190+
`HUGE_FAN_OUT`/`HUGE_FAN_IN` counts are per-walk locals, and there is no live
191+
node count. Rather than add one (an increment at every creation and
192+
disposal, on the idle observe path), the observe core registers the
193+
top-level roots weakly (`WeakRef` + `FinalizationRegistry`, one Set write per
194+
root) and the engine walks from them at navigation settle — the owner tree, then
195+
the reactive graph it reaches through dependencies and subscriptions (which
196+
is how an ownerless effect is found) — zero per-node cost, a walk at
197+
navigation cadence: 10k owners in 0.6ms, 50k in 3.6ms. Shipped as the
198+
`graph` attribution record (`GraphEvent`/`GraphSize`: `roots`, `owners`,
199+
`computations`, `signals`, `edges`), `graphSize()` on `solid-js/attribution`,
200+
and `GRAPH_GROWTH` (warn) with `graphGrowth: { visits, ratio }`, judging
201+
every series and naming which climbed. The count is the whole graph's, so
202+
the first route to complete its climb reports and names the others
203+
(`data.routes`), and the verdict resets for the graph.
188204

189205
- **Known:** the core keeps node and edge counters in the observe build
190206
(the graph-size checks `HUGE_FAN_OUT`/`HUGE_FAN_IN` read them); a router

‎documentation/solid-2.0/08-dev-diagnostics.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,14 @@ One recompute pass tracked an unusually large number of distinct sources (same t
426426

427427
Related: `WIDE_SCOPE_DEPS` (below) fires at a much lower threshold, but only while the attribution engine is enabled — it names the offending sources. `HUGE_FAN_IN` is the always-on backstop for the pathological case.
428428

429+
#### `GRAPH_GROWTH`
430+
431+
**Message:** "the live graph grew on 3 consecutive visits to `/orders`: computations 900 → 903 → 906; edges 900 → 903 → 906 (2 roots), across visits to `/orders`, `/`. Each visit left something behind that the next did not reclaim — the shape says an effect or memo created with no owner (a module-level or callback `createEffect`) that only its sources keep alive. Dispose what a visit creates (`onCleanup`, or return the disposer from `onSettled`) and own it under the route's component so leaving the route tears it down."
432+
433+
Attribution-engine only; the leak class a heap snapshot finds, as a finding. At every navigation's settle (`withOrigin({ kind: "navigation" })`) the engine measures the live graph with a **walk**, never a per-node counter: the owner tree from the registered top-level roots (`owners`: roots, component owners, owned computations), then everything reachable from it through the reactive graph — each computation's dependency links (`edges`) and the `signals` and computations they reach, and each reached node's subscriber list, which is how a computation **no owner holds** (a `createEffect` with no owner, kept alive only by the sources it reads) is found and counted in `computations`. It emits a `graph` record (`GraphEvent`: the `GraphSize` fields plus `at`, `route`, `navigation`) for a `subscribe("graph", …)` listener, and keeps the size at each settle of the same route. When any series — `owners`, `computations`, `signals`, `edges` — has climbed on `graphGrowth.visits` consecutive visits (default 3) to `ratio` or more of the first (default 1.25), the route reports, and _which_ series climbed names the leak: `owners` is an undisposed root or a Portal per visit; `computations` with `owners` flat is an ownerless effect; `edges` alone is a subscription per visit to something long-lived. The count is the whole graph's, so a leak shows at every route's settle: the first route to complete its climb reports and `data.routes` names the others seen; the verdict then resets. `data`: `route`, `grew` (the series that climbed), `history` (the `GraphSize` at each settle, oldest first), `roots`, `routes`, `interaction`. No subject.
434+
435+
The observe core's part is the root registry: `createOwner` with no parent registers the root (weakly — a `WeakRef`, reaped by a `FinalizationRegistry` — so an undisposed root nothing references still collects; one a subscription keeps alive is exactly what the walk counts), and its disposal unregisters it. One Set write per top-level root, nothing per node; the walk runs at navigation cadence, only when the check is on or something listens for `graph` records. Measured on the observe artifacts (components of 1 signal, 3 memos, 6 effects): 10k owners walk in 0.6ms, 50k in 3.6ms — a `Set` for the reached nodes is most of it. A reactive node the app's own graph never reaches (an ownerless effect over a signal nothing owned reads) is not counted. `graphSize()` is exported from `solid-js/attribution` for a consumer that wants the measure on its own schedule. `false` disables.
436+
429437
#### `HOT_SCOPE_RERUNS`, `HOT_SCOPE_TIME`, `WIDE_SCOPE_DEPS`
430438

431439
Perf-kind warnings emitted by the **attribution engine** — they only fire while the attribution engine (`solid-js/attribution`) is enabled (see the next section). Defaults:
@@ -856,6 +864,7 @@ The runtime derives a request's trace itself in every tier — the W3C `tracepar
856864
| `FLUSH_IN_EFFECT_CALLBACK` | warn | lifecycle | `flush()` from an effect callback (no-op; the drain is already running) |
857865
| `HUGE_FAN_OUT` | warn | graph | One change reached 2000 live subscribers (always on) |
858866
| `HUGE_FAN_IN` | warn | graph | One recompute tracked 2000 sources (always on) |
867+
| `GRAPH_GROWTH` | warn | perf | The live owner count at the same route's settle climbed on 3 consecutive visits to 1.25× — something each visit leaves behind (attribution enabled) |
859868
| `HOT_SCOPE_RERUNS` | warn | perf | 120+ re-runs of one scope in 1s (attribution enabled) |
860869
| `HOT_SCOPE_FANOUT` | warn | perf | 5+/50+/500+ scopes hot from one root cause (attribution enabled) |
861870
| `HOT_SCOPE_TIME` | warn | perf | 8ms+ self-time in one scope in 1s (attribution enabled) |

‎packages/signals/src/attribution.prod.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export const formatOrigin: typeof Engine.formatOrigin = () => "";
4343
// No hold is ever recorded in prod, so no hold is ever silent or long.
4444
export const isSilentHold: typeof Engine.isSilentHold = () => false;
4545
export const isLongHold: typeof Engine.isLongHold = () => false;
46+
// Prod registers no roots: the graph has no observable size.
47+
export const graphSize: typeof Engine.graphSize = () => ({
48+
roots: 0,
49+
owners: 0,
50+
computations: 0,
51+
signals: 0,
52+
edges: 0
53+
});
4654

4755
export type {
4856
Acknowledgement,
@@ -59,6 +67,8 @@ export type {
5967
FlightEvent,
6068
FlightLink,
6169
FlushEvent,
70+
GraphEvent,
71+
GraphSize,
6272
HeldWrite,
6373
HoldEvent,
6474
InteractionEvent,

‎packages/signals/src/attribution.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
attribution,
1515
formatOrigin,
1616
formatRerun,
17+
graphSize,
1718
isLongHold,
1819
isSilentHold
1920
} from "./core/attribution.js";
@@ -39,6 +40,8 @@ export type {
3940
FlightEvent,
4041
FlightLink,
4142
FlushEvent,
43+
GraphEvent,
44+
GraphSize,
4245
HeldWrite,
4346
HoldEvent,
4447
InteractionEvent,

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

Lines changed: 183 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
anyExcluded,
1010
emitDiagnostic,
1111
GRAPH_SIZE_WARN_AT,
12+
liveRootOwners,
1213
isExcluded,
1314
isSuppressed,
1415
ownerPath,
@@ -314,6 +315,43 @@ export interface FallbackEvent {
314315
interaction?: ChangeOrigin;
315316
}
316317

318+
/**
319+
* The live graph's size at a navigation's settle — the moment an app has
320+
* finished moving between two screens, so a count that climbs visit after
321+
* visit is a root or a subscription the previous screen left behind.
322+
* Delivered on `subscribe("graph", …)` per settled navigation; a walk of the
323+
* owner tree from the registered top-level roots, made only when something
324+
* listens or `graphGrowth` is on.
325+
*/
326+
export interface GraphEvent extends GraphSize {
327+
/** When the navigation settled (`performance.now()` clock). */
328+
at: number;
329+
/** The route pattern the navigation matched (`name`), else its `to`. */
330+
route?: string;
331+
/** The navigation this count belongs to. */
332+
navigation: NavigationEvent;
333+
}
334+
335+
/**
336+
* The live reactive graph's size, as `graphSize()` counts it: the owner tree
337+
* from the registered top-level roots, then every computation and signal
338+
* reachable from it through dependencies and subscriptions — which is how
339+
* an ownerless effect (created with no owner, kept alive by its sources)
340+
* is found — and the edges between them.
341+
*/
342+
export interface GraphSize {
343+
/** Top-level roots alive (`render()`'s, module-scope `createRoot()`s, panels). */
344+
roots: number;
345+
/** Owners in the tree: roots, component owners, owned computations. */
346+
owners: number;
347+
/** Computations (memos, effects, boundaries), owned or reached through a subscription. */
348+
computations: number;
349+
/** Signals reached through a computation's dependencies. */
350+
signals: number;
351+
/** Dependency links — each computation's sources, counted once. */
352+
edges: number;
353+
}
354+
317355
export interface AttributionOptions {
318356
/** Pretty-print each re-run to the console (default true). */
319357
log?: boolean;
@@ -428,6 +466,14 @@ export interface AttributionOptions {
428466
* `false` disables.
429467
*/
430468
longHolds?: { infoMs: number; warnMs: number } | false;
469+
/**
470+
* Graph-growth warning: emit GRAPH_GROWTH when the live owner count at the
471+
* settle of the same route has climbed on `visits` consecutive visits
472+
* (default 3) to `ratio` or more of the first (default 1.25) — a root or a
473+
* subscription each visit leaves behind, the leak class a heap snapshot
474+
* finds. The count is a walk at settle, never per node. `false` disables.
475+
*/
476+
graphGrowth?: { visits: number; ratio: number } | false;
431477
}
432478

433479
interface AttributedNode {
@@ -497,7 +543,8 @@ const defaultOptions = {
497543
wideWrites: 250 as number | false,
498544
waterfalls: { minFlightMs: 50 } as { minFlightMs: number } | false,
499545
holds: { infoMs: 100, warnMs: 200 } as { infoMs: number; warnMs: number } | false,
500-
longHolds: { infoMs: 500, warnMs: 1000 } as { infoMs: number; warnMs: number } | false
546+
longHolds: { infoMs: 500, warnMs: 1000 } as { infoMs: number; warnMs: number } | false,
547+
graphGrowth: { visits: 3, ratio: 1.25 } as { visits: number; ratio: number } | false
501548
};
502549
let options: typeof defaultOptions = { ...defaultOptions };
503550
let history: RerunEvent[] = [];
@@ -523,6 +570,8 @@ export interface AttributionRecords {
523570
interaction: InteractionEvent;
524571
hold: HoldEvent;
525572
navigation: NavigationEvent;
573+
/** Listener-gated (or on while `graphGrowth` is) — see `GraphEvent`. */
574+
graph: GraphEvent;
526575
}
527576
export type AttributionRecordType = keyof AttributionRecords;
528577
type RecordListeners = {
@@ -537,7 +586,8 @@ const recordListeners: RecordListeners = {
537586
fallback: new Set(),
538587
interaction: new Set(),
539588
hold: new Set(),
540-
navigation: new Set()
589+
navigation: new Set(),
590+
graph: new Set()
541591
};
542592
/** Whether anything listens for `type` — the pre-check the listener-gated records cost nothing without. */
543593
function listened(type: AttributionRecordType): boolean {
@@ -2826,6 +2876,135 @@ function checkLongHold(event: HoldEvent, subject: Signal<any>): void {
28262876
if (severity === "warn") reportDiagnostic(entry);
28272877
}
28282878

2879+
// --- Graph growth -----------------------------------------------------------------
2880+
2881+
/** Per route: the graph's size at its last `visits` settles, oldest first. */
2882+
const routeCounts = new Map<string, GraphSize[]>();
2883+
2884+
/**
2885+
* The live graph's size — a walk, not a counter: nothing is charged at node
2886+
* creation, disposal, write or re-run; the engine asks at a navigation's
2887+
* settle. Two passes. The owner tree from the registered top-level roots
2888+
* gives `owners` and seeds the computations. Then each computation's
2889+
* dependency links give `edges` and the `signals` and computations they
2890+
* reach, and each newly met source's subscriber list gives the computations
2891+
* that read it — including one created with no owner, which no chain holds
2892+
* and only its sources keep alive: the subscription leak a heap snapshot
2893+
* finds. Measured at 5–10 ns per link; a 50k-owner graph walks in under a
2894+
* millisecond. Dormant nodes are spliced out of their chain and are not
2895+
* counted unless a subscription still reaches them.
2896+
*/
2897+
export function graphSize(): GraphSize {
2898+
const roots = liveRootOwners();
2899+
const size: GraphSize = { roots: roots.length, owners: 0, computations: 0, signals: 0, edges: 0 };
2900+
const seen = new Set<object>();
2901+
// A worklist, not recursion: a subscriber chain can be thousands deep.
2902+
const work: (Signal<any> | Computed<any>)[] = [];
2903+
const stack: Owner[] = [];
2904+
const meet = (node: Signal<any> | Computed<any>): void => {
2905+
if (seen.has(node)) return;
2906+
seen.add(node);
2907+
if (typeof (node as Computed<any>)._fn === "function") size.computations++;
2908+
else size.signals++;
2909+
work.push(node);
2910+
};
2911+
for (const root of roots) {
2912+
size.owners++;
2913+
// Iterative: a deep tree must not grow the call stack.
2914+
let child = root._firstChild;
2915+
for (;;) {
2916+
while (child !== null) {
2917+
size.owners++;
2918+
if (typeof (child as Computed<any>)._fn === "function") meet(child as Computed<any>);
2919+
if (child._firstChild !== null) stack.push(child);
2920+
child = child._nextSibling;
2921+
}
2922+
const next = stack.pop();
2923+
if (next === undefined) break;
2924+
child = next._firstChild;
2925+
}
2926+
}
2927+
for (let i = 0; i < work.length; i++) {
2928+
const node = work[i];
2929+
// Whoever reads this node: owned computations are already known; an
2930+
// ownerless one is met only here.
2931+
for (let s = node._subs; s !== null; s = s._nextSub) meet(s._sub);
2932+
if (typeof (node as Computed<any>)._fn === "function")
2933+
for (let link = (node as Computed<any>)._deps; link !== null; link = link._nextDep) {
2934+
size.edges++;
2935+
meet(link._dep);
2936+
}
2937+
}
2938+
return size;
2939+
}
2940+
2941+
/**
2942+
* settleNavigation: the app has finished moving to a route — count the live
2943+
* graph. One record per settle for a listener; the growth check compares
2944+
* this settle of the route with its previous ones. A count that climbs on
2945+
* consecutive visits is something the previous visit left behind: a
2946+
* `createRoot` in an effect with no dispose, a subscription a component
2947+
* registered outside its owner. Nothing here runs per node; the walk is
2948+
* the cost, at navigation cadence.
2949+
*/
2950+
const GRAPH_SERIES: (keyof GraphSize)[] = ["owners", "computations", "signals", "edges"];
2951+
2952+
function trackGraph(navigation: NavigationEvent): void {
2953+
const cfg = options.graphGrowth;
2954+
if (cfg === false && !listened("graph")) return;
2955+
const size = graphSize();
2956+
const route = navigation.name ?? navigation.to;
2957+
const event: GraphEvent = { at: now(), ...size, navigation };
2958+
if (route !== undefined) event.route = route;
2959+
emitRecord("graph", event);
2960+
if (cfg === false || route === undefined) return;
2961+
let history = routeCounts.get(route);
2962+
if (history === undefined) routeCounts.set(route, (history = []));
2963+
history.push(size);
2964+
if (history.length > cfg.visits) history.shift();
2965+
if (history.length < cfg.visits) return;
2966+
// Any series climbing on every visit to the ratio is growth; which ones
2967+
// did says what leaked — computations with owners flat is an ownerless
2968+
// effect per visit, edges alone is a subscription per visit to something
2969+
// long-lived, owners is an undisposed root.
2970+
const grew = GRAPH_SERIES.filter(key => {
2971+
for (let i = 1; i < history!.length; i++)
2972+
if (history![i][key] <= history![i - 1][key]) return false;
2973+
return history![history!.length - 1][key] >= history![0][key] * cfg.ratio;
2974+
});
2975+
if (grew.length === 0) return;
2976+
// The count is the whole graph's, so a leak shows at every route's settle;
2977+
// the first route to complete its climb reports, naming the others seen.
2978+
const routes = [...routeCounts.keys()];
2979+
const series = grew.map(key => `${key} ${history!.map(h => h[key]).join(" → ")}`).join("; ");
2980+
const shape = grew.includes("owners")
2981+
? "a createRoot() in an effect or handler with no dispose, or a Portal or panel mounted per visit"
2982+
: grew.includes("computations")
2983+
? "an effect or memo created with no owner (a module-level or callback createEffect) that only its sources keep alive"
2984+
: "a subscription per visit to something long-lived — a global store or signal read by a computation that outlives the visit";
2985+
const message =
2986+
`[GRAPH_GROWTH] the live graph grew on ${cfg.visits} consecutive visits to ${route}: ${series} ` +
2987+
`(${size.roots} roots)${routes.length > 1 ? `, across visits to ${routes.join(", ")}` : ""}. Each ` +
2988+
`visit left something behind that the next did not reclaim — the shape says ${shape}. Dispose ` +
2989+
`what a visit creates (onCleanup, or return the disposer from onSettled) and own it under the ` +
2990+
`route's component so leaving the route tears it down.`;
2991+
const data: Record<string, unknown> = {
2992+
route,
2993+
grew,
2994+
history: history.map(h => ({ ...h })),
2995+
roots: size.roots,
2996+
routes
2997+
};
2998+
if (navigation.interaction !== undefined)
2999+
data.interaction = { type: navigation.interaction.name, target: navigation.interaction.target };
3000+
reportDiagnostic(
3001+
emitDiagnostic({ code: "GRAPH_GROWTH", kind: "perf", severity: "warn", message, data }, null)
3002+
);
3003+
// Judged once for the graph, not once per route: the next verdict needs
3004+
// another `visits` climbing settles.
3005+
routeCounts.clear();
3006+
}
3007+
28293008
// --- Navigations ------------------------------------------------------------------
28303009
//
28313010
// A navigation in Solid 2 is not a primitive: it is a plain write to the
@@ -3230,6 +3409,7 @@ function settleNavigation(
32303409
if (hold !== undefined) event.hold = hold;
32313410
for (const f of folds) f.navigation?.(event);
32323411
emitRecord("navigation", event);
3412+
trackGraph(event);
32333413
// The interaction that performed it may have been waiting only on this.
32343414
const under = openInteractionOf(event.interaction);
32353415
if (under !== undefined) maybeSettleInteraction(under);
@@ -3836,6 +4016,7 @@ function resetTracking(): void {
38364016
trackingGen++;
38374017
openNavs.clear();
38384018
openInteractions.clear();
4019+
routeCounts.clear();
38394020
drainSeq = 0;
38404021
originFrames.length = 0;
38414022
effectStack.length = 0;

0 commit comments

Comments
 (0)