Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(ngcc): performance improvements #38840

Closed
wants to merge 8 commits into from
Closed

Commits on Sep 15, 2020

  1. perf(ngcc): introduce cache for sharing data across entry-points

    ngcc creates typically two `ts.Program` instances for each entry-point,
    one for processing sources and another one for processing the typings.
    The creation of these programs is somewhat expensive, as it concerns
    module resolution and parsing of source files.
    
    This commit implements several layers of caching to optimize the
    creation of programs:
    
    1. A shared module resolution cache across all entry-points within a
       single invocation of ngcc. Both the sources and typings program
       benefit from this cache.
    2. Sharing the parsed `ts.SourceFile` for a single entry-point between
       the sources and typings program.
    3. Sharing parsed `ts.SourceFile`s of TypeScript's default libraries
       across all entry-points within a single invocation. Some of these
       default library typings are large and therefore expensive to parse,
       so sharing the parsed source files across all entry-points offers
       a significant performance improvement.
    
    Using a bare CLI app created using `ng new` + `ng add @angular/material`,
    the above changes offer a 3-4x improvement in ngcc's processing time
    when running synchronously and ~2x improvement for asynchronous runs.
    JoostK committed Sep 15, 2020
    Copy the full SHA
    361db00 View commit details
    Browse the repository at this point in the history
  2. Copy the full SHA
    3921763 View commit details
    Browse the repository at this point in the history
  3. Copy the full SHA
    caacb88 View commit details
    Browse the repository at this point in the history
  4. Copy the full SHA
    d96393a View commit details
    Browse the repository at this point in the history
  5. Copy the full SHA
    8e91377 View commit details
    Browse the repository at this point in the history
  6. perf(ngcc): reduce maximum worker count

    Recent optimizations to ngcc have significantly reduced the total time
    it takes to process `node_modules`, to such extend that sharding across
    multiple processes has become less effective. Previously, running
    ngcc asynchronously would allow for up to 8 workers to be allocated,
    however these workers have to repeat work that could otherwise be shared.
    Because ngcc is now able to reuse more shared computations, the overhead
    of multiple workers is increased and therefore becomes less effective.
    As an additional benefit, having fewer workers requires less memory and
    less startup time.
    
    To give an idea, using the following test setup:
    
    ```bash
    npx @angular/cli new perf-test
    cd perf-test
    yarn ng add @angular/material
    ./node_modules/.bin/ngcc --properties es2015 module main \
      --first-only --create-ivy-entry-points
    ```
    
    We observe the following figures on CI:
    
    |                   | 10.1.1    | PR angular#38840 |
    | ----------------- | --------- | --------- |
    | Sync              | 85s       | 25s       |
    | Async (8 workers) | 22s       | 16s       |
    | Async (4 workers) | -         | 11s       |
    
    In addition to changing the default number of workers, ngcc will now
    use the environment variable `NGCC_MAX_WORKERS` that may be configured
    to either reduce or increase the number of workers.
    JoostK committed Sep 15, 2020
    Copy the full SHA
    ad2c5c5 View commit details
    Browse the repository at this point in the history
  7. Copy the full SHA
    a90270a View commit details
    Browse the repository at this point in the history
  8. test(ngcc): load standard files only once

    In the integration test suite of ngcc, we load a set of files from
    `node_modules` into memory. This includes the `typescript` package and
    `@angular` scoped packages, which account for a large number of large
    files that needs to be loaded from disk. This commit moves this work
    to the top-level, such that it doesn't have to be repeated in all tests.
    JoostK committed Sep 15, 2020
    Copy the full SHA
    8bf1128 View commit details
    Browse the repository at this point in the history