Skip to content

Refactor export map cache to not store transient symbols #44816

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

Merged
merged 10 commits into from
Jul 6, 2021
95 changes: 95 additions & 0 deletions src/server/moduleSpecifierCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*@internal*/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved verbatim from utilities.ts

namespace ts.server {
export interface ModuleSpecifierResolutionCacheHost {
watchNodeModulesForPackageJsonChanges(directoryPath: string): FileWatcher;
}

export function createModuleSpecifierCache(host: ModuleSpecifierResolutionCacheHost): ModuleSpecifierCache {
let containedNodeModulesWatchers: ESMap<string, FileWatcher> | undefined;
let cache: ESMap<Path, ResolvedModuleSpecifierInfo> | undefined;
let currentKey: string | undefined;
const result: ModuleSpecifierCache = {
get(fromFileName, toFileName, preferences) {
if (!cache || currentKey !== key(fromFileName, preferences)) return undefined;
return cache.get(toFileName);
},
set(fromFileName, toFileName, preferences, modulePaths, moduleSpecifiers) {
ensureCache(fromFileName, preferences).set(toFileName, createInfo(modulePaths, moduleSpecifiers, /*isAutoImportable*/ true));

// If any module specifiers were generated based off paths in node_modules,
// a package.json file in that package was read and is an input to the cached.
// Instead of watching each individual package.json file, set up a wildcard
// directory watcher for any node_modules referenced and clear the cache when
// it sees any changes.
if (moduleSpecifiers) {
for (const p of modulePaths) {
if (p.isInNodeModules) {
// No trailing slash
const nodeModulesPath = p.path.substring(0, p.path.indexOf(nodeModulesPathPart) + nodeModulesPathPart.length - 1);
if (!containedNodeModulesWatchers?.has(nodeModulesPath)) {
(containedNodeModulesWatchers ||= new Map()).set(
nodeModulesPath,
host.watchNodeModulesForPackageJsonChanges(nodeModulesPath),
);
}
}
}
}
},
setModulePaths(fromFileName, toFileName, preferences, modulePaths) {
const cache = ensureCache(fromFileName, preferences);
const info = cache.get(toFileName);
if (info) {
info.modulePaths = modulePaths;
}
else {
cache.set(toFileName, createInfo(modulePaths, /*moduleSpecifiers*/ undefined, /*isAutoImportable*/ undefined));
}
},
setIsAutoImportable(fromFileName, toFileName, preferences, isAutoImportable) {
const cache = ensureCache(fromFileName, preferences);
const info = cache.get(toFileName);
if (info) {
info.isAutoImportable = isAutoImportable;
}
else {
cache.set(toFileName, createInfo(/*modulePaths*/ undefined, /*moduleSpecifiers*/ undefined, isAutoImportable));
}
},
clear() {
containedNodeModulesWatchers?.forEach(watcher => watcher.close());
cache?.clear();
containedNodeModulesWatchers?.clear();
currentKey = undefined;
},
count() {
return cache ? cache.size : 0;
}
};
if (Debug.isDebugging) {
Object.defineProperty(result, "__cache", { get: () => cache });
}
return result;

function ensureCache(fromFileName: Path, preferences: UserPreferences) {
const newKey = key(fromFileName, preferences);
if (cache && (currentKey !== newKey)) {
result.clear();
}
currentKey = newKey;
return cache ||= new Map();
}

function key(fromFileName: Path, preferences: UserPreferences) {
return `${fromFileName},${preferences.importModuleSpecifierEnding},${preferences.importModuleSpecifierPreference}`;
}

function createInfo(
modulePaths: readonly ModulePath[] | undefined,
moduleSpecifiers: readonly string[] | undefined,
isAutoImportable: boolean | undefined,
): ResolvedModuleSpecifierInfo {
return { modulePaths, moduleSpecifiers, isAutoImportable };
}
}
}
23 changes: 15 additions & 8 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ namespace ts.server {
public readonly getCanonicalFileName: GetCanonicalFileName;

/*@internal*/
private exportMapCache = createExportMapCache();
private exportMapCache: ExportInfoMap | undefined;
/*@internal*/
private changedFilesForExportMapCache: Set<Path> | undefined;
/*@internal*/
Expand Down Expand Up @@ -793,6 +793,7 @@ namespace ts.server {
this.cachedUnresolvedImportsPerFile = undefined!;
this.moduleSpecifierCache = undefined!;
this.directoryStructureHost = undefined!;
this.exportMapCache = undefined;
this.projectErrors = undefined;

// Clean up file watchers waiting for missing files
Expand Down Expand Up @@ -990,7 +991,7 @@ namespace ts.server {
/*@internal*/
markFileAsDirty(changedFile: Path) {
this.markAsDirty();
if (!this.exportMapCache.isEmpty()) {
if (this.exportMapCache && !this.exportMapCache.isEmpty()) {
(this.changedFilesForExportMapCache ||= new Set()).add(changedFile);
}
}
Expand Down Expand Up @@ -1192,7 +1193,8 @@ namespace ts.server {
}
}

if (!this.exportMapCache.isEmpty()) {
if (this.exportMapCache && !this.exportMapCache.isEmpty()) {
this.exportMapCache.releaseSymbols();
if (this.hasAddedorRemovedFiles || oldProgram && !this.program!.structureIsReused) {
this.exportMapCache.clear();
}
Expand All @@ -1201,10 +1203,10 @@ namespace ts.server {
const oldSourceFile = oldProgram.getSourceFileByPath(fileName);
const sourceFile = this.program!.getSourceFileByPath(fileName);
if (!oldSourceFile || !sourceFile) {
this.exportMapCache.clear();
this.exportMapCache!.clear();
return true;
}
return this.exportMapCache.onFileChanged(oldSourceFile, sourceFile, !!this.getTypeAcquisition().enable);
return this.exportMapCache!.onFileChanged(oldSourceFile, sourceFile, !!this.getTypeAcquisition().enable);
});
}
}
Expand Down Expand Up @@ -1666,8 +1668,13 @@ namespace ts.server {
}

/*@internal*/
getExportMapCache() {
return this.exportMapCache;
getCachedExportInfoMap() {
return this.exportMapCache ||= createCacheableExportInfoMap(this);
}

/*@internal*/
clearCachedExportInfoMap() {
this.exportMapCache?.clear();
}

/*@internal*/
Expand Down Expand Up @@ -2017,7 +2024,7 @@ namespace ts.server {
const oldProgram = this.getCurrentProgram();
const hasSameSetOfFiles = super.updateGraph();
if (oldProgram && oldProgram !== this.getCurrentProgram()) {
this.hostProject.getExportMapCache().clear();
this.hostProject.clearCachedExportInfoMap();
}
return hasSameSetOfFiles;
}
Expand Down
1 change: 1 addition & 0 deletions src/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"typingsCache.ts",
"project.ts",
"editorServices.ts",
"moduleSpecifierCache.ts",
"packageJsonCache.ts",
"session.ts",
"scriptVersionCache.ts"
Expand Down
Loading