Skip to content

Commit 5aee76d

Browse files
ryansolidClaude via Cursorcursoragent
committed
perf(store): stamp owned backings with $OWNER instead of weak-collection registrations (#3360)
Every backing the store allocates (CoW clones, privatized committed backings) now carries its owning target under an enumerable symbol. This replaces the per-draft ownedRaw.add + raw->target map.set that dominated the one-key write floor (V8 identity hash + ephemeron registration on a fresh object). storeNextLookup / family maps now hold only unowned raws; lookupTarget resolves stamp-first within a family. The stamp is hidden by the get/has/getOwnPropertyDescriptor/ownKeys traps, skipped by snapshot, membership and deep-witness diffs, reconcile, optimistic staging and affects walks, and never acquires a node. Steady-state single-key writes: 340 -> ~178 ns (rc.7 674, rc.0 214). Fixes #3360 Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 0f14430 commit 5aee76d

9 files changed

Lines changed: 387 additions & 94 deletions

File tree

‎.changeset/store-owner-stamp.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+
Store-owned backings now carry their owning target under an internal enumerable symbol stamp instead of registering in two weak collections (ownership set + raw→target map) on every draft — the identity-hash/ephemeron cost of those registrations was the remaining floor of a one-key store write (#3360, part two). Steady-state single-key writes drop from ~340 ns to ~180 ns; reconcile and projection benches improve 10–80%. The stamp is invisible through the proxy (`ownKeys`, `in`, descriptors, spreads), never appears in `snapshot()` output, never acquires a node, and is skipped by every key walk (membership/deep-witness diffs, reconcile, optimistic staging, affects scopes).

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ import {
7878
// Cycle with reconcile.js is benign: the binding resolves at call time (the
7979
// optimistic write), long after both modules initialize.
8080
import { sameKey } from "./reconcile.js";
81-
import { setOptHooks, storeNextLookup } from "./target.js";
81+
import { $OWNER, lookupTarget, setOptHooks } from "./target.js";
8282
type KeyFn = (item: any) => any;
8383
import { isRawValue, isWrappable, rawValuesUsed, setNextOptimisticViewResolver } from "../store.js";
8484
import type { StoreNextFamily, StoreNextTarget } from "./target.js";
@@ -501,7 +501,7 @@ function stagedApply(cur: any, incoming: any, keyFn: KeyFn | null): void {
501501
// Object merge; also the degenerate root-kind-change shape (arrays accept
502502
// keyed writes/deletes, so a wholesale restatement still lands staged).
503503
for (const k of Reflect.ownKeys(incoming)) {
504-
if (curArr && k === "length") continue;
504+
if ((curArr && k === "length") || k === $OWNER) continue;
505505
const nv = (incoming as any)[k];
506506
const pv = unwrapValue(cur[k]);
507507
if (pv === nv) continue;
@@ -522,7 +522,7 @@ function stagedApply(cur: any, incoming: any, keyFn: KeyFn | null): void {
522522
}
523523
}
524524
for (const k of Reflect.ownKeys(cur)) {
525-
if ((curArr && k === "length") || k in incoming) continue;
525+
if ((curArr && k === "length") || k === $OWNER || k in incoming) continue;
526526
delete cur[k];
527527
}
528528
}
@@ -571,7 +571,7 @@ export function notifyOptimisticWrites(t: StoreNextTarget, pb: Record<PropertyKe
571571
let structural = false;
572572
const isArr = Array.isArray(pb);
573573
for (const key of Reflect.ownKeys(pb)) {
574-
if (isArr && key === "length") continue;
574+
if ((isArr && key === "length") || key === $OWNER) continue;
575575
const nv = unwrapValue(pb[key as any]);
576576
if (!visiblePresent(key)) {
577577
// Optimistic add: value node + presence node + membership bump.
@@ -587,7 +587,7 @@ export function notifyOptimisticWrites(t: StoreNextTarget, pb: Record<PropertyKe
587587
}
588588
}
589589
for (const key of Reflect.ownKeys(old)) {
590-
if (isArr && key === "length") continue;
590+
if ((isArr && key === "length") || key === $OWNER) continue;
591591
if (key in pb || !visiblePresent(key)) continue;
592592
// Optimistic delete: node reads undefined, presence flips, membership bumps.
593593
setSignal(getNode(t, key, old[key as any]), () => undefined);
@@ -654,15 +654,15 @@ export function optimisticView(
654654
function applyTentative(t: StoreNextTarget, incoming: any, keyFn: KeyFn | null): void {
655655
const base = t.pb ?? t.v;
656656
const view = optimisticView(t, base);
657-
const map = t.fam!.map;
657+
const fam = t.fam!;
658658
const isArr = Array.isArray(incoming);
659659
if (Array.isArray(view) !== isArr) return; // kind change at root: flat overrides below
660660
const pairs: Array<[StoreNextTarget, any]> = [];
661661
let pbLike: any;
662662
if (isArr) pbLike = [...(incoming as any[])];
663663
else {
664664
pbLike = {};
665-
for (const k of Reflect.ownKeys(incoming)) pbLike[k] = (incoming as any)[k];
665+
for (const k of Reflect.ownKeys(incoming)) if (k !== $OWNER) pbLike[k] = (incoming as any)[k];
666666
}
667667
const match = (pv: any, nv: any): StoreNextTarget | null => {
668668
if (!isWrappable(pv) || !isWrappable(nv)) return null;
@@ -675,7 +675,7 @@ function applyTentative(t: StoreNextTarget, incoming: any, keyFn: KeyFn | null):
675675
// channel — NaN keys are self-equal.
676676
if (pk !== undefined && nk !== undefined && !sameKey(pk, nk)) return null;
677677
}
678-
return map.get(unwrapValue(pv)) ?? null;
678+
return lookupTarget(unwrapValue(pv), fam) ?? null;
679679
};
680680
if (isArr) {
681681
const viewRows = view as any[];
@@ -724,6 +724,7 @@ function applyTentative(t: StoreNextTarget, incoming: any, keyFn: KeyFn | null):
724724
}
725725
} else {
726726
for (const k of Reflect.ownKeys(incoming)) {
727+
if (k === $OWNER) continue;
727728
const pv = unwrapValue((view as any)[k]);
728729
const nv = (incoming as any)[k];
729730
const ct = match(pv, nv);

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ import {
4040
unwrapValue
4141
} from "./store.js";
4242
import {
43-
ownedRaw,
43+
$OWNER,
44+
isOwned,
45+
lookupTarget,
4446
storeNextLookup,
4547
type StoreNextFamily,
4648
type StoreNextTarget,
@@ -96,7 +98,10 @@ export function reconcileNextState(
9698
// across an entity change even when their own keys align (proj R7).
9799
// Displaced-raw unregistration (proj R10): the outgoing raw stops
98100
// resolving to this proxy; re-handed later it wraps fresh.
99-
(t.fam?.map ?? storeNextLookup).delete(t.pb ?? t.v);
101+
const out = t.pb ?? t.v;
102+
if (isOwned(out))
103+
delete (out as any)[$OWNER]; // disown: wraps fresh if re-handed
104+
else (t.fam?.map ?? storeNextLookup).delete(out);
100105
adoptPB(t, incoming);
101106
return;
102107
}
@@ -116,7 +121,7 @@ export function reconcileNextState(
116121
function applyAdopt(t: StoreNextTarget, incoming: any, keyFn: KeyFn | null, proj = false): void {
117122
const prev = t.pb ?? t.v;
118123
// The sound identity skip (O7): same reference AND we never diverged it.
119-
if (incoming === prev && !ownedRaw.has(prev)) return;
124+
if (incoming === prev && !isOwned(prev)) return;
120125
const fam = t.fam;
121126
// §6b (R28): the diff's previous-arrangement baseline is the LANE VIEW —
122127
// optimistic rows must be visible to key matching so a landing carrying the
@@ -181,7 +186,7 @@ function applyAdopt(t: StoreNextTarget, incoming: any, keyFn: KeyFn | null, proj
181186
break; // misaligned: fall to the keyed remainder below
182187
// Identity skip inline (FINDING-1 guard), then descend the pair.
183188
if (
184-
(pvRaw !== nv || (nv !== null && typeof nv === "object" && ownedRaw.has(nv))) &&
189+
(pvRaw !== nv || (nv !== null && typeof nv === "object" && isOwned(nv))) &&
185190
nv !== null &&
186191
typeof nv === "object"
187192
)
@@ -319,7 +324,7 @@ function applyAdopt(t: StoreNextTarget, incoming: any, keyFn: KeyFn | null, proj
319324
const isObj = nv !== null && typeof nv === "object";
320325
if (
321326
ov === nv &&
322-
(!isObj || !ownedRaw.has(nv)) &&
327+
(!isObj || !isOwned(nv)) &&
323328
(nodes === null || nodes[k] === undefined || !hasAccessorFlag(nodes[k]))
324329
) {
325330
if (nodes !== null && nodes[k] !== undefined) nodesHit++;
@@ -345,6 +350,7 @@ function applyAdopt(t: StoreNextTarget, incoming: any, keyFn: KeyFn | null, proj
345350
const syms = Object.getOwnPropertySymbols(incoming);
346351
for (let i = 0; i < syms.length; i++) {
347352
const k = syms[i];
353+
if (k === $OWNER) continue;
348354
const nv = (incoming as any)[k];
349355
if (!shallow && nv !== null && typeof nv === "object")
350356
descend(unwrapValue((prevView as any)[k]), nv, keyFn, fam, proj);
@@ -396,7 +402,7 @@ function descend(
396402
// wrappables acquire targets; rawValues never wrap) — one WeakMap get
397403
// replaces isWrappable(pv) + isRawValue(pv), and a miss prunes untracked
398404
// subtrees before any further checks.
399-
const ct = (fam?.map ?? storeNextLookup).get(pv);
405+
const ct = lookupTarget(pv, fam);
400406
if (ct === undefined) return; // nothing proxied below this pair
401407
// The NEW side still validates fully: a frozen/platform/markRaw'd incoming
402408
// value is a leaf for reconcile — replaced by reference, never recursed

0 commit comments

Comments
 (0)