Skip to content

Commit d20ce53

Browse files
committed
perf(signals): ask the swapped-raw rule only of a frame that carries a moved row
Asked of every eager pair, the shared rule built a raw set per frame and CodSpeed measured -13% to -25% on the saturated reconcile benches. The eager pairing now decides once per frame whether any value is a row of the family already at a changed slot (`hasRow`: one lookup per value, no allocation); a frame of fresh raws, the common one, pairs by slot and builds nothing. A raw that is a row elsewhere keeps it, unless that row is the slot's own (its raw re-sent). Tests cover the one-row-proxied swap (the row follows its raw) and the immutable update kept at its slot (no move). +78 B brotli / +159 B minified on the createStore scenario over the PR's held-only form, recorded in the cap notes. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_015Q7JD9PktsxtBJJbWmtuwr
1 parent eae477f commit d20ce53

5 files changed

Lines changed: 144 additions & 48 deletions

File tree

‎packages/signals/docs/INTERNALS-STORE-STATE.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,13 @@ there is nothing to diff.)
199199
same objects swapped or reordered (`{x: A, y: B}` to `{x: B, y: A}`): it
200200
moved, it did not change, and it is its own row (`ownRow`, the one rule
201201
`descend` and `adoptChild` share). Paired, the two would alias after the
202-
commit; unpaired, each raw keeps its target and the slots change.
202+
commit; unpaired, each raw keeps its target and the slots change. The rule
203+
reads a raw set per frame, so the eager pairing asks it only of a frame
204+
that carries a row already at a changed slot (`hasRow`: one lookup per
205+
value, decided once per frame); a frame of fresh raws, the common one,
206+
pairs by slot and builds nothing, and an unchanged subtree kept at its
207+
slot is no move. A raw that is a row of the family already keeps it there
208+
too, unless that row is the slot's own (its raw re-sent).
203209

204210
## 4. Identity rules
205211

‎packages/signals/docs/RULES-INDEX.md‎

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,19 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul
124124

