Skip to content

size regression due to host view factories generated for non-entryComponents #19857

Closed

Description

@IgorMinar

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

in v4 the AOT compiler generated host view factories only for entryComponents in v5.0.0.rc2 we generate them even for components that are not marked as entryComponents.

Example in aio is SearchResultsComponent, which is not marked as entryComponent. When upgrading from 5.0.0-beta.3, I noticed that we now started generating View_SearchResultsComponent_Host_0 for this component. And we do it in a way that tightly couples it with the rest of the component factories, so it can't be eliminated.

Relevant snippets of the generated code:

function View_SearchResultsComponent_Host_0(_l) {
  return viewDef(0, [ (_l()(), elementDef(0, 0, null, null, 1, "aio-search-results", [], null, null, null, View_SearchResultsComponent_0, RenderType_SearchResultsComponent)), directiveDef(1, 573440, null, 0, search_results_component_SearchResultsComponent, [], null, null) ], null, null);
}

styles_FileNotFoundSearchComponent = (createComponentFactory("aio-search-results", search_results_component_SearchResultsComponent, View_SearchResultsComponent_Host_0, {
  searchResults: "searchResults"
}

RenderType_FileNotFoundSearchComponent = createRendererType2({
  encapsulation: 2,
  styles: styles_FileNotFoundSearchComponent,
  data: {}
})

Is this expected? My understanding is that the host factories are only needed for entryComponents.

I can't estimate how significant this size regression is for aio because I'm unfortunately updating various dependencies. Due to bugs and api incompatibilities can't isolate the change just to the change of the compiler. Overall I'm looking for about 30kb size increase which might or might not contain other size regressions.

Activity

  1. added
    area: coreIssues related to the framework runtime
    regressionIndicates than the issue relates to something that worked in a previous version
    on Oct 22, 2017
  2. maxkoretskyi commented on Oct 23, 2017

    @maxkoretskyi

    I reported this behavior even for v4 in this issue

  3. deleted a comment from skatterwe on Oct 23, 2017
  4. IgorMinar commented on Oct 23, 2017

    @IgorMinar
    ContributorAuthor

    @MaximusK in my case the issue occurs with AOT compilation

  5. skatterwe commented on Oct 23, 2017

    @skatterwe
  6. alexzuza commented on Oct 23, 2017

    @alexzuza
    Contributor

    I tried it with 4.3.6 and it also generated Host factories for non-entryComponents

    Here's piece of the corresponding code:

    https://github.com/angular/angular/blob/4.3.x/packages/compiler/src/aot/compiler.ts#L303

    AFAIK only Compiler in JIT mode doesn't generate such factories for non-entryComponents

    https://github.com/angular/angular/blob/4.3.x/packages/compiler/src/jit/compiler.ts#L174-L199

    Jit compiler only generates host factories if:

    • we call compiler._compileModuleAndAllComponents
    • component is part of component's entryComponents metadata
    • component is part of module's entryComponents metadata
    • component is part of module's bootstrap metadata

    So i think it was intended to generate host factories for all component in AOT but i don't know why...

  7. tbosch commented on Oct 23, 2017

    @tbosch
    Contributor

    @IgorMinar yes, AOT always used to generate host view factories for all components, as entryComponents could be introduced by a downstream dependency that is compiled in a separate compilation unit.

  8. tbosch commented on Oct 23, 2017

    @tbosch
    Contributor

    The generated code here looks really wrong (it is after uglify and webpack though). The styles don't use the component factory, and the component factory should have been stored in a top level variable.

  9. tbosch commented on Oct 23, 2017

    @tbosch
    Contributor

    @chuckjaz Steps to reproduce the incorrect code above (from @IgorMinar):

    1. clone build(aio): update to @angular/core@5.0.0-rc.5 #19702
    2. cd aio
    3. yarn install (edited)
    4. then you need to go and patch cli to disable uglify mangling
    5. this is what I usually use:
                mangle: false,
                // compress: uglifyCompressOptions,
                compress: Object.assign(uglifyCompressOptions, {sequences: false}),
                // output: { ascii_only: true },
                output: { ascii_only: true, beautify: true },
                sourceMap: buildOptions.sourcemaps,
                comments: "all"
                // comments: false
    
    
    1. it goes into aio/node_modules/@angular/cli/models/webpack-configs/production.js
    2. find the uglify section and replace the config with the above
    3. then build using:
    4. ng build --target=production --environment=stable -sm --build-optimizer
    5. and inspect less dist/main.*.bundle.js
  10. IgorMinar commented on Oct 23, 2017

    @IgorMinar
    ContributorAuthor

    you can also disable the uglify completely by commenting it out in the webpack config. that will show you what we feed into uglify.

  11. self-assigned this
    on Oct 23, 2017
  12. chuckjaz commented on Oct 23, 2017

    @chuckjaz
    Contributor

    The code above is incorrect. The styles_FileNotFoundSearchComponent should be [] and, if it had it correct value, would have been tree-shaken.

    Removing uglify from the configuration, as described above by @IgorMinar, shows that the ngc is emitting the correct code. We also bashed the writeFile callback of the compiler_host.js in ng to save the intermediate output and the ngc output is correct.

    It looks like the code gets mangled in the uglify step.

    We tried executing uglifyjs directly, using several versions, but have not been able to duplicate the bug nor duplicate the output format so something might be going wrong in the uglify plugin or other code prior to uglify.

    @hansl Is taking over the investigation to narrow down where the problem is in the webpack chain.

  13. 4 remaining items

  14. IgorMinar commented on Oct 24, 2017

    @IgorMinar
    ContributorAuthor

    I got to the bottom of this!!!

    if I disable uglify I see this snippet of code in the bundle

    function View_SearchResultsComponent_Host_0(_l) { return viewDef(0, [(_l()(), elementDef(0, 0, null, null, 1, "aio-search-results", [], null, null, null, View_SearchResultsComponent_0, RenderType_SearchResultsComponent)), directiveDef(1, 573440, null, 0, search_results_component_SearchResultsComponent, [], null, null)], null, null); }
    
    var SearchResultsComponentNgFactory = createComponentFactory("aio-search-results", search_results_component_SearchResultsComponent, View_SearchResultsComponent_Host_0, { searchResults: "searchResults" }, { resultSelected: "resultSelected" }, []);

    The thing to notice is this line:

    var SearchResultsComponentNgFactory = createComponentFactory("aio-search-results", 
    

    The generated code has a top level function call createComponentFactory (and a few others) that are no longer being marked as @PURE, so uglify considers them as calls with potential side-effect and retains all of the code. (the code then gets inlined in various weird ways to decrease the size slightly, but it's essentially all retained.

    In the past we used to have special code in build-optimizer that would look for a few handpicked function calls in the generated code and mark them as pure. We however improved other bits and managed to inline core modules together with ng factories, which now means that we no longer can identify these calls via angular_core webpack module import, but those no longer exist, so we don't match these functions any more. This means that all of the generated code is fully retained even with build-optimizer turned on.

    @filipesilva created a patch for build-optimizer that uses different approach to marking these functions as pure - we assume that all of the generated code has no non-local side-effects, so we prefix all the top level function calls with pure.

    I tested this version of the optimization, and indeed all the generated code got marked as pure and removed by uglify if not retained by the rest of the app. This resulted in 35KB savings for aio.

    @filipesilva is working on getting the patch into a release of build-optimizer now.

    @tbosch @chuckjaz Now that we understand this, my question is: should we consider emitting the pure annotation directly from ngc for all generated code? That way people not using build-optimizer but using uglify would benefit.

    I don't like generating uglify-specific instructions in our code, but it makes a huge impact on size and doesn't harm anything else. What do you think?

  15. filipesilva commented on Oct 24, 2017

    @filipesilva
    Contributor

    angular/devkit#236 should address @IgorMinar's findings.

  16. hansl commented on Oct 24, 2017

    @hansl
    Contributor

    @IgorMinar The fix in build-optimizer has been released 20 minutes ago.

  17. tbosch commented on Oct 24, 2017

    @tbosch
    Contributor

    @IgorMinar If users are not using the build optimizer, a lot of other things are retained too, right?. So I would rather not add the dependency of our codegen to uglify specific comments but rather recommend our build optimizer in a stronger way.

  18. IgorMinar commented on Oct 25, 2017

    @IgorMinar
    ContributorAuthor
  19. IgorMinar commented on Oct 25, 2017

    @IgorMinar
    ContributorAuthor

    released as @angular/devkit@0.0.31

  20. angular-automatic-lock-bot commented on Sep 12, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  21. locked and limited conversation to collaborators on Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

regressionIndicates than the issue relates to something that worked in a previous version

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions