Skip to content

Commit a732d2b

Browse files
ryansolidClaude via Cursorcursoragent
committed
perf(web): ssrElement joins plain children in place and walks uniform sources without resolved lists
Profiled yak's element-dense SSR cases (dyn-translate, dyn-fair, dyn-inline) with ssrElement's array-sources form against the library's hand-written writer: the element path cost about 3.3x the writer per element, with everything else equal. Two structural causes. The exit. Every element ran resolveSSRNode(children, undefined, true), which allocates a fresh { t: [""], h: [], p: [] } result — an object and three arrays — to append one string to, then ssr([open, close], result) with a template array and the full hole loop to join it back. The content most elements end up with is one string (a text child, escaped above or by the compiler), a number, nothing, or one finished node; those now join the open and close tags in place. Arrays and pending nodes still take the resolver — they need it for separators and holes. The ssrTextTail marker state is not touched: the parent resets it on this element's finished node either way. Output is byte-identical on all fifteen yak cases and the server suite. The array form. sources/kinds lists were built per element even when every entry was a plain object, the normal case, and an omit over a merge allocated a fill(SOURCE_OMIT) list. kinds is now the per-entry array or, when every entry is of one kind, that kind itself, so the array handed in (or the view's entries) is walked as is. pushEntry classifies a plain object with one `$PROXY in` check instead of two. Same-process round-robin against next on yak's SSR lanes: dyn-translate +21%, dyn-fair +15%, dyn-inline +16% (1.76x -> 1.46x of the writer), multifile-composition +3%, tabs +6%; sampled allocation on dyn-translate 2959 -> 2161 bytes per instance. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 37ea885 commit a732d2b

3 files changed

Lines changed: 85 additions & 7 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@solidjs/web": patch
3+
---
4+
5+
`ssrElement` serializes the common element shapes without the general resolver's allocations
6+
7+
- Children that are one string, a number, nothing, or one finished node now join the open and close tags in place. The general path — `resolveSSRNode` into a fresh `{ t, h, p }` result (an object and three arrays), then `ssr()` over a template array — is taken only for arrays and pending nodes, which need it. Output is unchanged; on a text-content-heavy page this was the single largest cost of a spread element.
8+
- The array-sources form over plain objects, and an omit over a merge, walk the array they were handed: the resolved `sources`/`kinds` lists (and the `fill(SOURCE_OMIT)` list) are built only when entries differ in kind. A plain source in `pushEntry` is classified with one `$PROXY in` check instead of two.
9+
10+
On yak's element-dense SSR cases (`dyn-translate`, `dyn-fair`, `dyn-inline`) this is +15–21% throughput and −27% bytes allocated per instance, closing the gap to the hand-written writer from 1.76× to 1.46×; `multifile-composition`/`tabs` +3–6%.

‎packages/web/src/server.ts‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,6 +3889,12 @@ function pushEntry(resolved, kinds, s, kind) {
38893889
}
38903890
if (typeof s === "function") s = s();
38913891
if (s == null) return;
3892+
// The common source is a plain object: one check, no view lookup.
3893+
if (!($PROXY in s)) {
3894+
resolved.push(s);
3895+
kinds.push(SOURCE_PLAIN);
3896+
return;
3897+
}
38923898
const view = viewOf(s);
38933899
if (view instanceof OmitView) {
38943900
resolved.push(view);
@@ -3899,7 +3905,7 @@ function pushEntry(resolved, kinds, s, kind) {
38993905
for (let j = 0; j < f.length; j++) pushEntry(resolved, kinds, f[j], k[j]);
39003906
} else {
39013907
resolved.push(s);
3902-
kinds.push($PROXY in s ? SOURCE_PROXY : SOURCE_PLAIN);
3908+
kinds.push(SOURCE_PROXY);
39033909
}
39043910
}
39053911

@@ -3931,12 +3937,26 @@ export function ssrElement(tag, props, children, needsId, skip) {
39313937
// element — and nothing is asked of a store proxy per key but the read.
39323938
// The common case, one plain object, allocates nothing here.
39333939
let sources = null;
3940+
// The kind of every entry of `sources`: an array, one per entry, or — when
3941+
// they are all of one kind, the usual case — that kind itself, so the
3942+
// array form over plain objects and an omit over a merge walk the array
3943+
// they were handed and allocate no lists of their own.
39343944
let kinds = null;
39353945
let kind = SOURCE_PLAIN;
39363946
if (Array.isArray(props)) {
3937-
sources = [];
3938-
kinds = [];
3939-
for (let i = 0; i < props.length; i++) pushEntry(sources, kinds, props[i], SOURCE_MEMO);
3947+
let i = 0;
3948+
for (; i < props.length; i++) {
3949+
const s = props[i];
3950+
if (s == null || typeof s === "function" || $PROXY in s) break;
3951+
}
3952+
if (i === props.length) {
3953+
sources = props;
3954+
kinds = SOURCE_PLAIN;
3955+
} else {
3956+
sources = [];
3957+
kinds = [];
3958+
for (let i = 0; i < props.length; i++) pushEntry(sources, kinds, props[i], SOURCE_MEMO);
3959+
}
39403960
} else if (props == null) {
39413961
// A nullish source (static or resolved) is an empty spread (#3297).
39423962
props = {};
@@ -3953,7 +3973,7 @@ export function ssrElement(tag, props, children, needsId, skip) {
39533973
const entries = view.entries;
39543974
if (entries !== undefined) {
39553975
sources = entries;
3956-
kinds = new Array(entries.length).fill(SOURCE_OMIT);
3976+
kinds = SOURCE_OMIT;
39573977
} else {
39583978
props = view;
39593979
kind = SOURCE_OMIT;
@@ -3982,13 +4002,14 @@ export function ssrElement(tag, props, children, needsId, skip) {
39824002
let keysOf = null;
39834003
if (sources !== null) {
39844004
keysOf = new Array(last + 1);
3985-
for (let s = 0; s <= last; s++) keysOf[s] = sourceKeys(sources[s], kinds[s]);
4005+
for (let s = 0; s <= last; s++)
4006+
keysOf[s] = sourceKeys(sources[s], typeof kinds === "number" ? kinds : kinds[s]);
39864007
}
39874008
for (let s = 0; s <= last; s++) {
39884009
const keys = keysOf !== null ? keysOf[s] : sourceKeys(props, kind);
39894010
if (sources !== null) {
39904011
props = sources[s];
3991-
kind = kinds[s];
4012+
kind = typeof kinds === "number" ? kinds : kinds[s];
39924013
}
39934014
nextKey: for (let i = 0; i < keys.length; i++) {
39944015
const prop = keys[i];
@@ -4071,6 +4092,22 @@ export function ssrElement(tag, props, children, needsId, skip) {
40714092
// `/>` or the slash becomes part of the key's value.
40724093
if (skipChildren) return { t: result + " />" };
40734094
if (typeof children === "function") children = children();
4095+
// The content most elements end up with — one string (a text child, escaped
4096+
// above or by the compiler), a number, nothing, or one finished node — joins
4097+
// in place. That is exactly what the general path below produces for these
4098+
// shapes, minus its allocations: `resolveSSRNode` builds a `{ t, h, p }`
4099+
// result (an object and three arrays) to append one string to, and `ssr()`
4100+
// takes a template array and runs its hole loop to join it back. A spread
4101+
// element is serialized here on every render, so that was the largest
4102+
// single cost of the element path (profiled at a third of it on a
4103+
// text-content-heavy page). The text-adjacency marker state
4104+
// (`ssrTextTail`) is not touched: the parent resets it on this element's
4105+
// finished node either way.
4106+
const ct = typeof children;
4107+
if (ct === "string" || ct === "number") return { t: result + ">" + children + "</" + tag + ">" };
4108+
if (children == null || ct === "boolean") return { t: result + "></" + tag + ">" };
4109+
if (ct === "object" && !children.h && typeof children.t === "string")
4110+
return { t: result + ">" + children.t + "</" + tag + ">" };
40744111
return ssr([result + ">", `</${tag}>`], resolveSSRNode(children, undefined, true));
40754112
}
40764113
export function ssrAttribute(key: string, value: any): string;

‎packages/web/test/server/ssr-element-sources.spec.tsx‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,37 @@ describe("ssrElement with multiple sources", () => {
312312
expect(render("div", null)).toBe("<div></div>");
313313
expect(render("br", {}, undefined, true)).toMatch(/^<br _hk=\w+ \/>$/);
314314
});
315+
316+
// One string, a number, nothing, or one finished node joins the open and
317+
// close tags in place; anything else — arrays, pending nodes — goes through
318+
// the tree resolver. Same output either way; this pins the shapes.
319+
test("children of every shape serialize as the resolver would", () => {
320+
expect(render("p", {}, "text")).toBe("<p>text</p>");
321+
expect(render("p", {}, () => "thunked")).toBe("<p>thunked</p>");
322+
expect(render("p", {}, 42)).toBe("<p>42</p>");
323+
expect(render("p", {}, 0)).toBe("<p>0</p>");
324+
expect(render("p", {}, null)).toBe("<p></p>");
325+
expect(render("p", {}, undefined)).toBe("<p></p>");
326+
expect(render("p", {}, false)).toBe("<p></p>");
327+
expect(render("p", {}, true)).toBe("<p></p>");
328+
// a finished node from a nested element
329+
expect(render("p", {}, () => ssrElement("b", { id: "i" }, "in", false))).toBe(
330+
'<p><b id="i">in</b></p>'
331+
);
332+
// from the sources, escaped or raw
333+
expect(render("p", [{ children: 7 }])).toBe("<p>7</p>");
334+
expect(render("style", [{ children: "a > b {}" }])).toBe("<style>a > b {}</style>");
335+
// arrays take the resolver: an element's direct children are never
336+
// separated from each other, a nested array keeps the text separators the
337+
// client needs to claim two text nodes
338+
expect(render("p", {}, ["a", "b"])).toBe("<p>ab</p>");
339+
expect(render("p", {}, () => [1, 2])).toBe("<p>12</p>");
340+
expect(render("p", {}, ["a", ssrElement("i", {}, "x", false), "b"])).toBe("<p>a<i>x</i>b</p>");
341+
expect(render("p", {}, [["a", "b"]])).toBe("<p>a<!--!$-->b</p>");
342+
expect(
343+
renderToString(() => [ssrElement("p", {}, "a", false), ssrElement("p", {}, "b", false)])
344+
).toBe("<p>a</p><p>b</p>");
345+
});
315346
});
316347

317348
// omit() and merge() results are walked as VIEWS — the underlying sources,

0 commit comments

Comments
 (0)