Skip to content

Webpack's Performance at Airbnb #5718

Closed

Description

@gdborton

TLDR: We want to improve webpack's performance in our development environment, and after investigating many performance options we are looking for direction on how to make progress. We want to bring our cached production build and our webpack development server boot times down from 2m58s and 2m28s to ~15 seconds.

NOTE: Please don't close the issue because I deleted the template, if it's important leave a comment and I'll add the template w/ answers to this issue.

Some background on our build:

We have 4 build configurations

Name Description Chunks Entrypoints Modules
Client Builds our bundles with the browser in mind. 710 587 15314
Server Builds a subset of our client bundles with slightly different ways of handling async loading, externals, etc. 259 259 11152
Embed Builds a few files meant to be embedded on non airbnb sites (no externals, commons chunks, or code-splitting) 10 10 1151
AMP Builds a few amp specific files to help ensure we're AMP compliant (I'm sure compliant is the wrong term). 2 2 1928

Current production build timings

A full build with no babel/happypack cache and without uglify takes: ~4m31s

A full build with a cache, but no uglify takes: ~2m58s

Timing for restarting the webpack server and loading our homepage

These are measured in our ci system, which is slower than our actual development environment.

  • Current Master - full page load, 147673.2 ms (~2m28s)

Optimizations that we've investigated

Production build optimizations already in place

  • happypack - This saves us time in our production builds, we parallelize/cache our babel handlebars transforms. We don't use this in our devServer as babel-loader has a built in cache and that is far and away our most expensive loader.
  • webpack-parallel-uglify-plugin - This parallelizes/caches the uglification process. On a fully cached build this takes ~3 seconds.

Development server optimizations already in place

  • We boot our dev server with only a small subset of our build in place as it's unlikely a developer will need to compile all of airbnb.com locally. We start with any bundles needed for our CommonsChunkPlugin, and add addtional bundles to the compiler as they're needed. This helps significantly to reduce memory pressure as well as initial boot time, and rebuilds.
  • Any server bundles that are added to the server compiler automatically trigger a compile for client side equivalents, it's assumed if you server render something, you'll also want the client code.

Build optimizations that help dev and prod

  • Disabled symlink resolving, this helped both our dev boot times as well as our production build times.
  • babel-loader cache - This is also beneficial for production builds, but is hugely helpful in development as well.
  • devtool: 'cheap-module-eval-source-map'

Other stuff that we've tried

Our primary focus has been on dev server boot up time, as we believe this will have the highest impact on our developers productivity.

  • hard-source-webpack-plugin - We tried this plugin during its 0.4 releases. It was very promising at the time, shaving 25% off of our dev server boot time. We encountered lots of random caching errors though, and it often resulted in manually deleting the cache and restarting the server to correct. Also, we recently revisited this plugin and didn't see a performance improvement in our local environment, it's impact may have been dimished by recent webpack performance work. We also have not revisited this plugin's newer releases.
  • cache-loader - Reduces our dev server boot up time by ~3%. We assume this would have a larger impact without babel-loader's built in cache.
  • thread-loader - Increased our dev server boot up time by ~57%.
  • dll plugin - At the time of investigating this was incompatible with the CommonsChunkPlugin, this may have changed. We didn't see much performance improvement here, and didn't think it was worth the risk of development/production build differences.

Things we've considered but haven't tried

  • Disabling CommonsChunkPlugin in development, manually tracking module dependencies for bundles, and caching this information on disk. This would help our boot time as we could cache build results, and verify that they're still correct based on config/module changes. This comes at a cost though as bundles can't be compiled correctly (matching production) without also compiling with CommonsChunk targets in place.

Profiling numbers

Again, these are the times that it would take on average to restart our development server and load up our homepage.

  • thread-loader full page load, 232344.2ms (3m52s)
  • cache-loader full page load, 137157.8ms (2m16s)
  • hard-source-webpack-plugin full page load, 85980.6ms (1m25s)

Goals

What we REALLY want to see is our webpack development server rebooting and serving requests (with a warmed cache) within 10 seconds. The 10 second number is arbitrary, but I think a good heuristic would be the time it takes to read all the modules from disk + some % of that time.

