[clang] Optimize Module Dependency Handling for Efficient Memory Usage (PR #133524)

via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 28 14:51:11 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Ayush Pareek (ayushpareek2003)

<details>
<summary>Changes</summary>

-Optimized addModuleFiles functions for both CompilerInvocation and CowCompilerInvocation to reduce redundant function calls and improve efficiency

-Introduced memory preallocation using reserve() when eager load is enabled to reduce reallocation overhead

-Used try_emplace() instead of insert() for PrebuiltModuleFiles to avoid unnecessary overwrites

---
Full diff: https://github.com/llvm/llvm-project/pull/133524.diff


1 Files Affected:

- (modified) clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp (+38-21) 


``````````diff
diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
index 364f156566855..6a5876ef921b6 100644
--- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
+++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
@@ -396,40 +396,57 @@ void ModuleDepCollector::addModuleMapFiles(
   if (Service.shouldEagerLoadModules())
     return; // Only pcm is needed for eager load.
 
+  // Preallocate memory to avoid multiple allocations
+  CI.getFrontendOpts().ModuleMapFiles.reserve(
+       CI.getFrontendOpts().ModuleMapFiles.size() + ClangModuleDeps.size());
   for (const ModuleID &MID : ClangModuleDeps) {
-    ModuleDeps *MD = ModuleDepsByID.lookup(MID);
-    assert(MD && "Inconsistent dependency info");
-    CI.getFrontendOpts().ModuleMapFiles.push_back(MD->ClangModuleMapFile);
+    if (ModuleDeps *MD = ModuleDepsByID.lookup(MID)) { // Single lookup
+       assert(MD && "Inconsistent dependency info");
+       CI.getFrontendOpts().ModuleMapFiles.emplace_back(MD->ClangModuleMapFile);
+     }
   }
 }
 
 void ModuleDepCollector::addModuleFiles(
     CompilerInvocation &CI, ArrayRef<ModuleID> ClangModuleDeps) const {
+   // Preallocate memory if eager load is enabled
+  if (Service.shouldEagerLoadModules()) {
+     CI.getFrontendOpts().ModuleFiles.reserve(
+         CI.getFrontendOpts().ModuleFiles.size() + ClangModuleDeps.size());
+  }
   for (const ModuleID &MID : ClangModuleDeps) {
-    ModuleDeps *MD = ModuleDepsByID.lookup(MID);
-    std::string PCMPath =
-        Controller.lookupModuleOutput(*MD, ModuleOutputKind::ModuleFile);
-
-    if (Service.shouldEagerLoadModules())
-      CI.getFrontendOpts().ModuleFiles.push_back(std::move(PCMPath));
-    else
-      CI.getHeaderSearchOpts().PrebuiltModuleFiles.insert(
-          {MID.ModuleName, std::move(PCMPath)});
+    if (ModuleDeps *MD = ModuleDepsByID.lookup(MID)) {  
+      std::string PCMPath =
+           Controller.lookupModuleOutput(*MD, ModuleOutputKind::ModuleFile);
+      
+      if (Service.shouldEagerLoadModules()) {
+        CI.getFrontendOpts().ModuleFiles.emplace_back(std::move(PCMPath));
+      } else {
+        CI.getHeaderSearchOpts().PrebuiltModuleFiles.try_emplace(
+        MID.ModuleName, std::move(PCMPath));
+      }
+    }
   }
 }
 
 void ModuleDepCollector::addModuleFiles(
     CowCompilerInvocation &CI, ArrayRef<ModuleID> ClangModuleDeps) const {
+   // Preallocation
+   if (Service.shouldEagerLoadModules()) {
+     CI.getMutFrontendOpts().ModuleFiles.reserve(
+         CI.getMutFrontendOpts().ModuleFiles.size() + ClangModuleDeps.size());
+   }
   for (const ModuleID &MID : ClangModuleDeps) {
-    ModuleDeps *MD = ModuleDepsByID.lookup(MID);
-    std::string PCMPath =
-        Controller.lookupModuleOutput(*MD, ModuleOutputKind::ModuleFile);
-
-    if (Service.shouldEagerLoadModules())
-      CI.getMutFrontendOpts().ModuleFiles.push_back(std::move(PCMPath));
-    else
-      CI.getMutHeaderSearchOpts().PrebuiltModuleFiles.insert(
-          {MID.ModuleName, std::move(PCMPath)});
+    if (ModuleDeps *MD = ModuleDepsByID.lookup(MID)) {
+      std::string PCMPath =
+           Controller.lookupModuleOutput(*MD, ModuleOutputKind::ModuleFile);
+      if (Service.shouldEagerLoadModules()) {
+         CI.getMutFrontendOpts().ModuleFiles.emplace_back(std::move(PCMPath));
+      } else {
+        CI.getMutHeaderSearchOpts().PrebuiltModuleFiles.try_emplace(
+             MID.ModuleName, std::move(PCMPath));
+      }
+    }
   }
 }
 

``````````

</details>


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


More information about the cfe-commits mailing list