Skip to content

Commit acfcf40

Browse files
committed
Add uptodate checks for program object to avoid recomputation if not needed
1 parent 40f6ed0 commit acfcf40

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/services/services.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,21 +1264,43 @@ module ts {
12641264
};
12651265
}
12661266

1267+
function sourceFileUpToDate(sourceFile: SourceFile): boolean {
1268+
return sourceFile && sourceFile.version === hostCache.getVersion(sourceFile.filename) && sourceFile.isOpen === hostCache.isOpen(sourceFile.filename);
1269+
}
1270+
1271+
function programUpToDate(): boolean {
1272+
// If we haven't create a program yet, then it is not up-to-date
1273+
if (!program) return false;
1274+
1275+
// If number of files in the program do not match, it is not up-to-date
1276+
var hostFilenames = hostCache.getFilenames();
1277+
if (program.getSourceFiles().length !== hostFilenames.length) return false;
1278+
1279+
// If any file is not up-to-date, then the whole program is not up-to-date
1280+
for (var i = 0, n = hostFilenames.length; i < n; i++) {
1281+
if (!sourceFileUpToDate(program.getSourceFile(hostFilenames[i])))
1282+
return false;
1283+
}
1284+
1285+
// If the compilation settings do no match, then the program is not up-to-date
1286+
return compareDataObjects(program.getCompilerOptions(), hostCache.compilationSettings());
1287+
}
1288+
12671289
function synchronizeHostData(): void {
12681290
// Reset the cache at start of every refresh
12691291
hostCache = new HostCache(host);
12701292

1293+
// If the program is already up-to-date, we can reuse it
1294+
if (programUpToDate()) {
1295+
return;
1296+
}
1297+
12711298
var compilationSettings = hostCache.compilationSettings();
12721299

1273-
// TODO: check if we need to create a new compiler to start with
1274-
// 1. files are identical
1275-
// 2. compilation settings are identical
1276-
12771300
// Now, remove any files from the compiler that are no longer in the host.
12781301
var oldProgram = program;
12791302
if (oldProgram) {
12801303
var oldSettings = program.getCompilerOptions();
1281-
12821304
// If the language version changed, then that affects what types of things we parse. So
12831305
// we have to dump all syntax trees.
12841306
// TODO: handle propagateEnumConstants
@@ -1303,7 +1325,6 @@ module ts {
13031325
// doesn't know about it.). Or notify the compiler about any changes (if it does
13041326
// know about it.)
13051327
var hostfilenames = hostCache.getFilenames();
1306-
13071328
for (var i = 0, n = hostfilenames.length; i < n; i++) {
13081329
var filename = hostfilenames[i];
13091330

@@ -1316,7 +1337,7 @@ module ts {
13161337
//
13171338
// If the sourceFile is the same, assume no update
13181339
//
1319-
if (sourceFile.version === version && sourceFile.isOpen === isOpen) {
1340+
if (sourceFileUpToDate(sourceFile)) {
13201341
continue;
13211342
}
13221343

0 commit comments

Comments
 (0)