perf(core): avoid recursive scope recalculation when TestBed.overrideModule is used (#35454)

Currently if TestBed detects that TestBed.overrideModule was used for module X, transitive scopes are recalculated recursively for all modules that X imports and previously calculated data (stored in cache) is ignored. This behavior was introduced in https://github.com/angular/angular/pull/33787 to fix stale transitive scopes issue (cache was not updated if module overrides are present).

The perf issue comes from a "diamond" problem, where module X is overridden which imports modules A and B, which both import module C. Under previous logic, module C gets its transitive deps recomputed multiple times, during the recompute for both A and B. For deep graphs and big common/shared modules this can be super costly.

This commit updates the logic to recalculate ransitive scopes for the overridden module, while keeping previously calculated scopes of other modules untouched.

PR Close #35454
This commit is contained in:
Andrew Kushnir
2020-02-14 09:57:21 -08:00
committed by Miško Hevery
parent c414f45ddf
commit 0a1a989fa9
3 changed files with 27 additions and 24 deletions

View File

@ -355,8 +355,11 @@ export class R3TestBedCompiler {
// are present, always re-calculate transitive scopes to have the most up-to-date
// information available. The `moduleToScope` map avoids repeated re-calculation of
// scopes for the same module.
const forceRecalc = !isTestingModule && this.hasModuleOverrides;
moduleToScope.set(moduleType, transitiveScopesFor(realType, forceRecalc));
if (!isTestingModule && this.hasModuleOverrides) {
this.storeFieldOfDefOnType(moduleType as any, NG_MOD_DEF, 'transitiveCompileScopes');
(moduleType as any)[NG_MOD_DEF].transitiveCompileScopes = null;
}
moduleToScope.set(moduleType, transitiveScopesFor(realType));
}
return moduleToScope.get(moduleType) !;
};