angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

v7.0.0 bugs and 4/5x slower serve & compile AOT

Open elvisbegovic opened this issue 8 years ago • 100 comments

I hesitate to open this issue on angular page. I opened same issue title 2 week ago here without AOT : v7.rc.0 serving 2x - 3x slower than stable versions on my projects after moving to angular 7.0.0 all is slower (jit seems OK)

I'm submitting a...

[ x ] Performance issue [ x ] Bugs

Current behavior

All was OK with cli7rc3 and angular 6.1.10 This morning after updating to angular 7.0.0 serving with JIT is ok but serving with AOT or build my application or generating i18n xmb file is 2-3 times slower

Expected behavior

Get build/extract i18n time like with cli7rc3 and angular 6.1.10

1. Minimal info of AOT serving

  1. My project have >1200 files.
  2. Before update node --max-old-space-size=8192 ./node_modules/@angular/cli/bin/ng serve --aot --sourceMap=false --disableHostCheck --port=4200 first build finish in 45seconds and Now blocking:

3mn => 11% building modules 9/10 modules 1 active ...ojects\i10\src\assets\styles\styles.css and rest steps seems ok It finish in 228737ms

  1. Before update refresh(file edit) it finish in 30seconds and 3th refresh in 25seconds and Now it refresh in 200277ms and 3th refresh in WARNING : 381979ms it is >6 minutes

2. Minimal info of extract i18n

  1. Extract is 3-4 times slower than before update

Here is my angular.json config file

I tried debug with ndb and share my profile data with ng s --aot but it seems not work ndb crash ( disk is highly used...): cli

Environment

Angular CLI: 7.0.1
Node: 10.12.0
OS: win32 x64
Angular: 7.0.0
... common, compiler, compiler-cli, core, forms, http
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.10.1
@angular-devkit/build-angular     0.9.0-rc.3
@angular-devkit/build-optimizer   0.9.0-rc.3
@angular-devkit/build-webpack     0.9.0-rc.3
@angular-devkit/core              7.0.1
@angular-devkit/schematics        7.0.1
@angular/cdk                      6.4.7
@angular/cli                      7.0.1
@angular/material                 6.4.7
@ngtools/webpack                  7.0.0-rc.3
@schematics/angular               7.0.1
@schematics/update                0.10.1
rxjs                              6.3.3
typescript                        3.1.3
webpack                           4.19.1

Others: maybe related to : https://github.com/angular/angular/issues/26572 , https://github.com/angular/angular-cli/issues/12645

@filipesilva heya, sorry to make noise again if you consider this as angular compiler problem(as I think) close or move it please

elvisbegovic avatar Oct 19 '18 09:10 elvisbegovic

@dottodot off the top of my head I don't really think it's any external packages, but I might be wrong. If the problem ends up being related to third party libraries that would help, yes.

filipesilva avatar Oct 20 '18 09:10 filipesilva

@coryrylan you mentioned you were trying to get a repro, and I know that's not easy starting at a large application so I wanted to give you an approach that might help:

  • make a new git branch to keep debugging changes
  • set "outputHashing": "none", and "sourceMap": true, in angular.json
  • npm install -g source-map-explorer
  • make a new npm script that runs build and source-map-explorer (like "test": "ng build --prod && source-map-explorer dist/latest-project/main.js",)
  • try removing certain parts of the application and then running npm test, if @angular/compiler is still in the sourcemap then that part of the app is not relevant to the repro.

Starting from a full application, these are the parts I would try to remove in order:

  • comment out lazy routes, as they are individual compilation units
  • comment out non-lazy routes and delete their component imports
  • comment out NgModule imports
  • replace all templateUrl and template with template: '<div></div> to remove template compilation

This order should hopefully help eliminate big chunks of source code until the compiler stops appearing in the source-map-explorer visualization.

filipesilva avatar Oct 22 '18 09:10 filipesilva

Apologies for my repeat comments (now deleted). GitHub was having an outage and I repeated them by accident.

filipesilva avatar Oct 22 '18 11:10 filipesilva

@istiti and @crisbeto were kind enough to do some live debugging with me but we weren't able to get to the bottom of it.

Still managed to get some interesting insights though. In @istiti's project we could see that @angular/compiler was there, but went away when all the lazy routes were removed.

@crisbeto's project uses a custom webpack configuration and we removed everything until it was just a NgModule and an empty component but still saw @angular/compiler in the sourcemaps.

filipesilva avatar Oct 22 '18 12:10 filipesilva

I don't want add irrelevant information at this step but : I have 100+ lazy routes (loadChildren) Build with ALL routes > I have compiler.js Build without routes > I DON'T have compiler.js see sourcemaps graph

Build with only ONE loadChildren >I HAVE compiler.js see screenshot sourcemaps graph

@filipesilva

elvisbegovic avatar Oct 22 '18 12:10 elvisbegovic

@istiti I think that's actually relevant, in the context of how modules are optimized.

Can you try doing something please?

First make a build that has compiler in the sourcemaps graph, like the one with one loadchildren.

