Skip to content

Fix useSyncExternalStoreWithSelector to not call selectors when snapshot hasn't changed - #27776

Closed
gentlee wants to merge 3 commits into
react:mainfrom
gentlee:useSyncExternalStoreWithSelector/fix-calling-selectors-when-snapshot-not-changed
Closed

Fix useSyncExternalStoreWithSelector to not call selectors when snapshot hasn't changed#27776
gentlee wants to merge 3 commits into
react:mainfrom
gentlee:useSyncExternalStoreWithSelector/fix-calling-selectors-when-snapshot-not-changed

Conversation

@gentlee

@gentlee gentlee commented Dec 2, 2023

Copy link
Copy Markdown

Summary

Current implementation of useSyncExternalStoreWithSelector calls selectors even if snapshot hasn't changed.

Details

Up to 50% of all dispatched actions in the apps that use redux-saga or redux-thunk could be ones that don't change the redux state but start some async logic.

Current redux implementation notifies listeners when any action is called, even if it does not change the state, and this behavior won't change.

Current react-redux implementation uses useSyncExternalStoreWithSelector from React, which notifies subscribers for each such action even if state hasn't changed, causing all active selectors to be re-evaluated, which is actually a bug of useSyncExternalStoreWithSelector.

In huge apps there can be hundreds of subscribed selectors and dozens of actions per second, so this leads to additional CPU usage for no good reason. Especially noticable in react-native apps on old android devices.

How did you test this change?

I added a test which fails in current version, and fixed it:

    ...

    const container = document.createElement('div');
    const root = createRoot(container);
    await act(() => root.render(<App />));
    assertLog(['App', 'Selector', 'A0']);

    await act(() => store.set(store.getState()));
    await act(() => store.set({a: 0}));
    assertLog(['Selector']);

    await act(() => store.set(store.getState()));
    await act(() => store.set(store.getState()));
    assertLog([]);  // without the fix here we got ["Selector", "Selector"]

@react-sizebot

react-sizebot commented Dec 2, 2023

Copy link
Copy Markdown

Comparing: 640cceb...10ac3fb

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 175.90 kB 175.90 kB = 54.75 kB 54.75 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 177.97 kB 177.97 kB = 55.39 kB 55.39 kB
facebook-www/ReactDOM-prod.classic.js = 569.81 kB 569.81 kB = 100.29 kB 100.29 kB
facebook-www/ReactDOM-prod.modern.js = 553.67 kB 553.67 kB = 97.38 kB 97.38 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/use-sync-external-store/cjs/use-sync-external-store-with-selector.production.js +0.81% 5.08 kB 5.12 kB +0.12% 1.71 kB 1.71 kB
oss-stable-semver/use-sync-external-store/cjs/use-sync-external-store-with-selector.production.js +0.81% 5.08 kB 5.12 kB +0.12% 1.71 kB 1.71 kB
oss-stable/use-sync-external-store/cjs/use-sync-external-store-with-selector.production.js +0.81% 5.08 kB 5.12 kB +0.12% 1.71 kB 1.71 kB
oss-experimental/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.js +0.80% 5.14 kB 5.18 kB +0.12% 1.73 kB 1.73 kB
oss-stable-semver/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.js +0.80% 5.14 kB 5.18 kB +0.12% 1.73 kB 1.73 kB
oss-stable/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.js +0.80% 5.14 kB 5.18 kB +0.12% 1.73 kB 1.73 kB
oss-experimental/use-sync-external-store/cjs/use-sync-external-store-with-selector.development.js +0.71% 5.80 kB 5.84 kB +0.16% 1.88 kB 1.89 kB
oss-stable-semver/use-sync-external-store/cjs/use-sync-external-store-with-selector.development.js +0.71% 5.80 kB 5.84 kB +0.16% 1.88 kB 1.89 kB
oss-stable/use-sync-external-store/cjs/use-sync-external-store-with-selector.development.js +0.71% 5.80 kB 5.84 kB +0.16% 1.88 kB 1.89 kB
oss-experimental/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js +0.70% 5.85 kB 5.89 kB +0.05% 1.90 kB 1.90 kB
oss-stable-semver/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js +0.70% 5.85 kB 5.89 kB +0.05% 1.90 kB 1.90 kB
oss-stable/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js +0.70% 5.85 kB 5.89 kB +0.05% 1.90 kB 1.90 kB
oss-experimental/use-sync-external-store/cjs/use-sync-external-store-with-selector.production.min.js +0.36% 1.12 kB 1.12 kB +0.64% 0.62 kB 0.63 kB
oss-stable-semver/use-sync-external-store/cjs/use-sync-external-store-with-selector.production.min.js +0.36% 1.12 kB 1.12 kB +0.64% 0.62 kB 0.63 kB
oss-stable/use-sync-external-store/cjs/use-sync-external-store-with-selector.production.min.js +0.36% 1.12 kB 1.12 kB +0.64% 0.62 kB 0.63 kB
oss-experimental/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.js +0.35% 1.14 kB 1.15 kB +0.47% 0.64 kB 0.64 kB
oss-stable-semver/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.js +0.35% 1.14 kB 1.15 kB +0.47% 0.64 kB 0.64 kB
oss-stable/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.js +0.35% 1.14 kB 1.15 kB +0.47% 0.64 kB 0.64 kB

Generated by 🚫 dangerJS against 10ac3fb

@github-actions

github-actions Bot commented Apr 5, 2024

Copy link
Copy Markdown

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions Bot added the Resolution: Stale Automatically closed due to inactivity label Apr 5, 2024
@gentlee

gentlee commented Apr 5, 2024

Copy link
Copy Markdown
Author

bump

@github-actions

github-actions Bot commented Jul 9, 2024

Copy link
Copy Markdown

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions Bot added the Resolution: Stale Automatically closed due to inactivity label Jul 9, 2024
@github-actions

Copy link
Copy Markdown

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@github-actions github-actions Bot closed this Jul 16, 2024
@gentlee

gentlee commented Jul 22, 2024

Copy link
Copy Markdown
Author

reopen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Resolution: Stale Automatically closed due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants