Skip to content

Commit 44da46e

Browse files
perf(signals,web,universal): source kinds decided once at view build; no brand check per read
A read through merge(defaults, store) was 1.5x slower than next: the walk was the same (in, then get) but went through sourceHas/sourceGet, each starting with `s instanceof OmitView` — on a store proxy that is a getPrototypeOf trap (~20 ns, as much as the read itself), twice per read. spread()'s per-key walk over a store-backed view paid the same. Every source entry now carries its kind (plain / omit record / proxy / memo), decided in merge()'s flattening loop and omit()'s argument check where the information already exists (MergeView.kinds, OmitView.kind). mergeGet/has/descriptor/ownKeys and the consumers (spread in web and universal, ssrElement) switch on the kind; the exported entry helpers take it as a parameter. viewOf(o) hands consumers the record behind a merge or omit proxy in two fast traps. Table-backed views also cache their own-keys array and per-key descriptor shape, so an enumeration through the traps re-reads no leaf descriptor. merge(defaults, store) 84 ns -> 61 ns (next -> branch) merge(defaults, store) + 5 reads 404 ns -> 346 ns (was 635 before this) merge(defaults, store) every key 2081 ns -> 1824 ns (was 3149) omit(store, 5) every key 1470 ns -> 1287 ns Object.keys(omit(store, 5)) 6979 ns -> 5417 ns A trap-logging store-shaped proxy pins it: a read through a merge or omit asks the store exactly what a direct read would. Co-authored-by: Cursor <[email protected]>
1 parent 1ae4058 commit 44da46e

10 files changed

Lines changed: 665 additions & 291 deletions

File tree

‎.changeset/merge-omit-lazy-views.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ The two compose flat. An `omit()` over a `merge()` carries one filtered view per
1414

1515
Reads stay cheap: a view over plain objects resolves a key → owning-leaf table once, on first read, and every `get`/`has`/descriptor is one lookup after that. `spread()` (DOM and universal) and `ssrElement()` read the leaves directly — never through the proxies' traps — and walk that table when there is one, so an effect rerun costs one read per key, as it did over the copy. Both proxies use a class target and one shared handler (no per-instance closures).
1616

17+
A view over a store asks the store nothing but the read. Each source's kind (plain object, omit record, proxy, memo) is decided once, when the view is built, and carried beside it — every brand check on a Proxy is a trap (`instanceof` is a `getPrototypeOf` trap, as expensive as a store read), and store detection goes through `$TARGET`, a symbol the store's `get` trap answers on its fast path, never its generic tracked-read path. `merge(defaults, store)` constructs ~30% faster than the copy did and reads ~15% faster; `omit(store)` reads at parity.
18+
1719
The views tell the truth: `Object.getOwnPropertyDescriptor(view, key)` reports a data descriptor only when the key is a data property of a plain leaf (the compiler's encoding of a static prop) and an accessor for a getter, a store key, or a memo source. Together with the new internal `hasStaticKeys()`, `spread()` now skips the children effect for static children behind `omit`/`merge` layers (#3388 through views).
1820

1921
Behavior changes:
@@ -24,4 +26,4 @@ Behavior changes:
2426
- Sources are treated as own-keyed; a key added to a plain source after merging is not seen (the copy did not see it either).
2527
- Enumerating a view through its traps (`for…in`, `Object.keys`, `{ ...view }`) costs a trap per key, as any proxy does; the internal consumers avoid it. Environments without `Proxy` keep the copy paths.
2628

27-
Internal helpers for consumers, exported from `solid-js`: `omitView(o)`, `sourceKeys(entry)`, `sourceHas(entry, key)`, `sourceGet(entry, key)`, `hasStaticKeys(o)`, `resolvedTable(o)`.
29+
Internal helpers for consumers, exported from `solid-js`: `viewOf(o)`, `mergeView(o)`, `omitView(o)`, `sourceKeys(entry, kind)`, `sourceHas(entry, kind, key)`, `sourceGet(entry, kind, key)`, `hasStaticKeys(o)`, `resolvedTable(o)`, the `SOURCE_*` kinds.

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ export type { Merge, Omit } from "./utils.js";
1414
export { isWrappable, $TRACK, $PROXY, $TARGET } from "./store.js";
1515
export {
1616
mergeSources,
17+
mergeView,
18+
viewOf,
1719
omitView,
1820
sourceKeys,
1921
sourceHas,
2022
sourceGet,
2123
hasStaticKeys,
2224
resolvedTable,
23-
OmitView
25+
OmitView,
26+
MergeView,
27+
SOURCE_PLAIN,
28+
SOURCE_OMIT,
29+
SOURCE_PROXY,
30+
SOURCE_MEMO
2431
} from "./utils.js";
32+
export type { SourceKind } from "./utils.js";
2533

2634
import type { NoFn, ProjectionOptions, Store, StoreOptions, StoreSetter } from "./store.js";
2735
import type { Refreshable } from "../core/index.js";

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

Lines changed: 284 additions & 146 deletions
Large diffs are not rendered by default.

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

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@ import {
77
deep,
88
flush,
99
getOwner,
10+
$TARGET,
1011
merge,
1112
mergeSources,
1213
hasStaticKeys,
1314
omit,
1415
OmitView,
16+
MergeView,
17+
viewOf,
18+
resolvedTable,
19+
sourceKeys,
20+
sourceHas,
21+
sourceGet,
22+
SOURCE_PLAIN,
23+
SOURCE_OMIT,
24+
SOURCE_PROXY,
25+
SOURCE_MEMO,
1526
reconcile,
1627
snapshot,
1728
type Store
@@ -821,6 +832,127 @@ describe("view descriptors", () => {
821832
expect(Object.getOwnPropertyDescriptor(l5, "role")!.value).toBe("button");
822833
expect(Object.keys(l5).sort()).toEqual(["class", "extra", "label", "role"]);
823834
});
835+
// A store-shaped proxy that logs every trap it is asked. `$PROXY in`,
836+
// `$TARGET` and `$PROXY` are a store's fast paths; anything else — an
837+
// unknown symbol taking its generic read path, `getPrototypeOf` from an
838+
// `instanceof`, a descriptor per key — is a cost per read that the views
839+
// must not add over a direct read of the store.
840+
function storeShaped(data: Record<string, unknown>) {
841+
const log: string[] = [];
842+
const target = {};
843+
const proxy: any = new Proxy(target, {
844+
get(_, key, receiver) {
845+
if (key === $PROXY) return receiver;
846+
if (key === $TARGET) return target;
847+
log.push(`get ${String(key)}`);
848+
return data[key as string];
849+
},
850+
has(_, key) {
851+
if (key === $PROXY || key === $TARGET) return true;
852+
log.push(`has ${String(key)}`);
853+
return key in data;
854+
},
855+
ownKeys() {
856+
log.push("ownKeys");
857+
return Reflect.ownKeys(data);
858+
},
859+
getOwnPropertyDescriptor(_, key) {
860+
log.push(`descriptor ${String(key)}`);
861+
const desc = Reflect.getOwnPropertyDescriptor(data, key);
862+
return desc && { ...desc, configurable: true };
863+
},
864+
getPrototypeOf() {
865+
log.push("getPrototypeOf");
866+
return Object.prototype;
867+
}
868+
});
869+
return { proxy, log };
870+
}
871+
test("a read through a merge or omit asks a store exactly what a direct read would", () => {
872+
const { proxy: store, log } = storeShaped({ a: 1, b: 2 });
873+
const merged: any = merge({ a: 0, z: 9 }, store);
874+
expect(log).toEqual([]);
875+
expect(merged.a).toBe(1);
876+
expect(log).toEqual(["has a", "get a"]);
877+
log.length = 0;
878+
expect(merged.z).toBe(9);
879+
expect(log).toEqual(["has z"]);
880+
log.length = 0;
881+
expect("b" in merged).toBe(true);
882+
expect(log).toEqual(["has b"]);
883+
log.length = 0;
884+
885+
const rest: any = omit(store, "a");
886+
expect(log).toEqual([]);
887+
expect(rest.b).toBe(2);
888+
expect(rest.a).toBeUndefined();
889+
expect(log).toEqual(["get b"]);
890+
log.length = 0;
891+
892+
// Enumeration: one ownKeys, then per key one existence check for the
893+
// descriptor — never the store's own descriptor trap.
894+
expect(Object.keys(rest)).toEqual(["b"]);
895+
expect(log).toEqual(["ownKeys", "has b"]);
896+
log.length = 0;
897+
// A merge with a store leaf has no resolved table: the user-facing key
898+
// set is the enumerable keys of each source (a descriptor per store key,
899+
// #2769), then each key is a shadowing walk (`has` on the store) that
900+
// the descriptor reuses.
901+
expect(Object.keys(merged)).toEqual(["z", "a", "b"]);
902+
expect(log).toEqual(["ownKeys", "descriptor a", "descriptor b", "has z", "has a", "has b"]);
903+
log.length = 0;
904+
905+
// The consumers' entry helpers over the store's kind: no probe at all.
906+
expect(sourceKeys(store, SOURCE_PROXY)).toEqual(["a", "b"]);
907+
expect(sourceHas(store, SOURCE_PROXY, "a")).toBe(true);
908+
expect(sourceGet(store, SOURCE_PROXY, "a")).toBe(1);
909+
expect(log).toEqual(["ownKeys", "has a", "get a"]);
910+
log.length = 0;
911+
expect(hasStaticKeys(store)).toBe(false);
912+
expect(viewOf(store)).toBeUndefined();
913+
expect(resolvedTable(store)).toBeUndefined();
914+
expect(log).toEqual([]);
915+
});
916+
test("merge and omit record what each source is, once", () => {
917+
const store = createStore({ s: 1 })[0];
918+
const plain = { p: 1 };
919+
const rest = omit({ o: 1, hide: 1 }, "hide");
920+
const memo = () => ({ m: 1 });
921+
const merged = merge(plain, store, rest, memo);
922+
const view = viewOf(merged) as MergeView;
923+
expect(view).toBeInstanceOf(MergeView);
924+
expect(view.kinds).toEqual([SOURCE_PLAIN, SOURCE_PROXY, SOURCE_OMIT, SOURCE_MEMO]);
925+
expect(view.sources[0]).toBe(plain);
926+
expect(view.sources[1]).toBe(store);
927+
expect(view.sources[2]).toBeInstanceOf(OmitView);
928+
expect(typeof view.sources[3]).toBe("function");
929+
// A merge among the sources flattens with its kinds.
930+
const outer = viewOf(merge({ x: 1 }, merged)) as MergeView;
931+
expect(outer.kinds).toEqual([
932+
SOURCE_PLAIN,
933+
SOURCE_PLAIN,
934+
SOURCE_PROXY,
935+
SOURCE_OMIT,
936+
SOURCE_MEMO
937+
]);
938+
// An omit records its source's kind, and over a merge one entry per leaf.
939+
expect((viewOf(omit(store, "s")) as OmitView).kind).toBe(SOURCE_PROXY);
940+
expect((viewOf(omit(plain, "p")) as OmitView).kind).toBe(SOURCE_PLAIN);
941+
const overProxy: any = omit(merged, "p");
942+
const over = viewOf(overProxy) as OmitView;
943+
expect(over.kind).toBe(SOURCE_PROXY);
944+
expect(over.entries!.map(e => e.kind)).toEqual([
945+
SOURCE_PLAIN,
946+
SOURCE_PROXY,
947+
SOURCE_PLAIN,
948+
SOURCE_MEMO
949+
]);
950+
expect(overProxy.p).toBeUndefined();
951+
expect(overProxy.s).toBe(1);
952+
expect(overProxy.o).toBe(1);
953+
expect(overProxy.hide).toBeUndefined();
954+
expect(overProxy.m).toBe(1);
955+
});
824956
test("hasStaticKeys: plain objects and views over them; not stores, memos, or views over them", () => {
825957
createRoot(() => {
826958
const [store] = createStore({ a: 1 });

‎packages/solid/src/index.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,20 @@ export {
2121
mapArray,
2222
merge,
2323
mergeSources,
24+
mergeView,
25+
viewOf,
26+
OmitView,
27+
MergeView,
2428
omitView,
2529
sourceKeys,
2630
sourceHas,
2731
sourceGet,
2832
hasStaticKeys,
2933
resolvedTable,
34+
SOURCE_PLAIN,
35+
SOURCE_OMIT,
36+
SOURCE_PROXY,
37+
SOURCE_MEMO,
3038
omit,
3139
onCleanup,
3240
onSettled,
@@ -52,6 +60,7 @@ export {
5260
} from "@solidjs/signals";
5361

5462
export type {
63+
SourceKind,
5564
Accessor,
5665
ComputeFunction,
5766
EffectBundle,

‎packages/solid/src/server/index.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@ export {
3737
mapArray,
3838
merge,
3939
mergeSources,
40+
mergeView,
41+
viewOf,
42+
OmitView,
43+
MergeView,
4044
omitView,
4145
sourceKeys,
4246
sourceHas,
4347
sourceGet,
4448
hasStaticKeys,
4549
resolvedTable,
50+
SOURCE_PLAIN,
51+
SOURCE_OMIT,
52+
SOURCE_PROXY,
53+
SOURCE_MEMO,
4654
omit,
4755
onCleanup,
4856
onSettled,
@@ -72,6 +80,7 @@ export {
7280

7381
// All type re-exports from signals
7482
export type {
83+
SourceKind,
7584
Accessor,
7685
ComputeFunction,
7786
EffectFunction,

‎packages/solid/src/server/signals.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@ export {
3333
storePath,
3434
$PROXY,
3535
$TRACK,
36+
mergeView,
37+
viewOf,
38+
OmitView,
39+
MergeView,
3640
omitView,
3741
sourceKeys,
3842
sourceHas,
3943
sourceGet,
4044
hasStaticKeys,
41-
resolvedTable
45+
resolvedTable,
46+
SOURCE_PLAIN,
47+
SOURCE_OMIT,
48+
SOURCE_PROXY,
49+
SOURCE_MEMO
4250
} from "@solidjs/signals";
4351

4452
// === Type re-exports ===
@@ -53,6 +61,7 @@ import type {
5361
export type SourceAccessor<T> = Refreshable<SignalAccessor<T>>;
5462

5563
export type {
64+
SourceKind,
5665
Accessor,
5766
ComputeFunction,
5867
EffectFunction,

0 commit comments

Comments
 (0)