You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cache build information between webpack processes #250
The current watch mode for webpack is fairly good at caching load results and general workload between file changes as long as you keep the same webpack process running. The initial build for some projects, on the other hand, can be on the order of more than a minute. This can make the build process very slow if for some reason you need to stop your watcher, or node crashes, or you simply want to restart your connect server with different flags.
It would be nice if webpack were able to persist compilation information between invocations of the node process to the filesystem and reload that cache later -- so ideally, performance between different $ webpack calls would be close to how long it takes to --watch to recompile when it detects a change.
Reacted by Joey Baker, Kamil Pękala, Joe Lencioni, Sasha Gavrilov, Steve Sewell, Rafa Yepes, Michał Gołębiowski-Owczarek, Sean Huber, Razvan Stoenescu, Kevin Montag and 99 more
We need a way to serialize the cache object to the filesystem, but the cache is not a simple JSON object. It should be possible with a cool serializiation module that can rebuild the object structure.
this would be great. Currently if you have the bail option set then some JS errors cause the watch process to crash and you have to do an entire new build to start it again.
This could be very useful also for speeding up incremental builds in a continuos deployment setup. @sokra could you point us in the right direction if we want to help with this?
Reacted by Cansin Yildiz, Andrew Kane, Kevin Montag, Bernardo Farah, Revan Zhang, Ivan Voskoboinyk and Fergal Armstrong
I took a look at serialising the cache based on a TodoMVC project (babel, sass) that I have lying around and Webpack 1.12.0. Here is what I found.
The compiler.cache is provided by CachePlugin created by WebpackOptionsApply. In CachePlugin it is trivial to serialise on the existing "after-compile" step and reload the same file (if it exists) in the constructor. Sure it has circular dependencies but cycle may be used.
However this doesn't get us anywhere. The reason being that the cache is not a plain data structure. It is composed of Classes that would need to be identified on serialisation and reconstructed on deserialisation. Naive JSON deserialised does not implement the requisite methods and the compiler quickly fails on cacheModule.needRebuild().
I found that all my interesting files (in the small Todo project) were entries in the cache hash each with value instanceof ContextModule. Noting that ContextModule inherits Module inherits DependenciesBlock which uses DependenciesBlockVariable.
Enumerable on these module instances there was also instances of Parser, OriginalSource, SourceMapSource (or Array thereof).
At first glance it is plausible (but suboptimal) to write an external monolithic serialiser/deserialiser that can reconstruct these objects. It would support only this set of Classes but not necessitate any changes to the Webpack codebase.
Likely such and implementation would only be useful for a narrow set of use cases. Given the large number of Classes in the Webpack /lib I imagine there would be a quickly growing list of other classes that would need to be included in the monolith.
The alternative is where @sokra brings the difficult tag - Much or all of the Webpack /lib needs to support some sort of serialisation interface, in order that the cache items may be recursively serialised/ deserialised. This would be the more consistent solution but needs architectural buy-in.
Please let me know if any of this is misinformed. I'm interested to hear other thoughts on this.
I would like to try the monolithic serialiser. Obviously as its author it would be suited to my use case, but also because it might inform a more general solution.
If you know of any other Classes that would need inclusion (or you can discover them by running a similar test) then please let me know. But my hope is that the full cache would not need to be reinstated to get some performance benefit.
Ok, so I have a really naive implementation of the monolithic serialisation. Overall... interesting but currently unusable.
There were lots of classes in the compiler.cache that I initially didn't see. But thankfully I was able to use the require.cache to discover pretty much every Class and @sokra unintentionally did an absolutely beautiful job of structuring all the classes so that they could be easily reconstituted.
Without optimisation serialisation is 2 orders of magnitude in the red, which doesn't bode well. Deserialisation is quite fast, leading me to believe it is the class discovery hurting the serialisation. In the short term I added a persist:false option to turn off serialisation and just use any existing cache file.
Even under these conditions performance gain is only 10% on a medium project. For very small projects this is completely offset by the time to deserialise the cache. At this stage its pretty amazing it produces workable output at all. But what we are all expecting is incremental-size compile times. So I have to conclude that right not it is not useful.
Some learning:
The cache is much more complex than I expected from my experience working with the Browserify cache. I am sure this is essential complexity but I'm hoping we can be selective in what we persist. This really needs some expert knowledge.
Possibly doesn't need an architectural solution due to the clean nature of @sokra's classes. Notwithstanding the possibility that the whole concept just doesn't work.
Currently writing JSON to disk. I'm seeing 200MB cache file on a medium size project with built size around 4MB. Binary will help but how much (?).
With lots of copies of Webpack code it was difficult to detect Classes from the correct paths, making require.cache essential. I need to re-confirm this wasn't something stupid I was doing during development which is now fixed. I'd like to require-dir on the Webpack /lib folder instead.
The small project mentioned above builds correctly but that is no guarantee for other use cases. I don't have any medium/large projects in a stable state (migrating from Browserify) but I suspect a problem with at least one. All are AngularJS projects. It would be good if others can test their use-case to see where things stand.
I've spent a couple of days on this and need to let it sit for a while. But if it crashes for you certainly raise an issue and I will try to look at it right away.
This is such a missing link it would be awesome to crack it. If anyone has feedback or PR that can move this plugin forward please let me know.
@bholloway Nice work and writeup! Apologies if this is obvious, but how much of the 200MB is duplicated? Are the same references being serialized multiple times? You might also be running into memory pressure/gc just from serializing/deserializing the entire thing into memory at once (as opposed to streaming/multiple cache files).
Running an amended version of Douglas Crawford's decycle to remove
duplicates and circular references.
Forgot to mention that the emit phase is running for reach compiler. Need
to find an async phase for overall completion and act just once.
Since webpack@2 already uses a separate module to execute loaders, we could write a parallel-loader which spawns multiple processes (see our meeting notes for details). The parallel-loader would need to provide a webpack-like loader context and handle all the communication in the background.
Honestly, that's exactly what happypack does, although it doesn't use loader-runner since at the time I saw it it didn't actually work for a distributed application (at least in happypack's context). I can't recall the details but it's probably something easily amendable.
That's perfect. You could give us valuable feedback if the proposed loader API would make it possible to use happypack rather as a loader than a plugin.
@jhnns I'm investigating our webpack 2 build that takes around 2 minutes (.tsx project with about 800 modules).
Could you please describe how to get those flame graphs?
I think flame graphs could give us better insights on where so much time is spent. I'll try to get some from my current project and post instructions on how to get them (if you do not know it anyway ^^).
@abergs run your webpack with node's inspector (a command like the following):
node --debug-brk --inspect webpack
Then launch your browser to visit the URL that command will show you (make sure you're on node 6+, 7 preferably)
In the browser page, find the Profiles panel and click on "Start Profile", then let the debugger continue (you may need to go back to the Sources panel to do this). Once the run is complete, go to the Profiles panel and click on "Stop Profile" or such, then you'll find the collected profile in a list to the left. That profile will contain the flame graph you're after.
Reacted by Karl Horky, Joe Lencioni, Anders Åberg, Alec Mev, Jérémie Ledentu, Konstantin L, Jake Goulding and Alien ZHOU
Has some progress been made to make this feature at the core of webpack ?
The Rails' Asset Pipeline do it perfectly. Is it something worth investigating ?
The new webpack-contrib/cache-loader reduces consecutive builds in our environment from 50s to 27s. But you have to ensure that one cache folder isn't used for different configs (like dev/prod).
@sod that's an improvement; how much faster do you think it would be if Webpack could load its entire previous state from disk on startup, see what's changed, and then run an incremental rebuild in the exact same way it would in watch mode? That way the time to invoke webpack again after a single file change would be reduced to just
<time to load previous state from disk> +
<time to determine which files have changed> +
<time to run incremental rebuild for the files that changed>
In my environment incremental rebuilds take on the order of 10s, and I would hope that loading the previous state and determining which files have changed take far less time.
I agree with @jedwards1211
We have like 8000 modules in our build and most of the startup time seems to be taken by reading the files and doing the acorn parsing to determine deps. We are already using a babel cache so cache-loader would not help.
I am willing to pitch in and help but hoping somebody with more experience with the codebase/problem space could take the lead or at least give a direction to get started. The investigations above looked very impressive! I think this feature would make a lot of people very happy and help alleviate performance issues. @sokra@mzgoddard@markfinger
That's what you get when legacy code moves to webpack, lol. It's gotten slightly smaller now, but for the longest time we had a massive hairball chunk about 50mb in size, for which i had to write a custom plugin emulating @sokra's agressive splitting plugin to split it into slices that get loaded in parallel. Good times... 😂
I was running some tests on how hard-source-webpack-plugin could be added to the Angular CLI pipeline and the results seem promising.
It does seem fairly sensitive to Webpack internals though. Since it relies on serializing Webpack sources any change in them can possibly break the plugin.
This unfortunately means that it will necessarily lag behind latest Webpack stable its changes are integrated into hard-source-webpack-plugin. Webpack 4 also seems to be bringing in a fair number of performance improvements which will probably affect sources and thus need to be integrated into the plugin.
This makes me think that the only way a plugin such as hard-source-webpack-plugin could be stable is if it's integrated directly in Webpack, or has access to a stable API (e.g sources supporting serialization). Maybe the second option would provide plugin authors with more options.
Reacted by Sameer Singh, Yuri Pereira Constante, vladar and MohamadReza Mash'al
Activity
jhnns commentedon May 3, 2014
👍
sokra commentedon May 6, 2014
We need a way to serialize the cache object to the filesystem, but the cache is not a simple JSON object. It should be possible with a cool serializiation module that can rebuild the object structure.
jhnns commentedon May 6, 2014
What properties of the cache object can't be serialized with JSON.stringify? I guess toSrc isn't option too 😉
geddski commentedon May 15, 2014
this would be great. Currently if you have the
bailoption set then some JS errors cause the watch process to crash and you have to do an entire new build to start it again.Initial commit
damassi commentedon Jan 6, 2015
Running into this problem as well. Once its going its instant, but right now our initial build takes over a minute to warm up.
jtangelder commentedon Feb 12, 2015
This would be really cool.
nickdima commentedon Sep 3, 2015
This could be very useful also for speeding up incremental builds in a continuos deployment setup.
@sokra could you point us in the right direction if we want to help with this?
marcello3d commentedon Sep 22, 2015
Any pointers on where to start to solve this issue?
bholloway commentedon Oct 15, 2015
I took a look at serialising the cache based on a TodoMVC project (babel, sass) that I have lying around and Webpack 1.12.0. Here is what I found.
The
compiler.cacheis provided byCachePlugincreated byWebpackOptionsApply. InCachePluginit is trivial to serialise on the existing"after-compile"step and reload the same file (if it exists) in the constructor. Sure it has circular dependencies but cycle may be used.However this doesn't get us anywhere. The reason being that the cache is not a plain data structure. It is composed of Classes that would need to be identified on serialisation and reconstructed on deserialisation. Naive JSON deserialised does not implement the requisite methods and the compiler quickly fails on
cacheModule.needRebuild().So how big is this problem? I hacked this code in the
CachePlugin "after-compile"to see.I found that all my interesting files (in the small Todo project) were entries in the
cachehash each with valueinstanceof ContextModule. Noting thatContextModuleinheritsModuleinheritsDependenciesBlockwhich usesDependenciesBlockVariable.Enumerable on these module instances there was also instances of
Parser,OriginalSource,SourceMapSource(or Array thereof).At first glance it is plausible (but suboptimal) to write an external monolithic serialiser/deserialiser that can reconstruct these objects. It would support only this set of Classes but not necessitate any changes to the Webpack codebase.
Likely such and implementation would only be useful for a narrow set of use cases. Given the large number of Classes in the Webpack
/libI imagine there would be a quickly growing list of other classes that would need to be included in the monolith.The alternative is where @sokra brings the difficult tag - Much or all of the Webpack
/libneeds to support some sort of serialisation interface, in order that the cache items may be recursively serialised/ deserialised. This would be the more consistent solution but needs architectural buy-in.Please let me know if any of this is misinformed. I'm interested to hear other thoughts on this.
I would like to try the monolithic serialiser. Obviously as its author it would be suited to my use case, but also because it might inform a more general solution.
If you know of any other Classes that would need inclusion (or you can discover them by running a similar test) then please let me know. But my hope is that the full cache would not need to be reinstated to get some performance benefit.
bholloway commentedon Oct 16, 2015
Ok, so I have a really naive implementation of the monolithic serialisation. Overall... interesting but currently unusable.
There were lots of classes in the
compiler.cachethat I initially didn't see. But thankfully I was able to use therequire.cacheto discover pretty much every Class and @sokra unintentionally did an absolutely beautiful job of structuring all the classes so that they could be easily reconstituted.Without optimisation serialisation is 2 orders of magnitude in the red, which doesn't bode well. Deserialisation is quite fast, leading me to believe it is the class discovery hurting the serialisation. In the short term I added a
persist:falseoption to turn off serialisation and just use any existing cache file.Even under these conditions performance gain is only 10% on a medium project. For very small projects this is completely offset by the time to deserialise the cache. At this stage its pretty amazing it produces workable output at all. But what we are all expecting is incremental-size compile times. So I have to conclude that right not it is not useful.
Some learning:
require.cacheessential. I need to re-confirm this wasn't something stupid I was doing during development which is now fixed. I'd like to require-dir on the Webpack/libfolder instead.I've spent a couple of days on this and need to let it sit for a while. But if it crashes for you certainly raise an issue and I will try to look at it right away.
This is such a missing link it would be awesome to crack it. If anyone has feedback or PR that can move this plugin forward please let me know.
marcello3d commentedon Oct 16, 2015
@bholloway Nice work and writeup! Apologies if this is obvious, but how much of the 200MB is duplicated? Are the same references being serialized multiple times? You might also be running into memory pressure/gc just from serializing/deserializing the entire thing into memory at once (as opposed to streaming/multiple cache files).
bholloway commentedon Oct 16, 2015
21 remaining items
amireh commentedon Sep 12, 2016
Honestly, that's exactly what happypack does, although it doesn't use loader-runner since at the time I saw it it didn't actually work for a distributed application (at least in happypack's context). I can't recall the details but it's probably something easily amendable.
jhnns commentedon Sep 12, 2016
That's perfect. You could give us valuable feedback if the proposed loader API would make it possible to use happypack rather as a loader than a plugin.
abergs commentedon Feb 1, 2017
@jhnns I'm investigating our webpack 2 build that takes around 2 minutes (.tsx project with about 800 modules).
Could you please describe how to get those flame graphs?
amireh commentedon Feb 1, 2017
@abergs run your webpack with node's inspector (a command like the following):
Then launch your browser to visit the URL that command will show you (make sure you're on node 6+, 7 preferably)
In the browser page, find the Profiles panel and click on "Start Profile", then let the debugger continue (you may need to go back to the Sources panel to do this). Once the run is complete, go to the Profiles panel and click on "Stop Profile" or such, then you'll find the collected profile in a list to the left. That profile will contain the flame graph you're after.
abergs commentedon Feb 1, 2017
Thank you @amireh
abdelbk commentedon Apr 15, 2017
Has some progress been made to make this feature at the core of webpack ?
The Rails' Asset Pipeline do it perfectly. Is it something worth investigating ?
sod commentedon Jun 26, 2017
The new webpack-contrib/cache-loader reduces consecutive builds in our environment from 50s to 27s. But you have to ensure that one cache folder isn't used for different configs (like dev/prod).
jedwards1211 commentedon Nov 27, 2017
@sod that's an improvement; how much faster do you think it would be if Webpack could load its entire previous state from disk on startup, see what's changed, and then run an incremental rebuild in the exact same way it would in watch mode? That way the time to invoke webpack again after a single file change would be reduced to just
In my environment incremental rebuilds take on the order of 10s, and I would hope that loading the previous state and determining which files have changed take far less time.
MagicDuck commentedon Nov 29, 2017
I agree with @jedwards1211
We have like 8000 modules in our build and most of the startup time seems to be taken by reading the files and doing the acorn parsing to determine deps. We are already using a babel cache so cache-loader would not help.
I am willing to pitch in and help but hoping somebody with more experience with the codebase/problem space could take the lead or at least give a direction to get started. The investigations above looked very impressive! I think this feature would make a lot of people very happy and help alleviate performance issues.
@sokra @mzgoddard @markfinger
jedwards1211 commentedon Nov 29, 2017
Hot damn, I wonder if that's a record! 😅
MagicDuck commentedon Nov 29, 2017
That's what you get when legacy code moves to webpack, lol. It's gotten slightly smaller now, but for the longest time we had a massive hairball chunk about 50mb in size, for which i had to write a custom plugin emulating @sokra's agressive splitting plugin to split it into slices that get loaded in parallel. Good times... 😂
filipesilva commentedon Nov 29, 2017
I was running some tests on how
hard-source-webpack-plugincould be added to the Angular CLI pipeline and the results seem promising.It does seem fairly sensitive to Webpack internals though. Since it relies on serializing Webpack sources any change in them can possibly break the plugin.
This unfortunately means that it will necessarily lag behind latest Webpack stable its changes are integrated into
hard-source-webpack-plugin. Webpack 4 also seems to be bringing in a fair number of performance improvements which will probably affect sources and thus need to be integrated into the plugin.This makes me think that the only way a plugin such as
hard-source-webpack-plugincould be stable is if it's integrated directly in Webpack, or has access to a stable API (e.g sources supporting serialization). Maybe the second option would provide plugin authors with more options.alexander-akait commentedon Jun 6, 2018
It is very old issue and duplicate more detailed issue #6527. Please leave you opinions and how we can solve this in #6527 issue. Thanks!