Looking over our modules for our client config, it took 1337.9 ms to read in and compute a sha265 hash for each module. If we assume for configuration differences that each of our configs needs to process all of their modules independently (there is major overlap), we can come up with an overall time of 2581ms (1337.9 + 974.3 + 100.6 + 168.4). Let's also assume that we need to read them twice (once for calculating a cacheable hash, and again for reading from cache), and that we're caching source maps so reading from cache will take twice as long. That'll put our build at 7743ms.

If we add in some overhead for verifying previous module resolutions, let's say 2 seconds assuming some improvements here based on this RFC issue. Build would now be at 9743ms.

Of course we need to account for bundle concatenation, module deduplication, scope hoisting, etc. but I believe that this is not where the bulk of the work is happening. Let's put another 50% on our build time.

I believe that 14,614ms should be a realistic upper limit for a cached build. Probably 20 seconds for a mostly cached build with a few files changed (this will prevent only optimizing for a full cache hit).

After many profiles, there don't seem to be many items left in terms of low hanging performance improvements.

What's the best way for us to contribute to making this a reality?

Additional considerations

We were able to build a tool internally as a proof of concept regarding maximum performance. Without a significant effort investment we were able to configure a simple bundler/server combo that compiled our entire build (incorrectly, so grain of salt) in ~20 seconds.

We did this basically by moving module transforming/parsing to worker threads and caching the results of the transforms. The main thread then builds the bundle dependency trees, and sends a sort of manifest to the workers for them to compile the final output.

I think that this approach would lead to the best performance possible, but it definitely comes with caveats. For one thing, instances cannot be shared amoung workers, so data passed to them must be serializable.

