Skip to content

Commit 830d55b

Browse files
committed
Avoid calling stat twice for each item returned by NSURLDirectoryEnumerator
fts_read calls stat internally to determine the type of each file that is visited. If URL(fileURLWithPath:) is called with a path that doesn't end with a slash, it calls stat to determine whether the path represents a file or a directory. We know from the FTSENT's fts_info field whether the entry represents a file or a directory, so we can instead call URL(fileURLWithPath:isDirectory:) and avoid the second stat call. This reduces the runtime of FileManager.enumerator(at:includingPropertiesForKeys:options:errorHandler:) by around 40%.
1 parent 3cd1f79 commit 830d55b

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Foundation/FileManager+POSIX.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,13 +1099,13 @@ internal func _contentsEqual(atPath path1: String, andPath path2: String) -> Boo
10991099
fts_set(_stream, _current, FTS_SKIP)
11001100
}
11011101
if showFile {
1102-
return URL(fileURLWithPath: filename)
1102+
return URL(fileURLWithPath: filename, isDirectory: true)
11031103
}
11041104

11051105
case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
11061106
let (showFile, _) = match(filename: filename, to: _options, isDir: false)
11071107
if showFile {
1108-
return URL(fileURLWithPath: filename)
1108+
return URL(fileURLWithPath: filename, isDirectory: false)
11091109
}
11101110
case FTS_DNR, FTS_ERR, FTS_NS:
11111111
let keepGoing: Bool

0 commit comments

Comments
 (0)