Skip to content

Commit

Permalink
sys: Use readdir withFileTypes option to skip lots of stat syscalls.
Browse files Browse the repository at this point in the history
This makes walking large directory trees much more efficient.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Loading branch information
andersk committed Nov 22, 2019
1 parent f84fd30 commit 96a40c3
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1353,23 +1353,31 @@ namespace ts {
function getAccessibleFileSystemEntries(path: string): FileSystemEntries {
perfLogger.logEvent("ReadDir: " + (path || "."));
try {
const entries = _fs.readdirSync(path || ".").sort();
const entries = _fs.readdirSync(path || ".", { withFileTypes: true });
const files: string[] = [];
const directories: string[] = [];
for (const entry of entries) {
for (const dirent of entries) {
// withFileTypes is not supported before Node 10.10.
const entry = typeof dirent === "string" ? dirent : dirent.name;

// This is necessary because on some file system node fails to exclude
// "." and "..". See https://github.com/nodejs/node/issues/4002
if (entry === "." || entry === "..") {
continue;
}
const name = combinePaths(path, entry);

let stat: any;
try {
stat = _fs.statSync(name);
}
catch (e) {
continue;
if (typeof dirent === "string" || dirent.isSymbolicLink()) {
const name = combinePaths(path, entry);

try {
stat = _fs.statSync(name);
}
catch (e) {
continue;
}
} else {
stat = dirent;
}

if (stat.isFile()) {
Expand All @@ -1379,6 +1387,8 @@ namespace ts {
directories.push(entry);
}
}
files.sort();
directories.sort();
return { files, directories };
}
catch (e) {
Expand Down Expand Up @@ -1413,8 +1423,7 @@ namespace ts {
}

function getDirectories(path: string): string[] {
perfLogger.logEvent("ReadDir: " + path);
return filter<string>(_fs.readdirSync(path), dir => fileSystemEntryExists(combinePaths(path, dir), FileSystemEntryKind.Directory));
return getAccessibleFileSystemEntries(path).directories.slice();
}

function realpath(path: string): string {
Expand Down

0 comments on commit 96a40c3

Please sign in to comment.