-
Notifications
You must be signed in to change notification settings - Fork 47.6k
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
JSX Outlining #30956
JSX Outlining #30956
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
09fb081
to
0835c3a
Compare
0835c3a
to
5feb8d8
Compare
5feb8d8
to
94b1541
Compare
94b1541
to
c55943e
Compare
9e06cca
to
4aa4cbd
Compare
Awesome! Looking forward to reading through this. Quick question:
Why do we need this as an instruction, vs doing a quick scan through the instructions to find the outermost JSX instruction? |
Yeah, this is a good question! I initially started with the scan approach but then hit a roadblock for this case: // @enableJsxOutlining
function T({ t, a, b }) {
return t.map(() => {
let x = <Foo a={a} />
let y = <Baz b={b} />
// ... potential shenanigans ...
return <Bar>{y}{x}</Bar>
});
}
function B({t, a, b }) {
return t.map(() => <Bar><Foo a={com(a)} /><Baz b={b} /></Bar>)
} Here we want to differentiate between the two cases and not outline the first example. The jsx expressions, in either case, aren't in a consecutive sequence to make the quick scan work. For the second case, the children are read as jsx expressions and not from a local. This is potentially a way to differentiate but doesn't seem future proof as lowering to a local shouldn't have any semantic change, so I can see us doing this as part of some future change. I ended up going with the |
Hmm, it seems pretty reasonable to just use a scan and exclude StoreLocal, which would reject the first example and allow the second. We could always expand to allow StoreLocal if we determined that it was only used once, in the subsequent JSX code. This could be a backwards traversal with a narrow allowlist of instructions like jsx, primitives, globals. It's really really nice to avoid adding instruction variants. |
options: {eliminateStartJsx: boolean} = { | ||
eliminateStartJsx: false, | ||
}, |
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 why it's so nice to avoid adding instructions :-)
queue.push({ | ||
kind: 'outlined', | ||
fn, | ||
fnType: outlined.type, |
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.
yay i'm so glad this worked!
fd7ca51
to
5192ae9
Compare
Oh good call! I've updated the PR to do this instead |
compiler/packages/babel-plugin-react-compiler/src/Optimization/OutlineJsx.ts
Show resolved
Hide resolved
compiler/packages/babel-plugin-react-compiler/src/Optimization/OutlineJsx.ts
Show resolved
Hide resolved
compiler/packages/babel-plugin-react-compiler/src/Optimization/OutlineJsx.ts
Outdated
Show resolved
Hide resolved
compiler/packages/babel-plugin-react-compiler/src/Optimization/OutlineJsx.ts
Outdated
Show resolved
Hide resolved
...s/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.jsx-outlining-children.js
Show resolved
Hide resolved
...packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/jsx-outlining-simple.js
Show resolved
Hide resolved
...lugin-react-compiler/src/__tests__/fixtures/compiler/jsx-outlining-separate-nested.expect.md
Outdated
Show resolved
Hide resolved
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 really exciting! Seems pretty close, see comments about duplicate attribute names
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.
Requesting changes for handling intermediate instructions and duplicate attribute names
c16cf36
to
edf1dc7
Compare
compiler/packages/babel-plugin-react-compiler/src/Optimization/OutlineJsx.ts
Show resolved
Hide resolved
compiler/packages/babel-plugin-react-compiler/src/Optimization/OutlineJsx.ts
Show resolved
Hide resolved
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.
Awesome!
Adds a new pass to outline nested jsx to a separate function. For now, we only outline nested jsx inside callbacks, but this can be extended in the future (for ex, to outline nested jsx inside loops).
edf1dc7
to
d9eed8b
Compare
Currently, the react compiler can not compile within callbacks which can potentially cause over rendering. Consider this example:
In this case, there's no memoization of the nested jsx elements. But instead if we were to manually refactor the nested jsx into separate component like this:
The compiler can now optimise both these components:
Now, when
countries
is updated by adding one single value, only the newly added value is re-rendered and not the entire list. Rather than having to do this manually, this PR teaches the react compiler to do this transformation.This PR adds a new pass (
OutlineJsx
) to capture nested jsx statements and outline them in a separate component. This newly outlined component can then by memoized by the compiler, giving us more fine grained rendering.There's a lot of improvements we can do in the future: