[llvm] Avoid calling `GetFileAttributesW` in Windows' `fs::access` when checking for existence (PR #83495)

Alexandre Ganea via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 29 19:27:49 PST 2024


================
@@ -620,6 +621,14 @@ std::error_code access(const Twine &Path, AccessMode Mode) {
   if (std::error_code EC = widenPath(Path, PathUtf16))
     return EC;
 
+  if (Mode == AccessMode::Exist) {
+    if (::PathFileExistsW(PathUtf16.begin())) {
+      return std::error_code();
+    } else {
+      return errc::no_such_file_or_directory;
+    }
+  }
+
   DWORD Attributes = ::GetFileAttributesW(PathUtf16.begin());
 
   if (Attributes == INVALID_FILE_ATTRIBUTES) {
----------------
aganea wrote:

Adding a new DLL dependency is expensive on Windows and we should avoid it, if possible, in such core library. Can we rather simply add the `if (Mode == AccessMode::Exist)` test here? Wine implements `PathFileExistsW` in these terms: https://github.com/wine-mirror/wine/blob/1d3b312f225f79bec74d3d265128ead455467e2a/dlls/kernelbase/path.c#L2649

https://github.com/llvm/llvm-project/pull/83495


More information about the llvm-commits mailing list