1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 04:48:52 -05:00

Revert "perf(lsp): Don't retain SourceFileObjects in sourceFileCache longer than necessary (#23258)" (#23285)

This commit is contained in:
Nayeem Rahman 2024-04-08 18:57:25 +01:00 committed by GitHub
parent e3833b5a52
commit 9752a153ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -147,15 +147,8 @@ delete Object.prototype.__proto__;
} }
} }
// Cache of asset source files. // In the case of the LSP, this will only ever contain the assets.
/** @type {Map<string, ts.SourceFile>} */ /** @type {Map<string, ts.SourceFile>} */
const assetSourceFileCache = new Map();
// Cache of source files, keyed by specifier.
// Stores weak references to ensure we aren't
// retaining `ts.SourceFile` objects longer than needed.
// This should not include assets, which have a separate cache.
/** @type {Map<string, WeakRef<ts.SourceFile>>} */
const sourceFileCache = new Map(); const sourceFileCache = new Map();
/** @type {string[]=} */ /** @type {string[]=} */
@ -583,11 +576,7 @@ delete Object.prototype.__proto__;
// Needs the original specifier // Needs the original specifier
specifier = normalizedToOriginalMap.get(specifier) ?? specifier; specifier = normalizedToOriginalMap.get(specifier) ?? specifier;
const isAsset = specifier.startsWith(ASSETS_URL_PREFIX); let sourceFile = sourceFileCache.get(specifier);
let sourceFile = isAsset
? assetSourceFileCache.get(specifier)
: sourceFileCache.get(specifier)?.deref();
if (sourceFile) { if (sourceFile) {
return sourceFile; return sourceFile;
} }
@ -618,12 +607,10 @@ delete Object.prototype.__proto__;
); );
sourceFile.moduleName = specifier; sourceFile.moduleName = specifier;
sourceFile.version = version; sourceFile.version = version;
if (isAsset) { if (specifier.startsWith(ASSETS_URL_PREFIX)) {
sourceFile.version = "1"; sourceFile.version = "1";
assetSourceFileCache.set(specifier, sourceFile);
} else {
sourceFileCache.set(specifier, new WeakRef(sourceFile));
} }
sourceFileCache.set(specifier, sourceFile);
scriptVersionCache.set(specifier, version); scriptVersionCache.set(specifier, version);
return sourceFile; return sourceFile;
}, },
@ -786,12 +773,9 @@ delete Object.prototype.__proto__;
if (logDebug) { if (logDebug) {
debug(`host.getScriptSnapshot("${specifier}")`); debug(`host.getScriptSnapshot("${specifier}")`);
} }
const isAsset = specifier.startsWith(ASSETS_URL_PREFIX); let sourceFile = sourceFileCache.get(specifier);
let sourceFile = isAsset
? assetSourceFileCache.get(specifier)
: sourceFileCache.get(specifier)?.deref();
if ( if (
!isAsset && !specifier.startsWith(ASSETS_URL_PREFIX) &&
sourceFile?.version != this.getScriptVersion(specifier) sourceFile?.version != this.getScriptVersion(specifier)
) { ) {
sourceFileCache.delete(specifier); sourceFileCache.delete(specifier);
@ -1010,11 +994,13 @@ delete Object.prototype.__proto__;
function getAssets() { function getAssets() {
/** @type {{ specifier: string; text: string; }[]} */ /** @type {{ specifier: string; text: string; }[]} */
const assets = []; const assets = [];
for (const sourceFile of assetSourceFileCache.values()) { for (const sourceFile of sourceFileCache.values()) {
assets.push({ if (sourceFile.fileName.startsWith(ASSETS_URL_PREFIX)) {
specifier: sourceFile.fileName, assets.push({
text: sourceFile.text, specifier: sourceFile.fileName,
}); text: sourceFile.text,
});
}
} }
return assets; return assets;
} }
@ -1191,9 +1177,8 @@ delete Object.prototype.__proto__;
"lib.d.ts files have errors", "lib.d.ts files have errors",
); );
assert(buildSpecifier.startsWith(ASSETS_URL_PREFIX));
// remove this now that we don't need it anymore for warming up tsc // remove this now that we don't need it anymore for warming up tsc
assetSourceFileCache.delete(buildSpecifier); sourceFileCache.delete(buildSpecifier);
// exposes the functions that are called by `tsc::exec()` when type // exposes the functions that are called by `tsc::exec()` when type
// checking TypeScript. // checking TypeScript.