Skip to content

Commit 7f5f902

Browse files
ryansolidclaudecursoragent
committed
perf(signals,web): views build their key table on enumeration or after 16 reads, not on first read; ssrElement walks a view's entries
A merge()/omit() view over plain objects keeps a resolved key table so a client spread that reruns, or Object.keys/{...props}, is one lookup per key. Since #3454 the first per-key trap read built it too. On the server that is the wrong trade: a component reads its merged props a few times, the element serializes them once, and the view is gone — a Kobalte-shaped chain (Dialog.Trigger → Button.Root → Polymorphic) paid for a table per layer per element. Profiled under renderToString: a third of the time in mergeTable/tableSet/omitTable and their garbage, another 11% in Array.prototype.concat combining omit filters. Signals: get/has/getOwnPropertyDescriptor answer by a source walk until the view has been read 16 times — the break-even between a build (~60 ns per key of every leaf) and a walk (~20 ns per source) — then build as before, so a long-lived client view read on every rerun is one lookup per read from its first few updates on. Enumeration builds outright. An omit over one object never builds one. Combined hidden lists are copied by hand (concat is 4× the cost for three-or-four-key lists). Web: ssrElement never asks for a view's table; an omit over a merge is walked as its filtered leaf entries, the array-form walk it already had. Merged attribute order is unchanged. Same-process A/B, tier-1 polymorphic-chain SSR (200 rows): chain 8.2× → 5.8× the compiled floor, chain-static 7.7× → 5.6× (−27%). Signals props-chain: build −5…−17%, build+consume −6…−14%, steady-state reads on a prebuilt view identical. DOM lane flat (the client spread still builds its table by design). Tests: table undecided after a handful of reads on a merge and an omit-over-merge, built after the 16th with identical answers; enumeration builds on a fresh view; plain omit never; store-leaf view settles to none. SSR: spread over omit-over-merge and bare merge serializes the merged order and builds no table. Co-authored-by: Claude <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 1de80f4 commit 7f5f902

5 files changed

