Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Avoid re-rendering components if selector values don't change #749

Closed

Conversation

drarmstr
Copy link
Contributor

Summary:
An optimization that will avoid re-rendering React components if selector values they subscribe to don't change (based on reference equality).

Currently, changing an atom to the same value (based on reference equality) will not propagate any downstream updates. However, any updates to a selector would propagate updates to all downstream selectors and re-render all subscribing components.

This is only a partial optimization because downstream selectors will still re-evaluate. However, the React component itself will see that the final value has not changed and will suppress re-rendering. This is also only a partial fix because it only limits suppression based on reference-equality, and does not allow for a user-defined equality check.

Additionally - In order to compare the new and old values before re-rendering, the subscribing component will evaluate the selector before deciding to re-render. It gets the value again during rendering, however the value will be cached in atomValues, so this should be cheap. By moving this evaluation earlier, it may actually help "pre-fetch" asynchronous requests before rendering starts.

Differential Revision: D25040920

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported labels Nov 18, 2020
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D25040920

…okexperimental#749)

Summary:
Pull Request resolved: facebookexperimental#749

An optimization that will avoid re-rendering React components if selector values they subscribe to don't change (based on reference equality).

Currently, changing an atom to the same value (based on reference equality) will not propagate any downstream updates.  However, any updates to a selector would propagate updates to all downstream selectors and re-render all subscribing components.

This is only a partial optimization because downstream selectors will still re-evaluate.  However, the React component itself will see that the final value has not changed and will suppress re-rendering.  This is also only a partial fix because it only limits suppression based on reference-equality, and does not allow for a user-defined equality check.

Additionally - In order to compare the new and old values before re-rendering, the subscribing component will evaluate the selector before deciding to re-render.  It gets the value again during rendering, however the value will be cached in `atomValues`, so this should be cheap.  By moving this evaluation earlier, it may actually help "pre-fetch" asynchronous requests before rendering starts.

NOTE: This optimization is currently available under the `recoil_suppress_rerender_in_callback` GK

Reviewed By: csantos42

Differential Revision: D25040920

fbshipit-source-id: 353e41cebac8566ddf7ea4de91e3790492d1178a
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D25040920

@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 13964b3.

@drarmstr
Copy link
Contributor Author

Note that while this diff was merged it is currently behind a feature flag.

@salvoravida
Copy link
Contributor

maybe related #825

const source = useRecoilMutableSource();
const loadable = useMutableSource(source, getLoadableWithTesting, subscribe);
const prevLoadableRef = useRef(loadable);
useEffect(() => {
Copy link

Choose a reason for hiding this comment

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

@drarmstr why not layoutEffect? Postponed update of the ref could cause some propblem, don't you think?

@pthieu
Copy link

pthieu commented Jul 2, 2022

@drarmstr did this ever get released?

AlexGuz23 pushed a commit to AlexGuz23/Recoil that referenced this pull request Nov 3, 2022
Summary:
Pull Request resolved: facebookexperimental/Recoil#749

An optimization that will avoid re-rendering React components if selector values they subscribe to don't change (based on reference equality).

Currently, changing an atom to the same value (based on reference equality) will not propagate any downstream updates.  However, any updates to a selector would propagate updates to all downstream selectors and re-render all subscribing components.

This is only a partial optimization because downstream selectors will still re-evaluate.  However, the React component itself will see that the final value has not changed and will suppress re-rendering.  This is also only a partial fix because it only limits suppression based on reference-equality, and does not allow for a user-defined equality check.

Additionally - In order to compare the new and old values before re-rendering, the subscribing component will evaluate the selector before deciding to re-render.  It gets the value again during rendering, however the value will be cached in `atomValues`, so this should be cheap.  By moving this evaluation earlier, it may actually help "pre-fetch" asynchronous requests before rendering starts.

NOTE: This optimization is currently available under the `recoil_suppress_rerender_in_callback` GK

Reviewed By: csantos42

Differential Revision: D25040920

fbshipit-source-id: 1d49157fcc00b87bb1b3c4bee6179f5c3e3343ba
snipershooter0701 pushed a commit to snipershooter0701/Recoil that referenced this pull request Mar 5, 2023
Summary:
Pull Request resolved: facebookexperimental/Recoil#749

An optimization that will avoid re-rendering React components if selector values they subscribe to don't change (based on reference equality).

Currently, changing an atom to the same value (based on reference equality) will not propagate any downstream updates.  However, any updates to a selector would propagate updates to all downstream selectors and re-render all subscribing components.

This is only a partial optimization because downstream selectors will still re-evaluate.  However, the React component itself will see that the final value has not changed and will suppress re-rendering.  This is also only a partial fix because it only limits suppression based on reference-equality, and does not allow for a user-defined equality check.

Additionally - In order to compare the new and old values before re-rendering, the subscribing component will evaluate the selector before deciding to re-render.  It gets the value again during rendering, however the value will be cached in `atomValues`, so this should be cheap.  By moving this evaluation earlier, it may actually help "pre-fetch" asynchronous requests before rendering starts.

NOTE: This optimization is currently available under the `recoil_suppress_rerender_in_callback` GK

Reviewed By: csantos42

Differential Revision: D25040920

fbshipit-source-id: 1d49157fcc00b87bb1b3c4bee6179f5c3e3343ba
@chriszrc
Copy link

chriszrc commented May 3, 2024

eagle2722 added a commit to eagle2722/Recoil that referenced this pull request Sep 21, 2024
Summary:
Pull Request resolved: facebookexperimental/Recoil#749

An optimization that will avoid re-rendering React components if selector values they subscribe to don't change (based on reference equality).

Currently, changing an atom to the same value (based on reference equality) will not propagate any downstream updates.  However, any updates to a selector would propagate updates to all downstream selectors and re-render all subscribing components.

This is only a partial optimization because downstream selectors will still re-evaluate.  However, the React component itself will see that the final value has not changed and will suppress re-rendering.  This is also only a partial fix because it only limits suppression based on reference-equality, and does not allow for a user-defined equality check.

Additionally - In order to compare the new and old values before re-rendering, the subscribing component will evaluate the selector before deciding to re-render.  It gets the value again during rendering, however the value will be cached in `atomValues`, so this should be cheap.  By moving this evaluation earlier, it may actually help "pre-fetch" asynchronous requests before rendering starts.

NOTE: This optimization is currently available under the `recoil_suppress_rerender_in_callback` GK

Reviewed By: csantos42

Differential Revision: D25040920

fbshipit-source-id: 1d49157fcc00b87bb1b3c4bee6179f5c3e3343ba
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid re-rendering components if selector value hasn't changed.
6 participants