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

[Lens] Fully unmount React when flyout closes #95359

Merged
merged 5 commits into from
Mar 29, 2021

Conversation

wylieconlon
Copy link
Contributor

@wylieconlon wylieconlon commented Mar 24, 2021

Steps to reproduce:

  1. Configure a dimension
  2. Open the flyout
  3. Open the React devtools and look for one of the I18nProvider elements at the bottom of the inspector, which is rendered as a separate React root.
  4. Close the flyout
  5. Hover over each I18nProvider in the dev tools and verify that there is a matching and visible DOM element.

Screen Shot 2021-03-24 at 4 16 02 PM

There is a performance penalty associated with this change. The way I compared the performance before and after was by comparing the time to re-open the flyout, and found that it was 0.3ms to render when the element is kept in the virtual DOM, vs 15.9ms when the element is rendered from the first time. Steps to check:

  1. Follow all the steps above
  2. Switch to the React profiler tab in dev tools
  3. Start recording
  4. Click the dimension to open the flyout
  5. Close the flyout
  6. Stop recording

Then I tabbed through each commit cycle in React, and found React executes 9 cycles with this PR, vs 2 cycles before. It also takes much longer. I believe this is a necessary slowdown because keeping the flyout element in virtual DOM causes more problems and is a potential memory leak.

Checklist

@wylieconlon wylieconlon added Team:Visualizations Visualization editors, elastic-charts and infrastructure v8.0.0 release_note:skip Skip the PR/issue when compiling release notes Feature:Lens v7.13.0 auto-backport Deprecated - use backport:version if exact versions are needed labels Mar 24, 2021
@wylieconlon wylieconlon requested review from dej611, flash1293, mbondyra and a team March 24, 2021 20:19
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-app (Team:KibanaApp)

@flash1293
Copy link
Contributor

The failure looks real - maybe there's a leftover render function which is not containing an unmount callback?

22:18:18                 │ERROR browser[SEVERE] http://localhost:61131/41502/bundles/kbn-ui-shared-deps/kbn-ui-shared-deps.js 381:77071 TypeError: cleanupRef.current is not a function
22:18:18                 │          at http://localhost:61131/41502/bundles/plugin/lens/lens.chunk.0.js:8:258355

@wylieconlon
Copy link
Contributor Author

@flash1293 yes, it was a real failure. The editor frame uses an async function to do its rendering, and I am now correctly handling this case.

if (cleanupRef.current && typeof cleanupRef.current === 'function') {
cleanupRef.current(elementRef.current);
}
unmountComponentAtNode(elementRef.current);
Copy link
Contributor

Choose a reason for hiding this comment

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

This PR adds a way to return a cleanup function (even an async one) from the render function, but it seems like this is never used, right?

The

unmountComponentAtNode(elementRef.current);

line is basically a default implementation of the cleanup specific to react and it does all the actual cleanup here.

I guess my question is: Do we actually need the unmount callback logic? Or is it more of a future-facing thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the cleanupRef is only used in the new tests It's there to maintain the existing assumption that Lens is framework independent.

@mbondyra
Copy link
Contributor

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
lens 931.3KB 931.9KB +591.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Contributor

@flash1293 flash1293 left a comment

Choose a reason for hiding this comment

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

Tested and works as expected, thanks for explaining!

@wylieconlon wylieconlon merged commit fe66162 into elastic:master Mar 29, 2021
@wylieconlon wylieconlon deleted the lens/unmount-bug branch March 29, 2021 16:33
@kibanamachine
Copy link
Contributor

💚 Backport successful

7.x / #95677

This backport PR will be merged automatically after passing CI.

kibanamachine added a commit to kibanamachine/kibana that referenced this pull request Mar 29, 2021
* [Lens] Fully unmount React when flyout closes

* Fix bug with editor frame unmounting

* Fix type

Co-authored-by: Kibana Machine <[email protected]>
kibanamachine added a commit that referenced this pull request Mar 29, 2021
* [Lens] Fully unmount React when flyout closes

* Fix bug with editor frame unmounting

* Fix type

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Wylie Conlon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Deprecated - use backport:version if exact versions are needed Feature:Lens release_note:skip Skip the PR/issue when compiling release notes Team:Visualizations Visualization editors, elastic-charts and infrastructure v7.13.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants