Skip to content
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

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

Conversation

gentlee
Copy link

@gentlee gentlee commented Dec 2, 2023

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
Copy link

react-sizebot commented Dec 2, 2023

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

Copy link

github-actions bot commented Apr 5, 2024

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
Copy link
Author

gentlee commented Apr 5, 2024

bump

Copy link

github-actions bot commented Jul 9, 2024

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
Copy link

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
Copy link
Author

gentlee commented Jul 22, 2024

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