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: replace PureComponent with shouldComponentUpdate #534

Conversation

patrykkopycinski
Copy link
Contributor

@patrykkopycinski patrykkopycinski commented Feb 3, 2020

Summary

During working on elastic/kibana#56587 I have found out that charts-related components are probably re-renders unnecessarily, so I have decided to verify that and here is the outcome from https://github.com/welldone-software/why-did-you-render

Each line in the console means unnecessary component's re-render and after the row is expanded we can see which props are equal by value but not by reference, so it causes PureComponent to re-render.
Untitled_ Feb 3, 2020 11_16 PM (1)

After my changes
Untitled_ Feb 3, 2020 11_13 PM

Checklist

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

  • Any consumer-facing exports were added to src/index.ts (and stories only import from ../src except for test data & storybook)
  • This was checked for cross-browser compatibility, including a check against IE11
  • Proper documentation or storybook story was added for features that require explanation or tutorials
  • Unit tests were updated or added to match the most common scenarios
  • Each commit follows the convention

@patrykkopycinski patrykkopycinski self-assigned this Feb 3, 2020
@patrykkopycinski patrykkopycinski changed the title Chore/replace purecomponent with shouldcomponentupdate perf : replace purecomponent with shouldcomponentupdate Feb 3, 2020
@patrykkopycinski patrykkopycinski changed the title perf : replace purecomponent with shouldcomponentupdate perf: replace purecomponent with shouldcomponentupdate Feb 3, 2020
@patrykkopycinski patrykkopycinski changed the title perf: replace purecomponent with shouldcomponentupdate perf: replace PureComponent with shouldComponentUpdate Feb 3, 2020
@codecov-io
Copy link

codecov-io commented Feb 3, 2020

Codecov Report

Merging #534 into master will increase coverage by 0.05%.
The diff coverage is 67.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #534      +/-   ##
==========================================
+ Coverage   75.57%   75.63%   +0.05%     
==========================================
  Files         181      181              
  Lines        5651     5684      +33     
  Branches     1102     1102              
==========================================
+ Hits         4271     4299      +28     
- Misses       1364     1369       +5     
  Partials       16       16
Impacted Files Coverage Δ
src/components/legend/legend_item.tsx 100% <100%> (ø) ⬆️
src/components/icons/icon.tsx 100% <100%> (ø) ⬆️
src/components/chart_container.tsx 56.45% <100%> (+0.71%) ⬆️
...types/xy_chart/renderer/canvas/rect_annotation.tsx 42.85% <50%> (+6.49%) ⬆️
src/chart_types/xy_chart/renderer/canvas/grid.tsx 48.64% <50%> (+1.58%) ⬆️
...hart_types/xy_chart/renderer/canvas/bar_values.tsx 42.5% <50%> (+1.95%) ⬆️
...types/xy_chart/renderer/canvas/line_annotation.tsx 35.29% <50%> (+6.72%) ⬆️
src/chart_types/xy_chart/renderer/canvas/axis.tsx 21.6% <57.14%> (+1.6%) ⬆️
..._types/xy_chart/renderer/canvas/bar_geometries.tsx 33.33% <60%> (+7.61%) ⬆️
src/chart_types/xy_chart/specs/line_annotation.tsx 47.36% <60%> (+1.65%) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 178afc9...e4fdb05. Read the comment docs.

@markov00
Copy link
Member

markov00 commented Feb 4, 2020

hi @patrykkopycinski thanks for this. I'm in the meantime of a refactoring that will move away from react for some of these components (the one used to render the chart).
I'd like to apply some of these fixes and some others I prefer to apply them on the redux-re-selectors chain, to avoid unnecessary recomputation that will avoid rerendering upstream.

@patrykkopycinski
Copy link
Contributor Author

Sounds great @markov00! Do you know maybe when you will be able to merge your refactor? Because our app is heavily utilizing charts, so it has a big footprint on the performance

@markov00
Copy link
Member

markov00 commented Feb 4, 2020

Sounds great @markov00! Do you know maybe when you will be able to merge your refactor? Because our app is heavily utilizing charts, so it has a big footprint on the performance

I will push the PR today, it needs a review and then it will be merged

Copy link
Member

@markov00 markov00 left a comment

Choose a reason for hiding this comment

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

@patrykkopycinski let's merge this. I will merge mine changes later.
Thanks for the contribution

@@ -39,6 +40,11 @@ export class AreaGeometries extends React.PureComponent<AreaGeometriesDataProps,
overPoint: undefined,
};
}

shouldComponentUpdate(nextProps: AreaGeometriesDataProps, nextState: AreaGeometriesDataState) {
return !deepEqual(this.props, nextProps) || !deepEqual(this.state, nextState);
Copy link
Member

Choose a reason for hiding this comment

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

you can avoid checking the equality of the state. This is not used anymore in this component and should be removed (there is no relevant use of the overPoint state object in the component)

@@ -31,6 +32,11 @@ export class BarGeometries extends React.PureComponent<BarGeometriesDataProps, B
overBar: undefined,
};
}

shouldComponentUpdate(nextProps: BarGeometriesDataProps, nextState: BarGeometriesDataState) {
return !deepEqual(this.props, nextProps) || !deepEqual(this.state, nextState);
Copy link
Member

Choose a reason for hiding this comment

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

same here, overBar is no more used (no one is setting it), you can remove the deepEqual for the state

@@ -37,6 +38,10 @@ export class LineGeometries extends React.PureComponent<LineGeometriesDataProps,
};
}

shouldComponentUpdate(nextProps: LineGeometriesDataProps, nextState: LineGeometriesDataState) {
return !deepEqual(this.props, nextProps) || !deepEqual(this.state, nextState);
Copy link
Member

Choose a reason for hiding this comment

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

same here

@robgil
Copy link

robgil commented Feb 5, 2020

Codecov Report

Merging #534 into master will increase coverage by 0.22%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #534      +/-   ##
==========================================
+ Coverage   75.57%   75.80%   +0.22%     
==========================================
  Files         181      193      +12     
  Lines        5651     5844     +193     
  Branches     1102     1116      +14     
==========================================
+ Hits         4271     4430     +159     
- Misses       1364     1397      +33     
- Partials       16       17       +1     
Impacted Files Coverage Δ
src/mocks/series/index.ts 100.00% <0.00%> (ø)
src/mocks/scale/scale.ts 87.50% <0.00%> (ø)
src/mocks/series/utils.ts 88.88% <0.00%> (ø)
src/mocks/utils.ts 100.00% <0.00%> (ø)
src/mocks/specs/specs.ts 77.27% <0.00%> (ø)
src/mocks/index.ts 100.00% <0.00%> (ø)
src/mocks/series/seriesIdentifiers.ts 93.33% <0.00%> (ø)
src/mocks/hierarchical/palettes.ts 100.00% <0.00%> (ø)
src/mocks/scale/index.ts 100.00% <0.00%> (ø)
src/mocks/specs/index.ts 100.00% <0.00%> (ø)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 178afc9...e4fdb05. Read the comment docs.

@patrykkopycinski patrykkopycinski changed the title perf: replace PureComponent with shouldComponentUpdate fix: replace PureComponent with shouldComponentUpdate Feb 5, 2020
@codecov-io
Copy link

codecov-io commented Feb 5, 2020

Codecov Report

Merging #534 into master will increase coverage by 0.22%.
The diff coverage is 69.09%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #534      +/-   ##
=========================================
+ Coverage   75.57%   75.8%   +0.22%     
=========================================
  Files         181     193      +12     
  Lines        5651    5844     +193     
  Branches     1102    1116      +14     
=========================================
+ Hits         4271    4430     +159     
- Misses       1364    1397      +33     
- Partials       16      17       +1
Impacted Files Coverage Δ
src/components/legend/legend_item.tsx 100% <100%> (ø) ⬆️
src/components/icons/icon.tsx 100% <100%> (ø) ⬆️
src/components/chart_container.tsx 56.45% <100%> (+0.71%) ⬆️
...types/xy_chart/renderer/canvas/rect_annotation.tsx 42.85% <50%> (+6.49%) ⬆️
src/chart_types/xy_chart/renderer/canvas/grid.tsx 48.64% <50%> (+1.58%) ⬆️
...hart_types/xy_chart/renderer/canvas/bar_values.tsx 42.5% <50%> (+1.95%) ⬆️
...types/xy_chart/renderer/canvas/line_annotation.tsx 35.29% <50%> (+6.72%) ⬆️
src/chart_types/xy_chart/renderer/canvas/axis.tsx 21.6% <57.14%> (+1.6%) ⬆️
src/chart_types/xy_chart/specs/line_annotation.tsx 47.36% <60%> (+1.65%) ⬆️
..._types/xy_chart/renderer/canvas/bar_geometries.tsx 28.94% <75%> (+3.23%) ⬆️
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 178afc9...e4fdb05. Read the comment docs.

@patrykkopycinski patrykkopycinski merged commit 5043725 into elastic:master Feb 5, 2020
@patrykkopycinski patrykkopycinski deleted the chore/replace-purecomponent-with-shouldcomponentupdate branch February 5, 2020 10:34
@patrykkopycinski
Copy link
Contributor Author

Thank you very much for help @markov00 💪

markov00 pushed a commit that referenced this pull request Feb 5, 2020
## [17.0.1](v17.0.0...v17.0.1) (2020-02-05)

### Bug Fixes

* replace PureComponent with shouldComponentUpdate ([#534](#534)) ([5043725](5043725))
@markov00
Copy link
Member

markov00 commented Feb 5, 2020

🎉 This PR is included in version 17.0.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@markov00 markov00 added the released Issue released publicly label Feb 5, 2020
nickofthyme pushed a commit to nickofthyme/elastic-charts that referenced this pull request Feb 5, 2020
This commit replaces React.PureComponent with React.Component with custom shouldComponentUpdate that deeply compares props.
Also, it fixes shouldComponentUpdate logic in ChartContainerComponent that caused the component to re-render every time the props have changed.
nickofthyme added a commit that referenced this pull request Feb 5, 2020
This commit replaces React.PureComponent with React.Component with custom shouldComponentUpdate that deeply compares props.
Also, it fixes shouldComponentUpdate logic in ChartContainerComponent that caused the component to re-render every time the props have changed.

Co-authored-by: patrykkopycinski <contact@patrykkopycinski.com>
markov00 pushed a commit that referenced this pull request Feb 5, 2020
## [16.1.1](v16.1.0...v16.1.1) (2020-02-05)

### Bug Fixes

* replace PureComponent with shouldComponentUpdate ([#534](#534)) ([#535](#535)) ([f05f8cb](f05f8cb))
AMoo-Miki pushed a commit to AMoo-Miki/OpenSearch-Dashboards that referenced this pull request Feb 10, 2022
## [17.0.1](elastic/elastic-charts@v17.0.0...v17.0.1) (2020-02-05)

### Bug Fixes

* replace PureComponent with shouldComponentUpdate ([opensearch-project#534](elastic/elastic-charts#534)) ([e06aeb2](elastic/elastic-charts@e06aeb2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Issue released publicly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants