-
Notifications
You must be signed in to change notification settings - Fork 50.2k
[compiler][poc] Quick experiment with SSR-optimization pass #35102
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
Conversation
84c1d4b to
9d5ceb9
Compare
| setState(e.target.value); | ||
| }; | ||
| useEffect(() => { | ||
| log(ref.current.value); |
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 where the compiler machinery helps: the ref is referenced here, but DCE runs in a fixpoint. By pruning the effect call that makes its callback dead code, which then removes this reference to the ref. Then the OptimizeForSSR pass has already pruned the ref prop, so we're able to remove the useRef.
|
Another thing to add: for custom components we don't know for sure which callbacks are event handlers and which are render props. As implemented that means a lot of event/effect functions can't be pruned. But if we disallow setState during initial mount, then we know that any function which calls setState must be an event handler/effect. Similar for anything that calls startTransition. That should allow identifying a decent percentage of callbacks, though probably still far from all of them. edit: done |
d8a7469 to
2c5cdf0
Compare
2c5cdf0 to
5fa5200
Compare
mofeiZ
left a comment
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 sure you've already considered this, but it might be a pain to debug if we compile / replace these functions with a no-op -- but they actually get used during render |
|
Yeah fair, we could do a follow-up where we replace with a simple function that throws if called. |
Just a quick poc: * Inline useState when the initializer is known to not be a function. The heuristic could be improved but will handle a large number of cases already. * Prune effects * Prune useRef if the ref is unused, by pruning 'ref' props on primitive components. Then DCE does the rest of the work - with a small change to allow `useRef()` calls to be dropped since function calls aren't normally eligible for dropping. * Prune event handlers, by pruning props whose names start w "on" from primitive components. Then DCE removes the functions themselves. Per the fixture, this gets pretty far.
5fa5200 to
ab6d14b
Compare
Just a quick poc: * Inline useState when the initializer is known to not be a function. The heuristic could be improved but will handle a large number of cases already. * Prune effects * Prune useRef if the ref is unused, by pruning 'ref' props on primitive components. Then DCE does the rest of the work - with a small change to allow `useRef()` calls to be dropped since function calls aren't normally eligible for dropping. * Prune event handlers, by pruning props whose names start w "on" from primitive components. Then DCE removes the functions themselves. Per the fixture, this gets pretty far. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/35102). * #35112 * __->__ #35102 DiffTrain build for [4cf770d](4cf770d)
Just a quick poc: * Inline useState when the initializer is known to not be a function. The heuristic could be improved but will handle a large number of cases already. * Prune effects * Prune useRef if the ref is unused, by pruning 'ref' props on primitive components. Then DCE does the rest of the work - with a small change to allow `useRef()` calls to be dropped since function calls aren't normally eligible for dropping. * Prune event handlers, by pruning props whose names start w "on" from primitive components. Then DCE removes the functions themselves. Per the fixture, this gets pretty far. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/35102). * #35112 * __->__ #35102 DiffTrain build for [4cf770d](4cf770d)
Just a quick poc:
useRef()calls to be dropped since function calls aren't normally eligible for dropping.Per the fixture, this gets pretty far.
Stack created with Sapling. Best reviewed with ReviewStack.