Skip to content

Cache build information between webpack processes #250

Closed

Description

@zkat

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.

Activity

  1. jhnns commented on May 3, 2014

    @jhnns
    Contributor

    👍

  2. sokra commented on May 6, 2014

    @sokra
    Member

    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.

  3. jhnns commented on May 6, 2014

    @jhnns
    Contributor

    What properties of the cache object can't be serialized with JSON.stringify? I guess toSrc isn't option too 😉

  4. geddski commented on May 15, 2014

    @geddski

    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.

  5. added a commit that references this issue on Oct 20, 2014
  6. damassi commented on Jan 6, 2015

    @damassi

    Running into this problem as well. Once its going its instant, but right now our initial build takes over a minute to warm up.

  7. jtangelder commented on Feb 12, 2015

    @jtangelder

    This would be really cool.

  8. nickdima commented on Sep 3, 2015

    @nickdima
    Contributor

    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?

  9. marcello3d commented on Sep 22, 2015

    @marcello3d

    Any pointers on where to start to solve this issue?

  10. bholloway commented on Oct 15, 2015

    @bholloway

    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().

    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 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.

  11. bholloway commented on Oct 16, 2015

    @bholloway

    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.

  12. marcello3d commented on Oct 16, 2015

    @marcello3d

    @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).

  13. bholloway commented on Oct 16, 2015

    @bholloway
  14. 21 remaining items

  15. amireh commented on Sep 12, 2016

    @amireh

    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.

  16. jhnns commented on Sep 12, 2016

    @jhnns
    Contributor

    Honestly, that's exactly what happypack does

    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.

  17. abergs commented on Feb 1, 2017

    @abergs

    @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 ^^).

  18. amireh commented on Feb 1, 2017

    @amireh

    @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.

  19. abergs commented on Feb 1, 2017

    @abergs

    Thank you @amireh

  20. abdelbk commented on Apr 15, 2017

    @abdelbk

    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 ?

  21. sod commented on Jun 26, 2017

    @sod

    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).

  22. jedwards1211 commented on Nov 27, 2017

    @jedwards1211

    @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.

  23. MagicDuck commented on Nov 29, 2017

    @MagicDuck
    Contributor

    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

  24. jedwards1211 commented on Nov 29, 2017

    @jedwards1211

    We have like 8000 modules in our build

    Hot damn, I wonder if that's a record! 😅

  25. MagicDuck commented on Nov 29, 2017

    @MagicDuck
    Contributor

    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... 😂

  26. filipesilva commented on Nov 29, 2017

    @filipesilva
    Contributor

    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.

  27. alexander-akait commented on Jun 6, 2018

    @alexander-akait
    Member

    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!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions