Skip to content

Commit 6af149e

Browse files
ryansolidclaude
andauthored
fix(signals): net staged rewrites a custom equals calls equal to the committed value (D-8, #4483) (#4488)
* fix(signals): net staged rewrites a custom equals calls equal to the committed value (D-8, #4483) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * fix(signals): gate the custom-equals netting on the verdict engine Only isPending/latest read the netted test before a commit, so the write-back staging is gated on observeFlightHook and bundles without the verdict engine pay 0 B. Drops the floor re-records and Size-Exception. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * chore(size): re-record over next @ d0fc285 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * perf(signals): skip the custom-equals netting under the default comparator A rewrite loop over a staged signal (CodSpeed propagation:avoidable, updateSignals:update1to1000) ran the uninitialized/shown-error checks and isSame on every write. Under the default comparator only the committed object is equal and `v` is already it, so one load of `_equals` decides. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * perf(signals): share setSignal's comparator test between the gate and D-8 netting CodSpeed (simulation, --no-opt so setSignal runs as Sparkplug code) put `propagation:avoidable` 5.3% behind next: every staged rewrite paid a fresh `_equals` load and a strict compare for the netting, ~120 instructions a write under callgrind, on a bench that is 2,000 staged rewrites per iteration. With the verdict engine installed, `custom` (`_equals !== isEqual`) is now taken once: the gate runs the default comparator's `===` inline instead of calling `isSame`, and the netting tests the local. Without the engine `custom` is `null`, so the gate folds to the plain `isSame` call and the netting away: every store-free / non-verdict client bundle is byte for byte next's (hello world 9,973 B). callgrind, Ir per bench iteration (CodSpeed's V8 flags): next 3,294,205; PR head 3,536,042 (+7.3%); this 3,122,267 (-5.2% vs next). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 --------- Co-authored-by: Claude <[email protected]>
1 parent 20c115c commit 6af149e

3 files changed

Lines changed: 95 additions & 2 deletions

File tree

‎.changeset/equals-netting-4483.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+
A write that a custom `equals` calls equal to the committed value now nets to it (D-8) while another write to the signal is staged, so it pends nothing (#4483).

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,10 +2543,17 @@ export function setSignal<T>(el: Signal<T> | Computed<T>, v: T | ((prev: T) => T
25432543
// user comparator must not run against `undefined` (matches recompute).
25442544
// D-41: a write over a shown error is a change whatever the last good
25452545
// value was — the error leaves at its commit (`commitPendingNode`).
2546+
// `custom`: with the verdict engine, whether `el` carries a comparator of
2547+
// its own — `isSame`'s fast-path test, taken once for this gate and D-8's
2548+
// netting below, so the default comparator's `===` runs inline and a
2549+
// rewrite loop pays nothing for the netting (CodSpeed `propagation:
2550+
// avoidable`). Without the engine it is `null`: the gate folds to the
2551+
// plain `isSame` call and the netting away, byte for byte as before.
2552+
const custom = observeFlightHook ? el._equals !== isEqual : null;
25462553
const valueChanged =
25472554
!!((el as Computed<T>)._statusFlags & STATUS_UNINITIALIZED) ||
25482555
!!el._x?._shownError ||
2549-
!isSame(el, currentValue, v);
2556+
(custom === false ? currentValue !== v : !isSame(el, currentValue, v));
25502557
if (!valueChanged) return v;
25512558

25522559
// Attribution hook: this committed write is where a re-run chain begins.
@@ -2568,7 +2575,22 @@ export function setSignal<T>(el: Signal<T> | Computed<T>, v: T | ((prev: T) => T
25682575
// A28: a held node rewritten outside a flush keeps its flushed staging
25692576
// for every reader until the flush that carries the rewrite.
25702577
else if (el._config & CONFIG_HELD && !globalQueue._running) stashFlushed(el);
2571-
el._pendingValue = v;
2578+
// D-8 (#4483): a staged rewrite the comparator calls equal to the
2579+
// committed value nets to it; staging the committed value itself lets
2580+
// every netted test (`_pendingValue === _value`) see writes that made no
2581+
// proposal. (Unstaged, the gate above already compared it.)
2582+
// The netted test reads it only with the verdict engine (`isPending`,
2583+
// `latest`): a build without it stages `v` as written. Under the default
2584+
// comparator only the committed object itself is equal, and `v` is already
2585+
// it, so a rewrite loop pays one local test (`custom`, from the gate).
2586+
// (`custom` is truthy only with the engine installed.)
2587+
el._pendingValue =
2588+
custom &&
2589+
wasStaged &&
2590+
!((el as Computed<T>)._statusFlags & STATUS_UNINITIALIZED || el._x?._shownError) &&
2591+
isSame(el, el._value, v)
2592+
? el._value
2593+
: v;
25722594
// A28 arm, gated on the load the write already pays for (`context`); the
25732595
// arm itself is a cold helper so setSignal stays within every setter's
25742596
// inlining budget, ~300 B bytecode.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// #4483 (D-8: a tick whose writes net to the slot's committed value made no
2+
// proposal). An action rewrites a signal back to its committed value while a
3+
// mainline write to it is still staged. Under a custom `equals`, a new object
4+
// the comparator calls equal nets exactly like the committed object itself:
5+
// the slot pends nothing, so `isPending` of a memo over it stays `false`.
6+
// Without a custom `equals`, only identity nets; a new object is a proposal.
7+
import { afterEach, describe, expect, it } from "vitest";
8+
import {
9+
action,
10+
createMemo,
11+
createRenderEffect,
12+
createRoot,
13+
createSignal,
14+
flush,
15+
isPending,
16+
resetErrorHalt
17+
} from "../src/index.js";
18+
19+
afterEach(() => resetErrorHalt());
20+
const tick = async () => {
21+
await new Promise(r => setTimeout(r, 0));
22+
flush();
23+
};
24+
25+
const run = async (custom: boolean, write: "committed" | "copy") => {
26+
const one = { k: 1 };
27+
const [s, setS] = createSignal(one, custom ? { equals: (a, b) => a.k === b.k } : undefined);
28+
const seen: boolean[] = [];
29+
const dispose = createRoot(d => {
30+
const m0 = createMemo(() => {
31+
const v = s().k + 1;
32+
if (v % 4 === 2) throw new Error(`m0:${v}`);
33+
return v;
34+
});
35+
createRenderEffect(
36+
() => isPending(m0),
37+
v => void seen.push(v)
38+
);
39+
return d;
40+
});
41+
flush();
42+
const act = action(function* () {
43+
setS(write === "committed" ? one : { k: 1 });
44+
yield new Promise(() => {});
45+
});
46+
setS({ k: 2 });
47+
act();
48+
await tick();
49+
dispose();
50+
return seen;
51+
};
52+
53+
describe("#4483: writes that net under a custom equals pend nothing (D-8)", () => {
54+
it("nets the committed object itself (identity)", async () => {
55+
expect(await run(true, "committed")).toEqual([false]);
56+
});
57+
58+
it("nets a new object the comparator calls equal", async () => {
59+
expect(await run(true, "copy")).toEqual([false]);
60+
});
61+
62+
it("without a custom equals, nets only the committed object", async () => {
63+
expect(await run(false, "committed")).toEqual([false]);
64+
expect(await run(false, "copy")).toEqual([false, true]);
65+
});
66+
});

0 commit comments

Comments
 (0)