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

Copy link
Contributor

@mofeiZ mofeiZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exciting!

@mofeiZ
Copy link
Contributor

mofeiZ commented Nov 20, 2025

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.

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

@josephsavona
Copy link
Member Author

josephsavona commented Nov 20, 2025

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.
@josephsavona josephsavona merged commit 4cf770d into main Nov 20, 2025
18 checks passed
github-actions bot pushed a commit that referenced this pull request Nov 20, 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.

---
[//]: # (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)
github-actions bot pushed a commit that referenced this pull request Nov 20, 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.

---
[//]: # (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)
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.

3 participants