Skip to content

Commit 5ed808f

Browse files
committed
revert(signals): keep the swapped-raw rule on the held path only
Reverts eae477f (the rule on every eager pair: -13% to -25% on the saturated reconcile benches) and d20ce53 (the rule behind a per-frame scan: +86 B brotli over this form on createStore). The PR returns to b7ece8c's form, +36 B over its first commit; the eager record swap that the reverted commits fixed is on `next` today and gets its own issue. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_015Q7JD9PktsxtBJJbWmtuwr
1 parent d20ce53 commit 5ed808f

6 files changed

Lines changed: 73 additions & 262 deletions

File tree

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,7 @@ there is nothing to diff.)
195195
is asked. Container → key → leaf is the end of this series; there is no
196196
finer unit. Not paired (the hold stands): a shallow family, a reorder (the
197197
keys differ), and a `reconcile()` in a plain setter, whose key the target
198-
does not carry. Nor, in either pairing, a raw present in both frames — the
199-
same objects swapped or reordered (`{x: A, y: B}` to `{x: B, y: A}`): it
200-
moved, it did not change, and it is its own row (`ownRow`, the one rule
201-
`descend` and `adoptChild` share). Paired, the two would alias after the
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).
198+
does not carry.
209199

210200
## 4. Identity rules
211201

‎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: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- |
127+
| RUL-1 | resolved | `docs/INTERNALS-STORE-STATE.md:534` | 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:543` | — | 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:642` | optimistic.ts×1 | — | - **RUL-3 — RESOLVED (verified 2026-08-16).** Ownership already lives |
130+
| RUL-4 | resolved | `docs/INTERNALS-STORE-STATE.md:649` | — | 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:662` | — | 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:666` | — | — | - **RUL-6 — reclassified: SPEC WORK, not a ruling.** Live chaining (#2941 — |
133+
| RUL-7 | live | `docs/INTERNALS-STORE-STATE.md:675` | — | — | - **RUL-7 — SPEC'D (2026-08-17): §6c.** One status field on the root target, |
134+
| RUL-8 | live | `docs/INTERNALS-STORE-STATE.md:678` | — | 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:681` | — | — | - **RUL-9 — RESOLVED (2026-08-17, parity by construction).** Every piece of |
136+
| RUL-10 | live | `docs/INTERNALS-STORE-STATE.md:686` | — | — | - **RUL-10 — The equality trio.** One precise rule needed spanning: no-op |
137+
| RUL-11 | live | `docs/INTERNALS-STORE-STATE.md:692` | — | — | - **RUL-11 — SPEC'D (2026-08-17): §6d.** Sticky descendants flag ported from |
138+
| RUL-12 | live | `docs/INTERNALS-STORE-STATE.md:694` | 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:740` | — | — | - **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: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 |
384+
| §4 | live | `docs/INTERNALS-STORE-STATE.md:200` | — | — | Identity rules |
385+
| §5 | live | `docs/INTERNALS-STORE-STATE.md:212` | store.ts×1 target.ts×1 | — | Laziness invariants (candidates for `__TEST__` assertions) |
386+
| §5b | live | `docs/INTERNALS-STORE-STATE.md:230` | target.ts×2 | — | Creation budget (phase-1 fitness) |
387+
| §5c | live | `docs/INTERNALS-STORE-STATE.md:249` | — | — | Comparison method (shipped vs rewrite) |
388+
| §6 | live | `docs/INTERNALS-STORE-STATE.md:300` | target.ts×1 | — | Structural edits — the key-set node (resolves O2, RUL-8) |
389+
| §6b | live | `docs/INTERNALS-STORE-STATE.md:328` | — | adoption-lane-rollback.test.ts×1 | Lane-aware adoption (RUL-5) |
390+
| §6c | live | `docs/INTERNALS-STORE-STATE.md:347` | — | 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:360` | reconcile.ts×2 target.ts×2 | — | Diff reachability (RUL-11) |
392+
| §7 | live | `docs/INTERNALS-STORE-STATE.md:372` | optimistic.ts×1 projection.ts×1 | — | Projections & optimism layering |
393+
| §7b | live | `docs/INTERNALS-STORE-STATE.md:382` | 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:453` | — | l2-contract.test.ts×1 reconcile-resend-identity.test.ts×1 | Assumptions / open questions |
395+
| §8b | live | `docs/INTERNALS-STORE-STATE.md:509` | — | — | Suite-mined rules (2026-08-16) — index & rulings needed |
396+
| §9 | live | `docs/INTERNALS-STORE-STATE.md:745` | — | — | 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: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
notifyFoldTail,
3232
notifyKeyDiff,
3333
notifyKeyValue,
34-
ownRow,
3534
sameKey,
3635
sameRow,
3736
targetsEqual,
@@ -105,36 +104,6 @@ function applyAdopt(t: StoreTarget, incoming: any, keyFn: KeyFn | null, proj = f
105104
// — key matching from the arrangement the guesses made (R28), so a row
106105
// the guess added keeps its proxy when the server returns it.
107106
const prevView = fam?.opt === true ? optHooks!.view(t, prev) : prev;
108-
// Descend into a changed child pair — only where something is proxied
109-
// below (§6d), never into a raw-marked leaf, never across array/object or
110-
// 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). 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;
116-
const descend = (pv: any, nv: any): void => {
117-
if (pv === null || typeof pv !== "object" || nv === null || typeof nv !== "object") return;
118-
const ct = lookupTarget(pv, fam);
119-
if (ct === undefined) return; // nothing proxied below this pair
120-
if (!isWrappable(nv)) return;
121-
if (rawValuesUsed && isRawValue(nv)) return;
122-
nv = unwrapValue(nv);
123-
if (Array.isArray(pv) !== Array.isArray(nv)) return;
124-
if (keyFn) {
125-
const pk = keyFn(pv);
126-
const nk = keyFn(nv);
127-
if (pk !== undefined && nk !== undefined && !sameKey(pk, nk)) return;
128-
}
129-
if (!proj && keyFn !== null && !ct.d) 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;
136-
applyAdopt(ct, nv, keyFn, proj);
137-
};
138107
const nextArr = Array.isArray(incoming);
139108
const shallow = t.s === true;
140109
const old = prev;
@@ -164,7 +133,7 @@ function applyAdopt(t: StoreTarget, incoming: any, keyFn: KeyFn | null, proj = f
164133
nv !== null &&
165134
typeof nv === "object"
166135
)
167-
descend(unwrapValue(pvRaw), nv);
136+
descend(unwrapValue(pvRaw), nv, keyFn, fam, proj);
168137
if (
169138
t.dk !== null &&
170139
!dkBumpedA &&
@@ -216,7 +185,7 @@ function applyAdopt(t: StoreTarget, incoming: any, keyFn: KeyFn | null, proj = f
216185
} else {
217186
pv = unwrapValue(prevRows[i]); // keyless item: positional fallback
218187
}
219-
descend(pv, nv);
188+
descend(pv, nv, keyFn, fam, proj);
220189
}
221190
if (nodes !== null) {
222191
const node = nodes[i];
@@ -233,7 +202,7 @@ function applyAdopt(t: StoreTarget, incoming: any, keyFn: KeyFn | null, proj = f
233202
for (let i = 0; i < nlen; i++) {
234203
const nvP = nextRows[i];
235204
if (!shallow && i < dlen && nvP !== null && typeof nvP === "object")
236-
descend(unwrapValue(prevRows[i]), nvP);
205+
descend(unwrapValue(prevRows[i]), nvP, keyFn, fam, proj);
237206
if (
238207
t.dk !== null &&
239208
!dkBumpedP &&
@@ -279,7 +248,7 @@ function applyAdopt(t: StoreTarget, incoming: any, keyFn: KeyFn | null, proj = f
279248
if (nodes !== null && nodes[k] !== undefined) nodesHit++;
280249
continue;
281250
}
282-
if (isObj && !shallow) descend(unwrapValue((prevView as any)[k]), nv);
251+
if (isObj && !shallow) descend(unwrapValue((prevView as any)[k]), nv, keyFn, fam, proj);
283252
if (t.dk !== null && !dkBumped && !(isObj ? targetsEqual(ov, nv) : isEqual(ov, nv))) {
284253
bumpDeep(t);
285254
dkBumped = true;
@@ -298,7 +267,7 @@ function applyAdopt(t: StoreTarget, incoming: any, keyFn: KeyFn | null, proj = f
298267
if (k === $OWNER) continue;
299268
const nv = (incoming as any)[k];
300269
if (!shallow && nv !== null && typeof nv === "object")
301-
descend(unwrapValue((old as any)[k]), nv);
270+
descend(unwrapValue((old as any)[k]), nv, keyFn, fam, proj);
302271
if (nodes !== null) {
303272
const node = nodes[k as any];
304273
if (node !== undefined) {
@@ -318,15 +287,28 @@ function applyAdopt(t: StoreTarget, incoming: any, keyFn: KeyFn | null, proj = f
318287

319288
const hasOwnP = Object.prototype.hasOwnProperty;
320289

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;
290+
/** Descend into a changed child pair — only where something is proxied
291+
* below (§6d), never into a raw-marked leaf, never across array/object or a
292+
* key mismatch (that is a replacement, handled by the parent's slot). */
293+
function descend(
294+
pv: any,
295+
nv: any,
296+
keyFn: KeyFn | null,
297+
fam: StoreFamily | null,
298+
proj = false
299+
): void {
300+
if (pv === null || typeof pv !== "object" || nv === null || typeof nv !== "object") return;
301+
const ct = lookupTarget(pv, fam);
302+
if (ct === undefined) return; // nothing proxied below this pair
303+
if (!isWrappable(nv)) return;
304+
if (rawValuesUsed && isRawValue(nv)) return;
305+
nv = unwrapValue(nv);
306+
if (Array.isArray(pv) !== Array.isArray(nv)) return;
307+
if (keyFn) {
308+
const pk = keyFn(pv);
309+
const nk = keyFn(nv);
310+
if (pk !== undefined && nk !== undefined && !sameKey(pk, nk)) return;
330311
}
331-
return false;
312+
if (!proj && keyFn !== null && !ct.d) return;
313+
applyAdopt(ct, nv, keyFn, proj);
332314
}

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,28 +1592,16 @@ function heldKeyChanged(target: StoreTarget, key: PropertyKey, shape = true): bo
15921592
);
15931593
}
15941594

1595-
/** The values of a frame, built once per frame: an adoption's two frames
1596-
* do not change while it pairs their rows (a held one stands until its
1597-
* commit; a draft writes a clone), so the set stays true for every pairing
1598-
* the frame takes part in. */
1595+
/** The values of a held frame, built once per frame: neither the committed
1596+
* frame nor the held staging changes while the hold stands (a draft writes a
1597+
* clone), so the set stays true for every first read of the hold. */
15991598
const frameRaws = new WeakMap<object, Set<unknown>>();
16001599
function rawsOf(frame: object): Set<unknown> {
16011600
let raws = frameRaws.get(frame);
16021601
if (raws === undefined) frameRaws.set(frame, (raws = new Set(Object.values(frame))));
16031602
return raws;
16041603
}
16051604

1606-
/** A raw present in both frames is its own row (#3712): the same objects
1607-
* swapped or reordered — `[A, B]` to `[B, A]`, `{x: A, y: B}` to
1608-
* `{x: B, y: A}` — moved, they did not change. Pairing `pv`'s target with
1609-
* `nv` would alias the two after the commit (both slots reading one of
1610-
* them); unpaired, each raw keeps its target and the slots change. Checked
1611-
* where rows are paired, eagerly (`descend`) and at a held first read
1612-
* (`adoptChild`). */
1613-
export function ownRow(prev: object, next: object, pv: object, nv: object): boolean {
1614-
return rawsOf(prev).has(nv) || rawsOf(next).has(pv);
1615-
}
1616-
16171605
/** The same row at a slot, as the adoption pairs rows (reconcile.ts): two
16181606
* objects whose keys agree — positionally for an unkeyed family. */
16191607
export function sameRow(pv: any, nv: any, keyFn: ((item: any) => any) | null): boolean {
@@ -1649,7 +1637,10 @@ function adoptChild(target: StoreTarget, key: PropertyKey): boolean {
16491637
Array.isArray(ov) !== Array.isArray(nv) ||
16501638
lookupTarget(ov, target.fam) !== undefined ||
16511639
lookupTarget(nv, target.fam) !== undefined ||
1652-
ownRow(old, k._pendingValue as object, ov, nv)
1640+
// A raw in both frames (the same objects swapped or reordered) is its
1641+
// own row: pairing it with another would alias the two after the commit.
1642+
rawsOf(old).has(nv) ||
1643+
rawsOf(k._pendingValue as object).has(ov)
16531644
)
16541645
return false;
16551646
// A raw-marked value wraps to itself, a proxy to its own target: neither

0 commit comments

Comments
 (0)