Skip to content

Commit 6a37fd4

Browse files
committed
Cache results for readFile, fileExists, directory exists, sourceFiles for .d.ts files across the build (only first time)
1 parent 94d7e30 commit 6a37fd4

File tree

3 files changed

+114
-7
lines changed

3 files changed

+114
-7
lines changed

src/compiler/program.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ namespace ts {
7373
// TODO(shkamat): update this after reworking ts build API
7474
export function createCompilerHostWorker(options: CompilerOptions, setParentNodes?: boolean, system = sys): CompilerHost {
7575
const existingDirectories = createMap<boolean>();
76-
7776
function getCanonicalFileName(fileName: string): string {
7877
// if underlying system can distinguish between two files whose names differs only in cases then file name already in canonical form.
7978
// otherwise use toLowerCase as a canonical form.
@@ -84,7 +83,7 @@ namespace ts {
8483
let text: string | undefined;
8584
try {
8685
performance.mark("beforeIORead");
87-
text = system.readFile(fileName, options.charset);
86+
text = host.readFile(fileName);
8887
performance.mark("afterIORead");
8988
performance.measure("I/O Read", "beforeIORead", "afterIORead");
9089
}
@@ -113,7 +112,12 @@ namespace ts {
113112
if (directoryPath.length > getRootLength(directoryPath) && !directoryExists(directoryPath)) {
114113
const parentDirectory = getDirectoryPath(directoryPath);
115114
ensureDirectoriesExist(parentDirectory);
116-
system.createDirectory(directoryPath);
115+
if (host.createDirectory) {
116+
host.createDirectory(directoryPath);
117+
}
118+
else {
119+
system.createDirectory(directoryPath);
120+
}
117121
}
118122
}
119123

@@ -177,8 +181,7 @@ namespace ts {
177181

178182
const newLine = getNewLineCharacter(options, () => system.newLine);
179183
const realpath = system.realpath && ((path: string) => system.realpath!(path));
180-
181-
return {
184+
const host: CompilerHost = {
182185
getSourceFile,
183186
getDefaultLibLocation,
184187
getDefaultLibFileName: options => combinePaths(getDefaultLibLocation(), getDefaultLibFileName(options)),
@@ -194,8 +197,10 @@ namespace ts {
194197
getEnvironmentVariable: name => system.getEnvironmentVariable ? system.getEnvironmentVariable(name) : "",
195198
getDirectories: (path: string) => system.getDirectories(path),
196199
realpath,
197-
readDirectory: (path, extensions, include, exclude, depth) => system.readDirectory(path, extensions, include, exclude, depth)
200+
readDirectory: (path, extensions, include, exclude, depth) => system.readDirectory(path, extensions, include, exclude, depth),
201+
createDirectory: d => system.createDirectory(d)
198202
};
203+
return host;
199204
}
200205

201206
export function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic> {

src/compiler/tsbuild.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ namespace ts {
433433
const missingRoots = createMap<true>();
434434
let globalDependencyGraph: DependencyGraph | undefined;
435435
const writeFileName = (s: string) => host.trace && host.trace(s);
436+
let readFileWithCache = (f: string) => host.readFile(f);
436437

437438
// Watch state
438439
const diagnostics = createFileMap<ReadonlyArray<Diagnostic>>(toPath);
@@ -1072,7 +1073,7 @@ namespace ts {
10721073
let priorChangeTime: Date | undefined;
10731074
if (!anyDtsChanged && isDeclarationFile(fileName)) {
10741075
// Check for unchanged .d.ts files
1075-
if (host.fileExists(fileName) && host.readFile(fileName) === content) {
1076+
if (host.fileExists(fileName) && readFileWithCache(fileName) === content) {
10761077
priorChangeTime = host.getModifiedTime(fileName);
10771078
}
10781079
else {
@@ -1182,6 +1183,97 @@ namespace ts {
11821183

11831184
function buildAllProjects(): ExitStatus {
11841185
if (options.watch) { reportWatchStatus(Diagnostics.Starting_compilation_in_watch_mode); }
1186+
const originalReadFile = host.readFile;
1187+
const originalFileExists = host.fileExists;
1188+
const originalDirectoryExists = host.directoryExists;
1189+
const originalCreateDirectory = host.createDirectory;
1190+
const originalWriteFile = host.writeFile;
1191+
const originalGetSourceFile = host.getSourceFile;
1192+
const readFileCache = createMap<string | false>();
1193+
const fileExistsCache = createMap<boolean>();
1194+
const directoryExistsCache = createMap<boolean>();
1195+
const sourceFileCache = createMap<SourceFile>();
1196+
const savedReadFileWithCache = readFileWithCache;
1197+
1198+
// TODO:: In watch mode as well to use caches for incremental build once we can invalidate caches correctly and have right api
1199+
// Override readFile for json files and output .d.ts to cache the text
1200+
readFileWithCache = fileName => {
1201+
const key = toPath(fileName);
1202+
const value = readFileCache.get(key);
1203+
if (value !== undefined) return value || undefined;
1204+
return setReadFileCache(key, fileName);
1205+
};
1206+
const setReadFileCache = (key: Path, fileName: string) => {
1207+
const newValue = originalReadFile.call(host, fileName);
1208+
readFileCache.set(key, newValue || false);
1209+
return newValue;
1210+
};
1211+
host.readFile = fileName => {
1212+
const key = toPath(fileName);
1213+
const value = readFileCache.get(key);
1214+
if (value !== undefined) return value; // could be .d.ts from output
1215+
if (!fileExtensionIs(fileName, Extension.Json)) {
1216+
return originalReadFile.call(host, fileName);
1217+
}
1218+
1219+
return setReadFileCache(key, fileName);
1220+
};
1221+
host.getSourceFile = (fileName, languageVersion, onError, shouldCreateNewSourceFile) => {
1222+
const key = toPath(fileName);
1223+
const value = sourceFileCache.get(key);
1224+
if (value) return value;
1225+
1226+
const sourceFile = originalGetSourceFile.call(host, fileName, languageVersion, onError, shouldCreateNewSourceFile);
1227+
if (sourceFile && (isDeclarationFileName(fileName) || fileExtensionIs(fileName, Extension.Json))) {
1228+
sourceFileCache.set(key, sourceFile);
1229+
}
1230+
return sourceFile;
1231+
};
1232+
1233+
// fileExits for any kind of extension
1234+
host.fileExists = fileName => {
1235+
const key = toPath(fileName);
1236+
const value = fileExistsCache.get(key);
1237+
if (value !== undefined) return value;
1238+
const newValue = originalFileExists.call(host, fileName);
1239+
fileExistsCache.set(key, !!newValue);
1240+
return newValue;
1241+
};
1242+
host.writeFile = (fileName, data, writeByteOrderMark, onError, sourceFiles) => {
1243+
const key = toPath(fileName);
1244+
fileExistsCache.delete(key);
1245+
1246+
const value = readFileCache.get(key);
1247+
if (value && value !== data) {
1248+
readFileCache.delete(key);
1249+
sourceFileCache.delete(key);
1250+
}
1251+
else {
1252+
const sourceFile = sourceFileCache.get(key);
1253+
if (sourceFile && sourceFile.text !== data) {
1254+
sourceFileCache.delete(key);
1255+
}
1256+
}
1257+
originalWriteFile.call(host, fileName, data, writeByteOrderMark, onError, sourceFiles);
1258+
};
1259+
1260+
// directoryExists
1261+
if (originalDirectoryExists && originalCreateDirectory) {
1262+
host.directoryExists = directory => {
1263+
const key = toPath(directory);
1264+
const value = directoryExistsCache.get(key);
1265+
if (value !== undefined) return value;
1266+
const newValue = originalDirectoryExists.call(host, directory);
1267+
directoryExistsCache.set(key, !!newValue);
1268+
return newValue;
1269+
};
1270+
host.createDirectory = directory => {
1271+
const key = toPath(directory);
1272+
directoryExistsCache.delete(key);
1273+
originalCreateDirectory.call(host, directory);
1274+
};
1275+
}
1276+
11851277
const graph = getGlobalDependencyGraph();
11861278
reportBuildQueue(graph);
11871279
let anyFailed = false;
@@ -1232,6 +1324,13 @@ namespace ts {
12321324
anyFailed = anyFailed || !!(buildResult & BuildResultFlags.AnyErrors);
12331325
}
12341326
reportErrorSummary();
1327+
host.readFile = originalReadFile;
1328+
host.fileExists = originalFileExists;
1329+
host.directoryExists = originalDirectoryExists;
1330+
host.createDirectory = originalCreateDirectory;
1331+
host.writeFile = originalWriteFile;
1332+
readFileWithCache = savedReadFileWithCache;
1333+
host.getSourceFile = originalGetSourceFile;
12351334
return anyFailed ? ExitStatus.DiagnosticsPresent_OutputsSkipped : ExitStatus.Success;
12361335
}
12371336

src/compiler/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5014,6 +5014,9 @@ namespace ts {
50145014
/* @internal */ hasInvalidatedResolution?: HasInvalidatedResolution;
50155015
/* @internal */ hasChangedAutomaticTypeDirectiveNames?: boolean;
50165016
createHash?(data: string): string;
5017+
5018+
// TODO: later handle this in better way in builder host instead once the ap
5019+
/*@internal*/createDirectory?(directory: string): void;
50175020
}
50185021

50195022
/* @internal */

0 commit comments

Comments
 (0)