Then open node_modules/@angular/compiler/fesm5/compiler.js and find lines 1181 to 1210:

var BoundPropertyMapping = (_a = {},
    _a[4 /* Animation */] = 4 /* Animation */,
    _a[1 /* Attribute */] = 1 /* Attribute */,
    _a[2 /* Class */] = 2 /* Class */,
    _a[0 /* Property */] = 0 /* Property */,
    _a[3 /* Style */] = 3 /* Style */,
    _a);
/**
 * A binding for an element property (e.g. `[property]="expression"`) or an animation trigger (e.g.
 * `[@trigger]="stateExp"`)
 */
var BoundElementPropertyAst = /** @class */ (function () {
    function BoundElementPropertyAst(name, type, securityContext, value, unit, sourceSpan) {
        this.name = name;
        this.type = type;
        this.securityContext = securityContext;
        this.value = value;
        this.unit = unit;
        this.sourceSpan = sourceSpan;
        this.isAnimation = this.type === 4 /* Animation */;
    }
    BoundElementPropertyAst.fromBoundProperty = function (prop) {
        var type = BoundPropertyMapping[prop.type];
        return new BoundElementPropertyAst(prop.name, type, prop.securityContext, prop.value, prop.unit, prop.sourceSpan);
    };
    BoundElementPropertyAst.prototype.visit = function (visitor, context) {
        return visitor.visitElementProperty(this, context);
    };
    return BoundElementPropertyAst;
}());

And change them to:

/**
 * A binding for an element property (e.g. `[property]="expression"`) or an animation trigger (e.g.
 * `[@trigger]="stateExp"`)
 */
var BoundElementPropertyAst = /** @class */ (function () {

  var BoundPropertyMapping = (_a = {},
    _a[4 /* Animation */] = 4 /* Animation */,
    _a[1 /* Attribute */] = 1 /* Attribute */,
    _a[2 /* Class */] = 2 /* Class */,
    _a[0 /* Property */] = 0 /* Property */,
    _a[3 /* Style */] = 3 /* Style */,
    _a);


    function BoundElementPropertyAst(name, type, securityContext, value, unit, sourceSpan) {
        this.name = name;
        this.type = type;
        this.securityContext = securityContext;
        this.value = value;
        this.unit = unit;
        this.sourceSpan = sourceSpan;
        this.isAnimation = this.type === 4 /* Animation */;
    }
    BoundElementPropertyAst.fromBoundProperty = function (prop) {
        var type = BoundPropertyMapping[prop.type];
        return new BoundElementPropertyAst(prop.name, type, prop.securityContext, prop.value, prop.unit, prop.sourceSpan);
    };
    BoundElementPropertyAst.prototype.visit = function (visitor, context) {
        return visitor.visitElementProperty(this, context);
    };
    return BoundElementPropertyAst;
}());

(moving var BoundPropertyMapping inside BoundElementPropertyAst).

Then build again. Do you still get compiler in the sourcegraph? If you don't, do you see see increased build time overall?

filipesilva avatar Oct 22 '18 14:10 filipesilva

@filipesilva

both have compiler.js but size change a little bit

before replacing I get this graph and compiler.js in sourcmap is 201.85 KB

after replacing i get this graph and compiler.hs in sourcemap is 201.81 KB

elvisbegovic avatar Oct 22 '18 14:10 elvisbegovic

I can confirm that even without any routes the compiler.js is included in our bundle as well. (custom webpack config btw - for now I have found the workaround of declaring @angular/compiler as external in my webpack config)

dhardtke avatar Oct 22 '18 16:10 dhardtke

I am using a custom Webpack build (i.e. not using Angular CLI). After updating from Angular 6.1.7 to 7.0.0, and from @angular-devkit/build-optimizer 0.8.3 to 0.10.2, my AOT build now includes the compiler. Please let me know if I can help in some way!

stevethemacguy avatar Oct 22 '18 18:10 stevethemacguy

@stevethemacguy is your project open source or something that I could see?

For those using the CLI, could you try this please:

  • open ./node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js
  • find the following lines:
        resolve: {
            mainFields: [
                ...(wco.supportES2015 ? ['es2015'] : []),
               'browser', 'module', 'main'
            ]
        },
  • add 'esm5' before the others (so 'esm5', 'browser', 'module', 'main')
  • try building again

Does your build get faster and/or smaller?

filipesilva avatar Oct 22 '18 18:10 filipesilva

@filipesilva The code is not open source or publicly released. I removed the Angular router imports completely and the bundle still includes the compiler. Also, I'm using ES2015 (i.e. ES6) modules in my TS compiler config.

I have a separate, smaller CLI repo that I use for testing and Webpack Bundle Analyzer shows the compiler in the main bundle, but the file size difference between Angular 6 and 7 is negligible, so I'm not actually sure if the CLI repo is including the compiler or if it's just an issue with Webpack Bundle Analyzer displaying things differently.

stevethemacguy avatar Oct 22 '18 19:10 stevethemacguy