Lines changed: 215 additions & 49 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@solidjs/signals": patch
3+
"@solidjs/web": patch
4+
---
5+
6+
`merge()`/`omit()` views build their resolved key table on enumeration or once reads have paid for it, not on the first read; `ssrElement` walks a view's entries instead of asking for its table
7+
8+
A `merge()`/`omit()` view over plain objects keeps a resolved table — every key mapped to the leaf that owns it — so a client `spread` rerunning its effect, or `Object.keys`/`{...props}`, is one lookup per key. Since the views became lazy (#3454) that table was built by the **first** per-key trap read as well. On the server that is the wrong trade: a component reads its merged props a few times, the element serializes them once, and the view is discarded, so a Kobalte-shaped component chain (`Dialog.Trigger` → `Button.Root` → `Polymorphic`) paid for a table per layer per element. Profiled under `renderToString`, a third of the time was in the table code (`mergeTable`, `tableSet`, `omitTable`) and the garbage it produced, plus `Array.prototype.concat` combining omit filters.
9+
10+
- **Signals:** a per-key `get`/`has`/`getOwnPropertyDescriptor` answers by a source walk (last source first, one `in` each) until the view has been read 16 times — the break-even between a build (~60 ns per key of every leaf) and a walk (~20 ns per source) — and then builds the table as before, so a long-lived client view read on every reactive rerun is one lookup per read from its first few updates on. Enumeration (`ownKeys`, or a consumer asking `resolvedTable`) builds it outright, unchanged. An omit over one object never builds one. The read count lives in the table slot until it is decided, so a view carries no extra field. Combined omit filters are copied with `slice` + `push` instead of `concat` (2–3× cheaper once optimized, and — unlike a hand-written loop — no more expensive than the builtin in the interpreter and baseline tiers, which is what an instruction-count benchmark under Valgrind mostly runs).
11+
- **Web (SSR):** `ssrElement` no longer prefers a view's resolved table; it walks the view's entries the way it already walked the array form — an omit over a merge as its filtered leaf entries — so serializing an element builds no table at all. Attribute order is the same merged order (a key at the position of the last source that carries it).
12+
13+
Same-process A/B on the tier-1 `polymorphic-chain` SSR bench (200 rows, `renderToString`): the chain form goes from 8.2× the compiled floor to 5.8×, chain-static from 7.7× to 5.6× (−27% wall-clock). At the signals layer the props-chain bench improves 5–17% on build, 6–14% on build+consume, and steady-state per-key reads on a prebuilt view are unchanged (the table is in use). The only slower band is a view read 16–20 times and never enumerated (+4–17% at depth 1–3, faster at depth 7), which is the transition the threshold is designed around.
14+
15+
Tests: the table is undecided after a handful of reads on both a merge and an omit-over-merge, built after the 16th with identical answers before and after; enumeration builds it on a fresh view; a plain omit never builds one; a store-leaf view settles to "none" and keeps walking. SSR: a spread over an omit-over-merge and over a bare merge serializes the merged order and builds no table.

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

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ const $VIEW = Symbol(__DEV__ ? "MERGE_VIEW" : 0);
8787
* trap round-trip per layer. A leaf may be merge's memo for a function
8888
* source; it is resolved on access. */
8989
export class OmitView {
90-
/** see `resolvedTable` */
91-
table: Map<PropertyKey, any> | null | undefined = undefined;
90+
/** see `MergeView.table` */
91+
table: Map<PropertyKey, any> | null | number = 0;
9292
/** see `tableOwnKeys` / `tableDescriptor` */
9393
keys: (string | symbol)[] | undefined = undefined;
9494
descs: Map<PropertyKey, PropertyDescriptor> | undefined = undefined;
@@ -110,9 +110,20 @@ function isHidden(view: OmitView, key: PropertyKey): boolean {
110110
}
111111

112112
// Both filters as one. Two key lists stay a key list (one `includes`, no
113-
// closure); a predicate on either side needs a closure.
113+
// closure); a predicate on either side needs a closure. An omit over a
114+
// merge builds one combined list per leaf, per component layer, so the
115+
// copy's form matters in every tier: `concat` runs the species/spreadable
116+
// protocol (2–3× the cost of a copy once optimized), a hand loop is 2–4×
117+
// `concat` in the interpreter and baseline tiers (a bytecode per element
118+
// against one builtin), and a presized `new Array(n)` is holey, which takes
119+
// `includes` off its fast path. `slice` + `push` of the (short) second list
120+
// is within a third of the best form in every tier, and packed.
114121
function combineHidden(a: Hidden, b: Hidden): Hidden {
115-
if (typeof a !== "function" && typeof b !== "function") return a.concat(b);
122+
if (typeof a !== "function" && typeof b !== "function") {
123+
const out = a.slice();
124+
for (let i = 0; i < b.length; i++) out.push(b[i]);
125+
return out;
126+
}
116127
return key =>
117128
(typeof a === "function" ? a(key) : a.includes(key)) ||
118129
(typeof b === "function" ? b(key) : b.includes(key));
@@ -321,9 +332,14 @@ function sourceEnumerableKeys(s: any, kind: SourceKind): (string | symbol)[] {
321332
// `$SOURCES` answers.
322333
/** @internal */
323334
export class MergeView {
324-
/** key → the plain leaf that owns it (later sources win), built on first
325-
* read when every leaf has static keys; `null` when one doesn't. */
326-
table: Map<PropertyKey, any> | null | undefined = undefined;
335+
/** key → the plain leaf that owns it (later sources win), built by an
336+
* enumeration or once the reads have paid for it (see `resolvedTable`)
337+
* when every leaf has static keys; `null` when one doesn't. Until then
338+
* the slot counts the per-key trap reads so far. One slot rather than a
339+
* counter field of its own: a view is built per source per component
340+
* layer, and each field initializer is a measurable share of a
341+
* constructor that small in the lower JIT tiers. */
342+
table: Map<PropertyKey, any> | null | number = 0;
327343
/** see `tableOwnKeys` / `tableDescriptor` */
328344
keys: (string | symbol)[] | undefined = undefined;
329345
descs: Map<PropertyKey, PropertyDescriptor> | undefined = undefined;
@@ -357,13 +373,28 @@ export function viewOf(o: any): MergeView | OmitView | undefined {
357373
* keys can change, or the object is not a view at all.
358374
*
359375
* This is the flat object the eager copy used to build, made lazily and
360-
* without copying: one pass over the leaves' own keys on first read, then
361-
* every `get`/`has`/descriptor is one lookup plus one read of the owning
362-
* leaf, and a consumer (`spread` rerunning its effect, `ssrElement`) walks
363-
* the table instead of re-deriving shadowing from the leaves each time. Own
364-
* keys only, as the copy's were: a plain source's key set is fixed once
365-
* merged (keys added to it later are not seen — the copy didn't see them
366-
* either). */
376+
* without copying: one pass over the leaves' own keys, then every
377+
* `get`/`has`/descriptor is one lookup plus one read of the owning leaf, and
378+
* a consumer (`spread` rerunning its effect) walks the table instead of
379+
* re-deriving shadowing from the leaves each time. Own keys only, as the
380+
* copy's were: a plain source's key set is fixed once merged (keys added to
381+
* it later are not seen — the copy didn't see them either).
382+
*
383+
* It is built by an ENUMERATION — the `ownKeys` trap, or a consumer asking
384+
* for it here — or once per-key reads have paid for it (`READS_FOR_TABLE`),
385+
* not on the first read. A per-key read has a direct answer (a walk of the
386+
* sources, last to first, one `in` each) whose cost is the source count,
387+
* while the table's is every key of every leaf, so the walk wins until a
388+
* view has been read about as many times as it has keys. On the server it
389+
* never is: a component reads its props a few times, the element enumerates
390+
* them once through its own source walk, and the view is gone — building on
391+
* first read there cost a component chain a table per layer (profiled on
392+
* the Kobalte-shaped chain: a third of SSR time in the table code and its
393+
* garbage). On the
394+
* client a view read on every reactive rerun crosses the threshold in its
395+
* first few updates and is one lookup per read from then on, as before.
396+
* Once built — by a `spread`, `Object.keys`, `{...props}`, or the count —
397+
* every trap uses it. */
367398
export function resolvedTable(o: any): Map<PropertyKey, any> | undefined {
368399
if (o == null || !($PROXY in o) || o[$TARGET] !== undefined) return undefined;
369400
const view = o[$OMIT];
@@ -374,7 +405,8 @@ export function resolvedTable(o: any): Map<PropertyKey, any> | undefined {
374405

375406
function mergeTable(view: MergeView): Map<PropertyKey, any> | undefined {
376407
let table = view.table;
377-
if (table === undefined) {
408+
if (typeof table !== "object") {
409+
// a read count: not decided yet
378410
const f = view.sources,
379411
k = view.kinds;
380412
for (let i = 0; i < f.length; i++) {
@@ -418,7 +450,7 @@ function tableSet(table: Map<PropertyKey, any>, key: PropertyKey, leaf: any) {
418450
// own keys) minus the hidden keys. Cached on the record.
419451
function omitTable(view: OmitView): Map<PropertyKey, any> | undefined {
420452
let table = view.table;
421-
if (table === undefined) {
453+
if (typeof table !== "object") {
422454
const src = view.source;
423455
let base: Map<PropertyKey, any> | undefined;
424456
if (view.kind === SOURCE_MEMO) base = undefined;
@@ -485,8 +517,46 @@ function tableDescriptor(
485517
};
486518
}
487519

520+
// Per-key trap reads a view takes before building its table, from the
521+
// break-even: a build is ~60 ns per key (an `ownKeys` share, a `has`, a
522+
// `set`), a walk ~20 ns per source (an `in`, a hidden-list check), and a
523+
// leaf carries about five keys — so the table has paid for itself after
524+
// ~15 reads. Measured on the Kobalte-shaped chain: a walk is 2× a lookup at
525+
// depth 1 and up to 10× for a first-source key at depth 7 (15 entries with
526+
// long hidden lists), so a view read on every update wants the table; a
527+
// view read a handful of times (every server-side view) never wants it.
528+
const READS_FOR_TABLE = 16;
529+
530+
// The table for a per-key trap: the one a view HAS (built by an enumeration
531+
// or an earlier read, see `resolvedTable`), or the one this read pays for,
532+
// or none — a walk answers. One per view type, so a trap pays no type check.
533+
// A settled slot is an object (the Map, or `null`); a number is the count.
534+
function mergeReadTable(view: MergeView): Map<PropertyKey, any> | undefined {
535+
const table = view.table;
536+
if (typeof table === "object") return table === null ? undefined : table;
537+
if (table + 1 < READS_FOR_TABLE) {
538+
view.table = table + 1;
539+
return undefined;
540+
}
541+
return mergeTable(view);
542+
}
543+
544+
// Only for an omit over a merge (`entries` set; the caller checks, inline —
545+
// a call is not free in every tier): an omit over one object reads it
546+
// directly — a list check and a property read, nothing a table would
547+
// shorten.
548+
function omitReadTable(view: OmitView): Map<PropertyKey, any> | undefined {
549+
const table = view.table;
550+
if (typeof table === "object") return table === null ? undefined : table;
551+
if (table + 1 < READS_FOR_TABLE) {
552+
view.table = table + 1;
553+
return undefined;
554+
}
555+
return omitTable(view);
556+
}
557+
488558
function mergeGet(view: MergeView, property: PropertyKey): any {
489-
const table = mergeTable(view);
559+
const table = mergeReadTable(view);
490560
if (table !== undefined) {
491561
const leaf = table.get(property);
492562
return leaf === undefined ? undefined : leaf[property];
@@ -519,7 +589,7 @@ const mergeTraps: ProxyHandler<MergeView> = {
519589
if (property === $PROXY) return true;
520590
if (property === $TARGET || property === $OMIT || property === $SOURCES || property === $VIEW)
521591
return false;
522-
const table = mergeTable(view);
592+
const table = mergeReadTable(view);
523593
if (table !== undefined) return table.has(property);
524594
const f = view.sources,
525595
k = view.kinds;
@@ -537,7 +607,7 @@ const mergeTraps: ProxyHandler<MergeView> = {
537607
property === $VIEW
538608
)
539609
return undefined;
540-
const table = mergeTable(view);
610+
const table = mergeReadTable(view);
541611
if (table !== undefined) return tableDescriptor(view, table, property);
542612
const f = view.sources,
543613
k = view.kinds;
@@ -570,11 +640,11 @@ const mergeTraps: ProxyHandler<MergeView> = {
570640
}
571641
};
572642

573-
// Over a plain object an omit view reads its source directly — a hidden-key
574-
// check and one property read, nothing to cache. Over a MERGE it answers from
575-
// its resolved table when the merge has one (plain leaves only), so a read
576-
// is one lookup rather than a hop through the merge proxy's traps; the table
577-
// is the merge's, filtered, built once per view.
643+
// An omit view reads its source directly — a hidden-key check and one
644+
// property read (over a merge, a hop through the merge proxy's traps, which
645+
// walk). Once an enumeration or the read count has built its table (over a
646+
// merge with plain leaves only: the merge's, filtered) every trap answers
647+
// from that instead.
578648
const omitTraps: ProxyHandler<OmitView> = {
579649
get(view, property, receiver) {
580650
if (property === $PROXY) return receiver;
@@ -586,7 +656,7 @@ const omitTraps: ProxyHandler<OmitView> = {
586656
// re-merge the unfiltered objects and leak the omitted keys (#3014).
587657
if (property === $SOURCES) return view.entries;
588658
if (view.entries !== undefined) {
589-
const table = omitTable(view);
659+
const table = omitReadTable(view);
590660
if (table !== undefined) {
591661
const leaf = table.get(property);
592662
return leaf === undefined ? undefined : leaf[property];
@@ -600,7 +670,7 @@ const omitTraps: ProxyHandler<OmitView> = {
600670
if (property === $TARGET || property === $VIEW || property === $SOURCES || property === $OMIT)
601671
return false;
602672
if (view.entries !== undefined) {
603-
const table = omitTable(view);
673+
const table = omitReadTable(view);
604674
if (table !== undefined) return table.has(property);
605675
}
606676
if (isHidden(view, property)) return false;
@@ -618,7 +688,7 @@ const omitTraps: ProxyHandler<OmitView> = {
618688
)
619689
return undefined;
620690
if (view.entries !== undefined) {
621-
const table = omitTable(view);
691+
const table = omitReadTable(view);
622692
if (table !== undefined) return tableDescriptor(view, table, property);
623693
}
624694
return sourceDescriptor(view, SOURCE_OMIT, property);

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,61 @@ describe("view descriptors", () => {
954954
expect(overProxy.hide).toBeUndefined();
955955
expect(overProxy.m).toBe(1);
956956
});
957+
test("the resolved table is built by an enumeration or paid for by reads, not on first read", () => {
958+
// A component chain: defaults → omit → call-site statics → omit, plain
959+
// leaves only, so a table is possible. Reads and existence checks and
960+
// descriptor lookups answer by a source walk while the view is young.
961+
const user = {
962+
as: "a",
963+
class: "btn",
964+
get title() {
965+
return "t";
966+
}
967+
};
968+
const merged: any = merge({ type: "button", as: "button" }, omit(user, "class"));
969+
const rest: any = omit(merged, "type");
970+
const mergedView = viewOf(merged) as MergeView;
971+
const restView = viewOf(rest) as OmitView;
972+
const expectSame = () => {
973+
expect(merged.as).toBe("a");
974+
expect(merged.type).toBe("button");
975+
expect(merged.class).toBeUndefined();
976+
expect(merged.title).toBe("t");
977+
expect("class" in merged).toBe(false);
978+
expect(Reflect.getOwnPropertyDescriptor(merged, "as")!.value).toBe("a");
979+
expect(rest.type).toBeUndefined();
980+
expect(rest.as).toBe("a");
981+
expect("type" in rest).toBe(false);
982+
expect(Reflect.getOwnPropertyDescriptor(rest, "title")!.get).toBeTypeOf("function");
983+
};
984+
expectSame(); // 8 trap reads on the merge (two of them through `rest`), 4 on the omit
985+
expect(mergedView.table).toBe(8); // the slot counts the reads until it is decided
986+
expect(restView.table).toBe(4);
987+
// …and once the reads have paid for a table (16), it is built and every
988+
// trap answers from it with the same results.
989+
expectSame();
990+
expect(mergedView.table).toBeInstanceOf(Map);
991+
expect(restView.table).toBe(8);
992+
expectSame();
993+
expectSame();
994+
expect(restView.table).toBeInstanceOf(Map);
995+
expectSame();
996+
// Enumeration builds it outright, even on a fresh view.
997+
const fresh: any = merge({ a: 1 }, { b: 2 });
998+
expect((viewOf(fresh) as MergeView).table).toBe(0);
999+
expect(Object.keys(fresh)).toEqual(["a", "b"]);
1000+
expect((viewOf(fresh) as MergeView).table).toBeInstanceOf(Map);
1001+
// An omit over one object never builds one: its read is direct.
1002+
const plainRest: any = omit(user, "class");
1003+
for (let i = 0; i < 40; i++) expect(plainRest.as).toBe("a");
1004+
expect((viewOf(plainRest) as OmitView).table).toBe(0);
1005+
// A view with a store leaf can't have one; reads keep walking after the
1006+
// count runs out, and the answer is the same.
1007+
const [store] = createStore({ s: 1 });
1008+
const overStore: any = merge({ d: 0 }, store);
1009+
for (let i = 0; i < 40; i++) expect(overStore.s).toBe(1);
1010+
expect((viewOf(overStore) as MergeView).table).toBeNull();
1011+
});
9571012
test("hasStaticKeys: plain objects and views over them; not stores, memos, or views over them", () => {
9581013
createRoot(() => {
9591014
const [store] = createStore({ a: 1 });

0 commit comments

Comments
 (0)