Skip to content

Commit 6f34b78

Browse files
committed
perf: cache ts.ScriptSnapshot.fromString
1 parent e080029 commit 6f34b78

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/monaco/ls.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,18 @@ export async function setupLs(modelsMap: Ref<Map<string, monaco.editor.ITextMode
7575
monaco.languages.typescript.typescriptDefaults.addExtraLib(libPromiseModel.getValue(), libPromiseUrl.toString());
7676
monaco.languages.typescript.typescriptDefaults.addExtraLib(libDtsModel.getValue(), libDtsUrl.toString());
7777

78+
const scriptSnapshots = new Map<string, ts.IScriptSnapshot>();
79+
7880
const host: LanguageServiceHost = {
7981
readFile(fileName) {
80-
return modelsMap.value.get(fileName)?.getValue();
82+
return modelsMap.value.get(fileName)?.getValue()
83+
?? localMap.get(fileName)?.getValue()
84+
?? nodeModulesMap.get(fileName)?.getValue();
8185
},
8286
fileExists(fileName) {
83-
return modelsMap.value.has(fileName);
87+
return modelsMap.value.has(fileName)
88+
|| localMap.has(fileName)
89+
|| nodeModulesMap.has(fileName);
8490
},
8591
getCompilationSettings(): ts.CompilerOptions {
8692
console.log('getCompilationSettings');
@@ -112,16 +118,15 @@ export async function setupLs(modelsMap: Ref<Map<string, monaco.editor.ITextMode
112118
},
113119
getScriptSnapshot(fileName: string): ts.IScriptSnapshot | undefined {
114120
console.log('getScriptSnapshot', fileName);
115-
if (localMap.has(fileName)) {
116-
return ts.ScriptSnapshot.fromString(localMap.get(fileName)!.getValue());
117-
}
118-
if (nodeModulesMap.has(fileName)) {
119-
return ts.ScriptSnapshot.fromString(nodeModulesMap.get(fileName)!.getValue());
120-
}
121-
if (modelsMap.value.has(fileName)) {
122-
return ts.ScriptSnapshot.fromString(modelsMap.value.get(fileName)!.getValue());
121+
let scriptSnapshot = scriptSnapshots.get(fileName);
122+
if (!scriptSnapshot || scriptSnapshot.getText(0, scriptSnapshot.getLength()) !== this.readFile(fileName)) {
123+
const fileContent = this.readFile(fileName);
124+
if (fileContent !== undefined) {
125+
scriptSnapshot = ts.ScriptSnapshot.fromString(fileContent);
126+
scriptSnapshots.set(fileName, scriptSnapshot);
127+
}
123128
}
124-
return undefined;
129+
return scriptSnapshot;
125130
},
126131
getCurrentDirectory(): string {
127132
console.log('getCurrentDirectory');

0 commit comments

Comments
 (0)