-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[compiler] Add JSX inlining optimization #30867
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
/** | ||
* Fixup the HIR to restore RPO, ensure correct predecessors, and | ||
* renumber instructions. Note that the renumbering instructions | ||
* invalidates scope and identifier ranges, so we fix them in the | ||
* next step. | ||
*/ | ||
reversePostorderBlocks(fn.body); | ||
markPredecessors(fn.body); | ||
markInstructionIds(fn.body); | ||
|
||
/** | ||
* Fix scope and identifier ranges to account for renumbered instructions | ||
*/ | ||
for (const [, block] of fn.body.blocks) { | ||
const terminal = block.terminal; | ||
if (terminal.kind === 'scope' || terminal.kind === 'pruned-scope') { | ||
/* | ||
* Scope ranges should always align to start at the 'scope' terminal | ||
* and end at the first instruction of the fallthrough block | ||
*/ | ||
const fallthroughBlock = fn.body.blocks.get(terminal.fallthrough)!; | ||
const firstId = | ||
fallthroughBlock.instructions[0]?.id ?? fallthroughBlock.terminal.id; | ||
terminal.scope.range.start = terminal.id; | ||
terminal.scope.range.end = firstId; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I stole this from BuildReactiveScopeTerminalsHIR.ts. Should this be a shared utility?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should extract the "fix scope and identifier ranges..." for loop into a shared utility and then we can call it where necessary. There are some passes that need to fix up scopes but don't need to run reversePostorderBlocks and markPredecessors, so putting all 4 of these calls into a utility doesn't make sense.
This should not be shipped in code that's compiled for npm since it is not compatible with multiple React versions. It's also important that it doesn't ship in an RSC layer which needs a runtime to do the warm up pass. (In addition, previous testing showed that this is not actually better than a minimal runtime in most environments due to the additional compilation cost.) |
To clarify the last statement. Most testing of inline objects have compared it against the React runtime which has overhead mainly due to legacy features like the refs and the backwards compat with defaultProps. A proper A/B test should test the performance of inlining compared to an optimized runtime function that does the same thing that the inlining would do. Hopefully in React 20+ that can just be the same thing in the plain In that comparison it was previous discovered that the compilation time for VMs like V8 that can JIT a callsite to determine which hidden class to create but figuring that out for each callsite means it has to transition through the hidden class discovery like This effect wasn't as big in JSC possibly due to faster compiler but also due to lack of other call optimizations not being as good so the relative size isn't necessarily there. That's why you might see inlining being slightly faster in JSC. (Most comparisons like Bun's inlining 1) isn't comparing against an optimized function so not a proper A/B test 2) doesn't care about start up time. So that data is not applicable here.) For Hermes, since the compilation time is entirely offline the cost of inlining isn't as big so the runtime overhead of the extra call might make it worth it there but it comes as the cost of larger byte code which may or may not matter. Basically I'm skeptical overall it is actually good to inline in most circumstances and it would be better to just have a single shared runtime. Given that the output is not multi-version compatible (i.e. the strategy of using React Compiler before publishing no longer works) and it's actually worse in the largest platform (Chrome)*. And it's not compatible with JSX prewarming in RSC. *) This could use re-measurement though since it's based on stale data. |
@sebmarkbage For now this optimization is meant to be turned on for Hermes only. I benchmarked and profiled the inlining on some test apps and saw some regressions in V8 as you described. In some apps interactions were faster, but memory regressed significantly. Running with With Hermes, inlining showed wins to interaction performance and memory, even compared to a React 20 style jsx function. With this implementation we can test in production against real apps and understand the full impact of the change and if its worth maintaining the optimization for Hermes only. |
lvalue: {...symbolForPlace, effect: Effect.Read}, | ||
value: { | ||
kind: 'PropertyLoad', | ||
object: symbolInstruction.lvalue, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that places always have to be copied, not used by reference, so {...symbolPlace}
here
case 'BuiltinTag': | ||
const tagPropertyPlace = createTemporaryPlace(fn.env, instr.value.loc); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always wrap cases in {}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! A few minor changes and then we should be able to land and experiment.
24c51eb
to
8b69a6f
Compare
8b69a6f
to
2f90fcf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Just one quick thing to double-check for when there's a single child element.
type: "div", | ||
ref: ref, | ||
key: null, | ||
props: { children: [children] }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be an array ([children]
) or just the raw children
value?
Nice! Let's ship and iterate |
This adds an `InlineJsxTransform` optimization pass, toggled by the `enableInlineJsxTransform` flag. When enabled, JSX will be transformed into React Element object literals, preventing runtime overhead during element creation. TODO: - [ ] Add conditionals to make transform PROD-only - [ ] Make the React element symbol configurable so this works with runtimes that support `react.element` or `react.transitional.element` - [ ] Look into additional optimization to pass props spread through directly if none of the properties are mutated DiffTrain build for [5dcb009](5dcb009)
This adds an
InlineJsxTransform
optimization pass, toggled by theenableInlineJsxTransform
flag. When enabled, JSX will be transformed into React Element object literals, preventing runtime overhead during element creation.TODO:
react.element
orreact.transitional.element