Activity

  1. added this to the webpack 4 milestone on Sep 26, 2017
  2. gdborton commented on Sep 26, 2017

    @gdborton
    ContributorAuthor

    ❤️ the v4 milestone, but is there any way that we can help push this forward? I think that converting loaders/ast parsing to be purely functional, caching their results, and running them in workers by default get us a lot of the way there. I'd have no idea how to handle this though.

  3. graingert commented on Sep 27, 2017

    @graingert

    We boot our dev server with only a small subset of our build in place as it's unlikely a developer will need to compile all of airbnb.com locally. We start with any bundles needed for our CommonsChunkPlugin, and add addtional bundles to the compiler as they're needed. This helps significantly to reduce memory pressure as well as initial boot time, and rebuilds.

    is the source for this anywhere?

  4. graingert commented on Sep 27, 2017

    @graingert

    is this a dupe of #1905?

  5. gdborton commented on Sep 28, 2017

    @gdborton
    ContributorAuthor

    It's not public, this was something that was developed ad-hoc internally to get more oomph out of the server.

    It's certainly similar as both issues are about performance, but I like that this one includes specific performance goals. Also the scale for this issue is about 20x larger.

  6. ljharb commented on Sep 28, 2017

    @ljharb

    @sokra @TheLarkInn any update here? it'd be great to explore paths to making webpack performant enough for @airbnb's use cases :-)

  7. rafde commented on Sep 29, 2017

    @rafde
    Contributor

    #5660 has performance updates. Maybe pulling that branch could help test perf changes. That or wait for beta tag.

  8. gdborton commented on Sep 29, 2017

    @gdborton
    ContributorAuthor

    I'll give it a shot and see how it impacts our build.

  9. alexander-akait commented on Oct 5, 2017

    @alexander-akait
    Member

    About uglifyjs-webpack-plugin, i have PR that fix cache and parallels webpack-contrib/uglifyjs-webpack-plugin#108, it is allow to replace webpack-parallel-uglify-plugin on basic plugin and welcome to feedback.

  10. alexander-akait commented on Oct 5, 2017

    @alexander-akait
    Member

    Also about cache-loader, he have was merged PR (webpack-contrib/cache-loader#5) that avoid fs calls and it is should be improve perf, but now new version with merged PR will not publish.

  11. joshwiens commented on Oct 10, 2017

    @joshwiens
    Contributor

    @gdborton - [email protected] includes the performance improvements mentioned by @evilebottnawi

  12. gdborton commented on Oct 10, 2017

    @gdborton
    ContributorAuthor

    The uglify portion of the build is already very fast due to our caching w/ webpack-parallel-uglify-plugin, but it would certainly be nice to be back on the official plugin, but I don't think it'll get us any performance benefits.

  13. lencioni commented on Oct 13, 2017

    @lencioni
    Contributor

    Also, just a little more context. We are currently attempting to enable the ModuleConcatenationPlugin, but are having problems running out of memory. Without this plugin, we are able to build by giving webpack 8 GiB of memory (--max_old_space_size=8192), but when we enable this plugin after about 30 minutes of chugging along, we run out of memory even after bumping it to 30 GiB (--max_old_space_size=30720).

    Not sure if this is helpful, but here is some of the output:

    <--- Last few GCs --->
    
     1493642 ms: Mark-sweep 28067.1 (30766.7) -> 28067.1 (30766.7) MB, 3555.4 / 0.0 ms [allocation failure] [scavenge might not succeed].
     1497169 ms: Mark-sweep 28067.1 (30766.7) -> 28067.1 (30766.7) MB, 3522.8 / 0.0 ms [allocation failure] [scavenge might not succeed].
     1500673 ms: Mark-sweep 28067.1 (30766.7) -> 28080.9 (30742.7) MB, 3500.5 / 0.0 ms [last resort gc].
     1504325 ms: Mark-sweep 28080.9 (30742.7) -> 28097.6 (30738.7) MB, 3647.8 / 0.0 ms [last resort gc].
    
    
    <--- JS stacktrace --->
    
    ==== JS stack trace =========================================
    
    Security context: 0x25a46f43fa99 <JS Object>
        1: DoJoin(aka DoJoin) [native array.js:~129] [pc=0xc6fd4d20abc] (this=0x25a46f404241 <undefined>,w=0x27f6d75ffec9 <JS Array[78]>,x=78,N=0x25a46f404281 <true>,J=0x25a46f44a369 <String[1]:  >,I=0x8df922bb911 <JS Function ConvertToString (SharedFunctionInfo 0x25a46f45dbc9)>)
        2: Join(aka Join) [native array.js:180] [pc=0xc6fd11852d2] (this=0x25a46f404241 <undefined>,w=0x27f6d75ffec9 <JS Arr...
    
    FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
     1: node::Abort() [node]
     2: 0x10aa4ac [node]
     3: v8::Utils::ReportApiFailure(char const*, char const*) [node]
     4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [node]
     5: v8::internal::Factory::NewRawTwoByteString(int, v8::internal::PretenureFlag) [node]
     6: v8::internal::Runtime_StringBuilderJoin(int, v8::internal::Object**, v8::internal::Isolate*) [node]
     7: 0xc6fd11060c7
    Aborted
    

    Hopefully whatever work we end up doing to improve the speed of webpack will also help with the memory when this plugin is enabled, but it might be possible that we need to look at improvements in this plugin specifically as well.

  14. TheLarkInn commented on Oct 13, 2017

    @TheLarkInn
    Member

    @ljharb The PR that @gdborton has open I'm keeping a close eye. I believe that the cases described in the initial writeup can be solved through parallelization at the Module#build level like Gary suggests in his PR notes.

  15. 53 remaining items

  16. benneq commented on Mar 7, 2018

    @benneq

    What syntax you mean?

  17. sod commented on Mar 7, 2018

    @sod

    @benneq are you sure all those "thread-loader" things don't do more harm then good? The first post of this thread states that it made their build 60% slower. Should just try removing that.
    And if you keep using it maybe incorporate the warning with it's interaction with the sass-loader as mentioned in the webpack doc.

    node-sass has a bug which blocks threads from the Node.js threadpool. When using it with the thread-loader set workerParallelJobs: 2.

    And maybe remove all those minify configurations and use webpack --mode=production and webpack --mode=development --watch instead to boost development performance.

    https://medium.com/webpack/webpack-4-mode-and-optimization-5423a6bc597a

  18. benneq commented on Mar 7, 2018

    @benneq

    @montogeek I'll check that soon.

    @sod Yes, they improve our build times a lot.
    In Webpack 3 our build times (for the whole project. Not only the webpack Build) went from 10 minutes to 7 minutes. Just by adding a few thread-loaders to the webpack config.
    In Webpack 4 it's 14 minutes (without thread-loader) vs. 10 minutes.

    As I said, we just left our webpack 3 config untouched and use it with webpack 4. Only removed the Uglify plugin.

    Our start command for the config file I posted above is this:

    webpack --mode production --config webpack.config.prod.js
    

    I'm not really concerned about the watch mode. It went from 15 to 18 seconds startup time. That's acceptable. But 3 additional minutes on the build server are annoying.

    I'm not quite sure which plugins and options I can remove from the config, because I don't know if all plugins already use the new development and production flags. Though I just kept everything as it was with webpack 3.

  19. sokra commented on Mar 9, 2018

    @sokra
    Member

    Could you try without the ExtractTextWebpackPlugin?

  20. benneq commented on Mar 9, 2018

    @benneq

    @montogeek : changing the syntax didn't change build times at all.

    @sod : I removed webpack.LoaderOptionsPlugin. No improvement to build time. But bundle size increased by 5%. Though I'll keep the plugin (only in production build of course)

    @sokra : We use ExtractTextWebpackPlugin v4.0.0-beta.0
    I now just removed the lines concerning ExtractTextWebpackPlugin, but build time stays the same.
    Is there some built in way in webpack 4 to generate external css file?

  21. edmorley commented on Apr 20, 2018

    @edmorley
    Contributor

    Is there some built in way in webpack 4 to generate external css file?

    The recommendation with webpack 4 is to switch from extract-text-webpack-plugin to mini-css-extract-plugin (since the former has wontfixed official webpack 4 support, and the latter is more lightweight anyway).

  22. dtothefp commented on Apr 20, 2018

    @dtothefp

    Seems like this issue has gone quiet since the release of Wepack 4 and that others are reporting performance problems when upgrading from v3 to v4 #6767. I upgraded a quite large project from v2 to v4 and am considering downgrading to v3 as our rebuild times are in the range of 30s to 2mins. I'm using mini-css-extract-plugin and DLL Plugin along with HappyPack. Does anyone have any updates on Webpack v4 performance?

  23. alexander-akait commented on Apr 21, 2018

    @alexander-akait
    Member

    @dtothefp can you provide link on your repo or create minimum reproducible test repo?

  24. elevenpassin commented on Apr 27, 2018

    @elevenpassin
    Contributor

    @dtothefp Preferably in a different issue I guess :D

  25. Bnaya commented on Apr 27, 2018

    @Bnaya
    Contributor

    @dtothefp for dev, i'm swapping mini-css-extract-plugin with plain style-loader.
    So i get HMR and much faster rebuilds.
    Also, i'm on typescript project, and i set my ts-loader to transplieOnly, and i'm running tsc separately,
    And my rebuilds are very fast (1s~)

  26. abhinavsingi commented on May 29, 2018

    @abhinavsingi

    node version 8.9.4 should be used as webpack uses some optimisations done in that particular versions for build times

  27. benneq commented on May 29, 2018

    @benneq

    What's the equivalent 10.x version where these optimizations are included?

  28. sokra commented on Jun 7, 2018

    @sokra
    Member

    Seems like this issue has gone quiet since the release of Wepack 4 and that others are reporting performance problems when upgrading from v3 to v4

    Sorry we can't watch every discussion in older issues. I better close this one so people will open new issues.

    If you still have issue with rebuild performance here is a guide how to get it fixed:

    • Rebuilding in production mode is slow. It doesn't use caching. Check if you are using development mode.
    • Reduce your configuration to remove all 3rd-party plugins. Is it still slow?
    • Don't use a minimizer plugin.
    • Try with latest node.js version. And latest webpack version.
    • Delete lockfile and node_modules and reinstall.
    • Open a new issue.
    • If possible create a minimal repro repo which has the same effect.
    • If possible capture a cpu profile of the rebuild process and attach it to the issue.
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions