Skip to content

Commit e346e61

Browse files
perf(signals): store create-floor diet — slot-signal literals + first-read dedupe (#3270)
* perf(signals): slot-signal creation diet — one literal per store leaf node Replaces the per-node options object, equals closure, unobserved closure, NodeExtension, and three post-construction expandos with a pre-shaped literal carrying _host/_key backrefs; the unobserved sweep dispatches CONFIG_SLOT_NODE to one shared hook. Suites: signals 1461, web 667, solid 580 green. getNode self −23% (warm dbmon profile); mount med flat — the GC term is NOT these short-lived objects (V8 scalar-replaces them): remaining floor terms are the get-trap read path, subscription Links, and proxies. Next slices target those. Co-authored-by: Cursor <[email protected]> * perf(signals): store first-read diet — probe once, look up once, cache the first wrap The get trap's accessor probe verdict threads to getNode (accKnown) so a first tracked read scans the descriptor once, not twice; the trap's second node-map lookup is hoisted onto the hot-path's; serveDataKey captures the node it creates so the wrap-cache branch populates px/pxv at read #1. Suites green (signals 1461 / web 667 / solid 580). dbmon mount min 6.80 -> 6.50 ms; get self −19%; getNode off the top-cost list. Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Cursor <[email protected]>
1 parent ac5159a commit e346e61

8 files changed

Lines changed: 202 additions & 55 deletions

File tree

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 leaf nodes ride `slotSignal` — one pre-shaped literal with `_host`/`_key` backrefs replacing the per-node options object, equals closure, unobserved closure, and the NodeExtension that held it; the unobserved sweep dispatches CONFIG_SLOT_NODE nodes to one shared hook. getNode self-time −23% on dbmon warm mounts.
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 first-read diet: the get trap's accessor probe verdict threads through to node creation (one descriptor scan per first read, not two), the trap's duplicate node-map lookup is hoisted, and the first tracked read populates the wrap cache so the second read skips wrapNext. dbmon mount min −4%, get-trap self-time −19%.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ export const CONFIG_FRESH_READ = 1 << 16;
115115
* (A17). Cleared at commit (the commit IS the reveal); subscribers masked
116116
* during the hold are woken by finalizePureQueue's post-revert pass. */
117117
export const CONFIG_HELD_TRUTH = 1 << 17;
118+
/** SLOT node (store leaf): created through `slotSignal` with `_host`/`_key`
119+
* backrefs baked into the literal. The unobserved sweep dispatches these to
120+
* the ONE shared hook (`setSlotUnobserved`) instead of a per-node closure
121+
* held in a per-node extension — store mounts materialize one signal per
122+
* touched leaf, so per-node allocations (options object, equals closure,
123+
* unobserved closure, NodeExtension) were the measured create-floor bytes
124+
* (warm dbmon profile: store node machinery ~36% + GC ~29%). */
125+
export const CONFIG_SLOT_NODE = 1 << 18;
118126

119127
export const STATUS_NONE = 0;
120128
export const STATUS_PENDING = 1 << 0;

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
CONFIG_NO_SNAPSHOT,
2525
CONFIG_OPTIMISTIC,
2626
CONFIG_OWNED_WRITE,
27+
CONFIG_SLOT_NODE,
2728
CONFIG_SYNC,
2829
CONFIG_TRANSPARENT,
2930
defaultContext,
@@ -873,6 +874,71 @@ export function signal<T>(
873874
return s as Signal<T>;
874875
}
875876

877+
// ---------------------------------------------------------------------------
878+
// SLOT SIGNALS (store leaves) — the create-floor diet. Store mounts
879+
// materialize one signal per touched leaf (~13 × rows on dbmon), so per-node
880+
// allocations are mount bytes: the generic path costs an options object, an
881+
// equals closure, an unobserved closure, a NodeExtension to hold it, and
882+
// three post-construction expandos (acc/px/pxv → hidden-class transitions).
883+
// slotSignal bakes everything into ONE literal: `_host`/`_key` backrefs
884+
// replace the closures (equals is a method call — `this` is the node; the
885+
// unobserved sweep dispatches CONFIG_SLOT_NODE to one shared hook), and the
886+
// store's wrap-cache fields are pre-shaped.
887+
888+
/** The shared slot-node unobserved handler — a live binding read directly by
889+
* the sweep sites (no wrapper frame, no null check: a CONFIG_SLOT_NODE node
890+
* existing implies the store module loaded and registered the hook). */
891+
export let slotUnobservedHook: (node: Signal<any>) => void;
892+
/** Install the shared slot-node unobserved handler (store module, once). */
893+
export function setSlotUnobserved(fn: (node: Signal<any>) => void): void {
894+
slotUnobservedHook = fn;
895+
}
896+
897+
export function slotSignal<T>(
898+
v: T,
899+
equals: (a: T, b: T) => boolean,
900+
host: object,
901+
key: PropertyKey,
902+
acc: boolean,
903+
firewall: Computed<unknown> | null = null
904+
): Signal<T> {
905+
const s = {
906+
_equals: equals,
907+
_config: CONFIG_OWNED_WRITE | CONFIG_SLOT_NODE,
908+
_value: v,
909+
_subs: null,
910+
_subsTail: null,
911+
_time: clock,
912+
_firewall: firewall,
913+
_nextChild: firewall?._x?._child || null,
914+
_pendingValue: NOT_PENDING,
915+
_transition: null,
916+
_notifiedAt: -1,
917+
_x: null,
918+
// Slot backrefs: what the equals/unobserved closures used to capture.
919+
_host: host,
920+
_key: key,
921+
// Store read-path caches, pre-shaped (were post-construction expandos).
922+
acc,
923+
px: undefined,
924+
pxv: undefined
925+
};
926+
if (__DEV__) {
927+
(s as any)._name = "signal";
928+
(s as any)._internal = !!firewall;
929+
}
930+
if (firewall) {
931+
ext(firewall)._child = s as unknown as FirewallSignal<unknown>;
932+
firewall._config |= CONFIG_FW_CHILDREN;
933+
}
934+
if (snapshotCaptureActive && !((firewall?._statusFlags ?? 0) & STATUS_PENDING)) {
935+
ext(s as any)._snapshotValue = v === undefined ? NO_SNAPSHOT : v;
936+
(s as any)._config |= CONFIG_HAS_SNAPSHOT;
937+
snapshotSources!.add(s);
938+
}
939+
return s as unknown as Signal<T>;
940+
}
941+
876942
export function optimisticSignal<T>(v: T, options?: NodeOptions<T>): Signal<T> {
877943
const s = signal(v, options);
878944
ext(s)._overrideValue = NOT_PENDING;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {
22
CONFIG_AUTO_DISPOSE,
3+
CONFIG_SLOT_NODE,
34
REACTIVE_DISPOSED,
45
REACTIVE_RECOMPUTING_DEPS,
56
REACTIVE_ZOMBIE,
67
STATUS_PENDING
78
} from "./constants.js";
9+
import { slotUnobservedHook } from "./core.js";
810
import { noteGraphLink, unnoteGraphLink } from "./dev.js";
911
import { deleteFromHeap, queueFor } from "./heap.js";
1012
import { disposeChildren } from "./owner.js";
@@ -25,7 +27,10 @@ export function unlinkSubs(link: Link): Link | null {
2527
else {
2628
dep._subs = nextSub;
2729
if (nextSub === null) {
28-
dep._x?._unobserved?.();
30+
// Slot nodes (store leaves) dispatch to the ONE shared hook — no
31+
// per-node unobserved closure, no NodeExtension to hold it.
32+
if (dep._config & CONFIG_SLOT_NODE) slotUnobservedHook(dep as Signal<any>);
33+
else dep._x?._unobserved?.();
2934
// No more subscribers; only tear down if CONFIG_AUTO_DISPOSE is set.
3035
// A pending node is exempt: its in-flight async work (or the
3136
// transition holding it) is an observer — tearing down would orphan

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
CONFIG_HAS_COMPANIONS,
1414
CONFIG_HAS_LANE,
1515
CONFIG_HAS_SNAPSHOT,
16+
CONFIG_SLOT_NODE,
1617
REACTIVE_IN_HEAP_HEIGHT,
1718
REACTIVE_MANUAL_WRITE,
1819
REACTIVE_MISSED_WAKE,
@@ -24,7 +25,7 @@ import {
2425
STATUS_PENDING,
2526
STATUS_UNINITIALIZED
2627
} from "./constants.js";
27-
import { currentOptimisticLane, ext } from "./core.js";
28+
import { currentOptimisticLane, ext, slotUnobservedHook } from "./core.js";
2829
import { DEV, emitDiagnostic } from "./dev.js";
2930
import { NotReadyError } from "./error.js";
3031
import { sweepDormant } from "./graph.js";
@@ -122,7 +123,8 @@ function sweepTransientStoreNodes(): void {
122123
// unmarked node for the same property).
123124
if (node._x?._affectsCount) continue;
124125
transientStoreNodes.delete(node);
125-
node._x?._unobserved?.();
126+
if (node._config & CONFIG_SLOT_NODE) slotUnobservedHook(node);
127+
else node._x?._unobserved?.();
126128
}
127129
}
128130
export function resetUnhandledAsync(): void {

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

Lines changed: 83 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ import {
4141
readNodeFast,
4242
setLatestReadActive,
4343
setSignal,
44+
setSlotUnobserved,
4445
signal,
46+
slotSignal,
4547
untrack,
4648
ext
4749
} from "../../core/core.js";
@@ -222,50 +224,70 @@ export function unwrapValue(v: any): any {
222224
// ---------------------------------------------------------------------------
223225
// nodes: pure subscription points (values used only for equality gating)
224226

225-
export function getNode(target: StoreNextTarget, key: PropertyKey, current: any): Signal<any> {
227+
// Shared slot-node equality (create-floor diet): ONE function for every
228+
// store node — `this` is the node (method-call convention at every _equals
229+
// site), `_host` is the baked-in target backref. Logical-slot equality:
230+
// values resolving to the same child target are the same slot
231+
// (privatization/adoption swap raw identity without changing the logical
232+
// value — only changed leaves notify, R9).
233+
const slotNodeEquals = function (this: any, a: any, b: any): boolean {
234+
return isEqual(a, b) || sameLogicalSlot(this._host, a, b);
235+
};
236+
237+
// Shared slot-node unobserved handler (create-floor diet): registered once;
238+
// the core sweep dispatches CONFIG_SLOT_NODE nodes here instead of holding a
239+
// per-node closure in a per-node NodeExtension.
240+
setSlotUnobserved((node: any): void => {
241+
// A live affects() mark keeps the node addressable (sweep parity).
242+
if (node._x?._affectsCount) return;
243+
const t: StoreNextTarget = node._host;
244+
const key: PropertyKey = node._key;
245+
if (t.n && t.n[key as any] === node) {
246+
delete t.n[key as any];
247+
t.nc--;
248+
}
249+
});
250+
251+
export function getNode(
252+
target: StoreNextTarget,
253+
key: PropertyKey,
254+
current: any,
255+
// First-read dedupe (create-floor slice 2): the get trap probes
256+
// accessor-ness right before creating the node — pass the verdict through
257+
// so creation skips the second descriptor scan. -1 = unknown (other
258+
// callers), 0/1 = probed.
259+
accKnown: -1 | 0 | 1 = -1
260+
): Signal<any> {
226261
const nodes = (target.n ??= Object.create(null));
227262
let node: Signal<any> | undefined = nodes[key];
228263
if (node === undefined) {
229-
const created: Signal<any> = (node = signal(
264+
// Create-floor diet: slotSignal bakes the whole node into one literal —
265+
// no options object, no equals/unobserved closures, no NodeExtension,
266+
// no post-construction expandos (acc + the wrap cache px/pxv are
267+
// pre-shaped fields: the proxy last served for this key and the raw it
268+
// wrapped — one pointer compare replaces the per-read WeakMap lookup in
269+
// wrapNext). ownedWrite rides the literal's config: the setter carries
270+
// the owned-scope write guard; node-level setSignals are internal
271+
// notification machinery. Projection nodes carry the projection
272+
// computed as their firewall: reads through them link the derive's
273+
// status/lifecycle (§7b).
274+
const created: Signal<any> = (node = slotSignal(
230275
current,
231-
{
232-
// Attribution-only: name store property nodes by path segment so
233-
// attribution chains and wide-scope warnings read "store.todos", not
234-
// "signal". Gated on the engine being installed — node creation is
235-
// the hottest store path, and the disabled cost must stay one null
236-
// check (nodes created before enable() stay generically named).
237-
name: __DEV__ && attrHooks !== null ? "store." + String(key) : undefined,
238-
// Logical-slot equality: values resolving to the same child target
239-
// are the same slot (privatization/adoption swap raw identity without
240-
// changing the logical value — only changed leaves notify, R9).
241-
equals: (a: any, b: any) => isEqual(a, b) || sameLogicalSlot(target, a, b),
242-
unobserved() {
243-
// A live affects() mark keeps the node addressable (sweep parity).
244-
if ((created as any)._x?._affectsCount) return;
245-
if (target.n && target.n[key] === created) {
246-
delete target.n[key];
247-
target.nc--;
248-
}
249-
}
250-
},
251-
// Projection nodes carry the projection computed as their firewall:
252-
// reads through them link the derive's status/lifecycle (§7b).
276+
slotNodeEquals,
277+
target,
278+
key,
279+
// Accessor-ness resolved ONCE per node (no per-object descriptor
280+
// scan on reads): accessor keys serve through Reflect.get with the
281+
// proxy receiver.
282+
accKnown === -1 ? isOwnAccessor(target.pb ?? target.v, key) : accKnown === 1,
253283
(target.fam?.node as any) ?? undefined
254284
));
255-
// Store nodes are ownedWrite: the setter carries the owned-scope write
256-
// guard; node-level setSignals are internal notification machinery.
257-
created._config |= CONFIG_OWNED_WRITE;
258-
// Accessor-ness resolved ONCE per node (no per-object descriptor scan):
259-
// accessor keys serve through Reflect.get with the proxy receiver.
260-
(created as any).acc = isOwnAccessor(target.pb ?? target.v, key);
261-
// Wrap cache: the proxy last served for this key and the raw it wrapped.
262-
// Raw-as-truth stores raw in nodes, so every object read needs a wrapper;
263-
// one pointer compare (pxv === value) replaces the per-read WeakMap
264-
// lookup in wrapNext — the dominant read-path cost vs legacy, whose
265-
// nodes stored pre-wrapped values. A replaced child fails the compare
266-
// and re-wraps; at most one stale proxy is pinned until the next read.
267-
(created as any).px = undefined;
268-
(created as any).pxv = undefined;
285+
// Attribution-only: name store property nodes by path segment so
286+
// attribution chains and wide-scope warnings read "store.todos", not
287+
// "signal". Gated on the engine being installed — node creation is
288+
// the hottest store path, and the disabled cost must stay one null
289+
// check (nodes created before enable() stay generically named).
290+
if (__DEV__ && attrHooks !== null) (created as any)._name = "store." + String(key);
269291
// Optimistic families: arm the override slot — setSignal routes armed
270292
// nodes through the core engine (lanes, ownership, reverts all native).
271293
if (target.fam?.opt) {
@@ -1379,7 +1401,8 @@ function serveDataKey(
13791401
key: PropertyKey,
13801402
backingValue: any,
13811403
src: Record<PropertyKey, any>,
1382-
node?: Signal<any>
1404+
node?: Signal<any>,
1405+
accKnown: -1 | 0 | 1 = -1
13831406
): any {
13841407
const chained = target.ch && src === target.v;
13851408
let v = backingValue;
@@ -1428,7 +1451,9 @@ function serveDataKey(
14281451
v = nodeValue(node, backingValue);
14291452
}
14301453
} else if (getObserver() !== null) {
1431-
readNode(getNode(target, key, backingValue));
1454+
// First tracked read: create + link, and let the wrap-cache branch
1455+
// below populate px/pxv so read #2 skips wrapNext (slice 2).
1456+
readNode((node = getNode(target, key, backingValue, accKnown)));
14321457
}
14331458
}
14341459
// Shallow stores serve data raw; store-proxy slots get boundary wrappers.
@@ -1533,8 +1558,11 @@ const traps: ProxyHandler<StoreNextTarget> = {
15331558
// tracked read of a present data key — the dbmon/uibench effect re-read
15341559
// shape. Skips serveDataKey's frame, the FORCE compare (only accessor
15351560
// keys ever hold the sentinel), and isWrappable for primitives.
1561+
// ONE node-map lookup serves this block and the accessor probe below
1562+
// (nothing between them creates nodes).
1563+
const node0 = target.n?.[key as any];
15361564
if (target.ch === false && writeScopes === null) {
1537-
const nodeH = target.n?.[key as any];
1565+
const nodeH = node0;
15381566
if (nodeH !== undefined && (nodeH as any).acc !== true && getObserver() !== null) {
15391567
let nv = readNodeFast(nodeH);
15401568
if (nv === READ_SLOW) nv = readNode(nodeH);
@@ -1588,15 +1616,21 @@ const traps: ProxyHandler<StoreNextTarget> = {
15881616
// absent-key/accessor subscriptions for every store read during any
15891617
// derive, leaving nested projections permanently dependency-less when
15901618
// their sources hadn't materialized yet (#3037).
1591-
const node0 = target.n?.[key as any];
1619+
// First-read dedupe (create-floor slice 2): remember the probe verdict —
1620+
// node creation downstream reuses it instead of re-scanning the
1621+
// descriptor, but only when the probed object IS the one getNode would
1622+
// scan (pb ?? v).
1623+
let accProbe: -1 | 0 | 1 = -1;
15921624
{
1593-
const acc =
1594-
node0 !== undefined
1595-
? (node0 as any).acc === true
1596-
: !inDraft(target) && getObserver() !== null && isOwnAccessor(src, key);
1625+
let acc: boolean;
1626+
if (node0 !== undefined) acc = (node0 as any).acc === true;
1627+
else if (!inDraft(target) && getObserver() !== null) {
1628+
acc = isOwnAccessor(src, key);
1629+
if (src === (target.pb ?? target.v)) accProbe = acc ? 1 : 0;
1630+
} else acc = false;
15971631
if (acc) {
15981632
if (!inDraft(target) && getObserver() !== null)
1599-
readNode(node0 ?? getNode(target, key, undefined));
1633+
readNode(node0 ?? getNode(target, key, undefined, accProbe));
16001634
const v = Reflect.get(src, key, receiver);
16011635
if (target.s) return serveShallow(target, key, v);
16021636
return isWrappable(v) ? draftServe(target, wrapNext(v, target, key)) : v;
@@ -1626,7 +1660,7 @@ const traps: ProxyHandler<StoreNextTarget> = {
16261660
// Reading a currently-absent own key subscribes to it (R12) — for any
16271661
// target OUTSIDE its own draft scope, even mid-setter (#3037, above).
16281662
if (v === undefined && !inDraft(target)) {
1629-
if (getObserver() !== null) readNode(getNode(target, key, undefined));
1663+
if (getObserver() !== null) readNode(getNode(target, key, undefined, accProbe));
16301664
const node = target.n?.[key];
16311665
if (node) {
16321666
const nv = nodeValue(node, undefined);
@@ -1655,7 +1689,7 @@ const traps: ProxyHandler<StoreNextTarget> = {
16551689
!(viewOvl && hasOwn.call(target.v, key))
16561690
)
16571691
return v; // proto method
1658-
return serveDataKey(target, key, v, src, node0);
1692+
return serveDataKey(target, key, v, src, node0, accProbe);
16591693
},
16601694

16611695
has(target, key) {

0 commit comments

Comments
 (0)