Skip to content

Commit 7d723f8

Browse files
ryansolidClaude via Cursorcursoragent
committed
fix(signals): report undefined-output runs as changed so every waste consumer agrees
The exemption for runs that commit `undefined` lived only inside checkWastedRecompute, so the RerunEvent still said `changed: false` and costs().wastedMs, expectNoWaste, the performance tracks and the why-run log kept counting projection and writing-memo runs as waste. Apply it to `changed` in recomputeEnd instead, mirroring the effect-output rule, and drop the noValue parameter. This is the documented behavior: an `undefined`-output compute reports `changed: true` and is exempt. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 0ce6ffe commit 7d723f8

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎.changeset/wasted-recompute-undefined-output.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@solidjs/signals": patch
33
---
44

5-
`WASTED_RECOMPUTE` no longer counts a re-run that committed `undefined` as waste. A projection that mutates its draft, or a memo that does its work by writing a signal, has no output to compare, so its re-runs were reported as pure cost.
5+
A re-run that commits `undefined` is no longer counted as waste. A projection that mutates its draft or reconciles a returned value, or a memo that does its work by writing a signal, has no output to compare, so its re-runs were reported as pure cost. `RerunEvent.changed` now reports `true` for these runs (as it already did for side-effect-only effects), so `WASTED_RECOMPUTE` no longer fires for them and `costs().wastedMs`, `expectNoWaste` and the performance tracks stop counting them as wasted.

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ export interface RerunEvent {
169169
* effect phase re-fires on every recompute), so the engine derives this
170170
* fact itself: an effect run whose compute output is identical to the
171171
* previous run's reports `changed: false` — the phase re-fired with the
172-
* same input, pure waste. Side-effect-only computes (`undefined` output)
173-
* are exempt: identity of `undefined` proves nothing about their work.
172+
* same input, pure waste. Side-effect-only computes (`undefined` output —
173+
* effects, projections, memos that work by writing) report `true`:
174+
* identity of `undefined` proves nothing about their work.
174175
* Summed as `wastedMs` in costs() (plain, non-held runs only — see
175176
* `phase`).
176177
*/
@@ -1395,15 +1396,15 @@ function checkHotTime(el: Computed<any>, selfMs: number, causes: ChangeRecord[])
13951396
* fix is upstream: an equality boundary on the part of the input the scope
13961397
* depends on, or a narrower read. Plain runs only — a held or overlay run
13971398
* may be replayed and is never blamed as waste. A run that committed
1398-
* `undefined` (`noValue`) is never waste either: its work is a side effect
1399-
* (a projection's draft writes, a write from inside a memo), not its output.
1399+
* `undefined` arrives here as changed (see `recomputeEnd`): its work is a
1400+
* side effect (a projection's draft writes, a write from inside a memo), not
1401+
* its output.
14001402
*/
14011403
function checkWastedRecompute(
14021404
el: Computed<any>,
14031405
at: number,
14041406
phase: RerunEvent["phase"],
14051407
changed: boolean,
1406-
noValue: boolean,
14071408
selfMs: number,
14081409
causes: ChangeRecord[]
14091410
): void {
@@ -1420,7 +1421,7 @@ function checkWastedRecompute(
14201421
node._devWasteWarned = false;
14211422
}
14221423
node._devWasteRuns = node._devWasteRuns! + 1;
1423-
if (!changed && !noValue) {
1424+
if (!changed) {
14241425
node._devWasted = node._devWasted! + 1;
14251426
node._devWastedMs = node._devWastedMs! + selfMs;
14261427
}
@@ -1467,7 +1468,6 @@ function recordRerun(
14671468
frame: RunFrame,
14681469
timing: { selfMs: number; totalMs: number },
14691470
changed: boolean,
1470-
noValue: boolean,
14711471
phase: "plain" | "held" | "optimistic",
14721472
held: boolean
14731473
): void {
@@ -1502,7 +1502,7 @@ function recordRerun(
15021502
checkRelayTear(el, causes, prevCauses);
15031503
checkHotRuns(el, causes);
15041504
checkHotTime(el, timing.selfMs, causes);
1505-
checkWastedRecompute(el, frame.start, phase, changed, noValue, timing.selfMs, causes);
1505+
checkWastedRecompute(el, frame.start, phase, changed, timing.selfMs, causes);
15061506
checkDepWidth(el);
15071507
// The record: built only when something wanted it at run start (see
15081508
// `wantsRerun`) — a listener, a fold, the log.
@@ -4169,13 +4169,18 @@ const engineHooks: AttributionHooks = {
41694169
frame.prevValue,
41704170
el._pendingValue !== NOT_PENDING ? el._pendingValue : el._value
41714171
);
4172+
// The same exemption for memos: a run that committed `undefined` did its
4173+
// work as a side effect (a projection's draft writes or reconcile, a
4174+
// write from inside a memo), so core's `undefined === undefined` gate
4175+
// proves nothing. Reported as changed, so no consumer counts it as waste.
4176+
if (!changed && (el._pendingValue !== NOT_PENDING ? el._pendingValue : el._value) === undefined)
4177+
changed = true;
41724178
if (frame.causes !== null)
41734179
recordRerun(
41744180
el,
41754181
frame,
41764182
{ selfMs, totalMs },
41774183
changed,
4178-
(el._pendingValue !== NOT_PENDING ? el._pendingValue : el._value) === undefined,
41794184
optimistic ? "optimistic" : transition ? "held" : "plain",
41804185
held
41814186
);

‎packages/signals/tests/attribution-wasted-recompute.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* whose waste is cheap are not reported. `checks: false` folds it off.
99
*/
1010
import { afterEach, describe, expect, it, vi } from "vitest";
11-
import { attribution } from "../src/attribution.js";
11+
import { attribution, costs } from "../src/attribution.js";
1212
import type { AttributionOptions } from "../src/attribution.js";
1313
import {
1414
createEffect,
@@ -189,6 +189,13 @@ describe("WASTED_RECOMPUTE", () => {
189189
return () => s.v;
190190
});
191191
expect(events).toHaveLength(0);
192+
// Every consumer reads the same fact: the records and costs() agree.
193+
const reruns = attribution.history("rerun").filter(e => e.nodeName === "draft projection");
194+
expect(reruns).toHaveLength(10);
195+
expect(reruns.every(e => e.changed)).toBe(true);
196+
const scope = costs().scopes.find(s => s.name === "draft projection");
197+
expect(scope?.runs).toBe(10);
198+
expect(scope?.wastedMs).toBe(0);
192199
});
193200

194201
it("a projection that returns a value to reconcile is not waste", () => {

0 commit comments

Comments
 (0)