Skip to content

Commit 34a4435

Browse files
committed
perf: cache fileExists, directoryExists result
1 parent 146c350 commit 34a4435

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

packages/vue-language-server/src/projects.ts

+33-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ export function createProjects(
3131
uri: string,
3232
time: number,
3333
} | undefined;
34+
const fileExistsCache = shared.createPathMap<boolean>();
35+
const directoryExistsCache = shared.createPathMap<boolean>();
36+
const sys: ts.System = {
37+
...ts.sys,
38+
fileExists(path: string) {
39+
if (!fileExistsCache.fsPathHas(path)) {
40+
fileExistsCache.fsPathSet(path, ts.sys.fileExists(path));
41+
}
42+
return fileExistsCache.fsPathGet(path)!;
43+
},
44+
directoryExists(path: string) {
45+
if (!directoryExistsCache.fsPathHas(path)) {
46+
directoryExistsCache.fsPathSet(path, ts.sys.directoryExists(path));
47+
}
48+
return directoryExistsCache.fsPathGet(path)!;
49+
},
50+
};
3451

3552
const workspaces = new Map<string, ReturnType<typeof createWorkspace>>();
3653

@@ -40,6 +57,7 @@ export function createProjects(
4057
languageConfigs,
4158
rootPath,
4259
ts,
60+
sys,
4361
tsLocalized,
4462
options,
4563
documents,
@@ -77,6 +95,15 @@ export function createProjects(
7795
});
7896
connection.onDidChangeWatchedFiles(async handler => {
7997

98+
for (const change of handler.changes) {
99+
if (change.type === vscode.FileChangeType.Created) {
100+
fileExistsCache.uriSet(change.uri, true);
101+
}
102+
else if (change.type === vscode.FileChangeType.Deleted) {
103+
fileExistsCache.uriSet(change.uri, false);
104+
}
105+
}
106+
80107
const tsConfigChanges: vscode.FileEvent[] = [];
81108
const scriptChanges: vscode.FileEvent[] = [];
82109

@@ -233,6 +260,7 @@ function createWorkspace(
233260
languageConfigs: LanguageConfigs,
234261
rootPath: string,
235262
ts: typeof import('typescript/lib/tsserverlibrary'),
263+
sys: ts.System,
236264
tsLocalized: ts.MapLike<string> | undefined,
237265
options: shared.ServerInitializationOptions,
238266
documents: vscode.TextDocuments<TextDocument>,
@@ -241,12 +269,12 @@ function createWorkspace(
241269
getInferredCompilerOptions: () => Promise<ts.CompilerOptions>,
242270
) {
243271

244-
const rootTsConfigs = ts.sys.readDirectory(rootPath, rootTsConfigNames, undefined, ['**/*']);
272+
const rootTsConfigs = sys.readDirectory(rootPath, rootTsConfigNames, undefined, ['**/*']);
245273
const projects = shared.createPathMap<Project>();
246274
let inferredProject: Project | undefined;
247275

248276
const getRootPath = () => rootPath;
249-
const workspaceSys = ts.sys.getCurrentDirectory() === rootPath ? ts.sys : new Proxy(ts.sys, {
277+
const workspaceSys = sys.getCurrentDirectory() === rootPath ? sys : new Proxy(sys, {
250278
get(target, prop) {
251279
const fn = target[prop as keyof typeof target];
252280
if (typeof fn === 'function') {
@@ -380,13 +408,13 @@ function createWorkspace(
380408
let tsConfigPath = projectReference.path;
381409

382410
// fix https://github.com/johnsoncodehk/volar/issues/712
383-
if (!ts.sys.fileExists(tsConfigPath) && ts.sys.directoryExists(tsConfigPath)) {
411+
if (!sys.fileExists(tsConfigPath) && sys.directoryExists(tsConfigPath)) {
384412
const newTsConfigPath = path.join(tsConfigPath, 'tsconfig.json');
385413
const newJsConfigPath = path.join(tsConfigPath, 'jsconfig.json');
386-
if (ts.sys.fileExists(newTsConfigPath)) {
414+
if (sys.fileExists(newTsConfigPath)) {
387415
tsConfigPath = newTsConfigPath;
388416
}
389-
else if (ts.sys.fileExists(newJsConfigPath)) {
417+
else if (sys.fileExists(newJsConfigPath)) {
390418
tsConfigPath = newJsConfigPath;
391419
}
392420
}

packages/vue-language-service/src/languageService.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,6 @@ export function createLanguageService(
270270
const failedLookupLocations: string[] = (resolveResult as any).failedLookupLocations;
271271
const dirs = new Set<string>();
272272

273-
const fileExists = vueLsHost.fileExists ?? ts.sys.fileExists;
274-
const directoryExists = vueLsHost.directoryExists ?? ts.sys.directoryExists;
275-
276273
for (const failed of failedLookupLocations) {
277274
let path = failed;
278275
const fileName = upath.basename(path);
@@ -285,12 +282,12 @@ export function createLanguageService(
285282
else {
286283
continue;
287284
}
288-
if (fileExists(path)) {
285+
if (vueLsHost.fileExists(path)) {
289286
return isUri ? shared.fsPathToUri(path) : path;
290287
}
291288
}
292289
for (const dir of dirs) {
293-
if (directoryExists(dir)) {
290+
if (vueLsHost.directoryExists?.(dir) ?? true) {
294291
return isUri ? shared.fsPathToUri(dir) : dir;
295292
}
296293
}

0 commit comments

Comments
 (0)