From 0f990d9d927a0b25bc0eac32f2e7eee7c0460693 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sat, 2 Dec 2023 23:28:46 +0000 Subject: [PATCH] perf(lsp): fix redundant serialization of sources (#21435) --- cli/tsc/99_main_compiler.js | 50 +++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/cli/tsc/99_main_compiler.js b/cli/tsc/99_main_compiler.js index 6861e17442a952..b72a3b0efac6ec 100644 --- a/cli/tsc/99_main_compiler.js +++ b/cli/tsc/99_main_compiler.js @@ -555,9 +555,13 @@ delete Object.prototype.__proto__; } /** @type {{ data: string; scriptKind: ts.ScriptKind; version: string; }} */ - const { data, scriptKind, version } = ops.op_load( + const fileInfo = ops.op_load( { specifier }, ); + if (!fileInfo) { + return undefined; + } + const { data, scriptKind, version } = fileInfo; assert( data != null, `"data" is unexpectedly null for "${specifier}".`, @@ -713,10 +717,6 @@ delete Object.prototype.__proto__; if (logDebug) { debug(`host.getScriptVersion("${specifier}")`); } - const sourceFile = sourceFileCache.get(specifier); - if (sourceFile) { - return sourceFile.version ?? "1"; - } // tsc 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)) { @@ -730,30 +730,26 @@ delete Object.prototype.__proto__; if (logDebug) { debug(`host.getScriptSnapshot("${specifier}")`); } - const sourceFile = sourceFileCache.get(specifier); - if (sourceFile) { - return { - getText(start, end) { - return sourceFile.text.substring(start, end); - }, - getLength() { - return sourceFile.text.length; - }, - getChangeRange() { - return undefined; - }, - }; + let sourceFile = sourceFileCache.get(specifier); + if ( + !specifier.startsWith(ASSETS_URL_PREFIX) && + sourceFile?.version != this.getScriptVersion(specifier) + ) { + sourceFileCache.delete(specifier); + sourceFile = undefined; } - - const fileInfo = ops.op_load( - { specifier }, - ); - if (fileInfo) { - scriptVersionCache.set(specifier, fileInfo.version); - return ts.ScriptSnapshot.fromString(fileInfo.data); - } else { - return undefined; + if (!sourceFile) { + sourceFile = this.getSourceFile( + specifier, + specifier.endsWith(".json") + ? ts.ScriptTarget.JSON + : ts.ScriptTarget.ESNext, + ); + } + if (sourceFile) { + return ts.ScriptSnapshot.fromString(sourceFile.text); } + return undefined; }, };