Skip to content

Commit b856e36

Browse files
committed
perf: avoid garbage collection on diagnostics
Reverts the `getKey` change and updates diagnostics iteration to avoid allocating an array we later discard.
1 parent 3846dc6 commit b856e36

File tree

3 files changed

+57
-62
lines changed

3 files changed

+57
-62
lines changed

packages/language-server/src/plugins/typescript/DocumentSnapshot.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ const svelteTypesPattern = /node_modules[\/\\]svelte[\/\\]types[\/\\]index\.d\.t
7878
const shimsPattern =
7979
/svelte2tsx[\/\\]svelte-shims\.d\.ts$|svelte-check[\/\\]dist[\/\\]src[\/\\]svelte-shims\.d\.ts$/;
8080

81-
8281
export namespace DocumentSnapshot {
8382
/**
8483
* Returns a svelte snapshot from a svelte document.

packages/language-server/src/plugins/typescript/module-loader.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import ts from 'typescript';
2-
import path from 'node:path';
32
import { FileMap, FileSet } from '../../lib/documents/fileCollection';
43
import { createGetCanonicalFileName, getLastPartOfPath, toFileNameLowerCase } from '../../utils';
54
import { DocumentSnapshot } from './DocumentSnapshot';
@@ -81,11 +80,7 @@ class ModuleResolutionCache {
8180
}
8281

8382
private getKey(moduleName: string, containingFile: string) {
84-
return (
85-
path.dirname(containingFile) +
86-
CACHE_KEY_SEPARATOR +
87-
ensureRealSvelteFilePath(moduleName)
88-
);
83+
return containingFile + CACHE_KEY_SEPARATOR + ensureRealSvelteFilePath(moduleName);
8984
}
9085

9186
clearPendingInvalidations() {

packages/language-server/src/svelte-check.ts

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -252,66 +252,67 @@ export class SvelteCheck {
252252
const isKitFile = snapshot?.kitFile ?? false;
253253
const diagnostics: Diagnostic[] = [];
254254
if (!skipDiagnosticsForFile) {
255-
const originalDiagnostics = [
256-
...lang.getSyntacticDiagnostics(file.fileName),
257-
...lang.getSuggestionDiagnostics(file.fileName),
258-
...lang.getSemanticDiagnostics(file.fileName)
259-
];
260-
261-
for (let diagnostic of originalDiagnostics) {
262-
if (!diagnostic.start || !diagnostic.length || !isKitFile) {
263-
diagnostics.push(map(diagnostic));
264-
continue;
265-
}
255+
const diagnosticSources = [
256+
'getSyntacticDiagnostics',
257+
'getSuggestionDiagnostics',
258+
'getSemanticDiagnostics'
259+
] as const;
260+
for (const diagnosticSource of diagnosticSources) {
261+
for (let diagnostic of lang[diagnosticSource](file.fileName)) {
262+
if (!diagnostic.start || !diagnostic.length || !isKitFile) {
263+
diagnostics.push(map(diagnostic));
264+
continue;
265+
}
266266

267-
let range: Range | undefined = undefined;
268-
const inGenerated = isInGeneratedCode(
269-
file.text,
270-
diagnostic.start,
271-
diagnostic.start + diagnostic.length
272-
);
273-
if (inGenerated && snapshot) {
274-
const pos = snapshot.getOriginalPosition(
275-
snapshot.positionAt(diagnostic.start)
267+
let range: Range | undefined = undefined;
268+
const inGenerated = isInGeneratedCode(
269+
file.text,
270+
diagnostic.start,
271+
diagnostic.start + diagnostic.length
276272
);
277-
range = {
278-
start: pos,
279-
end: {
280-
line: pos.line,
281-
// adjust length so it doesn't spill over to the next line
282-
character: pos.character + 1
283-
}
284-
};
285-
// If not one of the specific error messages then filter out
286-
if (diagnostic.code === 2307) {
287-
diagnostic = {
288-
...diagnostic,
289-
messageText:
290-
typeof diagnostic.messageText === 'string' &&
291-
diagnostic.messageText.includes('./$types')
292-
? diagnostic.messageText +
293-
` (this likely means that SvelteKit's type generation didn't run yet - try running it by executing 'npm run dev' or 'npm run build')`
294-
: diagnostic.messageText
273+
if (inGenerated && snapshot) {
274+
const pos = snapshot.getOriginalPosition(
275+
snapshot.positionAt(diagnostic.start)
276+
);
277+
range = {
278+
start: pos,
279+
end: {
280+
line: pos.line,
281+
// adjust length so it doesn't spill over to the next line
282+
character: pos.character + 1
283+
}
295284
};
296-
} else if (diagnostic.code === 2694) {
297-
diagnostic = {
298-
...diagnostic,
299-
messageText:
300-
typeof diagnostic.messageText === 'string' &&
301-
diagnostic.messageText.includes('/$types')
302-
? diagnostic.messageText +
303-
` (this likely means that SvelteKit's generated types are out of date - try rerunning it by executing 'npm run dev' or 'npm run build')`
304-
: diagnostic.messageText
305-
};
306-
} else if (
307-
diagnostic.code !==
308-
2355 /* A function whose declared type is neither 'void' nor 'any' must return a value */
309-
) {
310-
continue;
285+
// If not one of the specific error messages then filter out
286+
if (diagnostic.code === 2307) {
287+
diagnostic = {
288+
...diagnostic,
289+
messageText:
290+
typeof diagnostic.messageText === 'string' &&
291+
diagnostic.messageText.includes('./$types')
292+
? diagnostic.messageText +
293+
` (this likely means that SvelteKit's type generation didn't run yet - try running it by executing 'npm run dev' or 'npm run build')`
294+
: diagnostic.messageText
295+
};
296+
} else if (diagnostic.code === 2694) {
297+
diagnostic = {
298+
...diagnostic,
299+
messageText:
300+
typeof diagnostic.messageText === 'string' &&
301+
diagnostic.messageText.includes('/$types')
302+
? diagnostic.messageText +
303+
` (this likely means that SvelteKit's generated types are out of date - try rerunning it by executing 'npm run dev' or 'npm run build')`
304+
: diagnostic.messageText
305+
};
306+
} else if (
307+
diagnostic.code !==
308+
2355 /* A function whose declared type is neither 'void' nor 'any' must return a value */
309+
) {
310+
continue;
311+
}
311312
}
312-
}
313313

314-
diagnostics.push(map(diagnostic, range));
314+
diagnostics.push(map(diagnostic, range));
315+
}
315316
}
316317
}
317318

0 commit comments

Comments
 (0)