[PATCH] Tiny optimization of atomic operations

Eugene Kosov via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 17 01:31:48 PST 2016


Hi.

This patch reduces some unneeded atomic inc/dec ops in std::shared_ptr and IntrusiveRefCntPtr which pointer types are inhevited from ThreadSafeRefCountedBase. Namely, patch replaces some copying with moves.

Diff is placed inline and attached as a separate file.

diff --git a/include/clang/Frontend/CompilerInstance.h b/include/clang/Frontend/CompilerInstance.h
index 83eed2c..4a6be47 100644
--- a/include/clang/Frontend/CompilerInstance.h
+++ b/include/clang/Frontend/CompilerInstance.h
@@ -380,7 +380,7 @@ public:
   /// \note Most clients should use setFileManager, which will implicitly reset
   /// the virtual file system to the one contained in the file manager.
   void setVirtualFileSystem(IntrusiveRefCntPtr<vfs::FileSystem> FS) {
-    VirtualFileSystem = FS;
+    VirtualFileSystem = std::move(FS);
   }
 
   /// }
diff --git a/lib/Basic/FileManager.cpp b/lib/Basic/FileManager.cpp
index ba016db..437f9bb 100644
--- a/lib/Basic/FileManager.cpp
+++ b/lib/Basic/FileManager.cpp
@@ -47,14 +47,14 @@ using namespace clang;
 
 FileManager::FileManager(const FileSystemOptions &FSO,
                          IntrusiveRefCntPtr<vfs::FileSystem> FS)
-  : FS(FS), FileSystemOpts(FSO),
+  : FS(std::move(FS)), FileSystemOpts(FSO),
     SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
   NumDirLookups = NumFileLookups = 0;
   NumDirCacheMisses = NumFileCacheMisses = 0;
 
   // If the caller doesn't provide a virtual file system, just grab the real
   // file system.
-  if (!FS)
+  if (!this->FS)
     this->FS = vfs::getRealFileSystem();
 }
 
diff --git a/lib/Basic/VirtualFileSystem.cpp b/lib/Basic/VirtualFileSystem.cpp
index 6977f40..94e4de1 100644
--- a/lib/Basic/VirtualFileSystem.cpp
+++ b/lib/Basic/VirtualFileSystem.cpp
@@ -271,14 +271,14 @@ directory_iterator RealFileSystem::dir_begin(const Twine &Dir,
 // OverlayFileSystem implementation
 //===-----------------------------------------------------------------------===/
 OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
-  FSList.push_back(BaseFS);
+  FSList.push_back(std::move(BaseFS));
 }
 
 void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
-  FSList.push_back(FS);
+  FSList.push_back(std::move(FS));
   // Synchronize added file systems by duplicating the working directory from
   // the first one in the list.
-  FS->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get());
+  FSList.back()->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get());
 }
 
 ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
@@ -833,7 +833,8 @@ class RedirectingFileSystem : public vfs::FileSystem {
 
 private:
   RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> ExternalFS)
-      : ExternalFS(ExternalFS), CaseSensitive(true), UseExternalNames(true) {}
+      : ExternalFS(std::move(ExternalFS)), CaseSensitive(true),
+        UseExternalNames(true) {}
 
   /// \brief Looks up \p Path in \c Roots.
   ErrorOr<Entry *> lookupPath(const Twine &Path);
@@ -1223,7 +1224,7 @@ RedirectingFileSystem *RedirectingFileSystem::create(
   RedirectingFileSystemParser P(Stream);
 
   std::unique_ptr<RedirectingFileSystem> FS(
-      new RedirectingFileSystem(ExternalFS));
+      new RedirectingFileSystem(std::move(ExternalFS)));
   if (!P.parse(Root, FS.get()))
     return nullptr;
 
@@ -1365,7 +1366,7 @@ vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
                     SourceMgr::DiagHandlerTy DiagHandler, void *DiagContext,
                     IntrusiveRefCntPtr<FileSystem> ExternalFS) {
   return RedirectingFileSystem::create(std::move(Buffer), DiagHandler,
-                                       DiagContext, ExternalFS);
+                                       DiagContext, std::move(ExternalFS));
 }
 
 UniqueID vfs::getNextVirtualUniqueID() {
@@ -1548,7 +1549,7 @@ vfs::recursive_directory_iterator::recursive_directory_iterator(FileSystem &FS_,
   directory_iterator I = FS->dir_begin(Path, EC);
   if (!EC && I != directory_iterator()) {
     State = std::make_shared<IterState>();
-    State->push(I);
+    State->push(std::move(I));
   }
 }
 
@@ -1562,7 +1563,7 @@ recursive_directory_iterator::increment(std::error_code &EC) {
     if (EC)
       return *this;
     if (I != End) {
-      State->push(I);
+      State->push(std::move(I));
       return *this;
     }
   }
diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp
index 766c56d..402faf3 100644
--- a/lib/Driver/Driver.cpp
+++ b/lib/Driver/Driver.cpp
@@ -49,11 +49,10 @@ using namespace llvm::opt;
 Driver::Driver(StringRef ClangExecutable, StringRef DefaultTargetTriple,
                DiagnosticsEngine &Diags,
                IntrusiveRefCntPtr<vfs::FileSystem> VFS)
-    : Opts(createDriverOptTable()), Diags(Diags), VFS(VFS), Mode(GCCMode),
-      SaveTemps(SaveTempsNone), LTOMode(LTOK_None),
-      ClangExecutable(ClangExecutable),
-      SysRoot(DEFAULT_SYSROOT), UseStdLib(true),
-      DefaultTargetTriple(DefaultTargetTriple),
+    : Opts(createDriverOptTable()), Diags(Diags), VFS(std::move(VFS)),
+      Mode(GCCMode), SaveTemps(SaveTempsNone), LTOMode(LTOK_None),
+      ClangExecutable(ClangExecutable), SysRoot(DEFAULT_SYSROOT),
+      UseStdLib(true), DefaultTargetTriple(DefaultTargetTriple),
       DriverTitle("clang LLVM compiler"), CCPrintOptionsFilename(nullptr),
       CCPrintHeadersFilename(nullptr), CCLogDiagnosticsFilename(nullptr),
       CCCPrintBindings(false), CCPrintHeaders(false), CCLogDiagnostics(false),
diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp
index 54ac604..4383c5b 100644
--- a/lib/Frontend/ASTUnit.cpp
+++ b/lib/Frontend/ASTUnit.cpp
@@ -671,7 +671,7 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
   AST->CaptureDiagnostics = CaptureDiagnostics;
   AST->Diagnostics = Diags;
   IntrusiveRefCntPtr<vfs::FileSystem> VFS = vfs::getRealFileSystem();
-  AST->FileMgr = new FileManager(FileSystemOpts, VFS);
+  AST->FileMgr = new FileManager(FileSystemOpts, std::move(VFS));
   AST->UserFilesAreVolatile = UserFilesAreVolatile;
   AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
                                      AST->getFileManager(),
@@ -1556,8 +1556,9 @@ ASTUnit::getMainBufferWithPrecompiledPreamble(
     return nullptr;
 
   // Create a file manager object to provide access to and cache the filesystem.
-  Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS));
-  
+  Clang->setFileManager(
+      new FileManager(Clang->getFileSystemOpts(), std::move(VFS)));
+
   // Create the source manager.
   Clang->setSourceManager(new SourceManager(getDiagnostics(),
                                             Clang->getFileManager()));
@@ -1712,7 +1713,7 @@ ASTUnit *ASTUnit::create(CompilerInvocation *CI,
       createVFSFromCompilerInvocation(*CI, *Diags);
   if (!VFS)
     return nullptr;
-  AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
+  AST->FileMgr = new FileManager(AST->FileSystemOpts, std::move(VFS));
   AST->UserFilesAreVolatile = UserFilesAreVolatile;
   AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
                                      UserFilesAreVolatile);
@@ -1983,7 +1984,7 @@ ASTUnit *ASTUnit::LoadFromCommandLine(
       createVFSFromCompilerInvocation(*CI, *Diags);
   if (!VFS)
     return nullptr;
-  AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
+  AST->FileMgr = new FileManager(AST->FileSystemOpts, std::move(VFS));
   AST->OnlyLocalDecls = OnlyLocalDecls;
   AST->CaptureDiagnostics = CaptureDiagnostics;
   AST->TUKind = TUKind;
diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp
index 2b191ca..bd531ac 100644
--- a/lib/Frontend/CompilerInvocation.cpp
+++ b/lib/Frontend/CompilerInvocation.cpp
@@ -2356,7 +2356,7 @@ createVFSFromCompilerInvocation(const CompilerInvocation &CI,
       Diags.Report(diag::err_invalid_vfs_overlay) << File;
       return IntrusiveRefCntPtr<vfs::FileSystem>();
     }
-    Overlay->pushOverlay(FS);
+    Overlay->pushOverlay(std::move(FS));
   }
   return Overlay;
 }
diff --git a/lib/Frontend/FrontendAction.cpp b/lib/Frontend/FrontendAction.cpp
index cdaa18a..aaaf2cb 100644
--- a/lib/Frontend/FrontendAction.cpp
+++ b/lib/Frontend/FrontendAction.cpp
@@ -226,7 +226,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
     if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
           createVFSFromCompilerInvocation(CI.getInvocation(),
                                           CI.getDiagnostics()))
-      CI.setVirtualFileSystem(VFS);
+      CI.setVirtualFileSystem(std::move(VFS));
     else
       goto failure;
   }
diff --git a/lib/Tooling/Tooling.cpp b/lib/Tooling/Tooling.cpp
index f223fd0..50730c5 100644
--- a/lib/Tooling/Tooling.cpp
+++ b/lib/Tooling/Tooling.cpp
@@ -49,8 +49,9 @@ FrontendActionFactory::~FrontendActionFactory() {}
 static clang::driver::Driver *newDriver(
     clang::DiagnosticsEngine *Diagnostics, const char *BinaryName,
     IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
-  clang::driver::Driver *CompilerDriver = new clang::driver::Driver(
-      BinaryName, llvm::sys::getDefaultTargetTriple(), *Diagnostics, VFS);
+  clang::driver::Driver *CompilerDriver =
+      new clang::driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(),
+                                *Diagnostics, std::move(VFS));
   CompilerDriver->setTitle("clang_based_tool");
   return CompilerDriver;
 }
@@ -133,7 +134,7 @@ bool runToolOnCodeWithArgs(
       new vfs::InMemoryFileSystem);
   OverlayFileSystem->pushOverlay(InMemoryFileSystem);
   llvm::IntrusiveRefCntPtr<FileManager> Files(
-      new FileManager(FileSystemOptions(), OverlayFileSystem));
+      new FileManager(FileSystemOptions(), std::move(OverlayFileSystem)));
   ToolInvocation Invocation(getSyntaxOnlyToolArgs(ToolName, Args, FileNameRef),
                             ToolAction, Files.get(), PCHContainerOps);
 
@@ -490,7 +491,7 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
       new vfs::InMemoryFileSystem);
   OverlayFileSystem->pushOverlay(InMemoryFileSystem);
   llvm::IntrusiveRefCntPtr<FileManager> Files(
-      new FileManager(FileSystemOptions(), OverlayFileSystem));
+      new FileManager(FileSystemOptions(), std::move(OverlayFileSystem)));
   ToolInvocation Invocation(getSyntaxOnlyToolArgs(ToolName, Args, FileNameRef),
                             &Action, Files.get(), PCHContainerOps);
 
diff --git a/tools/clang-fuzzer/ClangFuzzer.cpp b/tools/clang-fuzzer/ClangFuzzer.cpp
index d07cf50..65db7ca 100644
--- a/tools/clang-fuzzer/ClangFuzzer.cpp
+++ b/tools/clang-fuzzer/ClangFuzzer.cpp
@@ -41,7 +41,7 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
       tooling::newFrontendActionFactory<clang::SyntaxOnlyAction>());
   std::shared_ptr<PCHContainerOperations> PCHContainerOps =
       std::make_shared<PCHContainerOperations>();
-  action->runInvocation(Invocation.release(), Files.get(), PCHContainerOps,
-                        &Diags);
+  action->runInvocation(Invocation.release(), Files.get(),
+                        std::move(PCHContainerOps), &Diags);
   return 0;
 }
diff --git a/tools/libclang/CIndexer.h b/tools/libclang/CIndexer.h
index 94c27a0..51ac080 100644
--- a/tools/libclang/CIndexer.h
+++ b/tools/libclang/CIndexer.h
@@ -46,7 +46,8 @@ public:
   CIndexer(std::shared_ptr<PCHContainerOperations> PCHContainerOps =
                std::make_shared<PCHContainerOperations>())
       : OnlyLocalDecls(false), DisplayDiagnostics(false),
-        Options(CXGlobalOpt_None), PCHContainerOps(PCHContainerOps) {}
+        Options(CXGlobalOpt_None), PCHContainerOps(std::move(PCHContainerOps)) {
+  }
 
   /// \brief Whether we only want to see "local" declarations (that did not
   /// come from a previous precompiled header). If false, we want to see all
diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp
index 64f4b14..f40a21b 100644
--- a/tools/libclang/Indexing.cpp
+++ b/tools/libclang/Indexing.cpp
@@ -356,7 +356,7 @@ class IndexingFrontendAction : public ASTFrontendAction {
 public:
   IndexingFrontendAction(std::shared_ptr<CXIndexDataConsumer> dataConsumer,
                          SessionSkipBodyData *skData)
-    : DataConsumer(dataConsumer), SKData(skData) { }
+    : DataConsumer(std::move(dataConsumer)), SKData(skData) { }
 
   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
                                                  StringRef InFile) override {

--
Eugene
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.diff
Type: text/x-diff
Size: 12210 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160217/190a24f9/attachment-0001.diff>


More information about the cfe-commits mailing list