Skip to content

Commit 36ba7be

Browse files
perf(compiler): SSR props literals with getters compile to hoisted constructors (hoistProps)
A component's props literal with getters — the compiler's own `{ get x() { return sig() }, ... }` — is built by V8 in dictionary mode with a closure per getter per instance. With `hoistProps` (default on) both compilers emit a module-level constructor whose getters live on the prototype and read their captures through symbol slots on the instance; the call site becomes `new _P$(a, b)`. Own keys, order, descriptors and prototype are unchanged, so `Object.keys`, spread, `hasOwn`, `isStatic()` and `hasStaticKeys()` answer as before. The contract that changes: a props getter is defined only for a read through its own object. Copying its descriptor onto another object (`Object.defineProperty(target, k, getOwnPropertyDescriptor(props, k))`) no longer works; in dev the getter throws naming the rule. Our own primitives never copy descriptors except omit()'s no-Proxy fallback, fixed in #3544 to re-home accessors. Sites the pass leaves as literals: getters that close over a binding that is reassigned or declared after the site, `this`/`super`/`arguments`, or a module-level site (nothing to hoist above). Compiled template temps that belong to a single getter move into it. Babel (`src/ssr/props.ts`) and the native compiler (`src/ssr/props.rs`) emit byte-identical output, including Babel's `_p`, `_p2`…`_p9`, `_p0`, `_p1`, `_p10` parameter sequence; the native compiler additionally marks the IIFE it generates for module-level `this` capture as transparent to the depth rule, which Babel does not need. Measured (yak-bench, SSR CPU per render, native compiler, mprim lane): component-heavy pages +15–31%, geomean 1.12×; Octane ssr-throughput and streaming-ssr flat (element-heavy fixtures, no getter literals on the hot path). Co-Authored-By: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 4b01d81 commit 36ba7be

29 files changed

Lines changed: 2713 additions & 344 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@solidjs/babel-plugin": patch
3+
"@solidjs/compiler": patch
4+
---
5+
6+
SSR: a component's props literal with getters compiles to a module-level constructor with shared getters (`hoistProps`, default on) instead of an object literal, which V8 builds in dictionary mode with a closure per getter per instance. Same own keys, order, descriptors and prototype; a props getter is now defined only for a read through its own object — copying its descriptor elsewhere throws (dev names the rule). Sites closing over a reassigned or later-declared binding, `this`, or `arguments` keep the literal. Babel and the native compiler emit the same output.

