Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Introduce NativeCompileCache - #9318

Merged
as-cii merged 14 commits into
masterfrom
as-compilation-cache
Nov 3, 2015
Merged

as-cii merged 14 commits into
masterfrom
as-compilation-cache

Conversation

@as-cii

@as-cii as-cii commented Oct 28, 2015

Copy link
Copy Markdown
Contributor

This PR speeds up source code compile times by using a hacked version of the vm module (https://github.com/as-cii/cached-run-in-this-context).

We've been searching very hard for a way to make startup faster: the biggest low hanging fruit seemed to be vm.runInThisContext which, before this improvement, took more than 250ms-300ms. That function is used inside Module.prototype._compile for, surprise, compiling the source code: although v8 compiler is quite fast already, our codebase has grown quite a lot and the more files we added, the worse the situation would get.

The snapshot approach tried in #9265 should have indirectly solved this, but it felt way too complicated to construct an AtomEnvironment inside the snapshot context. Instead, we are taking advantage of a v8 compiler feature that allows to speed up source code compilation by producing/consuming a cache blob. By using this mechanism, we have managed to change startup times from (without any tab open):

screen shot 2015-10-28 at 11 51 42

to:

screen shot 2015-10-28 at 11 48 05

🎉 🎉 🎉

There's still some aspect we could improve, such as where these cache blobs get stored: I used files as the storage mechanism, as it allows us to keep the code pretty simple, but they take ~40ms to get read. There are alternatives to this (e.g. asar), but I am not sure it would improve the situation that much: even a 50% improvement, would make this just ~20ms faster at the expense of making code a bit more complex.

Please, note that whenever DevTools are open, v8 seems to make this optimization a bit worse: if you, however, spawn Atom:

  • via cmd;
  • by running the executable;
  • by reloading the window with DevTools closed;

You will notice the speedup. @zcbenz: any ideas why this happens? I guess this covers already 100% of the average use cases and won't matter to the end user, but if possible it seems to be still worth investigating.

After this lands, I will go on hunting other things that are slowing startup down: we feel like IO could be the main cause but we'll get a better picture after this improvement gets 🚢ped.

/cc: @atom/feedback @nathansobo

@zcbenz

zcbenz commented Oct 28, 2015

Copy link
Copy Markdown
Contributor

✨

whenever DevTools are open, v8 seems to make this optimization a bit worse @zcbenz: any ideas why this happens?

I guess devtools disabled the cache optimization, devtools is known to affect V8's optimization when opened since it needs to receive full information of the JavaScript context.

@as-cii

as-cii commented Oct 30, 2015

Copy link
Copy Markdown
Contributor Author

This seems to work pretty nicely and the I/O improvements made in b365ba5 seems to let Atom load within 670ms-690ms almost always on my machine.

@maxbrunsfeld @nathansobo: I have polished up the code as well, so your feedback would be super appreciated! 🙏

@izuzak: features-wise nothing should have changed, but it would be great if you could have a look and try to break 💣 it. 🙇

@as-cii
as-cii force-pushed the as-compilation-cache branch from b365ba5 to 71b6871 Compare October 30, 2015 09:14
@as-cii
as-cii force-pushed the as-compilation-cache branch from cbb6f21 to f13e516 Compare October 30, 2015 11:45
Comment thread src/file-system-cache-blob-storage.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you think about naming this class something generic like BlobStore, since its interface isn't specific to a caching use case? For me, I think that kind of name would more quickly communicate that the specific v8-code-caching logic lives in the NativeCompileCache, not here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call. 👍 I think I still like the explicitness of mentioning that this is a file-system cache, though: so, what about naming it FileSystemBlobStore?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, I like that. It implements the BlobStore interface using the file-system.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Another very minor thing, maybe NativeCompileCache::setCacheStorage -> ::setBlobStore?

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

❤️ Clean design. Clean tests. Super nice.

@thomasjo

Copy link
Copy Markdown
Contributor

Is there a reason the new bits are not written using Babel/ES2015? 😕

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

I commented above about using es6. Don't think we need babel though, now that we're on electron 34, unless we're using async or await.

@thomasjo

Copy link
Copy Markdown
Contributor

I commented above about using es6. Don't think we need babel though, now that we're on electron 34, unless we're using async or await.

Does the ES6 bits in Electron support new module loader syntax (e.g. import)? Makes code so much nicer 🎨

Also var is generally evil, and should be avoided due to hoisting nonsense. Prefer let (and const when applicable).

Other than the ES syntax comment, this looks great! Awesome job as usual @as-cii 👏

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

No, it doesn't support import. Personally, I probably wouldn't introduce transpilation just for that feature, but I guess I'd be open to it. Agreed about const and let, which electron also supports natively. You might have to use strict, but that's fine.

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Also, in order to work in dev mode, in which files are transpiled on-the-fly, modules like these, that are loaded early in the startup process (compile-cache.js and earlier), need to not be transpiled.

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

I've been using this branch this morning, and I can definitely feel the improvement in startup.

@as-cii
as-cii force-pushed the as-compilation-cache branch from bce16f6 to 27a6f36 Compare October 30, 2015 17:49
@as-cii

as-cii commented Oct 30, 2015

Copy link
Copy Markdown
Contributor Author

I've been using this branch this morning, and I can definitely feel the improvement in startup.

Clean design. Clean tests. Super nice.

Awesome job as usual

@thomasjo @maxbrunsfeld: ❤️ ❤️ ❤️

I have just ported the code over ES6 and removed references to cache. It reads so better now, thanks for the suggestion guys! I'll give this another spin and merge in the weekend if everything is 🆒. 😎

@YurySolovyov

Copy link
Copy Markdown

Is that cached-run-in-this-context module is something worth upstreaming?

Comment thread static/index.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We might want to do this a different way, to avoid doing these two synchronous IPC calls to the browser process (.require and .on) during startup, and also to avoid contentious writes by multiple windows during shutdown. Maybe the browser process could actually control this in its before-quit callback, telling only one window to save its state, using some synchronous IPC method like WebContents::executeJavascript.

...so that we can pass BlobStore to AtomEnvironment
as-cii added a commit that referenced this pull request Nov 3, 2015
@as-cii
as-cii merged commit 5d0ba5f into master Nov 3, 2015
@as-cii
as-cii deleted the as-compilation-cache branch November 3, 2015 13:36
@as-cii

as-cii commented Nov 3, 2015

Copy link
Copy Markdown
Contributor Author

Thanks everyone for all the great suggestions! ✨ If needed we can refine this on a separate branch/commit.

@nathansobo

Copy link
Copy Markdown
Contributor

🐎🌠

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Yeah!! Nice job @as-cii

@thomasjo

thomasjo commented Nov 3, 2015

Copy link
Copy Markdown
Contributor

Fantastic work as usual, @as-cii! 🙇

@nathansobo

Copy link
Copy Markdown
Contributor

Hey @as-cii: @kevinsawicki and I were talking yesterday and he brought up that we should include the v8 version in the compile cache key in case they change their internal formats. Did a quick audit and not sure this is currently happening, but we should probably get that in before this goes to beta if we can.

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

we should include the v8 version in the compile cache key in case they change their internal formats

V8 does have a built-in notion of rejecting a cache blob, and @as-cii has handled this case by deleting it from our blob-store, so we may not need to explicitly include it in the cache key.

@nathansobo

Copy link
Copy Markdown
Contributor

Oh right, that makes sense. They handle their own invalidation internally.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants