Skip to content

Commit 93e5359

Browse files
ryansolidClaude via Cursorcursoragent
committed
perf(signals): copy folded filters by exact concat while short, chain only past 8 keys
A chain from the first fold cut allocation by a quarter at depth 7 and build time by a third under TurboFan, but the walk per key of every leaf cost the bytecode tiers more than the copies had: CodSpeed read the tier-1 polymorphic-chain SSR bench 10% worse, and omit-static reads paid an extra call layer for a case with no chain at all. Two lists now combine with concat — one builtin call, exact-size (the slice() + push it replaces grew the backing store to 1.5n + 16 slots) — while the result is within 8 keys; past that, or with a predicate on either side, they chain as before. The tier-1 bench's four omits hide 1 + 1 + 2 + 1 keys, so every leaf on that shape stays one flat list and a check is one includes; a deep leaf on a longer chain is one short list plus a few links. isHidden is the walk itself (no second call layer), and combineHidden tells a list, a predicate and a link apart by typeof and constructor rather than Array.isArray / instanceof, which are builtin calls in the bytecode tiers. Tier-1 polymorphic-chain SSR bench (Kobalte shape, renderToString, 200 rows), the harness driven in-process under each tier, min of 5 vs next: interpreter -7%, Sparkplug -12%, TurboFan -17%; the compiled control is flat. Depth-7 props chain: TurboFan build -14%, build + consume -9%; interpreter and Sparkplug +3-5%. A limit of 12 was measured and is worse in every tier (more copies, less chain). Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent fbc2ae2 commit 93e5359

3 files changed

Lines changed: 72 additions & 35 deletions

File tree

‎.changeset/omit-hidden-chain.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"@solidjs/signals": patch
33
---
44

5-
`omit()` chains folded filters instead of copying them
5+
`omit()` combines folded filters into one exact-size list while short, and chains them past that
66

7-
When an omit folds over another view — an omit of an omit, or of a merge with omit leaves — the two filters were combined into one key list per leaf, per layer. On a component chain (defaults → omit → statics → omit …) each copy held every key hidden so far, growing with the depth: at depth 7 the copies were the largest allocation of the views (100–450 bytes a leaf, more with `push` growth of the backing store). A filter is now one two-field link over the filter it folds; a check walks the links, doing the same `includes` work the combined list did. A filter with nothing to hide adds no link, and a predicate filter no longer needs a closure to combine.
7+
When an omit folds over another view — an omit of an omit, or of a merge with omit leaves — the two filters were combined into one key list per leaf, per layer, by `slice()` + `push`. V8 grows the backing store to `1.5n + 16` slots on that push, so each copy allocated 2.4–2.7× an exact one, and on a component chain (defaults → omit → statics → omit …) the copies held every key hidden so far, growing with the depth. Two short lists now combine with `concat` — one builtin call, exact-size — up to 8 keys; past that, or with a predicate on either side, they chain as one two-field link over the filter folded, with no copy at all. A predicate filter no longer needs a closure to combine, and a filter with nothing to hide adds nothing.
88

