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

Don't bother comparing constructor when deps are not provided #14594

Merged
merged 3 commits into from
Jan 17, 2019

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Jan 15, 2019

When no dependencies are passed to an effect hook, what we used to do is compare the effect constructor. If there was no change, then we would skip firing the effect. In practice, this is a useless optimization because the constructor will always be different when you pass an inline closure. And if you don't pass an inline closure, then you can't access any props or state.

There are some edge cases where an effect that doesn't close over props or state could be useful, like reference counting the number of mounted components. But those are rare and can be addressed by passing an empty array of dependencies.

By removing this "optimization," we can avoid retaining the constructor in the majority of cases where it's a closure that changes on every render.

I made corresponding changes to the other hooks that accept dependencies, too (useMemo, useCallback, and useImperativeHandle).

@acdlite acdlite requested a review from sebmarkbage January 15, 2019 02:47
@acdlite acdlite force-pushed the never-memoize-if-no-deps branch 2 times, most recently from 8714c53 to c0d53ae Compare January 15, 2019 03:21
@sizebot
Copy link

sizebot commented Jan 15, 2019

ReactDOM: size: 0.0%, gzip: -0.0%

Details of bundled changes.

Comparing: 71b64d5...c7d8ebc

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js +0.2% +0.1% 729.32 KB 730.47 KB 168.51 KB 168.74 KB UMD_DEV
react-dom.production.min.js 0.0% -0.0% 98.88 KB 98.88 KB 32.2 KB 32.2 KB UMD_PROD
react-dom.profiling.min.js 0.0% -0.0% 101.86 KB 101.86 KB 32.84 KB 32.84 KB UMD_PROFILING
react-dom.development.js +0.2% +0.1% 724.38 KB 725.53 KB 167.08 KB 167.31 KB NODE_DEV
react-dom.production.min.js 0.0% -0.0% 98.88 KB 98.88 KB 31.67 KB 31.67 KB NODE_PROD
react-dom.profiling.min.js 0.0% -0.0% 101.98 KB 101.98 KB 32.3 KB 32.29 KB NODE_PROFILING
ReactDOM-dev.js +0.2% +0.1% 745.93 KB 747.08 KB 168.18 KB 168.41 KB FB_WWW_DEV
ReactDOM-prod.js -0.0% -0.0% 310.8 KB 310.8 KB 57.43 KB 57.42 KB FB_WWW_PROD
ReactDOM-profiling.js -0.0% -0.0% 317.97 KB 317.96 KB 58.76 KB 58.75 KB FB_WWW_PROFILING
react-dom-unstable-fire.development.js +0.2% +0.1% 729.61 KB 730.76 KB 168.62 KB 168.85 KB UMD_DEV
react-dom-unstable-fire.production.min.js 0.0% -0.0% 98.89 KB 98.89 KB 32.21 KB 32.21 KB UMD_PROD
react-dom-unstable-fire.profiling.min.js 0.0% -0.0% 101.88 KB 101.88 KB 32.85 KB 32.85 KB UMD_PROFILING
react-dom-unstable-fire.development.js +0.2% +0.1% 724.67 KB 725.82 KB 167.19 KB 167.42 KB NODE_DEV
react-dom-unstable-fire.production.min.js 0.0% -0.0% 98.89 KB 98.89 KB 31.68 KB 31.67 KB NODE_PROD
react-dom-unstable-fire.profiling.min.js 0.0% -0.0% 102 KB 102 KB 32.31 KB 32.3 KB NODE_PROFILING
ReactFire-dev.js +0.2% +0.1% 745.08 KB 746.23 KB 168.1 KB 168.33 KB FB_WWW_DEV
ReactFire-prod.js -0.0% -0.0% 299.4 KB 299.39 KB 55.16 KB 55.15 KB FB_WWW_PROD
ReactFire-profiling.js -0.0% -0.0% 306.49 KB 306.48 KB 56.44 KB 56.43 KB FB_WWW_PROFILING
react-dom-test-utils.development.js 0.0% -0.0% 44.87 KB 44.87 KB 12.3 KB 12.3 KB UMD_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 9.97 KB 9.97 KB 3.71 KB 3.71 KB UMD_PROD
react-dom-test-utils.development.js 0.0% -0.0% 44.59 KB 44.59 KB 12.24 KB 12.24 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 9.74 KB 9.74 KB 3.65 KB 3.65 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.61 KB 60.61 KB 15.92 KB 15.92 KB UMD_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.1% 11.01 KB 11.01 KB 3.81 KB 3.81 KB UMD_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.29 KB 60.29 KB 15.79 KB 15.79 KB NODE_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% 0.0% 10.75 KB 10.75 KB 3.7 KB 3.71 KB NODE_PROD
react-dom-server.browser.development.js +0.6% +0.5% 123.97 KB 124.7 KB 33.11 KB 33.28 KB UMD_DEV
react-dom-server.browser.production.min.js 0.0% -0.0% 16.96 KB 16.96 KB 6.53 KB 6.53 KB UMD_PROD
react-dom-server.browser.development.js +0.6% +0.5% 120.1 KB 120.83 KB 32.18 KB 32.35 KB NODE_DEV
react-dom-server.browser.production.min.js 0.0% -0.0% 16.87 KB 16.87 KB 6.51 KB 6.51 KB NODE_PROD
ReactDOMServer-dev.js +0.6% +0.6% 121.25 KB 122.02 KB 31.81 KB 32 KB FB_WWW_DEV
ReactDOMServer-prod.js 🔺+0.5% 🔺+0.3% 44.49 KB 44.72 KB 10.3 KB 10.33 KB FB_WWW_PROD
react-dom-server.node.development.js +0.6% +0.5% 122.15 KB 122.89 KB 32.72 KB 32.9 KB NODE_DEV
react-dom-server.node.production.min.js 0.0% -0.0% 17.74 KB 17.74 KB 6.83 KB 6.83 KB NODE_PROD
react-dom-unstable-fizz.browser.development.js 0.0% -0.1% 3.63 KB 3.63 KB 1.44 KB 1.44 KB UMD_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.1% 1.21 KB 1.21 KB 707 B 706 B UMD_PROD
react-dom-unstable-fizz.browser.development.js 0.0% -0.1% 3.45 KB 3.45 KB 1.39 KB 1.39 KB NODE_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.3% 1.05 KB 1.05 KB 638 B 636 B NODE_PROD
react-dom-unstable-fizz.node.development.js 0.0% -0.1% 3.7 KB 3.7 KB 1.42 KB 1.42 KB NODE_DEV
react-dom-unstable-fizz.node.production.min.js 0.0% -0.1% 1.1 KB 1.1 KB 668 B 667 B NODE_PROD

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js +0.2% +0.2% 509.71 KB 510.86 KB 112.47 KB 112.71 KB UMD_DEV
react-art.production.min.js 0.0% -0.0% 90.97 KB 90.97 KB 27.96 KB 27.96 KB UMD_PROD
react-art.development.js +0.3% +0.2% 441.22 KB 442.38 KB 95.36 KB 95.59 KB NODE_DEV
react-art.production.min.js 0.0% -0.0% 55.95 KB 55.95 KB 17.24 KB 17.24 KB NODE_PROD
ReactART-dev.js +0.3% +0.2% 449.61 KB 450.76 KB 94.39 KB 94.62 KB FB_WWW_DEV
ReactART-prod.js -0.0% 0.0% 186.85 KB 186.84 KB 32.07 KB 32.08 KB FB_WWW_PROD

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactNativeRenderer-dev.js +0.2% +0.2% 574.1 KB 575.25 KB 124.92 KB 125.15 KB RN_FB_DEV
ReactNativeRenderer-prod.js -0.0% -0.0% 242.38 KB 242.38 KB 42.68 KB 42.67 KB RN_FB_PROD
ReactNativeRenderer-profiling.js -0.0% -0.0% 248.53 KB 248.53 KB 44.02 KB 44.02 KB RN_FB_PROFILING
ReactNativeRenderer-dev.js +0.2% +0.2% 574.02 KB 575.16 KB 124.88 KB 125.11 KB RN_OSS_DEV
ReactFabric-dev.js +0.2% +0.2% 564.96 KB 566.1 KB 122.63 KB 122.86 KB RN_FB_DEV
ReactFabric-prod.js -0.0% 0.0% 235.58 KB 235.58 KB 41.19 KB 41.2 KB RN_FB_PROD
ReactFabric-profiling.js -0.0% -0.0% 240.88 KB 240.88 KB 42.55 KB 42.55 KB RN_FB_PROFILING
ReactFabric-dev.js +0.2% +0.2% 564.86 KB 566.01 KB 122.59 KB 122.82 KB RN_OSS_DEV

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js +0.3% +0.2% 454.44 KB 455.59 KB 98.13 KB 98.36 KB UMD_DEV
react-test-renderer.production.min.js 0.0% 0.0% 57.35 KB 57.35 KB 17.62 KB 17.62 KB UMD_PROD
react-test-renderer.development.js +0.3% +0.2% 449.4 KB 450.55 KB 96.91 KB 97.13 KB NODE_DEV
react-test-renderer.production.min.js 0.0% -0.0% 57.02 KB 57.02 KB 17.46 KB 17.46 KB NODE_PROD
ReactTestRenderer-dev.js +0.3% +0.2% 458 KB 459.14 KB 96.29 KB 96.51 KB FB_WWW_DEV
react-test-renderer-shallow.development.js 0.0% -0.0% 25.67 KB 25.67 KB 6.95 KB 6.95 KB UMD_DEV
react-test-renderer-shallow.production.min.js 0.0% -0.0% 7.31 KB 7.31 KB 2.38 KB 2.38 KB UMD_PROD
react-test-renderer-shallow.development.js 0.0% -0.0% 19.97 KB 19.97 KB 5.51 KB 5.51 KB NODE_DEV
react-test-renderer-shallow.production.min.js 0.0% -0.0% 7.96 KB 7.96 KB 2.64 KB 2.64 KB NODE_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler.development.js +0.3% +0.2% 439.05 KB 440.21 KB 93.88 KB 94.11 KB NODE_DEV
react-reconciler-persistent.development.js +0.3% +0.2% 437.43 KB 438.59 KB 93.23 KB 93.46 KB NODE_DEV
react-reconciler-persistent.production.min.js 0.0% -0.0% 57.12 KB 57.12 KB 17.08 KB 17.08 KB NODE_PROD
react-reconciler-reflection.development.js 0.0% -0.0% 15.4 KB 15.4 KB 4.84 KB 4.83 KB NODE_DEV
react-reconciler-reflection.production.min.js 0.0% -0.2% 2.56 KB 2.56 KB 1.13 KB 1.13 KB NODE_PROD

