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

[ML] Limit exposing shared static code through ml/public/index.ts. #77745

Merged
merged 26 commits into from
Sep 29, 2020

Conversation

walterra
Copy link
Contributor

@walterra walterra commented Sep 17, 2020

Summary

Fixes #77041

This PR is inspired by the fixes for the APM/Logs/Metrics to reduce the page load bundle size (see #71843, #67654).

The bundle size of the page load bundle gets reduced from 873.6KB to 40.2KB.

  • Clean up of lodash import methods, now all imports are done with just lodash instead of individual imports. Note this PR intentionally doesn't get rid of lodash imports we might be able to migrate away from to reduce code churn for this PR. lodash related tree shaking was fixed recently so it's fine again to import from the main lodash import (see Adding comment about importing lodash library #78156).
  • One problem we had is that we exported a lot of static code through plugins/ml/public. This increased the bundle size even if it wasn't needed at runtime. To avoid that, this PR exposes the async function getMlSharedImports(). This allows sharing public static code without making it part of the ml page load bundle. This is now used by the transforms plugin. I kept the exports getSeverityColor, getSeverityType, ANOMALY_SEVERITY as is to avoid having to refactor other consuming plugins. I changed a bit how this functions get exported though so the impact on bundle size is minimal. The platform team is exploring ways to improve export linting rules so hopefully all of this is just a temporary solution and at some point we'll be able to just do plain exports again.
  • Where applicable, files directly related to the page load bundle make use of import type to avoid importing runtime code.
  • <OverviewPage /> and <EmbeddableSwimLaneContainer /> use React.lazy() with React's Suspense to load code for an inner React tree on demand. I didn't do this for all routes since the other changes in this PR satisfy the reduction of the page load bundle, but it might be something we want to explore further in a follow up.
  • Code related to register-functions in public/plugin.ts now get loaded on demand to avoid making their code part of the page load bundle.

Checklist

@walterra walterra self-assigned this Sep 17, 2020
@walterra walterra added the release_note:skip Skip the PR/issue when compiling release notes label Sep 17, 2020
@walterra walterra force-pushed the ml-bundle branch 4 times, most recently from b72faee to 99a78f5 Compare September 23, 2020 12:08
@walterra walterra marked this pull request as ready for review September 28, 2020 10:10
@walterra walterra requested a review from a team as a code owner September 28, 2020 10:10
@elasticmachine
Copy link
Contributor

Pinging @elastic/ml-ui (:ml)

}
} else {
// if ml is disabled in elasticsearch, disable ML in kibana
this.appUpdater.next(() => ({
status: AppStatus.inaccessible,
}));
if (managementApp) {
managementApp.disable();
Copy link
Member

@jgowdyelastic jgowdyelastic Sep 28, 2020

Choose a reason for hiding this comment

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

is this no longer needed? or was it never actually needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ran into issues with a race condition with loading related code asynchronously for the management section when the section was first registered and only later a decision was made wether to enabled/disable it. I was able to solve it by flipping the checks around and both register and enable the ML part for management only inside the check where the decision is made to enable it here: https://github.com/elastic/kibana/pull/77745/files/98223ebfe878487f69933d3c7a142a0e42df46ef#diff-c9362f58636a69dc517555a6f0c7f28aR139

Copy link
Contributor

@peteharverson peteharverson 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 LGTM

Copy link
Member

@jgowdyelastic jgowdyelastic left a comment

Choose a reason for hiding this comment

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

LGTM

@walterra
Copy link
Contributor Author

@elasticmachine merge upstream

Copy link
Contributor

@darnautov darnautov 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 job 👍

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

@kbn/optimizer bundle module count

id value diff baseline
ml 1184 -237 1421
transform 335 -180 515
total -417

async chunks size

id value diff baseline
ml 10.5MB ⚠️ +1.6MB 8.9MB
transform 1.2MB -70.2KB 1.2MB
total ⚠️ +1.6MB

distributable file count

id value diff baseline
default 45843 +21 45822

page load bundle size

id value diff baseline
ml 40.3KB -694.5KB 734.8KB
transform 25.5KB +229.0B 25.3KB
total -694.3KB

History

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

@walterra walterra merged commit 61aac71 into elastic:master Sep 29, 2020
@walterra walterra deleted the ml-bundle branch September 29, 2020 09:57
@walterra
Copy link
Contributor Author

backport is waiting for #78667

phillipb added a commit to phillipb/kibana that referenced this pull request Sep 29, 2020
…-to-timeline

* 'master' of github.com:elastic/kibana: (22 commits)
  update apm index pattern (elastic#78732)
  78024: move transform out of dataset (elastic#78216)
  [QA][Code Coverage] Upload the coverage static site before ingestion (elastic#78695)
  [Discover] Make _source field not clickable (elastic#78698)
  [Fleet] Rename Ingest Manager => Fleet, Fleet => Agents in the UI (elastic#78685)
  [APM] Review feedback from distribution + transaction metrics (elastic#78752)
  [Ingest pipelines] Add ability to stop pipeline simulation  (elastic#78183)
  [CSM] Fix core vital legend background (elastic#78273)
  [Usage Collection] [schema] Support spreads + `canvas` definition (elastic#78481)
  fix lodash imports (elastic#78456)
  [Maps] Add layer type preview icons (elastic#78650)
  [APM] Use transaction metrics for distribution charts (elastic#78484)
  [Uptime] Ml anomaly alert edit (elastic#76909)
  [ML] Limit exposing shared static code through ml/public/index.ts. (elastic#77745)
  making expression debug info serializable (elastic#78727)
  fix lodahs imports in app-arch code (elastic#78582)
  Make Field a React.lazy export (elastic#78483)
  [Security Solution] Improves detections tests (elastic#77295)
  [TSVB] Different field format on different series is ignored (elastic#78138)
  RFC: Improve saved object migrations (elastic#66056)
  ...
@kibanamachine kibanamachine added the backport missing Added to PRs automatically when the are determined to be missing a backport. label Oct 1, 2020
@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 77745 or prevent reminders by adding the backport:skip label.

walterra added a commit to walterra/kibana that referenced this pull request Oct 2, 2020
…lastic#77745)

- Clean up lodash imports.
- Import types only where applicable.
- Reduce page load bundle size by fetching more code asynchronously.
# Conflicts:
#	x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/expanded_row.test.tsx
@kibanamachine kibanamachine removed the backport missing Added to PRs automatically when the are determined to be missing a backport. label Oct 2, 2020
walterra added a commit that referenced this pull request Oct 2, 2020
…77745) (#79252)

- Clean up lodash imports.
- Import types only where applicable.
- Reduce page load bundle size by fetching more code asynchronously.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:ml refactoring release_note:skip Skip the PR/issue when compiling release notes v7.10.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ML] Improve bundles sizes related to shared code ml/transform
6 participants