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

Kill unnecessary re-renders in ElementWrapper #31734

Merged
merged 4 commits into from
Feb 25, 2019

Conversation

clintandrewhall
Copy link
Contributor

@clintandrewhall clintandrewhall commented Feb 21, 2019

Summary (fixes #29700)

This is a small change that creates a huge impact on UI performance in Canvas.

As the mouse moves over a workpad, several parts of the state of the workpad, (the element hovered, the element moved if one is being held, etc) change. These changes in state were causing every active Element to re-render in React.

In short, you may hover Element 10, but Elements 1-9 re-render because the state of the entire workpad changes. This would happen even if the mouse were moved a single pixel.

By short-circuiting that re-render with a fast comparison, we prevent any Elements that are not affected from re-rendering... freeing up the main thread. This performance impact intensifies as one adds Elements to the workpad.

Here is a performance shapshot before this change was made. Notice the React tree reconciliation encompasses the entire 280ms. This reflects a single state change flowing through all of the Elements on the workpad, (each orange "stalactite" descending downwards is an Element):

screen shot 2019-02-21 at 11 49 48 am

Here is a snapshot after. That reconciliation now completes in 50ms or less:

screen shot 2019-02-21 at 11 49 30 am

This is not the end of this problem. There are many other opportunities to prevent re-rendering, and we need to focus on killing the need for the comparison in the first place. @monfera and @w33ble will be instrumental in making these changes in the future... for now, though, this gets the job done.

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers

@clintandrewhall clintandrewhall added v7.0.0 performance Team:Presentation Presentation Team for Dashboard, Input Controls, and Canvas v6.7.0 labels Feb 21, 2019
@clintandrewhall clintandrewhall requested a review from a team as a code owner February 21, 2019 20:00
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-canvas

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Copy link
Contributor

@monfera monfera left a comment

Choose a reason for hiding this comment

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

LGTM, great change!

There's some minor yarn.lock conflicts due to earlier merges.

Apparently unrelated to this PR or your preceding PR (EventEmitters leak), but I saw this just now, after doing some crazyman drag&drop testing (15-20 shapes and one uploaded image being dragged and dropped prolifically): <= @w33ble just told me this warning has been around for a while when an image is present, so pls. ignore this from the angle of this PR

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
    in withState(withState(lifecycle(Connect(branch(ArgFormComponent))))) (created by withState(withState(withState(lifecycle(Connect(branch(ArgFormComponent)))))))
    in withState(withState(withState(lifecycle(Connect(branch(ArgFormComponent)))))) (created by FunctionFormComponent)
    in div (created by EuiPanel)
    in EuiPanel (created by SidebarSection)
    in SidebarSection (created by FunctionFormComponent)
    in div (created by FunctionFormComponent)
    in FunctionFormComponent (created by branch(FunctionFormComponent))
    in branch(FunctionFormComponent) (created by branch(branch(FunctionFormComponent)))
    in branch(branch(FunctionFormComponent)) (created by branch(branch(branch(FunctionFormComponent))))
    in branch(branch(branch(FunctionFormComponent))) (created by Connect(branch(branch(branch(FunctionFormComponent)))))
    in Connect(branch(branch(branch(FunctionFormComponent)))) (created by FunctionFormList)

@w33ble
Copy link
Contributor

w33ble commented Feb 22, 2019

FYI, when I try to merge master and bootstrap to fix the yarn conflict, I get a build error on some types in the i18n package. No idea what's up with that, but the conflict might be tricky to fix because of it...

@monfera
Copy link
Contributor

monfera commented Feb 22, 2019

Wondering how it can happen as this PR seems to have no i18n aspect on a superficial look. Maybe rebasing it on current master would fix whatever glitch is preventing it. Btw. I saw it in full green (no merge conflict, and CI passed), no idea if me merging Clint's other PR caused the merge conflict or build issue, or something unrelated that went in during that approx. one hour (other folks merged to master too).

@w33ble
Copy link
Contributor

w33ble commented Feb 22, 2019

Can confirm that the re-renders on mouse move is no longer an issue. There's still seemingly still a lot of re-renders happening when you move things around, but this is an awesome start!

feb-22-2019 16-17-24

Copy link
Contributor

@w33ble w33ble left a comment

Choose a reason for hiding this comment

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

One question, but this looks good. The app seems to load a big faster, and interactions seem smoother too. I'm not sure how much of that is placebo, but I'll take it 😉.

I was hoping that this might fix #25070, but no dice, it's still really laggy. All the same, this is a win.

   __    ___  _____           _ 
  / /   / _ \/__   \/\/\     / \
 / /   / /_\/  / /\/    \   /  /
/ /___/ /_\\  / / / /\/\ \ /\_/ 
\____/\____/  \/  \/    \/ \/   
                                

@@ -130,6 +130,7 @@
"ts-loader": "^5.2.2",
"typescript": "^3.0.3",
"vinyl-fs": "^3.0.2",
"why-did-you-update": "^1.0.6",
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this in the package? You're not using it anywhere, and it seems easy enough to only pull it in locally when you want to diagnose re-renders.

@monfera
Copy link
Contributor

monfera commented Feb 23, 2019

@clintandrewhall @w33ble since it's a personal feature branch, I can't push a commit to it (I contemplated merging it haha), but here's what I did:

  1. Remove why-did-you-update from package.json (though it's a devDependency and seems useful enough to have it around)
  2. Restore yarn.lock to what it was before - the yarn.lock diff in this PR has all kinds of version number differences
  3. Rebase on current master - might not be necessary
  4. Run yarn kbn bootstrap, it completes nicely and yarn.lock is good

@monfera
Copy link
Contributor

monfera commented Feb 23, 2019

... looks like we have this perf enhancement opportunity in 24 other components that use connect in Canvas and maybe more that don't - as you said, the real solution is flattening out state and props to more closely tie viewModels to views and I guess to avoid somewhat expensive equality checks

@w33ble
Copy link
Contributor

w33ble commented Feb 25, 2019

Most of the connect use probably isn't an issue, but where it is, or where the selector is the issue (as in this PR), I agree, there's big opportunity for performance gains.

@clintandrewhall
Copy link
Contributor Author

I'm already investigating more improvements... I'll keep shipping them as I find them! See #31779.

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

clintandrewhall added a commit to clintandrewhall/kibana that referenced this pull request Feb 25, 2019
## Summary (fixes elastic#29700)

This is a small change that creates a huge impact on UI performance in Canvas.

As the mouse moves over a workpad, several parts of the state of the workpad, (the element hovered, the element moved if one is being held, etc) change.  These changes in state were causing every active Element to re-render in React.

In short, you may hover Element 10, but Elements 1-9 re-render because the state of the entire workpad changes.  This would happen even if the mouse were moved a single pixel.

By short-circuiting that re-render with a fast comparison, we prevent any Elements that are not affected from re-rendering... freeing up the main thread.  This performance impact intensifies as one adds Elements to the workpad.

Here is a performance shapshot before this change was made.  Notice the React tree reconciliation encompasses the entire 280ms.  This reflects a single state change flowing through all of the Elements on the workpad, (each orange "stalactite" descending downwards is an Element):

![screen shot 2019-02-21 at 11 49 48 am](https://user-images.githubusercontent.com/297604/53197762-76960380-35e0-11e9-9069-a30df5fd4b2f.png)

Here is a snapshot after.  That reconciliation now completes in 50ms or less:

![screen shot 2019-02-21 at 11 49 30 am](https://user-images.githubusercontent.com/297604/53197845-afce7380-35e0-11e9-8991-572462b441eb.png)

*This is not the end of this problem.*  There are many other opportunities to prevent re-rendering, and we need to focus on killing the need for the comparison in the first place.  @monfera and @w33ble will be instrumental in making these changes in the future... for now, though, this gets the job done.

### Checklist

Use ~~strikethroughs~~ to remove checklist items you don't feel are applicable to this PR.

- [x] This was checked for cross-browser compatibility, [including a check against IE11](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility)
- [ ] ~~Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/master/packages/kbn-i18n/README.md)~~
- [ ] ~~[Documentation](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#writing-documentation) was added for features that require explanation or tutorials~~
- [ ] ~~[Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios~~
- [ ] ~~This was checked for [keyboard-only and screenreader accessibility](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Accessibility#Accessibility_testing_checklist)~~

### For maintainers

- [x] This was checked for breaking API changes and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)
- [ ] ~~This includes a feature addition or change that requires a release note and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)~~
clintandrewhall added a commit to clintandrewhall/kibana that referenced this pull request Feb 25, 2019
## Summary (fixes elastic#29700)

This is a small change that creates a huge impact on UI performance in Canvas.

As the mouse moves over a workpad, several parts of the state of the workpad, (the element hovered, the element moved if one is being held, etc) change.  These changes in state were causing every active Element to re-render in React.

In short, you may hover Element 10, but Elements 1-9 re-render because the state of the entire workpad changes.  This would happen even if the mouse were moved a single pixel.

By short-circuiting that re-render with a fast comparison, we prevent any Elements that are not affected from re-rendering... freeing up the main thread.  This performance impact intensifies as one adds Elements to the workpad.

Here is a performance shapshot before this change was made.  Notice the React tree reconciliation encompasses the entire 280ms.  This reflects a single state change flowing through all of the Elements on the workpad, (each orange "stalactite" descending downwards is an Element):

![screen shot 2019-02-21 at 11 49 48 am](https://user-images.githubusercontent.com/297604/53197762-76960380-35e0-11e9-9069-a30df5fd4b2f.png)

Here is a snapshot after.  That reconciliation now completes in 50ms or less:

![screen shot 2019-02-21 at 11 49 30 am](https://user-images.githubusercontent.com/297604/53197845-afce7380-35e0-11e9-8991-572462b441eb.png)

*This is not the end of this problem.*  There are many other opportunities to prevent re-rendering, and we need to focus on killing the need for the comparison in the first place.  @monfera and @w33ble will be instrumental in making these changes in the future... for now, though, this gets the job done.

### Checklist

Use ~~strikethroughs~~ to remove checklist items you don't feel are applicable to this PR.

- [x] This was checked for cross-browser compatibility, [including a check against IE11](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility)
- [ ] ~~Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/master/packages/kbn-i18n/README.md)~~
- [ ] ~~[Documentation](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#writing-documentation) was added for features that require explanation or tutorials~~
- [ ] ~~[Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios~~
- [ ] ~~This was checked for [keyboard-only and screenreader accessibility](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Accessibility#Accessibility_testing_checklist)~~

### For maintainers

- [x] This was checked for breaking API changes and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)
- [ ] ~~This includes a feature addition or change that requires a release note and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)~~
clintandrewhall added a commit to clintandrewhall/kibana that referenced this pull request Feb 25, 2019
## Summary (fixes elastic#29700)

This is a small change that creates a huge impact on UI performance in Canvas.

As the mouse moves over a workpad, several parts of the state of the workpad, (the element hovered, the element moved if one is being held, etc) change.  These changes in state were causing every active Element to re-render in React.

In short, you may hover Element 10, but Elements 1-9 re-render because the state of the entire workpad changes.  This would happen even if the mouse were moved a single pixel.

By short-circuiting that re-render with a fast comparison, we prevent any Elements that are not affected from re-rendering... freeing up the main thread.  This performance impact intensifies as one adds Elements to the workpad.

Here is a performance shapshot before this change was made.  Notice the React tree reconciliation encompasses the entire 280ms.  This reflects a single state change flowing through all of the Elements on the workpad, (each orange "stalactite" descending downwards is an Element):

![screen shot 2019-02-21 at 11 49 48 am](https://user-images.githubusercontent.com/297604/53197762-76960380-35e0-11e9-9069-a30df5fd4b2f.png)

Here is a snapshot after.  That reconciliation now completes in 50ms or less:

![screen shot 2019-02-21 at 11 49 30 am](https://user-images.githubusercontent.com/297604/53197845-afce7380-35e0-11e9-8991-572462b441eb.png)

*This is not the end of this problem.*  There are many other opportunities to prevent re-rendering, and we need to focus on killing the need for the comparison in the first place.  @monfera and @w33ble will be instrumental in making these changes in the future... for now, though, this gets the job done.

### Checklist

Use ~~strikethroughs~~ to remove checklist items you don't feel are applicable to this PR.

- [x] This was checked for cross-browser compatibility, [including a check against IE11](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility)
- [ ] ~~Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/master/packages/kbn-i18n/README.md)~~
- [ ] ~~[Documentation](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#writing-documentation) was added for features that require explanation or tutorials~~
- [ ] ~~[Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios~~
- [ ] ~~This was checked for [keyboard-only and screenreader accessibility](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Accessibility#Accessibility_testing_checklist)~~

### For maintainers

- [x] This was checked for breaking API changes and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)
- [ ] ~~This includes a feature addition or change that requires a release note and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)~~
clintandrewhall added a commit that referenced this pull request Feb 26, 2019
## Summary (fixes #29700)

This is a small change that creates a huge impact on UI performance in Canvas.

As the mouse moves over a workpad, several parts of the state of the workpad, (the element hovered, the element moved if one is being held, etc) change.  These changes in state were causing every active Element to re-render in React.

In short, you may hover Element 10, but Elements 1-9 re-render because the state of the entire workpad changes.  This would happen even if the mouse were moved a single pixel.

By short-circuiting that re-render with a fast comparison, we prevent any Elements that are not affected from re-rendering... freeing up the main thread.  This performance impact intensifies as one adds Elements to the workpad.

Here is a performance shapshot before this change was made.  Notice the React tree reconciliation encompasses the entire 280ms.  This reflects a single state change flowing through all of the Elements on the workpad, (each orange "stalactite" descending downwards is an Element):

![screen shot 2019-02-21 at 11 49 48 am](https://user-images.githubusercontent.com/297604/53197762-76960380-35e0-11e9-9069-a30df5fd4b2f.png)

Here is a snapshot after.  That reconciliation now completes in 50ms or less:

![screen shot 2019-02-21 at 11 49 30 am](https://user-images.githubusercontent.com/297604/53197845-afce7380-35e0-11e9-8991-572462b441eb.png)

*This is not the end of this problem.*  There are many other opportunities to prevent re-rendering, and we need to focus on killing the need for the comparison in the first place.  @monfera and @w33ble will be instrumental in making these changes in the future... for now, though, this gets the job done.

### Checklist

Use ~~strikethroughs~~ to remove checklist items you don't feel are applicable to this PR.

- [x] This was checked for cross-browser compatibility, [including a check against IE11](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility)
- [ ] ~~Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/master/packages/kbn-i18n/README.md)~~
- [ ] ~~[Documentation](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#writing-documentation) was added for features that require explanation or tutorials~~
- [ ] ~~[Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios~~
- [ ] ~~This was checked for [keyboard-only and screenreader accessibility](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Accessibility#Accessibility_testing_checklist)~~

### For maintainers

- [x] This was checked for breaking API changes and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)
- [ ] ~~This includes a feature addition or change that requires a release note and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)~~
clintandrewhall added a commit that referenced this pull request Feb 26, 2019
## Summary (fixes #29700)

This is a small change that creates a huge impact on UI performance in Canvas.

As the mouse moves over a workpad, several parts of the state of the workpad, (the element hovered, the element moved if one is being held, etc) change.  These changes in state were causing every active Element to re-render in React.

In short, you may hover Element 10, but Elements 1-9 re-render because the state of the entire workpad changes.  This would happen even if the mouse were moved a single pixel.

By short-circuiting that re-render with a fast comparison, we prevent any Elements that are not affected from re-rendering... freeing up the main thread.  This performance impact intensifies as one adds Elements to the workpad.

Here is a performance shapshot before this change was made.  Notice the React tree reconciliation encompasses the entire 280ms.  This reflects a single state change flowing through all of the Elements on the workpad, (each orange "stalactite" descending downwards is an Element):

![screen shot 2019-02-21 at 11 49 48 am](https://user-images.githubusercontent.com/297604/53197762-76960380-35e0-11e9-9069-a30df5fd4b2f.png)

Here is a snapshot after.  That reconciliation now completes in 50ms or less:

![screen shot 2019-02-21 at 11 49 30 am](https://user-images.githubusercontent.com/297604/53197845-afce7380-35e0-11e9-8991-572462b441eb.png)

*This is not the end of this problem.*  There are many other opportunities to prevent re-rendering, and we need to focus on killing the need for the comparison in the first place.  @monfera and @w33ble will be instrumental in making these changes in the future... for now, though, this gets the job done.

### Checklist

Use ~~strikethroughs~~ to remove checklist items you don't feel are applicable to this PR.

- [x] This was checked for cross-browser compatibility, [including a check against IE11](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility)
- [ ] ~~Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/master/packages/kbn-i18n/README.md)~~
- [ ] ~~[Documentation](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#writing-documentation) was added for features that require explanation or tutorials~~
- [ ] ~~[Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios~~
- [ ] ~~This was checked for [keyboard-only and screenreader accessibility](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Accessibility#Accessibility_testing_checklist)~~

### For maintainers

- [x] This was checked for breaking API changes and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)
- [ ] ~~This includes a feature addition or change that requires a release note and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)~~
clintandrewhall added a commit that referenced this pull request Feb 26, 2019
Backports the following commits to 6.7:
 - Kill unnecessary re-renders in ElementWrapper  (#31734)
@clintandrewhall clintandrewhall deleted the element-wrapper-perf branch June 6, 2019 04:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Team:Presentation Presentation Team for Dashboard, Input Controls, and Canvas v6.7.0 v7.0.0 v7.2.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Canvas] Some element types rerender unnecessarily
4 participants