Skip to content

Conversation

@josephsavona
Copy link
Member

@josephsavona josephsavona commented Nov 11, 2025

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.


Stack created with Sapling. Best reviewed with ReviewStack.

@meta-cla meta-cla bot added the CLA Signed label Nov 11, 2025
@github-actions github-actions bot added the React Core Team Opened by a member of the React Core Team label Nov 11, 2025
setState(e.target.value);
};
useEffect(() => {
log(ref.current.value);
Copy link
Member Author

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.

@josephsavona
Copy link
Member Author

josephsavona commented Nov 11, 2025

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

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.
@josephsavona josephsavona marked this pull request as ready for review November 14, 2025 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants