Skip to content

Commit d7d8bfb

Browse files
committed
perf(language-server): memoize possibly heavy IO utils
1 parent 169da71 commit d7d8bfb

File tree

4 files changed

+42
-40
lines changed

4 files changed

+42
-40
lines changed

server/package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"dependencies": {
3333
"chokidar": "^3.5.1",
34+
"memize": "^2.1.0",
3435
"vscode-jsonrpc": "^8.0.1",
3536
"vscode-languageserver": "^8.0.1",
3637
"vscode-languageserver-protocol": "^3.17.1"

server/src/server.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,6 @@ let codeActionsFromDiagnostics: codeActions.filesCodeActions = {};
7474
// will be properly defined later depending on the mode (stdio/node-rpc)
7575
let send: (msg: p.Message) => void = (_) => {};
7676

77-
let findRescriptBinary = (projectRootPath: p.DocumentUri | null) =>
78-
config.extensionConfiguration.binaryPath == null
79-
? lookup.findFilePathFromProjectRoot(
80-
projectRootPath,
81-
path.join(c.nodeModulesBinDir, c.rescriptBinName)
82-
)
83-
: utils.findBinary(
84-
config.extensionConfiguration.binaryPath,
85-
c.rescriptBinName
86-
);
87-
8877
let createInterfaceRequest = new v.RequestType<
8978
p.TextDocumentIdentifier,
9079
p.TextDocumentIdentifier,
@@ -271,7 +260,7 @@ let openedFile = (fileUri: string, fileContent: string) => {
271260
// TODO: sometime stale .bsb.lock dangling. bsb -w knows .bsb.lock is
272261
// stale. Use that logic
273262
// TODO: close watcher when lang-server shuts down
274-
if (findRescriptBinary(projectRootPath) != null) {
263+
if (utils.findRescriptBinary(projectRootPath) != null) {
275264
let payload: clientSentBuildAction = {
276265
title: c.startBuildAction,
277266
projectRootPath: projectRootPath,
@@ -1295,7 +1284,7 @@ function onMessage(msg: p.Message) {
12951284
// TODO: close watcher when lang-server shuts down. However, by Node's
12961285
// default, these subprocesses are automatically killed when this
12971286
// language-server process exits
1298-
let rescriptBinaryPath = findRescriptBinary(projectRootPath);
1287+
let rescriptBinaryPath = utils.findRescriptBinary(projectRootPath);
12991288
if (rescriptBinaryPath != null) {
13001289
let bsbProcess = utils.runBuildWatcherUsingValidBuildPath(
13011290
rescriptBinaryPath,

server/src/utils.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import * as childProcess from "child_process";
1+
import * as path from "node:path";
2+
import * as fs from "node:fs";
3+
import * as os from "node:os";
4+
import * as childProcess from "node:child_process";
25
import * as p from "vscode-languageserver-protocol";
3-
import * as path from "path";
46
import * as t from "vscode-languageserver-types";
57
import {
68
RequestMessage,
79
ResponseMessage,
810
} from "vscode-languageserver-protocol";
9-
import fs from "fs";
10-
import * as os from "os";
11+
import memo from "memize";
1112

1213
import * as codeActions from "./codeActions";
1314
import * as c from "./constants";
@@ -26,7 +27,7 @@ export let createFileInTempDir = (extension = "") => {
2627

2728
// TODO: races here?
2829
// TODO: this doesn't handle file:/// scheme
29-
export let findProjectRootOfFile = (
30+
export let findProjectRootOfFile = memo((
3031
source: p.DocumentUri
3132
): null | p.DocumentUri => {
3233
let dir = path.dirname(source);
@@ -43,10 +44,10 @@ export let findProjectRootOfFile = (
4344
return findProjectRootOfFile(dir);
4445
}
4546
}
46-
};
47+
});
4748

4849
// Check if binaryName exists inside binaryDirPath and return the joined path.
49-
export let findBinary = (
50+
export let findBinary = memo((
5051
binaryDirPath: p.DocumentUri | null,
5152
binaryName: string
5253
): p.DocumentUri | null => {
@@ -59,7 +60,21 @@ export let findBinary = (
5960
} else {
6061
return null;
6162
}
62-
};
63+
});
64+
65+
export let findRescriptBinary = memo((
66+
projectRootPath: p.DocumentUri | null
67+
) =>
68+
config.extensionConfiguration.binaryPath == null
69+
? lookup.findFilePathFromProjectRoot(
70+
projectRootPath,
71+
path.join(c.nodeModulesBinDir, c.rescriptBinName)
72+
)
73+
: findBinary(
74+
config.extensionConfiguration.binaryPath,
75+
c.rescriptBinName
76+
)
77+
);
6378

6479
type execResult =
6580
| {
@@ -138,28 +153,14 @@ export let formatCode = (
138153
}
139154
};
140155

141-
let findReScriptVersion = (filePath: p.DocumentUri): string | undefined => {
142-
let projectRoot = findProjectRootOfFile(filePath);
143-
if (projectRoot == null) {
144-
return undefined;
145-
}
146-
147-
let rescriptBinary = lookup.findFilePathFromProjectRoot(
148-
projectRoot,
149-
path.join(c.nodeModulesBinDir, c.rescriptBinName)
150-
);
151-
152-
if (rescriptBinary == null) {
153-
return undefined;
154-
}
155-
156+
let findReScriptVersion = memo((): string | undefined => {
156157
try {
157-
let version = childProcess.execSync(`${rescriptBinary} -v`);
158-
return version.toString().trim();
158+
let { version } = require('rescript/package.json');
159+
return version;
159160
} catch (e) {
160161
return undefined;
161162
}
162-
};
163+
});
163164

164165
export let runAnalysisAfterSanityCheck = (
165166
filePath: p.DocumentUri,
@@ -179,7 +180,7 @@ export let runAnalysisAfterSanityCheck = (
179180
if (projectRootPath == null && projectRequired) {
180181
return null;
181182
}
182-
let rescriptVersion = findReScriptVersion(filePath);
183+
let rescriptVersion = findReScriptVersion();
183184
let options: childProcess.ExecFileSyncOptions = {
184185
cwd: projectRootPath || undefined,
185186
maxBuffer: Infinity,

0 commit comments

Comments
 (0)