Skip to content

Commit

Permalink
perf: cache fileExists, directoryExists result
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jul 25, 2022
1 parent 146c350 commit 34a4435
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
38 changes: 33 additions & 5 deletions packages/vue-language-server/src/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ export function createProjects(
uri: string,
time: number,
} | undefined;
const fileExistsCache = shared.createPathMap<boolean>();
const directoryExistsCache = shared.createPathMap<boolean>();
const sys: ts.System = {
...ts.sys,
fileExists(path: string) {
if (!fileExistsCache.fsPathHas(path)) {
fileExistsCache.fsPathSet(path, ts.sys.fileExists(path));
}
return fileExistsCache.fsPathGet(path)!;
},
directoryExists(path: string) {
if (!directoryExistsCache.fsPathHas(path)) {
directoryExistsCache.fsPathSet(path, ts.sys.directoryExists(path));
}
return directoryExistsCache.fsPathGet(path)!;
},
};

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

Expand All @@ -40,6 +57,7 @@ export function createProjects(
languageConfigs,
rootPath,
ts,
sys,
tsLocalized,
options,
documents,
Expand Down Expand Up @@ -77,6 +95,15 @@ export function createProjects(
});
connection.onDidChangeWatchedFiles(async handler => {

for (const change of handler.changes) {
if (change.type === vscode.FileChangeType.Created) {
fileExistsCache.uriSet(change.uri, true);
}
else if (change.type === vscode.FileChangeType.Deleted) {
fileExistsCache.uriSet(change.uri, false);
}
}

const tsConfigChanges: vscode.FileEvent[] = [];
const scriptChanges: vscode.FileEvent[] = [];

Expand Down Expand Up @@ -233,6 +260,7 @@ function createWorkspace(
languageConfigs: LanguageConfigs,
rootPath: string,
ts: typeof import('typescript/lib/tsserverlibrary'),
sys: ts.System,
tsLocalized: ts.MapLike<string> | undefined,
options: shared.ServerInitializationOptions,
documents: vscode.TextDocuments<TextDocument>,
Expand All @@ -241,12 +269,12 @@ function createWorkspace(
getInferredCompilerOptions: () => Promise<ts.CompilerOptions>,
) {

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

const getRootPath = () => rootPath;
const workspaceSys = ts.sys.getCurrentDirectory() === rootPath ? ts.sys : new Proxy(ts.sys, {
const workspaceSys = sys.getCurrentDirectory() === rootPath ? sys : new Proxy(sys, {
get(target, prop) {
const fn = target[prop as keyof typeof target];
if (typeof fn === 'function') {
Expand Down Expand Up @@ -380,13 +408,13 @@ function createWorkspace(
let tsConfigPath = projectReference.path;

// fix https://github.com/johnsoncodehk/volar/issues/712
if (!ts.sys.fileExists(tsConfigPath) && ts.sys.directoryExists(tsConfigPath)) {
if (!sys.fileExists(tsConfigPath) && sys.directoryExists(tsConfigPath)) {
const newTsConfigPath = path.join(tsConfigPath, 'tsconfig.json');
const newJsConfigPath = path.join(tsConfigPath, 'jsconfig.json');
if (ts.sys.fileExists(newTsConfigPath)) {
if (sys.fileExists(newTsConfigPath)) {
tsConfigPath = newTsConfigPath;
}
else if (ts.sys.fileExists(newJsConfigPath)) {
else if (sys.fileExists(newJsConfigPath)) {
tsConfigPath = newJsConfigPath;
}
}
Expand Down
7 changes: 2 additions & 5 deletions packages/vue-language-service/src/languageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,6 @@ export function createLanguageService(
const failedLookupLocations: string[] = (resolveResult as any).failedLookupLocations;
const dirs = new Set<string>();

const fileExists = vueLsHost.fileExists ?? ts.sys.fileExists;
const directoryExists = vueLsHost.directoryExists ?? ts.sys.directoryExists;

for (const failed of failedLookupLocations) {
let path = failed;
const fileName = upath.basename(path);
Expand All @@ -285,12 +282,12 @@ export function createLanguageService(
else {
continue;
}
if (fileExists(path)) {
if (vueLsHost.fileExists(path)) {
return isUri ? shared.fsPathToUri(path) : path;
}
}
for (const dir of dirs) {
if (directoryExists(dir)) {
if (vueLsHost.directoryExists?.(dir) ?? true) {
return isUri ? shared.fsPathToUri(dir) : dir;
}
}
Expand Down

0 comments on commit 34a4435

Please sign in to comment.