‎packages/babel-plugin/README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,34 @@ Inline style attributes in templates when the value is a string or `Record<strin
250250

251251
SSR-only: emit behavior-claim (`_bnd`) markers for `ref` / `on*` on intrinsic elements.
252252

253+
### hoistProps
254+
255+
- Type: `boolean`
256+
- Default: `true`
257+
258+
SSR-only: a component's props literal with getters compiles to a module-level constructor whose getters are shared across instances, instead of an object literal (which V8 builds in dictionary mode, allocating a closure per getter per instance):
259+
260+
```js
261+
var _m$ = Symbol();
262+
var _d$ = {
263+
get() {
264+
const props = this[_m$];
265+
return props.label;
266+
},
267+
enumerable: true,
268+
configurable: true
269+
};
270+
function _P$(_p, _p2) {
271+
this[_m$] = _p;
272+
this.as = _p2;
273+
Object.defineProperty(this, "label", _d$);
274+
}
275+
_P$.prototype = Object.prototype;
276+
Comp(new _P$(props, "a"));
277+
```
278+
279+
The instance has the same own keys, order and descriptors as the literal and `Object.prototype` as its prototype; `Object.keys`, spread, `hasOwn` and `isStatic()` answer as before. One contract follows: a props getter is defined only for a read through its own object (`props.x`, spread, `merge()`/`omit()`). Copying its property descriptor onto another object and reading it there throws — define a getter that reads through the source instead. Sites whose getters close over a binding that is reassigned, declared after the site, or `this`/`arguments` keep the literal. `false` keeps the literal everywhere.
280+
253281
## TSRX (experimental)
254282

255283
TSRX (TypeScript Render Extensions) is a syntax for declarative UI. `.tsrx` sources desugar to the same Solid JSX this plugin already compiles: `@if`/`@else`, `@for … @empty`, `@switch`/`@case`, and `@try`/`@catch`/`@pending` lower to the corresponding control-flow components (`Show`, `For`, `Switch`/`Match`, `Errored`, `Loading`), and `@{}` statement containers mix setup statements with rendered elements.

‎packages/babel-plugin/src/config.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ export interface PluginConfig {
3939
validate: boolean;
4040
inlineStyles: boolean;
4141
serverComponents: boolean;
42+
/** SSR only: emit each component's props literal with getters as a
43+
* module-level constructor with shared getters (one hidden class per call
44+
* site, no closure per getter per instance) instead of an object literal,
45+
* which V8 builds in dictionary mode. Same own keys, order, descriptors
46+
* and prototype; a getter is defined only for a read through its own
47+
* object (#3511). `false` keeps the literal everywhere. */
48+
hoistProps: boolean;
4249
renderers?: RendererConfig[];
4350
}
4451

@@ -75,7 +82,8 @@ const config: PluginConfig = {
7582
memoWrapper: "memo",
7683
validate: true,
7784
inlineStyles: true,
78-
serverComponents: false
85+
serverComponents: false,
86+
hoistProps: true
7987
};
8088

8189
export default config;

‎packages/babel-plugin/src/shared/component.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
convertJSXIdentifier
1111
} from "./utils";
1212
import { transformNode, getCreateTemplate, thisTagIdentifiers } from "./transform";
13+
import { markPropsLiteral } from "../ssr/props";
1314
import type { PluginConfig } from "../config";
1415
import type { BabelPath, JSXNode, TransformResult } from "../types";
1516

@@ -82,6 +83,13 @@ export default function transformComponent(
8283
runningObject: Array<t.ObjectProperty | t.ObjectMethod> = [],
8384
dynamicSpread = false,
8485
hasChildren = path.node.children.length > 0;
86+
// Each props literal this call emits is a candidate for the SSR hoisted
87+
// shape (ssr/props.ts), decided at Program exit once every body is final.
88+
const propsLiteral = (properties: Array<t.ObjectProperty | t.ObjectMethod>) => {
89+
const literal = t.objectExpression(properties);
90+
markPropsLiteral(literal, path, config);
91+
return literal;
92+
};
8593

8694
if (
8795
t.isIdentifier(tagId) &&
@@ -99,7 +107,7 @@ export default function transformComponent(
99107
const node = attribute.node;
100108
if (t.isJSXSpreadAttribute(node)) {
101109
if (runningObject.length) {
102-
props.push(t.objectExpression(runningObject));
110+
props.push(propsLiteral(runningObject));
103111
runningObject = [];
104112
}
105113
props.push(
@@ -345,7 +353,7 @@ export default function transformComponent(
345353
);
346354
} else runningObject.push(t.objectProperty(t.identifier("children"), childResult[0]));
347355
}
348-
if (runningObject.length || !props.length) props.push(t.objectExpression(runningObject));
356+
if (runningObject.length || !props.length) props.push(propsLiteral(runningObject));
349357

350358
if (props.length > 1 || dynamicSpread) {
351359
props = [t.callExpression(registerImportMethod(path, "mergeProps"), props)];

‎packages/babel-plugin/src/shared/postprocess.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getRendererConfig, registerImportMethod } from "./utils";
33
import { appendTemplates as appendTemplatesDOM } from "../dom/template";
44
import { appendTemplates as appendTemplatesSSR } from "../ssr/template";
55
import { isInvalidMarkup } from "./validate";
6+
import { hoistProps, takeHoistedProps } from "../ssr/props";
67
import type { NodePath } from "@babel/traverse";
78
import type { BabelHubWithMetadata, PluginPass, ProgramScopeData } from "../types";
89

@@ -14,6 +15,15 @@ export default (path: NodePath<t.Program>, state: PluginPass) => {
1415
const config = (path.hub as unknown as BabelHubWithMetadata).file.metadata.config;
1516
if (!config) return;
1617

18+
// SSR hoisted props shapes: analysed now that every getter body is final,
19+
// placed at the top of the module (before the templates, which unshift
20+
// after this) so a shape exists before any component runs.
21+
if (config.generate === "ssr" && config.hoistProps) {
22+
hoistProps(path, config);
23+
const hoisted = takeHoistedProps(path);
24+
if (hoisted) path.node.body.unshift(...hoisted);
25+
}
26+
1727
if (data.events) {
1828
path.node.body.push(
1929
t.expressionStatement(

0 commit comments

Comments
 (0)