https://github.com/ayushpareek2003 updated https://github.com/llvm/llvm-project/pull/132294
>From 53a31ec6ed3d9ace8e7d822c2b2de4f6f58ece8d Mon Sep 17 00:00:00 2001 From: Ayush Pareek <ayushpareek1...@gmail.com> Date: Fri, 21 Mar 2025 03:44:05 +0530 Subject: [PATCH 1/2] Optimize Module Dependency Handling for Efficient Memory Usage -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 --- .../DependencyScanning/ModuleDepCollector.cpp | 61 ++++++++++++------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp index d715ef874e002..e172285c4b32d 100644 --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -393,41 +393,58 @@ void ModuleDepCollector::addModuleMapFiles( CompilerInvocation &CI, ArrayRef<ModuleID> ClangModuleDeps) const { 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)); + } + } } } >From 153cc53186c60454154e205e48249177b1695eff Mon Sep 17 00:00:00 2001 From: Ayush Pareek <ayushpareek1...@gmail.com> Date: Mon, 24 Mar 2025 03:02:03 +0530 Subject: [PATCH 2/2] Update README.md --- bolt/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/bolt/README.md b/bolt/README.md index fe54bd82a356a..44ef33bcebeb0 100644 --- a/bolt/README.md +++ b/bolt/README.md @@ -7,6 +7,7 @@ An overview of the ideas implemented in BOLT along with a discussion of its potential and current results is available in [CGO'19 paper](https://research.fb.com/publications/bolt-a-practical-binary-optimizer-for-data-centers-and-beyond/). + ## Input Binary Requirements BOLT operates on X86-64 and AArch64 ELF binaries. At the minimum, the binaries _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits