r341455 - [VFS] Cache the current working directory for the real FS.

Eric Liu via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 5 02:45:27 PDT 2018


Author: ioeric
Date: Wed Sep  5 02:45:27 2018
New Revision: 341455

URL: http://llvm.org/viewvc/llvm-project?rev=341455&view=rev
Log:
[VFS] Cache the current working directory for the real FS.

Reviewers: sammccall

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D51641

Modified:
    cfe/trunk/lib/Basic/VirtualFileSystem.cpp

Modified: cfe/trunk/lib/Basic/VirtualFileSystem.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/VirtualFileSystem.cpp?rev=341455&r1=341454&r2=341455&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/VirtualFileSystem.cpp (original)
+++ cfe/trunk/lib/Basic/VirtualFileSystem.cpp Wed Sep  5 02:45:27 2018
@@ -49,6 +49,7 @@
 #include <limits>
 #include <map>
 #include <memory>
+#include <mutex>
 #include <string>
 #include <system_error>
 #include <utility>
@@ -244,6 +245,9 @@ public:
   std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
   std::error_code getRealPath(const Twine &Path,
                               SmallVectorImpl<char> &Output) const override;
+private:
+  mutable std::mutex CWDMutex;
+  mutable std::string CWDCache;
 };
 
 } // namespace
@@ -266,10 +270,14 @@ RealFileSystem::openFileForRead(const Tw
 }
 
 llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const {
+  std::lock_guard<std::mutex> Lock(CWDMutex);
+  if (!CWDCache.empty())
+    return CWDCache;
   SmallString<256> Dir;
   if (std::error_code EC = llvm::sys::fs::current_path(Dir))
     return EC;
-  return Dir.str().str();
+  CWDCache = Dir.str();
+  return CWDCache;
 }
 
 std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
@@ -280,7 +288,13 @@ std::error_code RealFileSystem::setCurre
   // difference for example on network filesystems, where symlinks might be
   // switched during runtime of the tool. Fixing this depends on having a
   // file system abstraction that allows openat() style interactions.
-  return llvm::sys::fs::set_current_path(Path);
+  if (auto EC = llvm::sys::fs::set_current_path(Path))
+    return EC;
+
+  // Invalidate cache.
+  std::lock_guard<std::mutex> Lock(CWDMutex);
+  CWDCache.clear();
+  return std::error_code();
 }
 
 std::error_code




More information about the cfe-commits mailing list