Skip to content

Commit 5b1789b

Browse files
committed
Merge pull request #597 from Microsoft/cacheFileLookups
Cache unsuccessful file lookups for improved performance.
2 parents 3cc4b73 + f62bbc2 commit 5b1789b

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/compiler/parser.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3691,23 +3691,22 @@ module ts {
36913691

36923692
// Get source file from normalized filename
36933693
function findSourceFile(filename: string, isDefaultLib: boolean, refFile?: SourceFile, refStart?: number, refLength?: number): SourceFile {
3694-
// Look through existing source files to see if we've encountered it.
36953694
var canonicalName = host.getCanonicalFileName(filename);
3696-
var file = getSourceFile(filename);
3697-
if (file) {
3698-
if (host.useCaseSensitiveFileNames() && canonicalName !== file.filename) {
3695+
if (hasProperty(filesByName, canonicalName)) {
3696+
// We've already looked for this file, use cached result
3697+
var file = filesByName[canonicalName];
3698+
if (file && host.useCaseSensitiveFileNames() && canonicalName !== file.filename) {
36993699
errors.push(createFileDiagnostic(refFile, refStart, refLength,
37003700
Diagnostics.Filename_0_differs_from_already_included_filename_1_only_in_casing, filename, file.filename));
3701-
}
3701+
}
37023702
}
37033703
else {
3704-
// If we haven't, read the file.
3705-
file = host.getSourceFile(filename, options.target, hostErrorMessage => {
3704+
// We haven't looked for this file, do so now and cache result
3705+
var file = filesByName[canonicalName] = host.getSourceFile(filename, options.target, hostErrorMessage => {
37063706
errors.push(createFileDiagnostic(refFile, refStart, refLength,
37073707
Diagnostics.Cannot_read_file_0_Colon_1, filename, hostErrorMessage));
37083708
});
37093709
if (file) {
3710-
filesByName[host.getCanonicalFileName(filename)] = file;
37113710
seenNoDefaultLib = seenNoDefaultLib || file.hasNoDefaultLib;
37123711
if (!options.noResolve) {
37133712
var basePath = getDirectoryPath(filename);
@@ -3725,7 +3724,6 @@ module ts {
37253724
});
37263725
}
37273726
}
3728-
37293727
return file;
37303728
}
37313729

0 commit comments

Comments
 (0)