summaryrefslogtreecommitdiff
path: root/cli/tsc
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2021-05-18 08:51:35 +1000
committerGitHub <noreply@github.com>2021-05-18 08:51:35 +1000
commit3318c495f670b2c2ad6418276b5aff0781724e16 (patch)
treeaa31dfc36792beb943699715f0077328a4d7ef55 /cli/tsc
parentafaac64737f04844ee6897a5ede81d11140b9950 (diff)
refactor(lsp): memoize script versions per tsc request (#10601)
Diffstat (limited to 'cli/tsc')
-rw-r--r--cli/tsc/99_main_compiler.js18
1 files changed, 15 insertions, 3 deletions
diff --git a/cli/tsc/99_main_compiler.js b/cli/tsc/99_main_compiler.js
index b4626374d..935ab5951 100644
--- a/cli/tsc/99_main_compiler.js
+++ b/cli/tsc/99_main_compiler.js
@@ -72,6 +72,9 @@ delete Object.prototype.__proto__;
/** @type {Map<string, ts.SourceFile>} */
const sourceFileCache = new Map();
+ /** @type {Map<string, string>} */
+ const scriptVersionCache = new Map();
+
/** @param {ts.DiagnosticRelatedInformation} diagnostic */
function fromRelatedInformation({
start,
@@ -368,7 +371,15 @@ delete Object.prototype.__proto__;
if (sourceFile) {
return sourceFile.version ?? "1";
}
- return core.opSync("op_script_version", { specifier });
+ // tsc neurotically requests the script version multiple times even though
+ // it can't possibly have changed, so we will memoize it on a per request
+ // basis.
+ if (scriptVersionCache.has(specifier)) {
+ return scriptVersionCache.get(specifier);
+ }
+ const scriptVersion = core.opSync("op_script_version", { specifier });
+ scriptVersionCache.set(specifier, scriptVersion);
+ return scriptVersion;
},
getScriptSnapshot(specifier) {
debug(`host.getScriptSnapshot("${specifier}")`);
@@ -386,8 +397,7 @@ delete Object.prototype.__proto__;
},
};
}
- /** @type {string | undefined} */
- const version = core.opSync("op_script_version", { specifier });
+ const version = host.getScriptVersion(specifier);
if (version != null) {
return new ScriptSnapshot(specifier, version);
}
@@ -526,6 +536,8 @@ delete Object.prototype.__proto__;
*/
function serverRequest({ id, ...request }) {
debug(`serverRequest()`, { id, ...request });
+ // evict all memoized source file versions
+ scriptVersionCache.clear();
switch (request.method) {
case "configure": {
const { options, errors } = ts