-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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] transform fire calls #31796
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Config flag for `fire` --
Makes `fire` a known export for type-based analysis --
Config flag for `fire` -- --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31794). * #31811 * #31798 * #31797 * #31796 * #31795 * __->__ #31794
Makes `fire` a known export for type-based analysis -- --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31795). * #31811 * #31798 * #31797 * #31796 * __->__ #31795 * #31794
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.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.
Exciting!!!! I'm still wrapping my head around this, sharing some early feedback
compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts
Outdated
Show resolved
Hide resolved
compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts
Outdated
Show resolved
Hide resolved
const rewriteInstrs = new Map<InstructionId, Array<Instruction>>(); | ||
const deleteInstrs = new Set<InstructionId>(); |
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.
the pattern we usually use for this is a nullable nextInstructions
array, initialized by slicing up to the point where you need to start making edits. then after that you push instructions to keep/add, don't push instructions you want to delete
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.
that's an inconvenient approach for this pass specifically.
We insert the useFire calls before the load of the useEffect
. If we were to wait until the point at which we know to generate them to insert, which is at the useEffect call, then we have to keep track of the index of the useEffect load index and insert the instructions there. This is easy to do with the InstructionIds because adding new instructions to the mapping does not shift the index of another useEffect load that occurs in the same block. If we push to the array, we'll need to additionally keep track of the number of added instructions so that we can account for the shifted offset in the nextInstructions array at which we should insert the useFire calls. If we naively insert the useFire calls right before the useEffect call then we introduce something like:
const effectLambda = () => {
fireFunction();
}
const fireFunction = useFire(aFunction);
useEffect(effectLambda);
This code is legit, but the HIR representation for this is not as simple (requires a DeclareContext HoistedConst
). It doesn't cause any of the tests to fail, but I'm not sure if that's really saying much.
compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts
Outdated
Show resolved
Hide resolved
} { | ||
const useFirePlace = createTemporaryPlace(env, GeneratedSource); | ||
useFirePlace.effect = Effect.Read; | ||
useFirePlace.identifier.type = DefaultNonmutatingHook; |
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.
can we be more precise and use the BuiltInFireId 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.
BuiltInFireId is for fire
, not useFire
- is there a benefit to also introducing a useFire
built in type?
compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts
Outdated
Show resolved
Hide resolved
compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts
Show resolved
Hide resolved
This is the diff with the meaningful changes. The approach is: 1. Collect fire callees and remove fire() calls, create a new binding for the useFire result 2. Update LoadLocals for captured callees to point to the useFire result 3. Update function context to reference useFire results 4. Insert useFire calls after getting to the component scope This approach aims to minimize the amount of new bindings we introduce for the function expressions to minimize bookkeeping for dependency arrays. We keep all of the LoadLocals leading up to function calls as they are and insert new instructions to load the originally captured function, call useFire, and store the result in a new promoted temporary. The lvalues that referenced the original callee are changed to point to the new useFire result. This is the minimal diff to implement the expected behavior (up to importing the useFire call, next diff) and further stacked diffs implement error handling. The rules for fire are: 1. If you use fire for a callee in the effect once you must use it for every time you call it in that effect 2. You can only use fire in a useEffect lambda/functions defined inside the useEffect lambda There is still more work to do here, like updating the effect dependency array and handling object methods --
This is the diff with the meaningful changes. The approach is:
This approach aims to minimize the amount of new bindings we introduce for the function expressions
to minimize bookkeeping for dependency arrays. We keep all of the LoadLocals leading up to function
calls as they are and insert new instructions to load the originally captured function, call useFire,
and store the result in a new promoted temporary. The lvalues that referenced the original callee are
changed to point to the new useFire result.
This is the minimal diff to implement the expected behavior (up to importing the useFire call, next diff)
and further stacked diffs implement error handling. The rules for fire are:
There is still more work to do here, like updating the effect dependency array and handling object methods
--
Stack created with Sapling. Best reviewed with ReviewStack.