Skip to content

Commit 7f6332a

Browse files
perf(web): spread() creates fewer reactive nodes and accepts a sources array (#3388) (#3419)
`ref` folds into the attribute effect: it is collected with the other props in the compute half and re-applied in the commit half only when its identity differs from the last applied one. `ref()` runs the callback untracked with no owner, so nothing a ref creates is disposed when the effect reruns; that is what makes the fold safe. Children keep their own owned `insert`. That effect owns the child subtree (components, memos and effects created while the children getter runs are disposed when it reruns), so merging it into the attribute effect would tear the children down and rebuild them on every attribute change. A plain object whose `children` is a data property inserts the value with no effect at all. Three nodes become two when children flow through the spread, one when they don't. `spread(el, [a, b], skipChildren, skip)` takes an array of sources: the union of own string keys with later sources winning, only the winning source read, function sources called inline with no memo and so no hydration id, `skip(key)` never read nor applied. Same contract as the server `ssrElement` array form. Web effects are transparent for hydration ids, so dropping the ref effect shifts no sibling ids. Co-authored-by: Cursor <[email protected]>
1 parent 40977c9 commit 7f6332a

3 files changed

Lines changed: 585 additions & 53 deletions

File tree

‎.changeset/spread-fewer-nodes.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/web": patch
3+
---
4+
5+
`spread()` creates fewer reactive nodes per element (#3388): `ref` folds into the attribute effect and is re-applied only when its identity changes (refs run with no owner, so nothing they create is disposed by the fold); children keep their own owned `insert` — that effect owns the child subtree, and merging it would rebuild the children on every attribute change — but a plain object whose `children` is a data property inserts the value with no effect at all. Three nodes become two when children flow through the spread, one when they don't. `spread` also accepts an array of sources with an optional `skip` predicate — `spread(el, [a, b], skipChildren, skip)` — the union of own keys with later sources winning, only the winning source read, function sources called inline with no memo (and so no hydration id), matching the server `ssrElement` array form.

‎packages/web/src/client.ts‎

Lines changed: 129 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -825,67 +825,143 @@ export function setStyleProperty(node, name, value) {
825825
if (isHydrating(node)) return;
826826
value != null ? node.style.setProperty(name, value) : node.style.removeProperty(name);
827827
} /** Compiler-emitted primitive; not for hand-written code. @internal */
828-
export function spread<T>(node: Element, accessor: T, skipChildren?: Boolean): void;
828+
export function spread(
829+
node: Element,
830+
sources: unknown[],
831+
skipChildren?: Boolean,
832+
skip?: (key: string) => boolean
833+
): void;
834+
export function spread<T>(
835+
node: Element,
836+
accessor: T,
837+
skipChildren?: Boolean,
838+
skip?: (key: string) => boolean
839+
): void;
829840

830-
// TODO: make this better
831-
export function spread(node, props, skipChildren) {
841+
// At most TWO reactive nodes per element (#3388) — one when nothing flows
842+
// through `children`:
843+
//
844+
// - The children `insert` effect stays separate. It OWNS the child subtree:
845+
// components, memos and effects created while the children getter runs are
846+
// disposed when it reruns, so folding it into the attribute effect would
847+
// tear the children down and rebuild them on every attribute change. When
848+
// the source is a plain object (no accessor, no proxy) whose `children` is
849+
// a data property, the value is inserted directly — `insert` with a
850+
// non-function creates no effect at all. Compiled JSX children are getters
851+
// and keep the effect path.
852+
// - `ref` FOLDS into the attribute effect. It is collected with the other
853+
// props in the compute half and applied in the commit half only when its
854+
// identity differs from the last applied one (`prevProps.ref`, recorded by
855+
// assign()). `ref()` runs the callback untracked with NO owner — refs
856+
// deliberately own nothing — so anything a ref callback creates survives
857+
// the effect rerunning; that is what makes the fold safe.
858+
//
859+
// Sources. A single source is an object, a merge() result or a bare
860+
// accessor. A lone reactive spread compiles to its accessor directly: merging
861+
// one source is pure overhead, and the mergeProps memo would consume a
862+
// hydration id the server-side fast path never allocates (#3105). The
863+
// accessor resolves inside each tracking scope instead. A nullish source
864+
// (`{...props()}` where the optional props are absent, or no source at all)
865+
// is an empty spread: attributes applied by the previous value are removed,
866+
// nothing throws (#3297).
867+
//
868+
// An ARRAY of sources is the union of their own string keys, later sources
869+
// winning per key (Object.assign / merge()'s contract); only the winning
870+
// source's value is read, so a shadowed getter never runs. A function source
871+
// is called inline in the compute half, tracked, once per run — NO memo and
872+
// so NO hydration id, matching the server's `ssrElement` array form. Nullish
873+
// sources are skipped. `skip(key)` → the key is never read nor applied.
874+
export function spread(node, props, skipChildren, skip) {
832875
const prevProps = {};
833-
// A lone reactive spread compiles to its accessor directly: merging one
834-
// source is pure overhead, and the mergeProps memo would consume a
835-
// hydration id the server-side fast path never allocates (#3105). The
836-
// accessor resolves inside each tracking scope instead. A nullish source
837-
// (`{...props()}` where the optional props are absent, or no source at all)
838-
// is an empty spread: attributes applied by the previous value are removed,
839-
// nothing throws (#3297).
840-
const get = () => (typeof props === "function" ? props() : props) ?? {};
841-
if (!skipChildren)
842-
insert(node, () => {
843-
const source = get();
844-
return hasOwn.call(source, "children") ? source.children : undefined;
845-
});
846-
effect(
847-
() => {
848-
const source = get();
849-
const r = hasOwn.call(source, "ref") && source.ref;
850-
(typeof r === "function" || Array.isArray(r)) && ref(() => r, node);
851-
},
852-
() => {}
853-
);
854-
effect(
855-
() => {
856-
const source = get();
857-
const newProps = {};
858-
// A merge() proxy is read through its SOURCES, not through the proxy: a
859-
// spread mixed with other attributes compiles to
860-
// `spread(el, merge(statics, () => rest))`, and going through the proxy
861-
// costs merge's `keys()` (a Set plus an own-enumerable scan of every
862-
// source) and then, per key, a right-to-left `in` walk of the sources.
863-
// The union of own string keys with later sources overriding earlier
864-
// — Object.assign order, merge's own contract — is all a spread needs.
865-
// omit() is not a merge: it stays a proxy and is enumerated through its
866-
// own filtering trap.
867-
const sources = mergeSources(source);
868-
if (sources !== undefined) {
869-
for (let i = 0; i < sources.length; i++) {
870-
let s = sources[i];
871-
if (typeof s === "function") s = s();
872-
if (s != null) collectProps(newProps, s);
876+
const apply = newProps => {
877+
const r = newProps.ref;
878+
if (r !== prevProps.ref && (typeof r === "function" || Array.isArray(r))) ref(() => r, node);
879+
assign(node, newProps, true, prevProps, true);
880+
};
881+
if (Array.isArray(props)) {
882+
if (!skipChildren && !(skip !== undefined && skip("children")))
883+
insert(node, () => {
884+
for (let i = props.length - 1; i >= 0; i--) {
885+
const s = resolveSource(props[i]);
886+
if (s != null && "children" in s) return s.children;
873887
}
874-
} else collectProps(newProps, source);
875-
return newProps;
876-
},
877-
props => assign(node, props, true, prevProps, true)
878-
);
888+
});
889+
effect(() => collectSources({}, props, skip), apply);
890+
return prevProps;
891+
}
892+
if (!skipChildren && !(skip !== undefined && skip("children"))) {
893+
if (typeof props !== "function" && props != null && props[$PROXY] !== props) {
894+
// A plain object's key set can't change reactively: no `children` key
895+
// means nothing to insert, a data property inserts its value with no
896+
// effect, only a getter needs the tracking scope.
897+
const desc = Object.getOwnPropertyDescriptor(props, "children");
898+
if (desc !== undefined) {
899+
if (desc.get === undefined) insert(node, desc.value);
900+
else insert(node, () => props.children);
901+
}
902+
} else
903+
insert(node, () => {
904+
const source = resolveSource(props);
905+
return source != null && hasOwn.call(source, "children") ? source.children : undefined;
906+
});
907+
}
908+
effect(() => {
909+
const source = resolveSource(props);
910+
const newProps = {};
911+
// A merge() proxy is read through its SOURCES, not through the proxy: a
912+
// spread mixed with other attributes compiles to
913+
// `spread(el, merge(statics, () => rest))`, and going through the proxy
914+
// costs merge's `keys()` (a Set plus an own-enumerable scan of every
915+
// source) and then, per key, a right-to-left `in` walk of the sources.
916+
// The union of own string keys with later sources overriding earlier
917+
// — Object.assign order, merge's own contract — is all a spread needs.
918+
// omit() is not a merge: it stays a proxy and is enumerated through its
919+
// own filtering trap.
920+
const sources = mergeSources(source);
921+
if (sources !== undefined) return collectSources(newProps, sources, skip);
922+
if (source != null) collectProps(newProps, source, skip);
923+
return newProps;
924+
}, apply);
879925
return prevProps;
880926
}
881927

882-
// One layer of a spread source into `out`: own string keys, children/ref
883-
// excluded, object-valued style/class read HERE, tracked (see readShallow()).
884-
function collectProps(out, s) {
928+
function resolveSource(s) {
929+
return typeof s === "function" ? s() : s;
930+
}
931+
932+
// Layered sources into `out`. Every function source is resolved once, up
933+
// front; keys are then collected left-to-right (Object.assign order — the
934+
// order assign() applies them in, which `type`/`value`/`min`/`max` style
935+
// pairs care about), and a key any LATER source has is skipped unread. `in`
936+
// is merge()'s own resolution test, so a proxy source (store, omit) answers
937+
// through its `has` trap rather than a per-key descriptor trap.
938+
function collectSources(out, sources, skip) {
939+
const n = sources.length;
940+
const resolved = new Array(n);
941+
for (let i = 0; i < n; i++) resolved[i] = resolveSource(sources[i]);
942+
for (let i = 0; i < n; i++) {
943+
const s = resolved[i];
944+
if (s != null) collectProps(out, s, skip, resolved, i + 1);
945+
}
946+
return out;
947+
}
948+
949+
// One layer of a spread source into `out`: own string keys, `children`
950+
// excluded (it has its own insert), `ref` carried through for the commit
951+
// half, object-valued style/class read HERE, tracked (see readShallow()).
952+
// With `later` (the sources after this one, from index `from`), a key one of
953+
// them defines is shadowed and never read here.
954+
function collectProps(out, s, skip, later?, from?) {
885955
const keys = ownKeys(s);
886-
for (let i = 0; i < keys.length; i++) {
956+
outer: for (let i = 0; i < keys.length; i++) {
887957
const prop = keys[i];
888-
if (typeof prop !== "string" || prop === "children" || prop === "ref") continue;
958+
if (typeof prop !== "string" || prop === "children") continue;
959+
if (skip !== undefined && skip(prop)) continue;
960+
if (later !== undefined)
961+
for (let j = from; j < later.length; j++) {
962+
const t = later[j];
963+
if (t != null && prop in t) continue outer;
964+
}
889965
const v = s[prop];
890966
out[prop] = prop === "style" || prop === "class" ? readShallow(v) : v;
891967
}

0 commit comments

Comments
 (0)