Skip to content

Commit 893940c

Browse files
Merge pull request #367 from Microsoft/theDisintegrationOfThePersistenceOfMemory
Fixed memory leak when using '--watch' flag.
2 parents 891d379 + eaa28fe commit 893940c

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

src/compiler/core.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,31 @@ module ts {
152152

153153
export function mapToArray<T>(map: Map<T>): T[] {
154154
var result: T[] = [];
155-
for (var id in map) result.push(map[id]);
155+
156+
for (var id in map) {
157+
result.push(map[id]);
158+
}
159+
160+
return result;
161+
}
162+
163+
/**
164+
* Creates a map from the elements of an array.
165+
*
166+
* @param array the array of input elements.
167+
* @param makeKey a function that produces a key for a given element.
168+
*
169+
* This function makes no effort to avoid collisions; if any two elements produce
170+
* the same key with the given 'makeKey' function, then the element with the higher
171+
* index in the array will be the one associated with the produced key.
172+
*/
173+
export function arrayToMap<T>(array: T[], makeKey: (value: T) => string): Map<T> {
174+
var result: Map<T> = {};
175+
176+
forEach(array, value => {
177+
result[makeKey(value)] = value
178+
});
179+
156180
return result;
157181
}
158182

src/compiler/tc.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,14 @@ module ts {
245245

246246
function addWatchers(program: Program) {
247247
forEach(program.getSourceFiles(), f => {
248-
var filename = f.filename;
248+
var filename = getCanonicalName(f.filename);
249249
watchers[filename] = sys.watchFile(filename, fileUpdated);
250250
});
251251
}
252252

253253
function removeWatchers(program: Program) {
254254
forEach(program.getSourceFiles(), f => {
255-
var filename = f.filename;
255+
var filename = getCanonicalName(f.filename);
256256
if (hasProperty(watchers, filename)) {
257257
watchers[filename].close();
258258
}
@@ -264,8 +264,7 @@ module ts {
264264
// Fired off whenever a file is changed.
265265
function fileUpdated(filename: string) {
266266
var firstNotification = isEmpty(updatedFiles);
267-
268-
updatedFiles[filename] = true;
267+
updatedFiles[getCanonicalName(filename)] = true;
269268

270269
// Only start this off when the first file change comes in,
271270
// so that we can batch up all further changes.
@@ -285,20 +284,22 @@ module ts {
285284
// specified since the last compilation cycle.
286285
removeWatchers(program);
287286

288-
// Gets us syntactically correct files from the last compilation.
289-
var getUnmodifiedSourceFile = program.getSourceFile;
287+
// Reuse source files from the last compilation so long as they weren't changed.
288+
var oldSourceFiles = arrayToMap(
289+
filter(program.getSourceFiles(), file => !hasProperty(changedFiles, getCanonicalName(file.filename))),
290+
file => getCanonicalName(file.filename));
290291

291292
// We create a new compiler host for this compilation cycle.
292293
// This new host is effectively the same except that 'getSourceFile'
293294
// will try to reuse the SourceFiles from the last compilation cycle
294295
// so long as they were not modified.
295296
var newCompilerHost = clone(compilerHost);
296297
newCompilerHost.getSourceFile = (fileName, languageVersion, onError) => {
297-
if (!hasProperty(changedFiles, fileName)) {
298-
var sourceFile = getUnmodifiedSourceFile(fileName);
299-
if (sourceFile) {
300-
return sourceFile;
301-
}
298+
fileName = getCanonicalName(fileName);
299+
300+
var sourceFile = lookUp(oldSourceFiles, fileName);
301+
if (sourceFile) {
302+
return sourceFile;
302303
}
303304

304305
return compilerHost.getSourceFile(fileName, languageVersion, onError);
@@ -308,6 +309,10 @@ module ts {
308309
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Compilation_complete_Watching_for_file_changes));
309310
addWatchers(program);
310311
}
312+
313+
function getCanonicalName(fileName: string) {
314+
return compilerHost.getCanonicalFileName(fileName);
315+
}
311316
}
312317

313318
function compile(commandLine: ParsedCommandLine, compilerHost: CompilerHost) {

0 commit comments

Comments
 (0)