9-
Depth-7 Kobalte-shaped chain, optimized: build −32%, build + consume −11%, bytes allocated −23%. The `polymorphic-chain` SSR case allocates 17% less per instance. In the bytecode tiers (interpreter, Sparkplug) the check-heavy consume is 5–9% more instructions, since one `includes` builtin over a flat list is the cheapest possible check there; that is the trade, made for the optimized tiers and the allocation rate a server sees.
9+
Tier-1 `polymorphic-chain` SSR bench (Kobalte shape, `renderToString`, 200 rows): −17% optimized, −12% Sparkplug, −7% interpreter. Depth-7 props chain: build −14%, build + consume −9% optimized; +3–5% in the bytecode tiers, where the walk over the links past the copy limit costs more than one `includes` over a flat list would.

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

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,18 @@ export class OmitView {
104104

105105
// What an omit view hides: the caller's key list or predicate, or — for a
106106
// view folded over another (an omit of an omit, or of a merge with omit
107-
// leaves) — both filters, CHAINED. A link is one two-field object where a
108-
// combined key list was a copy of every key hidden so far: an omit over a
109-
// merge combines once per leaf per layer, so on a component chain
110-
// (defaults → omit → statics → omit …) the copies grew with the depth and
111-
// were the largest allocation of the views (100–450 bytes a leaf on a
112-
// Kobalte-shaped chain, more with `push` growth). A check walks the links,
113-
// the same `includes` work a combined list did.
107+
// leaves) — both filters. Two short lists combine into one exact-size list
108+
// (one `includes` per check, the cheapest check in every tier); past
109+
// `COPY_LIMIT` keys, or with a predicate on either side, they CHAIN — one
110+
// two-field link over the filter folded, no copy. An omit over a merge
111+
// combines once per leaf per layer, so on a component chain (defaults →
112+
// omit → statics → omit …) a list that kept growing by copy was the largest
113+
// allocation of the views (100–450 bytes a leaf on a Kobalte-shaped chain,
114+
// quadratic in the depth); a chain that started at the first fold cost a
115+
// link walk per key of every leaf on the same shape, and the bytecode tiers
116+
// paid more for the walk than the copies had cost. The limit keeps most
117+
// leaves a flat list and bounds a deep leaf to a short list plus a few
118+
// links.
114119
type Filter = PropertyKey[] | ((key: PropertyKey) => boolean);
115120
type Hidden = Filter | HiddenChain;
116121
class HiddenChain {
@@ -121,18 +126,15 @@ class HiddenChain {
121126
public outer: Filter
122127
) {}
123128
}
124-
125-
function isHidden(view: OmitView, key: PropertyKey): boolean {
126-
return hides(view.hidden, key);
127-
}
129+
const COPY_LIMIT = 8;
128130

129131
// One loop, no recursion, and a link is told from a list or a predicate by
130132
// its constructor (a load and a compare; `instanceof` and `Array.isArray`
131133
// are builtin calls in the bytecode tiers): the check runs per key of every
132-
// leaf when a view is enumerated or its table is built, and on a chain that
133-
// is several links deep, so it is written for those tiers as much as for
134-
// the optimizer.
135-
function hides(h: Hidden, key: PropertyKey): boolean {
134+
// leaf when a view is enumerated or its table is built, so it is written
135+
// for those tiers as much as for the optimizer.
136+
function isHidden(view: OmitView, key: PropertyKey): boolean {
137+
let h = view.hidden;
136138
for (;;) {
137139
if (h.constructor !== HiddenChain)
138140
return typeof h === "function" ? h(key) : (h as PropertyKey[]).includes(key);
@@ -142,17 +144,30 @@ function hides(h: Hidden, key: PropertyKey): boolean {
142144
}
143145
}
144146

145-
// Both filters as one — a link, never a copy. A filter with nothing to hide
146-
// (an `omit(props)` with no keys) adds no link. `outer` is normally the
147-
// current omit's own filter; when it is a chain (an omit over an omit that
148-
// was itself over a merge), its links are re-hung over `inner` one by one so
149-
// every link's own filter stays atomic.
147+
// Both filters as one. A filter with nothing to hide (an `omit(props)` with
148+
// no keys) adds nothing. Two lists within the limit copy — `concat`, an
149+
// exact-size allocation in one builtin call (a `slice` + `push` grows the
150+
// backing store to 1.5n + 16 slots on the push; a hand loop is a bytecode
151+
// per element). `outer` is normally the current omit's own filter; when it
152+
// is a chain (an omit over an omit that was itself over a merge), its links
153+
// are re-hung over `inner` one by one so every link's own filter stays
154+
// atomic.
150155
function combineHidden(inner: Hidden, outer: Hidden): Hidden {
151-
if (outer instanceof HiddenChain)
152-
return combineHidden(combineHidden(inner, outer.inner), outer.outer);
153-
if (Array.isArray(outer) && outer.length === 0) return inner;
154-
if (Array.isArray(inner) && inner.length === 0) return outer;
155-
return new HiddenChain(inner, outer);
156+
if (outer.constructor === HiddenChain) {
157+
const o = outer as HiddenChain;
158+
return combineHidden(combineHidden(inner, o.inner), o.outer);
159+
}
160+
// From here `outer` is a list or a predicate; a list is told by `length`
161+
// (a predicate's is its arity — never read, the typeof comes first).
162+
const outerList = typeof outer !== "function";
163+
if (outerList && (outer as PropertyKey[]).length === 0) return inner;
164+
if (typeof inner !== "function" && inner.constructor !== HiddenChain) {
165+
const list = inner as PropertyKey[];
166+
if (list.length === 0) return outer;
167+
if (outerList && list.length + (outer as PropertyKey[]).length <= COPY_LIMIT)
168+
return list.concat(outer as PropertyKey[]);
169+
}
170+
return new HiddenChain(inner, outer as Filter);
156171
}
157172

158173
// The object a view filters (see `leafOf`).

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,8 @@ describe("view descriptors", () => {
824824
// l3's statics, l1's defaults, the user's props — in merge order
825825
expect((leaves[0] as OmitView).hidden).toEqual(["as"]);
826826
expect((leaves[2] as OmitView).source).toBe(user);
827-
// both filters, chained rather than copied: the inner omit's list and the
828-
// outer's, in that order
829-
expect((leaves[2] as OmitView).hidden).toEqual({ inner: ["type"], outer: ["as"] });
827+
// both filters as one short list: the inner omit's keys, then the outer's
828+
expect((leaves[2] as OmitView).hidden).toEqual(["type", "as"]);
830829
// and the truth reaches the top
831830
expect(Object.getOwnPropertyDescriptor(l5, "class")!.value).toBe("btn");
832831
expect(typeof Object.getOwnPropertyDescriptor(l5, "label")!.get).toBe("function");
@@ -835,20 +834,43 @@ describe("view descriptors", () => {
835834
expect(Object.getOwnPropertyDescriptor(l5, "role")!.value).toBe("button");
836835
expect(Object.keys(l5).sort()).toEqual(["class", "extra", "label", "role"]);
837836
});
838-
test("folded filters chain: lists, predicates and empty omits over each other", () => {
837+
test("folded filters: short lists copy, predicates and long lists chain", () => {
839838
const user = { a: 1, b: 2, c: 3, d: 4, e: 5 };
840839
// omit of omit of predicate-omit of a no-key omit — every combination
841-
const o1 = omit(user); // nothing hidden: adds no link
840+
const o1 = omit(user); // nothing hidden: adds nothing
842841
const o2 = omit(o1, (key: PropertyKey) => key === "a");
843-
const o3 = omit(o2, "b");
842+
const o3 = omit(o2, "b"); // a predicate cannot copy: one link
844843
const o4 = omit(o3); // still nothing added
845-
const o5 = omit(o4, "c", "zz");
844+
const o5 = omit(o4, "c", "zz"); // a list over a chain: another link
846845
const view = viewOf(o5) as OmitView;
847846
expect(view.source).toBe(user);
848847
expect(view.hidden).toEqual({
849848
inner: { inner: expect.any(Function), outer: ["b"] },
850849
outer: ["c", "zz"]
851850
});
851+
// lists copy while short, then chain: eight keys in one list, the ninth
852+
// fold a link over it
853+
let deep: any = user;
854+
for (let i = 0; i < 4; i++) deep = omit(deep, `k${i}a`, `k${i}b`);
855+
expect((viewOf(deep) as OmitView).hidden).toEqual([
856+
"k0a",
857+
"k0b",
858+
"k1a",
859+
"k1b",
860+
"k2a",
861+
"k2b",
862+
"k3a",
863+
"k3b"
864+
]);
865+
deep = omit(deep, "k4a", "k4b");
866+
expect((viewOf(deep) as OmitView).hidden).toEqual({
867+
inner: ["k0a", "k0b", "k1a", "k1b", "k2a", "k2b", "k3a", "k3b"],
868+
outer: ["k4a", "k4b"]
869+
});
870+
deep = omit(deep, "a");
871+
expect(Object.keys(deep)).toEqual(["b", "c", "d", "e"]);
872+
expect("a" in deep).toBe(false);
873+
expect("k4a" in deep).toBe(false);
852874
expect(Object.keys(o5)).toEqual(["d", "e"]);
853875
expect("a" in o5).toBe(false);
854876
expect("b" in o5).toBe(false);

0 commit comments

Comments
 (0)