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

Create Synthetic Events Lazily #19909

Merged
merged 1 commit into from
Sep 25, 2020
Merged

Create Synthetic Events Lazily #19909

merged 1 commit into from
Sep 25, 2020

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Sep 25, 2020

Currently we create them very aggressively — especially with the eager change — but many may not end up being used. While the allocation itself is not a significant cost, we are actually running a bunch of code there, including iterating over the event interface, copying every known property into this, and running our normalization polyfills. In some cases, our polyfills also do some other extra work (e.g. some calculations).

However, this is just an artifact of the initial code structure. We don't actually need to create the synthetic events early except the enter/leave case where it's much more convenient. In other cases, we can make them lazy and create if we actually found some listeners. This also lets us delay any other extra work.

I should note that with a throttled CPU, the SyntheticEvent constructor takes a noticeable amount of time for noops, which is why I want to optimize this.

@gaearon gaearon requested a review from trueadm September 25, 2020 10:59
@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Sep 25, 2020
): void {
const bubbleName = event._reactName;
const captureName = bubbleName !== null ? bubbleName + 'Capture' : null;
reactName: string,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is not nullable now so we don't need extra checks below.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 25, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit e3dde61:

Sandbox Source
React Configuration

const listeners: Array<DispatchListener> = [];

const eventListeners = getEventHandlerListeners(currentTarget);
if (eventListeners !== null) {
const targetType = ((event.type: any): DOMEventName);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure if the discrepancy (the code path above reads event.nativeEvent.type instead) was intentional but for now I'm keeping it.

@sizebot
Copy link

sizebot commented Sep 25, 2020

Details of bundled changes.

Comparing: 0a00804...e3dde61

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js 0.0% 0.0% 909.41 KB 909.56 KB 206.66 KB 206.72 KB NODE_DEV
ReactDOMForked-prod.js 🔺+0.1% 🔺+0.2% 374.15 KB 374.55 KB 69.42 KB 69.54 KB FB_WWW_PROD
react-dom-server.node.development.js 0.0% -0.0% 138.57 KB 138.57 KB 36.59 KB 36.58 KB NODE_DEV
react-dom.production.min.js 🔺+0.1% 🔺+0.1% 122.31 KB 122.4 KB 39.38 KB 39.41 KB NODE_PROD
ReactDOMForked-profiling.js +0.1% +0.2% 388.13 KB 388.53 KB 71.88 KB 72 KB FB_WWW_PROFILING
react-dom-server.browser.development.js 0.0% -0.0% 144.73 KB 144.73 KB 36.77 KB 36.76 KB UMD_DEV
react-dom-test-utils.production.min.js 0.0% -0.0% 13.71 KB 13.71 KB 5.32 KB 5.32 KB UMD_PROD
ReactDOMTesting-dev.js +0.1% +0.1% 911.74 KB 912.36 KB 205.35 KB 205.47 KB FB_WWW_DEV
react-dom-test-utils.development.js 0.0% -0.0% 66.29 KB 66.29 KB 18.84 KB 18.84 KB NODE_DEV
ReactDOMTesting-prod.js 🔺+0.1% 🔺+0.2% 371.34 KB 371.74 KB 70.12 KB 70.23 KB FB_WWW_PROD
react-dom-unstable-fizz.node.development.js 0.0% +0.1% 5.52 KB 5.52 KB 1.84 KB 1.84 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% -0.0% 13.7 KB 13.7 KB 5.27 KB 5.26 KB NODE_PROD
react-dom-unstable-fizz.node.production.min.js 0.0% -0.3% 1.17 KB 1.17 KB 666 B 664 B NODE_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.3% 1.22 KB 1.22 KB 712 B 710 B UMD_PROD
react-dom.development.js 0.0% 0.0% 955.58 KB 955.77 KB 209.26 KB 209.31 KB UMD_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.5% 1.01 KB 1.01 KB 617 B 614 B NODE_PROD
react-dom.production.min.js 🔺+0.1% 🔺+0.1% 122.13 KB 122.23 KB 40.12 KB 40.15 KB UMD_PROD
react-dom.profiling.min.js +0.1% 0.0% 127.39 KB 127.48 KB 41.78 KB 41.79 KB UMD_PROFILING
ReactDOMForked-dev.js +0.1% +0.1% 968.82 KB 969.43 KB 215.32 KB 215.46 KB FB_WWW_DEV
react-dom.profiling.min.js +0.1% 0.0% 127.75 KB 127.84 KB 41.07 KB 41.08 KB NODE_PROFILING
react-dom-server.browser.production.min.js 0.0% -0.0% 20.34 KB 20.34 KB 7.55 KB 7.55 KB UMD_PROD
ReactDOM-dev.js +0.1% +0.1% 960.97 KB 961.58 KB 214.73 KB 214.86 KB FB_WWW_DEV
ReactDOM-prod.js 🔺+0.1% 🔺+0.1% 373.93 KB 374.33 KB 69.3 KB 69.41 KB FB_WWW_PROD
react-dom-server.browser.development.js 0.0% -0.0% 137.3 KB 137.3 KB 36.33 KB 36.33 KB NODE_DEV
ReactDOM-profiling.js +0.1% +0.1% 387.09 KB 387.49 KB 71.76 KB 71.85 KB FB_WWW_PROFILING
react-dom-server.browser.production.min.js 0.0% -0.0% 20.24 KB 20.24 KB 7.5 KB 7.5 KB NODE_PROD
ReactDOMServer-dev.js 0.0% -0.0% 141.58 KB 141.58 KB 36.3 KB 36.3 KB FB_WWW_DEV
ReactDOMServer-prod.js 0.0% -0.0% 46.44 KB 46.44 KB 10.83 KB 10.83 KB FB_WWW_PROD
react-dom-test-utils.development.js 0.0% -0.0% 71.48 KB 71.48 KB 19.36 KB 19.36 KB UMD_DEV

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

Size changes (experimental)

Generated by 🚫 dangerJS against e3dde61

@sizebot
Copy link

sizebot commented Sep 25, 2020

Details of bundled changes.

Comparing: 0a00804...e3dde61

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js 0.0% 0.0% 873.96 KB 874.11 KB 200.15 KB 200.21 KB NODE_DEV
ReactDOMForked-prod.js 🔺+0.1% 🔺+0.1% 385.48 KB 385.88 KB 71.23 KB 71.32 KB FB_WWW_PROD
react-dom-server.node.development.js 0.0% -0.0% 137.06 KB 137.06 KB 36.37 KB 36.37 KB NODE_DEV
react-dom.production.min.js 🔺+0.1% -0.1% 117.75 KB 117.84 KB 38.07 KB 38.04 KB NODE_PROD
ReactDOMForked-profiling.js +0.1% +0.1% 399.51 KB 399.91 KB 73.63 KB 73.71 KB FB_WWW_PROFILING
react-dom-server.browser.development.js 0.0% -0.0% 143.14 KB 143.14 KB 36.57 KB 36.57 KB UMD_DEV
react-dom-server.node.production.min.js 0.0% -0.0% 20.2 KB 20.2 KB 7.58 KB 7.58 KB NODE_PROD
react-dom-test-utils.production.min.js 0.0% -0.0% 13.7 KB 13.7 KB 5.32 KB 5.31 KB UMD_PROD
ReactDOMTesting-dev.js +0.1% +0.1% 940.08 KB 940.69 KB 210.83 KB 210.96 KB FB_WWW_DEV
react-dom-test-utils.development.js 0.0% -0.0% 66.28 KB 66.28 KB 18.84 KB 18.83 KB NODE_DEV
ReactDOMTesting-prod.js 🔺+0.1% 🔺+0.1% 384.41 KB 384.81 KB 72.29 KB 72.39 KB FB_WWW_PROD
react-dom-test-utils.production.min.js 0.0% -0.0% 13.68 KB 13.68 KB 5.26 KB 5.26 KB NODE_PROD
react-dom.development.js 0.0% 0.0% 918.39 KB 918.57 KB 202.69 KB 202.75 KB UMD_DEV
react-dom.production.min.js 🔺+0.1% 0.0% 117.64 KB 117.74 KB 38.73 KB 38.74 KB UMD_PROD
react-dom.profiling.min.js +0.1% 0.0% 121.54 KB 121.63 KB 39.94 KB 39.96 KB UMD_PROFILING
ReactDOMForked-dev.js +0.1% +0.1% 994.4 KB 995.02 KB 220.06 KB 220.19 KB FB_WWW_DEV
react-dom.profiling.min.js +0.1% -0.1% 121.81 KB 121.91 KB 39.27 KB 39.22 KB NODE_PROFILING
react-dom-server.browser.production.min.js 0.0% -0.0% 19.88 KB 19.88 KB 7.46 KB 7.46 KB UMD_PROD
ReactDOM-dev.js +0.1% +0.1% 986.55 KB 987.16 KB 219.41 KB 219.54 KB FB_WWW_DEV
ReactDOM-prod.js 🔺+0.1% 🔺+0.1% 385.19 KB 385.59 KB 70.99 KB 71.07 KB FB_WWW_PROD
react-dom-server.browser.development.js 0.0% -0.0% 135.79 KB 135.79 KB 36.12 KB 36.12 KB NODE_DEV
ReactDOM-profiling.js +0.1% +0.1% 398.39 KB 398.79 KB 73.42 KB 73.5 KB FB_WWW_PROFILING
react-dom-server.browser.production.min.js 0.0% -0.0% 19.78 KB 19.78 KB 7.42 KB 7.42 KB NODE_PROD
ReactDOMServer-dev.js 0.0% -0.0% 145.61 KB 145.61 KB 37.31 KB 37.31 KB FB_WWW_DEV
ReactDOMServer-prod.js 0.0% 0.0% 47.3 KB 47.3 KB 11.04 KB 11.04 KB FB_WWW_PROD
react-dom-test-utils.development.js 0.0% -0.0% 71.47 KB 71.47 KB 19.35 KB 19.35 KB UMD_DEV

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

Size changes (stable)

Generated by 🚫 dangerJS against e3dde61

Copy link
Contributor

@trueadm trueadm left a comment

Choose a reason for hiding this comment

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

Looks good to me :)

@gaearon gaearon merged commit 480626a into facebook:master Sep 25, 2020
@gaearon gaearon deleted the lazy-synth branch September 25, 2020 12:33
koto pushed a commit to koto/react that referenced this pull request Jun 15, 2021
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.

4 participants