@filipesilva I tested adding 'esm5' to my CLI repo. The main bundle size increased from 567Kb to 694kb after adding the line. The build produced a new warning, but otherwise succeeded. Note: this is a stripped down repo and not my real app.

Before adding 'esm5': before adding esm5

before

After adding esm5: after adding esm5

after

stevethemacguy avatar Oct 22 '18 20:10 stevethemacguy

@filipesilva

I've also tried adding esm5 build time was about the same but I got a slight reduction in bundle size

1.59 MB before 1.55 MB after

also got the same error as above.

dottodot avatar Oct 22 '18 20:10 dottodot

Angular Compiler was also included in my production build after updating to Angular v7 and Angular CLI v7 screen shot 2018-10-23 at 1 31 27 pm

exequiel09 avatar Oct 23 '18 05:10 exequiel09

I managed to boil down my project to this that showcases the issue.

It has a single .ts file that looks like this:

import { enableProdMode } from '@angular/core';
console.log(enableProdMode);

crisbeto avatar Oct 23 '18 07:10 crisbeto

I've been having a look at @crisbeto's reproduction. I've reduced it a bit further (https://github.com/filipesilva/cli-12646-custom-config) and think I have something that can help some configurations.

If you are using a custom webpack configuration, make sure you have turned off the node builtins (see https://github.com/filipesilva/cli-12646-custom-config/commit/c9f28d0632a5da114c57d3ff6667b870734b5622).

This is something we do on the CLI since v6. Having them on can prevent @angular/core from being concatenated via https://webpack.js.org/plugins/module-concatenation-plugin/.

Doing this prevents most of @angular/core and @angular/compiler from being retained. But not all. Still investigating why some is still retained.

This advice has no effect on CLI projects since they already have it turned off.

filipesilva avatar Oct 23 '18 10:10 filipesilva

Hi all,

Just wanted to give another update. With a lot of help from @istiti and @crisbeto, I think I've identified what causes compiler to be included and builds to take more memory, time and be bigger.

This is related to https://github.com/angular/angular/issues/25645. In Angular v7, there is a direct dependency from @angular/core to @angular/compiler.

This should not be a problem because treeshaking should eliminate that dependency as it is not used anymore in the final code. The most advanced treeshaking optimizations are enabled through the joint work of Webpack's Module Concatenation, Terser/Uglify and Build Optimizer over a projects source code.

There are cases where module concatenation can be prevented, called bailouts. One such case is importing a module through require().

Specifically in this issue, whenever @angular/core is imported through require() anywhere in a project (including in dependencies), it will not be correctly optimized. Although this was still true of Angular v6, now with the new @angular/compiler import the consequences are greater and more code is retained.

More code being retained means bigger bundles, but also means that processing the bundles takes longer and needs more memory.

I've prepared two repos that show this:

  • without the CLI just importing a module that uses require: https://github.com/filipesilva/cli-12646-custom-config
  • with the CLI importing a library that uses require: https://github.com/filipesilva/cli-12646-cli-app

Now that we have identified the root cause of this problem, we are looking at solutions.

I really want to give a huge shoutout and thanks to both @istiti and @crisbeto. They helped a lot over the last few days by running tests, diagnostics and experiments on their code bases that lead to identifying the root cause.

Without that we still wouldn't have a reproduction, and without a reproduction there is little hope of fixing problems. Personally I believe making reproductions is the most important part of open source, because once a reproduction is identified anyone can try to work out a solution. But without a reproduction there's no hope of fixing truly a problem. So in cases where the reproduction is not trivial and projects in the wild are exhibiting issues, we really have to rely on help from those projects to figure out what's wrong.

filipesilva avatar Oct 23 '18 15:10 filipesilva

Proposal:

  • ng build --prod will check if @angular/compiler got retained in your optimized prod bundle
  • if so, it prints something like "We detected a de-optimization affecting your production bundle size, run ng build --prod --verbose=optimizations
  • This command uses information known to Webpack to find CJS imports that bring in @angular/core (could parse the stats file or add a new plugin after the graph is built)
  • It prints something that helps the user take action to file an issue against the right library which caused the de-optimization

alexeagle avatar Oct 23 '18 17:10 alexeagle

@filipesilva I see require("@angular/core") in a number of my dependencies (mostly in the included UMD files). Is there something I should do about that? I added "node: false" to the Webpack config, but it had no effect. If a more general solution is on it's way, then I can wait.

@dhardtke, How did you mark the compiler as external? I was able to exclude the compiler using the following, but the build fails:

externals: { '@angular/compiler': '', },

stevethemacguy avatar Oct 23 '18 19:10 stevethemacguy

Same here, I too have a bunch of dependencies that have require() statements in them (e.g. https://github.com/tjoskar/ng-lazyload-image)...

@stevethemacguy I did this:

externals: {
    "@angular/compiler": JSON.stringify("@angular/compiler")
},

which essentially tells webpack to replace an import of "@angular/compiler" with a string. You might also use JSON.stringify("")

dhardtke avatar Oct 23 '18 19:10 dhardtke