Skip to content

Commit 833cbdd

Browse files
perf(signals): shared traps for merge()/omit() proxies, state on the target
Each merge()/omit() proxy allocated a target holding three fresh closures (get/has/keys) over the instance's sources and forwarded every trap call through them. Keep the sources/keys on the target under symbol keys and share one handler per primitive instead: a props proxy is a Proxy plus a one- or two-slot object, and a read goes straight from the trap to the sources. The state keys are never reported by ownKeys nor answered by get/has; a string defineProperty on the proxy lands on the target without touching them (previously it could overwrite the target's `get` method). Behaviour is unchanged: `$SOURCES` still answers the flat list on a merge proxy (mergeSources/spread) and undefined on an omit proxy (#3014); the has/ownKeys filtering is the same. Port of #3391 (main) to next. Interleaved A/B, old vs new dist in one process, on battery (±20% noise): retained 200-instance merge+omit batch 0.87–0.93×, nested merge over a merge proxy 0.65–0.75×, Object.keys / spread 0.90–0.98×; single construct and reads within noise in process-isolated alternating runs. Co-authored-by: Cursor <[email protected]>
1 parent 5b31076 commit 833cbdd

3 files changed

Lines changed: 119 additions & 66 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+
`merge()` and `omit()` proxies keep their per-instance state on the proxy target under symbol keys and share one handler each, instead of allocating a target with three closures (`get`/`has`/`keys`) per instance and forwarding every trap through them. Creating a props proxy is now a proxy plus a one- or two-slot object, and reads go straight from the trap to the sources. The state keys are never reported or answered through the proxy, and a string `defineProperty` on the proxy can no longer clobber its internals. Port of solidjs/solid#3391 to the 2.0 primitives.

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

Lines changed: 89 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,101 @@ function trueFn() {
77
return true;
88
}
99

10-
const propTraps: ProxyHandler<{
11-
get: (k: string | number | symbol) => any;
12-
has: (k: string | number | symbol) => boolean;
13-
keys: () => (string | symbol)[];
14-
}> = {
15-
get(_, property, receiver) {
10+
// The merge() and omit() proxies keep their per-instance state on the proxy
11+
// TARGET under symbol keys and share one handler each, so creating one costs
12+
// a proxy plus a one- or two-slot object — no per-instance trap closures —
13+
// and a read goes straight from the trap to the sources. The state keys are
14+
// never reported by `ownKeys` and never answered by `get`/`has`, so they are
15+
// invisible through the proxy.
16+
17+
const $SOURCES = Symbol(__DEV__ ? "MERGE_SOURCE" : 0);
18+
type MergeTarget = { [$SOURCES]: any[] };
19+
20+
function mergeGet(sources: any[], property: PropertyKey) {
21+
for (let i = sources.length - 1; i >= 0; i--) {
22+
const s = resolveSource(sources[i]);
23+
if (property in s) return s[property];
24+
}
25+
}
26+
27+
const mergeTraps: ProxyHandler<MergeTarget> = {
28+
get(target, property, receiver) {
29+
if (property === $PROXY) return receiver;
30+
const sources = target[$SOURCES];
31+
// The flat source list, for mergeSources() and for a nested merge.
32+
if (property === $SOURCES) return sources;
33+
return mergeGet(sources, property);
34+
},
35+
has(target, property) {
36+
if (property === $PROXY) return true;
37+
const sources = target[$SOURCES];
38+
for (let i = sources.length - 1; i >= 0; i--) {
39+
if (property in resolveSource(sources[i])) return true;
40+
}
41+
return false;
42+
},
43+
set: trueFn,
44+
deleteProperty: trueFn,
45+
getOwnPropertyDescriptor(target, property) {
46+
return {
47+
configurable: true,
48+
enumerable: true,
49+
get: () => mergeGet(target[$SOURCES], property),
50+
set: trueFn
51+
};
52+
},
53+
ownKeys(target) {
54+
const sources = target[$SOURCES];
55+
const keys = new Set<string | symbol>();
56+
for (let i = 0; i < sources.length; i++) {
57+
const sourceKeys = ownEnumerableKeys(resolveSource(sources[i]));
58+
for (let j = 0; j < sourceKeys.length; j++) keys.add(sourceKeys[j]);
59+
}
60+
return [...keys];
61+
}
62+
};
63+
64+
const $OMIT_PROPS = Symbol(__DEV__ ? "OMIT_PROPS" : 0);
65+
const $OMIT_KEYS = Symbol(__DEV__ ? "OMIT_KEYS" : 0);
66+
type OmitTarget = { [$OMIT_PROPS]: Record<PropertyKey, any>; [$OMIT_KEYS]: readonly PropertyKey[] };
67+
68+
function omitGet(target: OmitTarget, property: PropertyKey) {
69+
// $SOURCES must not tunnel through the filter: merge() flattens whatever
70+
// answers it, so forwarding would hand a re-merge the UNFILTERED sources
71+
// of an underlying merge proxy and the omitted keys leak back in (#3014 —
72+
// the SSR element-spread path re-merges static attributes with the rest
73+
// object). Opaque here: merge composes omit proxies through their traps.
74+
return property === $SOURCES || target[$OMIT_KEYS].includes(property)
75+
? undefined
76+
: target[$OMIT_PROPS][property];
77+
}
78+
79+
const omitTraps: ProxyHandler<OmitTarget> = {
80+
get(target, property, receiver) {
1681
if (property === $PROXY) return receiver;
17-
return _.get(property);
82+
return omitGet(target, property);
1883
},
19-
has(_, property) {
84+
has(target, property) {
2085
if (property === $PROXY) return true;
21-
return _.has(property);
86+
return (
87+
property !== $SOURCES &&
88+
!target[$OMIT_KEYS].includes(property) &&
89+
property in target[$OMIT_PROPS]
90+
);
2291
},
2392
set: trueFn,
2493
deleteProperty: trueFn,
25-
getOwnPropertyDescriptor(_, property) {
94+
getOwnPropertyDescriptor(target, property) {
2695
return {
2796
configurable: true,
2897
enumerable: true,
29-
get() {
30-
return _.get(property);
31-
},
32-
set: trueFn,
33-
deleteProperty: trueFn
98+
get: () => omitGet(target, property),
99+
set: trueFn
34100
};
35101
},
36-
ownKeys(_) {
37-
return _.keys();
102+
ownKeys(target) {
103+
const keys = target[$OMIT_KEYS];
104+
return ownEnumerableKeys(target[$OMIT_PROPS]).filter(k => !keys.includes(k));
38105
}
39106
};
40107

@@ -79,7 +146,6 @@ function resolveSource(s: any) {
79146
return !(s = typeof s === "function" ? s() : s) ? {} : s;
80147
}
81148

82-
const $SOURCES = Symbol(__DEV__ ? "MERGE_SOURCE" : 0);
83149
/** @internal The flattened sources behind a `merge()` PROXY, or undefined.
84150
* Only the proxy form: its writes are no-ops, so the sources are the whole
85151
* truth. merge()'s plain-object form also records `$SOURCES` (so nested
@@ -125,32 +191,7 @@ export function merge<T extends unknown[]>(...sources: T): Merge<T> {
125191
);
126192
}
127193
if (SUPPORTS_PROXY && proxy) {
128-
return new Proxy(
129-
{
130-
get(property: string | number | symbol) {
131-
if (property === $SOURCES) return flattened;
132-
for (let i = flattened.length - 1; i >= 0; i--) {
133-
const s = resolveSource(flattened[i]);
134-
if (property in s) return s[property];
135-
}
136-
},
137-
has(property: string | number | symbol) {
138-
for (let i = flattened.length - 1; i >= 0; i--) {
139-
if (property in resolveSource(flattened[i])) return true;
140-
}
141-
return false;
142-
},
143-
keys() {
144-
const keys = new Set<string | symbol>();
145-
for (let i = 0; i < flattened.length; i++) {
146-
const sourceKeys = ownEnumerableKeys(resolveSource(flattened[i]));
147-
for (let j = 0; j < sourceKeys.length; j++) keys.add(sourceKeys[j]);
148-
}
149-
return [...keys];
150-
}
151-
},
152-
propTraps
153-
) as unknown as Merge<T>;
194+
return new Proxy({ [$SOURCES]: flattened }, mergeTraps) as unknown as Merge<T>;
154195
}
155196

156197
const defined: Record<string, PropertyDescriptor> = Object.create(null);
@@ -226,28 +267,10 @@ export function omit<T extends Record<any, any>, K extends readonly (keyof T)[]>
226267
...keys: K
227268
): Omit<T, K> {
228269
if (SUPPORTS_PROXY && $PROXY in props) {
229-
return new Proxy(
230-
{
231-
get(property) {
232-
// $SOURCES must not tunnel through the filter: merge() flattens
233-
// whatever answers it, so forwarding would hand a re-merge the
234-
// UNFILTERED sources of an underlying merge proxy and the omitted
235-
// keys leak back in (#3014 — the SSR element-spread path re-merges
236-
// static attributes with the rest object). Opaque here: merge
237-
// composes omit proxies through their traps instead.
238-
return property === $SOURCES || keys.includes(property as keyof T)
239-
? undefined
240-
: props[property as any];
241-
},
242-
has(property) {
243-
return property !== $SOURCES && !keys.includes(property as keyof T) && property in props;
244-
},
245-
keys() {
246-
return ownEnumerableKeys(props).filter(k => !keys.includes(k as keyof T));
247-
}
248-
},
249-
propTraps
250-
) as unknown as Omit<T, K>;
270+
return new Proxy({ [$OMIT_PROPS]: props, [$OMIT_KEYS]: keys }, omitTraps) as unknown as Omit<
271+
T,
272+
K
273+
>;
251274
}
252275
const result: Record<string, any> = {};
253276
const propNames = Object.getOwnPropertyNames(props);

‎packages/signals/tests/store/utilities.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,31 @@ describe("merge", () => {
171171
expect(merge(merge({ value: 1 }, { value: 2 }), { value: 3 }).value).toBe(3);
172172
expect(merge({ value: 1 }, merge({ value: 2 }, { value: 3 })).value).toBe(3);
173173
});
174+
it("keeps proxy state off the visible surface and safe from defines", () => {
175+
const [store, setStore] = createStore({ id: 1, title: "Title" });
176+
const merged = merge({ size: "m" }, store);
177+
const rest = omit(merged, "size");
178+
expect(Object.getOwnPropertySymbols(merged)).toEqual([]);
179+
expect(Object.getOwnPropertySymbols(rest)).toEqual([]);
180+
expect(Object.keys(merged).sort()).toEqual(["id", "size", "title"]);
181+
expect(Object.keys(rest).sort()).toEqual(["id", "title"]);
182+
// string defines land on the target and stay invisible; reads still go to the sources
183+
Object.defineProperty(merged, "sources", { value: [], configurable: true });
184+
Object.defineProperty(rest, "props", { value: { id: 99 }, configurable: true });
185+
expect(merged.id).toBe(1);
186+
expect(rest.id).toBe(1);
187+
expect("sources" in merged).toBe(false);
188+
expect("props" in rest).toBe(false);
189+
expect(Object.keys(merged).sort()).toEqual(["id", "size", "title"]);
190+
setStore(s => {
191+
s.id = 2;
192+
});
193+
flush();
194+
expect(merged.id).toBe(2);
195+
expect(rest.id).toBe(2);
196+
expect(Object.getOwnPropertyDescriptor(merged, "id")!.get!()).toBe(2);
197+
expect(Object.getOwnPropertyDescriptor(rest, "size")!.get!()).toBeUndefined();
198+
});
174199
it("does not clone nested objects", () => {
175200
const b = { value: 1 };
176201
const props = merge({ a: 1 }, { b });

0 commit comments

Comments
 (0)