fix(language-service): create StaticReflector once only (#32543)
The creation of StaticReflector in createMetadataResolver() is a very expensive operation because it involves numerous module resolutions. To make matter worse, since the API of the Reflector does not provide the ability to invalidate its internal caches, it has to be destroyed and recreated on *every* program change. This has a HUGE impact on performance. This PR fixes this problem by carefully invalidating all StaticSymbols in a file that has changed, thereby reducing the overhead of recomputation on program change. PR Close #32543
This commit is contained in:
@ -51,22 +51,24 @@ describe('TypeScriptServiceHost', () => {
|
||||
expect(analyzedModules.files.length).toBe(0);
|
||||
expect(analyzedModules.ngModules.length).toBe(0);
|
||||
expect(analyzedModules.ngModuleByPipeOrDirective.size).toBe(0);
|
||||
expect(analyzedModules.symbolsMissingModule).toBeUndefined();
|
||||
expect(analyzedModules.symbolsMissingModule).toEqual([]);
|
||||
});
|
||||
|
||||
it('should clear the caches if program changes', () => {
|
||||
it('should clear the caches if new script is added', () => {
|
||||
// First create a TypescriptHost with empty script names
|
||||
const tsLSHost = new MockTypescriptHost([]);
|
||||
const tsLS = ts.createLanguageService(tsLSHost);
|
||||
const ngLSHost = new TypeScriptServiceHost(tsLSHost, tsLS);
|
||||
expect(ngLSHost.getAnalyzedModules().ngModules).toEqual([]);
|
||||
const oldModules = ngLSHost.getAnalyzedModules();
|
||||
expect(oldModules.ngModules).toEqual([]);
|
||||
// Now add a script, this would change the program
|
||||
const fileName = '/app/main.ts';
|
||||
const content = tsLSHost.readFile(fileName) !;
|
||||
tsLSHost.addScript(fileName, content);
|
||||
// If the caches are not cleared, we would get back an empty array.
|
||||
// But if the caches are cleared then the analyzed modules will be non-empty.
|
||||
expect(ngLSHost.getAnalyzedModules().ngModules.length).not.toEqual(0);
|
||||
const newModules = ngLSHost.getAnalyzedModules();
|
||||
expect(newModules.ngModules.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('should throw if getSourceFile is called on non-TS file', () => {
|
||||
|
Reference in New Issue
Block a user