Generated by 🚫 dangerJS

Copy link
Collaborator

@sebmarkbage sebmarkbage left a comment

Choose a reason for hiding this comment

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

nits

@acdlite acdlite force-pushed the never-memoize-if-no-deps branch from 04cb310 to 3ef1280 Compare January 17, 2019 01:40
When no dependencies are passed to an effect hook, what we used to do is
compare the effect constructor. If there was no change, then we would
skip firing the effect. In practice, this is a useless optimization
because the constructor will always be different when you pass an inline
closure. And if you don't pass an inline closure, then you can't access
any props or state.

There are some edge cases where an effect that doesn't close over props
or state could be useful, like reference counting the number of mounted
components. But those are rare and can be addressed by passing an empty
array of dependencies.

By removing this "optimization," we can avoid retaining the constructor
in the majority of cases where it's a closure that changes on
every render.

I made corresponding changes to the other hooks that accept
dependencies, too (useMemo, useCallback, and useImperativeHandle).
It now includes the name of the hook in the message.
@acdlite acdlite force-pushed the never-memoize-if-no-deps branch from 3ef1280 to c7d8ebc Compare January 17, 2019 01:53
@acdlite acdlite merged commit 1454a8b into facebook:master Jan 17, 2019
gaearon added a commit to gaearon/react that referenced this pull request Jan 18, 2019
gaearon added a commit that referenced this pull request Jan 18, 2019
* Small tweaks to SSR to match #14594

* Remove unnecessary comparison
@@ -57,6 +60,48 @@ function resolveCurrentlyRenderingComponent(): Object {
return currentlyRenderingComponent;
}

function areHookInputsEqual(
Copy link
Contributor

Choose a reason for hiding this comment

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

should this have been areHookDepsEqual?

jetoneza pushed a commit to jetoneza/react that referenced this pull request Jan 23, 2019
…ok#14594)

* Don't bother comparing constructor when deps are not provided

When no dependencies are passed to an effect hook, what we used to do is
compare the effect constructor. If there was no change, then we would
skip firing the effect. In practice, this is a useless optimization
because the constructor will always be different when you pass an inline
closure. And if you don't pass an inline closure, then you can't access
any props or state.

There are some edge cases where an effect that doesn't close over props
or state could be useful, like reference counting the number of mounted
components. But those are rare and can be addressed by passing an empty
array of dependencies.

By removing this "optimization," we can avoid retaining the constructor
in the majority of cases where it's a closure that changes on
every render.

I made corresponding changes to the other hooks that accept
dependencies, too (useMemo, useCallback, and useImperativeHandle).

* Improve hook dependencies warning

It now includes the name of the hook in the message.

* Nits
jetoneza pushed a commit to jetoneza/react that referenced this pull request Jan 23, 2019
* Small tweaks to SSR to match facebook#14594

* Remove unnecessary comparison
);
}
}
for (let i = 0; i < prevDeps.length && i < nextDeps.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

according to the previous check - those lengths should be the same, so shouldnt this iteration just assume that fact?

n8schloss pushed a commit to n8schloss/react that referenced this pull request Jan 31, 2019
…ok#14594)

* Don't bother comparing constructor when deps are not provided

When no dependencies are passed to an effect hook, what we used to do is
compare the effect constructor. If there was no change, then we would
skip firing the effect. In practice, this is a useless optimization
because the constructor will always be different when you pass an inline
closure. And if you don't pass an inline closure, then you can't access
any props or state.

There are some edge cases where an effect that doesn't close over props
or state could be useful, like reference counting the number of mounted
components. But those are rare and can be addressed by passing an empty
array of dependencies.

By removing this "optimization," we can avoid retaining the constructor
in the majority of cases where it's a closure that changes on
every render.

I made corresponding changes to the other hooks that accept
dependencies, too (useMemo, useCallback, and useImperativeHandle).

* Improve hook dependencies warning

It now includes the name of the hook in the message.

* Nits
n8schloss pushed a commit to n8schloss/react that referenced this pull request Jan 31, 2019
* Small tweaks to SSR to match facebook#14594

* Remove unnecessary comparison
This was referenced Sep 20, 2019
NMinhNguyen referenced this pull request in enzymejs/react-shallow-renderer Jan 29, 2020
* Small tweaks to SSR to match #14594

* Remove unnecessary comparison
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants