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

Delay load google-earth-dbroot-parser #7140

Merged
merged 3 commits into from
Oct 16, 2018
Merged

Delay load google-earth-dbroot-parser #7140

merged 3 commits into from
Oct 16, 2018

Conversation

mramato
Copy link
Contributor

@mramato mramato commented Oct 11, 2018

google-earth-dbroot-parser.js is a huge dependency (487 KB source, 202KB minified). It's also only needed if you are using a Google Earth server.

In order to avoid bloat and paying the penalty every time Cesium is loaded, this change loads it on demand the first time it's needed.

I didn't use jsonp for this because there's no server to parse the query and wrap the code in a callback. Instead we just load the script directly into a unlikely to collide global variable. (also reset the old value if it does exist for some reason).

The file gets minified and copied to ThirdParty individually, similar to what we were already doing with the wasm file.

Also fixed a bug in buildCesiumViewer where it wasn't copying all the necessary files to its own build output.

Also extracted loadAndExecuteScript out of Resource.js, but had to keep a version of it in Resource.js because cleaning up test abuse of it is a much larger change best for another PR. (I'll write up an issue).

I talked this over a bit with @shunter and he agreed this is a stopgap solution at best and we either need to leverage require more directly as a module loading system within Cesium itself, or better yet just spend the time to migrate to ES6 (which we want to do but simply don't have the bandwidth for). Until that happens, we might be able to use this approach for a few other large dependencies as well.

@likangning93 you should also be able to use loadAndExecuteScript in #6986, but you'll need to tweak it for Workers.

@ggetz can you review of this? I think I covered every possible use case, but let me know if you find anything.

`google-earth-dbroot-parser.js` is a huge dependency (487 KB source, 202KB
minified).  It's also only needed if you are using a Google Earth server.

In order to avoid bloat and paying the penalty every time Cesium is loaded,
this change loads it on demand the first time it's needed.

I didn't use jsonp for this because there's no server to parse the query
and wrap the code in a callback. Instead we just load the script directly
into a unlikely to collide global variable.

The file gets minified and copied to ThirdParty, similar to what we were
already doing with the wasm file.

Also fixed a bug in buildCesiumViewer where it wasn't copying all the
necessary files to its own build output.

Also extracted `loadAndExecuteScript` out of Resource.js, but had to keep
a version of it in Resource.js because cleaning up test abuse of it is
a much larger change best for another PR. (I'll write up an issue).
@mramato mramato requested a review from ggetz October 11, 2018 19:07
@cesium-concierge
Copy link

Thanks for the pull request @mramato!

  • ✔️ Signed CLA found.
  • CHANGES.md was not updated.
    • If this change updates the public API in any way, please add a bullet point to CHANGES.md.
  • ❔ Changes to third party files were made.
    • Looks like a file in one of our ThirdParty folders (ThirdParty/, Source/ThirdParty/) has been added or modified. Please verify that it has a section in LICENSE.md and that its license information is up to date with this new version.

Reviewers, don't forget to make sure that:

  • Cesium Viewer works.
  • Works in 2D/CV.
  • Works (or fails gracefully) in IE11.

I am a bot who helps you make Cesium awesome! Contributions to my configuration are welcome.

🌍 🌎 🌏

@mramato
Copy link
Contributor Author

mramato commented Oct 11, 2018

FYI, full gzipped release went from 737.1 KB to 710.2 KB (~3.7% decrease)

@mramato
Copy link
Contributor Author

mramato commented Oct 11, 2018

No need to update CHANGES as this was a completely behind the scenes change.

@pjcozzi
Copy link
Contributor

pjcozzi commented Oct 13, 2018

Cool, I think a mention in CHANGES.md is valuable given the size difference for those not using GEE.

Copy link
Contributor

@ggetz ggetz 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 this is working with both built and non-built Cesium.

script.async = true;
script.src = url;

var head = document.getElementsByTagName('head')[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't there a (small) chance the page may not have a head element? Can we just append it to the document?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The head element will always exist.

.then(function() {
dbrootParser = window.cesiumGoogleEarthDbRootParser(protobufMinimal);
if (defined(oldValue)) {
window.cesiumGoogleEarthDbRootParser = oldValue;
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like we're using window.cesiumGoogleEarthDbRootParser for both the loaded script, and the function evaluated with protobufMinimal. Which means you have to set back the oldValue after evaluating. Would this be clearer if we just loaded the module to window.cesiumGoogleEarthDbRootModule or something similar?

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'm not sure what you mean. Here's what's currently happening

  1. We get the old value of cesiumGoogleEarthDbRootParser on the off-chance it exists
  2. We load the script, which defines cesiumGoogleEarthDbRootParser.
  3. We call the cesiumGoogleEarthDbRootParser function and store the result into dbrootParser (module scoped)
  4. We set back the old value of cesiumGoogleEarthDbRootParser or delete it completely if it didn't exist to begin with.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it, sorry for the confusion. Makes sense.

@ggetz
Copy link
Contributor

ggetz commented Oct 15, 2018

Thanks @mramato! This is a nice improvement for most users, who are not using GEE.

@ggetz
Copy link
Contributor

ggetz commented Oct 15, 2018

Looks good. Just update CHANGES.md. I think any specs for loadAndExecuteScript should probably be moved sooner than #7141 so they are in sync with any further updates to this function.

@mramato
Copy link
Contributor Author

mramato commented Oct 15, 2018

I think any specs for loadAndExecuteScript should probably be moved sooner than #7141 so they are in sync with any further updates to this function.

It's not the specs for loadAndExecuteScript that are the problem. It's everything abusing spies to mock it. Either way, this clean up can happen at any time but it's unlikely there will be any "out of sync" issues. If I can get to it anytime soon, I will.

Ready.

@ggetz ggetz merged commit ae98c62 into master Oct 16, 2018
@ggetz ggetz deleted the delay-load-gihugic-file branch October 16, 2018 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants