Skip to content

Commit da6ed76

Browse files
perf(web): fold the SSR walker entry wrappers into the walkers (#3430)
#3394 split `tryResolveString` and `resolveSSRNode` into an entry function that resets `ssrTextTail` and a recursive body. The bodies are too big for V8 to inline, so every template hole paid one extra call — about 2% of SSR throughput on element-heavy pages, up to 7% where holes are dense. Pass a `nested` flag through the recursion instead: entry calls (no flag) reset the separator state, recursive calls keep it. One function each, identical output. `server-mock.ts` mirrors the signature. Co-authored-by: Cursor <[email protected]>
1 parent 5426ffb commit da6ed76

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

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+
SSR: fold the text-separator entry wrappers back into `tryResolveString` and `resolveSSRNode`. #3394 split each walker into an entry function that reset the separator state and a recursive body; the bodies are too large for V8 to inline, so every template hole paid an extra call. The recursion now passes a `nested` flag instead — one function each, same output — recovering the ~2% SSR throughput that split cost on element-heavy pages (up to 7% where holes are dense).

‎packages/web/src/server-mock.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ export function ssrHydrationKey(): string {}
384384
* Not meant for hand-written code.
385385
* @internal
386386
*/
387-
export function resolveSSRNode(node: any, result?: any, top?: boolean): any {}
387+
export function resolveSSRNode(node: any, result?: any, top?: boolean, nested?: boolean): any {}
388388
/**
389389
* Escapes a string for safe inclusion in HTML output. Used by the SSR
390390
* runtime; not generally part of user code.

‎packages/web/src/server.ts‎

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4565,11 +4565,6 @@ function flattenClassList(list, result) {
45654565
// read stateful getters such as JSX `props.children`
45664566
// whose backing component rebuilds an owner subtree on
45674567
// each access, producing a divergent hydration tree.
4568-
function tryResolveString(node) {
4569-
ssrTextTail = false;
4570-
return tryResolveStr(node);
4571-
}
4572-
45734568
// Text-hole separators. The client claims a multi-insert's nodes positionally
45744569
// after FLATTENING its value (memos resolved, nested arrays spliced, nullish
45754570
// dropped), so two consecutive items that both land as TEXT must arrive in
@@ -4584,11 +4579,15 @@ function tryResolveString(node) {
45844579
// being walked was text. Leaves maintain it; arrays and functions pass it
45854580
// through (flattening semantics); an unresolved async hole counts as text on
45864581
// both sides since its content is unknown. Each independent region — a root
4587-
// resolve, a template hole, an element's children — starts fresh through the
4588-
// exported wrappers; only the internal walkers recurse.
4582+
// resolve, a template hole, an element's children — starts fresh at an entry
4583+
// call; the walkers' own recursion passes `nested` and keeps the state. (A
4584+
// parameter rather than a separate entry wrapper: these bodies are too big for
4585+
// V8 to inline, and the extra call per hole cost 2–5% of SSR throughput on
4586+
// element-heavy pages.)
45894587
let ssrTextTail = false;
45904588

4591-
function tryResolveStr(node) {
4589+
function tryResolveString(node, nested?: boolean) {
4590+
if (!nested) ssrTextTail = false;
45924591
const t = typeof node;
45934592
if (t === "string" || t === "number") {
45944593
const s = ssrTextTail ? "<!--!$-->" + node : "" + node;
@@ -4605,7 +4604,7 @@ function tryResolveStr(node) {
46054604
}
46064605
let s = "";
46074606
for (let i = 0, len = node.length; i < len; i++) {
4608-
const r = tryResolveStr(node[i]);
4607+
const r = tryResolveString(node[i], true);
46094608
if (typeof r !== "string") return { bail: node };
46104609
s += r;
46114610
}
@@ -4633,11 +4632,13 @@ function tryResolveStr(node) {
46334632
// Recurse on the evaluated value. If recursion bails, propagate the
46344633
// bail object unchanged — its `bail` field already carries the
46354634
// deepest evaluated form, so the caller never re-invokes `node`.
4636-
return tryResolveStr(v);
4635+
return tryResolveString(v, true);
46374636
}
46384637
return "";
46394638
}
4640-
export function resolveSSRNode(node: any, result?: any, top?: boolean): any;
4639+
// `nested` is the walker's own recursion flag (see `ssrTextTail` above);
4640+
// callers never pass it.
4641+
export function resolveSSRNode(node: any, result?: any, top?: boolean, nested?: boolean): any;
46414642

46424643
export function resolveSSRNode(
46434644
node,
@@ -4646,13 +4647,10 @@ export function resolveSSRNode(
46464647
h: [],
46474648
p: []
46484649
},
4649-
top
4650+
top,
4651+
nested
46504652
) {
4651-
ssrTextTail = false;
4652-
return walkSSRNode(node, result, top);
4653-
}
4654-
4655-
function walkSSRNode(node, result, top) {
4653+
if (!nested) ssrTextTail = false;
46564654
const t = typeof node;
46574655
if (t === "string" || t === "number") {
46584656
result.t[result.t.length - 1] += ssrTextTail ? "<!--!$-->" + node : node;
@@ -4671,7 +4669,7 @@ function walkSSRNode(node, result, top) {
46714669
// An element's direct children (`top`) are never separated from each
46724670
// other; separators still apply inside any nested array.
46734671
if (top) ssrTextTail = false;
4674-
walkSSRNode(node[i], result, false);
4672+
resolveSSRNode(node[i], result, false, true);
46754673
}
46764674
} finally {
46774675
if (slotLive) slotLive.suppressed--;
@@ -4704,10 +4702,10 @@ function walkSSRNode(node, result, top) {
47044702
// an unresolved hole.
47054703
result.t[result.t.length - 1] += ssrTextTail ? "<!--!$-->" + liveNode : liveNode;
47064704
ssrTextTail = true;
4707-
} else walkSSRNode(liveNode, result, false);
4705+
} else resolveSSRNode(liveNode, result, false, true);
47084706
} else {
47094707
try {
4710-
walkSSRNode(node(), result, false);
4708+
resolveSSRNode(node(), result, false, true);
47114709
} catch (err) {
47124710
const wrap = buildAsyncWrap(err, node);
47134711
if (wrap) {

0 commit comments

Comments
 (0)