fix(ivy): TestBed rewrite to avoid unnecessary recompilations (#29483)

Prior to this change, Ivy version of TestBed was not designed to support the logic to avoid recompilations - most of the Components/Directives/Pipes were recompiled for each test, even if there were no overrides defined for a given Type. Additional checks to avoid recompilation were introduced in one of the previous commits (0244a2433e), but there were still some corner cases that required attention. In order to support the necessary logic better, Ivy TestBed was rewritten/refactored. Main results of this rewrite are:

* no recompilation for Components/Directives/Pipes without overrides
* the logic to restore state between tests (isolate tests) was improved
* transitive scopes calculation no longer performs recompilation (it works with compiled defs)

As a result of these changes we see reduction in memory consumption (3.5-4x improvement) and pefromance increase (4-4.5x improvement).

PR Close #29483
This commit is contained in:
Andrew Kushnir
2019-03-20 17:58:20 -07:00
committed by Miško Hevery
parent fea2a0f2ac
commit 309ffe7e16
6 changed files with 801 additions and 605 deletions

View File

@ -16,7 +16,11 @@ const reflection = new ReflectionCapabilities();
/**
* Base interface to resolve `@Component`, `@Directive`, `@Pipe` and `@NgModule`.
*/
export interface Resolver<T> { resolve(type: Type<any>): T|null; }
export interface Resolver<T> {
addOverride(type: Type<any>, override: MetadataOverride<T>): void;
setOverrides(overrides: Array<[Type<any>, MetadataOverride<T>]>): void;
resolve(type: Type<any>): T|null;
}
/**
* Allows to override ivy metadata for tests (via the `TestBed`).
@ -27,13 +31,16 @@ abstract class OverrideResolver<T> implements Resolver<T> {
abstract get type(): any;
addOverride(type: Type<any>, override: MetadataOverride<T>) {
const overrides = this.overrides.get(type) || [];
overrides.push(override);
this.overrides.set(type, overrides);
this.resolved.delete(type);
}
setOverrides(overrides: Array<[Type<any>, MetadataOverride<T>]>) {
this.overrides.clear();
overrides.forEach(([type, override]) => {
const overrides = this.overrides.get(type) || [];
overrides.push(override);
this.overrides.set(type, overrides);
});
overrides.forEach(([type, override]) => { this.addOverride(type, override); });
}
getAnnotation(type: Type<any>): T|null {