125125
| id | status | defined | cited in src | cited in tests | statement (at definition) |
126126
|---|---|---|---|---|---|
127-
| RUL-1 | resolved | `docs/INTERNALS-STORE-STATE.md:538` | store.ts×2 | next-smoke.test.ts×1 | - **RUL-1 — RESOLVED (2026-08-16, verified per "mirror signals if verified" — |
128-
| RUL-2 | ruled | `docs/INTERNALS-STORE-STATE.md:547` | — | createOptimisticStore.test.ts×1 | - **RUL-2 — RULED (Ryan, 2026-08-17): no landing matrix. Two orthogonal |
129-
| RUL-3 | resolved | `docs/INTERNALS-STORE-STATE.md:646` | optimistic.ts×1 | — | - **RUL-3 — RESOLVED (verified 2026-08-16).** Ownership already lives |
130-
| RUL-4 | resolved | `docs/INTERNALS-STORE-STATE.md:653` | — | optimistic-signal-refetch-hold.test.ts×1 | - **RUL-4 — RESOLVED (2026-08-17, signal parity — verified empirically).** |
131-
| RUL-5 | live | `docs/INTERNALS-STORE-STATE.md:666` | — | adoption-lane-rollback.test.ts×1 | - **RUL-5 — SPEC'D (2026-08-17): §6b.** Lane backing + lane-view/committed- |
132-
| RUL-6 | live | `docs/INTERNALS-STORE-STATE.md:670` | — | — | - **RUL-6 — reclassified: SPEC WORK, not a ruling.** Live chaining (#2941 — |
133-
| RUL-7 | live | `docs/INTERNALS-STORE-STATE.md:679` | — | — | - **RUL-7 — SPEC'D (2026-08-17): §6c.** One status field on the root target, |
134-
| RUL-8 | live | `docs/INTERNALS-STORE-STATE.md:682` | — | adoption-lane-rollback.test.ts×1 | - **RUL-8 — SPEC'D (2026-08-17): §6 rewrite, resolves O2.** Per-transaction |
135-
| RUL-9 | resolved | `docs/INTERNALS-STORE-STATE.md:685` | — | — | - **RUL-9 — RESOLVED (2026-08-17, parity by construction).** Every piece of |
136-
| RUL-10 | live | `docs/INTERNALS-STORE-STATE.md:690` | — | — | - **RUL-10 — The equality trio.** One precise rule needed spanning: no-op |
137-
| RUL-11 | live | `docs/INTERNALS-STORE-STATE.md:696` | — | — | - **RUL-11 — SPEC'D (2026-08-17): §6d.** Sticky descendants flag ported from |
138-
| RUL-12 | live | `docs/INTERNALS-STORE-STATE.md:698` | store.ts×1 types.ts×1 | createProjection.async.test.ts×1 shared-child-multiparent.test.ts×1 | - **RUL-12 — Smaller rulings, each with a proposed default** (proceeding on |
139-
| RUL-13 | resolved | `docs/INTERNALS-STORE-STATE.md:744` | — | — | - **RUL-13 — RESOLVED (verified 2026-08-16)**: `optimistic-lane-transaction- |
127+
| RUL-1 | resolved | `docs/INTERNALS-STORE-STATE.md:544` | store.ts×2 | next-smoke.test.ts×1 | - **RUL-1 — RESOLVED (2026-08-16, verified per "mirror signals if verified" — |
128+
| RUL-2 | ruled | `docs/INTERNALS-STORE-STATE.md:553` | — | createOptimisticStore.test.ts×1 | - **RUL-2 — RULED (Ryan, 2026-08-17): no landing matrix. Two orthogonal |
129+
| RUL-3 | resolved | `docs/INTERNALS-STORE-STATE.md:652` | optimistic.ts×1 | — | - **RUL-3 — RESOLVED (verified 2026-08-16).** Ownership already lives |
130+
| RUL-4 | resolved | `docs/INTERNALS-STORE-STATE.md:659` | — | optimistic-signal-refetch-hold.test.ts×1 | - **RUL-4 — RESOLVED (2026-08-17, signal parity — verified empirically).** |
131+
| RUL-5 | live | `docs/INTERNALS-STORE-STATE.md:672` | — | adoption-lane-rollback.test.ts×1 | - **RUL-5 — SPEC'D (2026-08-17): §6b.** Lane backing + lane-view/committed- |
132+
| RUL-6 | live | `docs/INTERNALS-STORE-STATE.md:676` | — | — | - **RUL-6 — reclassified: SPEC WORK, not a ruling.** Live chaining (#2941 — |
133+
| RUL-7 | live | `docs/INTERNALS-STORE-STATE.md:685` | — | — | - **RUL-7 — SPEC'D (2026-08-17): §6c.** One status field on the root target, |
134+
| RUL-8 | live | `docs/INTERNALS-STORE-STATE.md:688` | — | adoption-lane-rollback.test.ts×1 | - **RUL-8 — SPEC'D (2026-08-17): §6 rewrite, resolves O2.** Per-transaction |
135+
| RUL-9 | resolved | `docs/INTERNALS-STORE-STATE.md:691` | — | — | - **RUL-9 — RESOLVED (2026-08-17, parity by construction).** Every piece of |
136+
| RUL-10 | live | `docs/INTERNALS-STORE-STATE.md:696` | — | — | - **RUL-10 — The equality trio.** One precise rule needed spanning: no-op |
137+
| RUL-11 | live | `docs/INTERNALS-STORE-STATE.md:702` | — | — | - **RUL-11 — SPEC'D (2026-08-17): §6d.** Sticky descendants flag ported from |
138+
| RUL-12 | live | `docs/INTERNALS-STORE-STATE.md:704` | store.ts×1 types.ts×1 | createProjection.async.test.ts×1 shared-child-multiparent.test.ts×1 | - **RUL-12 — Smaller rulings, each with a proposed default** (proceeding on |
139+
| RUL-13 | resolved | `docs/INTERNALS-STORE-STATE.md:750` | — | — | - **RUL-13 — RESOLVED (verified 2026-08-16)**: `optimistic-lane-transaction- |
140140
## R — core-store (`CS-R<n>`)
141141

142142
| id | status | defined | cited in src | cited in tests | statement (at definition) |
@@ -381,19 +381,19 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul
381381
| §1 | live | `docs/INTERNALS-STORE-STATE.md:27` | target.ts×1 | reveal-gating-contract.test.ts×1 | Storage model (the single-home rule) |
382382
| §2 | live | `docs/INTERNALS-STORE-STATE.md:81` | — | — | Read paths |
383383
| §3 | live | `docs/INTERNALS-STORE-STATE.md:116` | optimistic.ts×1 reconcile.ts×1 store.ts×2 | — | Write paths (all must stay equivalent) |
384-
| §4 | live | `docs/INTERNALS-STORE-STATE.md:204` | — | — | Identity rules |
385-
| §5 | live | `docs/INTERNALS-STORE-STATE.md:216` | store.ts×1 target.ts×1 | — | Laziness invariants (candidates for `__TEST__` assertions) |
386-
| §5b | live | `docs/INTERNALS-STORE-STATE.md:234` | target.ts×2 | — | Creation budget (phase-1 fitness) |
387-
| §5c | live | `docs/INTERNALS-STORE-STATE.md:253` | — | — | Comparison method (shipped vs rewrite) |
388-
| §6 | live | `docs/INTERNALS-STORE-STATE.md:304` | target.ts×1 | — | Structural edits — the key-set node (resolves O2, RUL-8) |
389-
| §6b | live | `docs/INTERNALS-STORE-STATE.md:332` | — | adoption-lane-rollback.test.ts×1 | Lane-aware adoption (RUL-5) |
390-
| §6c | live | `docs/INTERNALS-STORE-STATE.md:351` | — | createProjection.async.test.ts×1 flight-owned-transaction.test.ts×1 | Store-wide status gating (RUL-7) |
391-
| §6d | live | `docs/INTERNALS-STORE-STATE.md:364` | reconcile.ts×2 target.ts×2 | — | Diff reachability (RUL-11) |
392-
| §7 | live | `docs/INTERNALS-STORE-STATE.md:376` | optimistic.ts×1 projection.ts×1 | — | Projections & optimism layering |
393-
| §7b | live | `docs/INTERNALS-STORE-STATE.md:386` | scheduler.ts×1 affects.ts×1 optimistic.ts×2 projection.ts×1 reconcile.ts×1 store.ts×8 target.ts×4 | optimistic-chained-revert-3672-memo.test.ts×1 optimistic-removed-prop-3883.test.ts×1 | Chained backing (cross-store) — spec |
394-
| §8 | live | `docs/INTERNALS-STORE-STATE.md:457` | — | l2-contract.test.ts×1 reconcile-resend-identity.test.ts×1 | Assumptions / open questions |
395-
| §8b | live | `docs/INTERNALS-STORE-STATE.md:513` | — | — | Suite-mined rules (2026-08-16) — index & rulings needed |
396-
| §9 | live | `docs/INTERNALS-STORE-STATE.md:749` | — | — | Decision log |
384+
| §4 | live | `docs/INTERNALS-STORE-STATE.md:210` | — | — | Identity rules |
385+
| §5 | live | `docs/INTERNALS-STORE-STATE.md:222` | store.ts×1 target.ts×1 | — | Laziness invariants (candidates for `__TEST__` assertions) |
386+
| §5b | live | `docs/INTERNALS-STORE-STATE.md:240` | target.ts×2 | — | Creation budget (phase-1 fitness) |
387+
| §5c | live | `docs/INTERNALS-STORE-STATE.md:259` | — | — | Comparison method (shipped vs rewrite) |
388+
| §6 | live | `docs/INTERNALS-STORE-STATE.md:310` | target.ts×1 | — | Structural edits — the key-set node (resolves O2, RUL-8) |
389+
| §6b | live | `docs/INTERNALS-STORE-STATE.md:338` | — | adoption-lane-rollback.test.ts×1 | Lane-aware adoption (RUL-5) |
390+
| §6c | live | `docs/INTERNALS-STORE-STATE.md:357` | — | createProjection.async.test.ts×1 flight-owned-transaction.test.ts×1 | Store-wide status gating (RUL-7) |
391+
| §6d | live | `docs/INTERNALS-STORE-STATE.md:370` | reconcile.ts×2 target.ts×2 | — | Diff reachability (RUL-11) |
392+
| §7 | live | `docs/INTERNALS-STORE-STATE.md:382` | optimistic.ts×1 projection.ts×1 | — | Projections & optimism layering |
393+
| §7b | live | `docs/INTERNALS-STORE-STATE.md:392` | scheduler.ts×1 affects.ts×1 optimistic.ts×2 projection.ts×1 reconcile.ts×1 store.ts×8 target.ts×4 | optimistic-chained-revert-3672-memo.test.ts×1 optimistic-removed-prop-3883.test.ts×1 | Chained backing (cross-store) — spec |
394+
| §8 | live | `docs/INTERNALS-STORE-STATE.md:463` | — | l2-contract.test.ts×1 reconcile-resend-identity.test.ts×1 | Assumptions / open questions |
395+
| §8b | live | `docs/INTERNALS-STORE-STATE.md:519` | — | — | Suite-mined rules (2026-08-16) — index & rulings needed |
396+
| §9 | live | `docs/INTERNALS-STORE-STATE.md:755` | — | — | Decision log |
397397
| §11 | live | `docs/NODE-SHAPE.md:29` | — | — | Stage 3 opener: the core tax map (2026-08-21) |
398398
| §11b | live | `docs/NODE-SHAPE.md:53` | constants.ts×1 | rules-index.test.ts×1 treeshake.test.ts×1 | Presence bits — hot-path monomorphism |
399399
| §11c | live | `docs/NODE-SHAPE.md:72` | — | — | Stage-3 increment log |

‎packages/signals/src/store/reconcile.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ function applyAdopt(t: StoreTarget, incoming: any, keyFn: KeyFn | null, proj = f
108108
// Descend into a changed child pair — only where something is proxied
109109
// below (§6d), never into a raw-marked leaf, never across array/object or
110110
// a key mismatch (that is a replacement, handled by the parent's slot),
111-
// never into a raw of both frames (it moved: its own row, #3712).
111+
// never into a raw of both frames (it moved: its own row, #3712). That
112+
// last rule costs a raw set per frame, so it runs only on a frame that
113+
// carries a row already (`hasRow`, one lookup per value, decided once):
114+
// a frame of fresh raws, the common one, pairs by slot.
115+
let moved: boolean | undefined;
112116
const descend = (pv: any, nv: any): void => {
113117
if (pv === null || typeof pv !== "object" || nv === null || typeof nv !== "object") return;
114118
const ct = lookupTarget(pv, fam);
@@ -123,7 +127,12 @@ function applyAdopt(t: StoreTarget, incoming: any, keyFn: KeyFn | null, proj = f
123127
if (pk !== undefined && nk !== undefined && !sameKey(pk, nk)) return;
124128
}
125129
if (!proj && keyFn !== null && !ct.d) return;
126-
if (ownRow(prevView, incoming, pv, nv)) return;
130+
// A raw that is a row already keeps it (its own, re-sent, is `ct`).
131+
if (
132+
(moved ??= hasRow(prevView, incoming, fam)) &&
133+
((lookupTarget(nv, fam) ?? ct) !== ct || ownRow(prevView, incoming, pv, nv))
134+
)
135+
return;
127136
applyAdopt(ct, nv, keyFn, proj);
128137
};
129138
const nextArr = Array.isArray(incoming);
@@ -308,3 +317,16 @@ function applyAdopt(t: StoreTarget, incoming: any, keyFn: KeyFn | null, proj = f
308317
}
309318

310319
const hasOwnP = Object.prototype.hasOwnProperty;
320+
321+
/** Does `next` carry a row of `fam` already at a slot `prev` did not — a
322+
* proxied raw that changed slot? That is the signature of a move within
323+
* the family (#3712); a frame of fresh raws has none, and an unchanged
324+
* subtree kept at its slot (an immutable update) is no move. */
325+
function hasRow(prev: any, next: any, fam: StoreFamily | null): boolean {
326+
for (const k in next) {
327+
const v = next[k];
328+
if (v !== prev[k] && v !== null && typeof v === "object" && lookupTarget(v, fam) !== undefined)
329+
return true;
330+
}
331+
return false;
332+
}

‎packages/signals/tests/adoption-lazy-child-pairing-3712.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,62 @@ describe("#3712 edges: the same raws swapped with both rows already proxied (the
438438
});
439439
}
440440
});
441+
442+
describe("#3712 edges: the same raws swapped with one row proxied (the eager pairing's slot scan)", () => {
443+
it("the proxied row follows its raw to the new slot; the other slot gets its own row", () => {
444+
const A = { name: "a" };
445+
const B = { name: "b" };
446+
const [server, setServer] = createSignal<Record<string, { name: string }>>({ x: A, y: B });
447+
let store!: Record<string, { name: string }>;
448+
let dispose!: () => void;
449+
createRoot(d => {
450+
dispose = d;
451+
[store] = createStore(() => server(), {});
452+
// Only x is read, so only A is a row before the swap.
453+
createRenderEffect(
454+
createMemo(() => store.x.name),
455+
() => {}
456+
);
457+
});
458+
flush();
459+
const xBefore = store.x;
460+
expect(xBefore.name).toBe("a");
461+
setServer({ x: B, y: A });
462+
flush();
463+
expect([store.x.name, store.y.name]).toEqual(["b", "a"]);
464+
expect(store.x).not.toBe(store.y);
465+
// A's row moved with A: the proxy read at x before the swap is y's now.
466+
expect(store.y).toBe(xBefore);
467+
expect(xBefore.name).toBe("a");
468+
dispose();
469+
});
470+
471+
it("an unchanged subtree kept at its slot pairs as before (no move)", () => {
472+
const A = { name: "a", n: 1 };
473+
const B = { name: "b", n: 1 };
474+
const [server, setServer] = createSignal<Record<string, { name: string; n: number }>>({
475+
x: A,
476+
y: B
477+
});
478+
let store!: Record<string, { name: string; n: number }>;
479+
let dispose!: () => void;
480+
createRoot(d => {
481+
dispose = d;
482+
[store] = createStore(() => server(), {});
483+
createRenderEffect(
484+
createMemo(() => store.x.n + store.y.n),
485+
() => {}
486+
);
487+
});
488+
flush();
489+
const xBefore = store.x;
490+
const yBefore = store.y;
491+
// An immutable update: x is a fresh raw, y is the same raw at its slot.
492+
setServer({ x: { name: "a", n: 2 }, y: B });
493+
flush();
494+
expect([store.x.n, store.y.n]).toEqual([2, 1]);
495+
expect(store.x).toBe(xBefore);
496+
expect(store.y).toBe(yBefore);
497+
dispose();
498+
});
499+
});

