Skip to content

Commit 2a016fc

Browse files
authored
Use a map for read file lookups (microsoft#19487)
1 parent 1a6022d commit 2a016fc

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

src/harness/loggedIO.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ interface PlaybackControl {
8989
namespace Playback {
9090
let recordLog: IOLog = undefined;
9191
let replayLog: IOLog = undefined;
92+
let replayFilesRead: ts.Map<IOLogFile> | undefined = undefined;
9293
let recordLogFileNameBase = "";
9394

9495
interface Memoized<T> {
@@ -222,10 +223,15 @@ namespace Playback {
222223
replayLog = log;
223224
// Remove non-found files from the log (shouldn't really need them, but we still record them for diagnostic purposes)
224225
replayLog.filesRead = replayLog.filesRead.filter(f => f.result.contents !== undefined);
226+
replayFilesRead = ts.createMap();
227+
for (const file of replayLog.filesRead) {
228+
replayFilesRead.set(ts.normalizeSlashes(file.path).toLowerCase(), file);
229+
}
225230
};
226231

227232
wrapper.endReplay = () => {
228233
replayLog = undefined;
234+
replayFilesRead = undefined;
229235
};
230236

231237
wrapper.startRecord = (fileNameBase) => {
@@ -254,7 +260,7 @@ namespace Playback {
254260
path => callAndRecord(underlying.fileExists(path), recordLog.fileExists, { path }),
255261
memoize(path => {
256262
// If we read from the file, it must exist
257-
if (findFileByPath(replayLog.filesRead, path, /*throwFileNotFoundError*/ false)) {
263+
if (findFileByPath(path, /*throwFileNotFoundError*/ false)) {
258264
return true;
259265
}
260266
else {
@@ -298,7 +304,7 @@ namespace Playback {
298304
recordLog.filesRead.push(logEntry);
299305
return result;
300306
},
301-
memoize(path => findFileByPath(replayLog.filesRead, path, /*throwFileNotFoundError*/ true).contents));
307+
memoize(path => findFileByPath(path, /*throwFileNotFoundError*/ true).contents));
302308

303309
wrapper.readDirectory = recordReplay(wrapper.readDirectory, underlying)(
304310
(path, extensions, exclude, include, depth) => {
@@ -379,14 +385,12 @@ namespace Playback {
379385
return results[0].result;
380386
}
381387

382-
function findFileByPath(logArray: IOLogFile[],
383-
expectedPath: string, throwFileNotFoundError: boolean): FileInformation {
388+
function findFileByPath(expectedPath: string, throwFileNotFoundError: boolean): FileInformation {
384389
const normalizedName = ts.normalizePath(expectedPath).toLowerCase();
385390
// Try to find the result through normal fileName
386-
for (const log of logArray) {
387-
if (ts.normalizeSlashes(log.path).toLowerCase() === normalizedName) {
388-
return log.result;
389-
}
391+
const result = replayFilesRead.get(normalizedName);
392+
if (result) {
393+
return result.result;
390394
}
391395

392396
// If we got here, we didn't find a match

0 commit comments

Comments
 (0)