‎scripts/size/scenarios.js‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,12 +1029,16 @@ module.exports = [
10291029
// to 0.01 KB. Accepted by the maintainer 2026-10-12.
10301030
// Same PR, the swapped-raw rule moved into the pairing both paths share
10311031
// (`ownRow`, checked by the eager `descend` as well as the held
1032-
// `adoptChild`; the eager record swap aliased too): 14,879 -> 14,884 B
1033-
// brotli, 45,276 -> 45,281 B minified (+5 B each; `descend` became a
1034-
// closure of `applyAdopt`, which paid for the two frames it now reads).
1035-
// Cap set at measured + 10 B rounded up to 0.01 KB.
1036-
limit: "14.90 KB",
1037-
capMinified: 45281,
1032+
// `adoptChild`; the eager record swap aliased too). Asked of every pair
1033+
// it cost +5 B and a raw set per frame (CodSpeed: -13% to -25% on the
1034+
// saturated reconcile benches), so the eager pairing asks it only of a
1035+
// frame that carries a row at a changed slot (`hasRow`, one lookup per
1036+
// value, once per frame): 14,879 -> 14,957 B brotli, 45,276 -> 45,435 B
1037+
// minified (+78 B / +159 B; the scan, the gate, and `descend` as a
1038+
// closure of `applyAdopt` for the two frames it reads). Cap set at
1039+
// measured + 10 B rounded up to 0.01 KB.
1040+
limit: "14.97 KB",
1041+
capMinified: 45435,
10381042
alias
10391043
},
10401044
{
@@ -2365,10 +2369,12 @@ module.exports = [
23652369
// Size-Exception (#3712, 2026-10-12; the `+ createStore` note): 29.274 KB
23662370
// -> 29.36 KB, measured locally at 29,350 B against `next` @ 49a765cc's
23672371
// 29,205 (+145 B; +587 B minified, 92,291 -> 92,878). Cap set at measured
2368-
// + 10 B rounded up to 0.01 KB. The shared `ownRow` rule (the createStore
2369-
// note) measures 29,336 B brotli / 92,883 B minified: the cap holds.
2370-
limit: "29.36 KB",
2371-
capMinified: 92883,
2372+
// + 10 B rounded up to 0.01 KB. The shared `ownRow` rule behind its slot
2373+
// scan (the createStore note): 29.36 KB -> 29.39 KB, measured locally at
2374+
// 29,371 B brotli / 93,038 B minified. Cap set at measured + 10 B rounded
2375+
// up to 0.01 KB.
2376+
limit: "29.39 KB",
2377+
capMinified: 93038,
23722378
alias
23732379
},
23742380
{
@@ -3489,10 +3495,12 @@ module.exports = [
34893495
// app's store pays the held-read pairing — 25.51 KB -> 25.65 KB, measured
34903496
// locally at 25,637 B against `next` @ 49a765cc's 25,492 (+145 B; +586 B
34913497
// minified, 79,919 -> 80,505). Cap set at measured + 10 B rounded up to
3492-
// 0.01 KB. The shared `ownRow` rule (the createStore note) adds ~5 B
3493-
// minified here too, within the 20 B allowance; CI's run records it.
3494-
limit: "25.65 KB",
3495-
capMinified: 80505,
3498+
// 0.01 KB. The shared `ownRow` rule behind its slot scan (the createStore
3499+
// note) adds ~80 B brotli here too; the compiled scenarios do not build
3500+
// in the authoring container, so CI's run records the exact bytes and
3501+
// the cap takes them.
3502+
limit: "25.73 KB",
3503+
capMinified: 80670,
34963504
alias
34973505
},
34983506
{
@@ -3577,11 +3585,12 @@ module.exports = [
35773585
// Size-Exception (#3712, 2026-10-12; the `+ createStore` note): 31.386 KB
35783586
// -> 31.48 KB, measured locally at 31,466 B against `next` @ 49a765cc's
35793587
// 31,215 (+251 B; +587 B minified, 99,808 -> 100,395). Cap set at measured
3580-
// + 10 B rounded up to 0.01 KB. The shared `ownRow` rule (the createStore
3581-
// note) adds ~5 B minified here too, within the 20 B allowance; CI's run
3582-
// records it.
3583-
limit: "31.48 KB",
3584-
capMinified: 100395,
3588+
// + 10 B rounded up to 0.01 KB. The shared `ownRow` rule behind its slot
3589+
// scan (the createStore note) adds ~80 B brotli here too; the compiled
3590+
// scenarios do not build in the authoring container, so CI's run records
3591+
// the exact bytes and the cap takes them.
3592+
limit: "31.56 KB",
3593+
capMinified: 100560,
35853594
alias
35863595
},
35873596
{

0 commit comments

Comments
 (0)