Skip to content

Commit 1c09daa

Browse files
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.
1 parent ad5cac3 commit 1c09daa

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -394,40 +394,62 @@ void ModuleDepCollector::addModuleMapFiles(
394394
if (Service.shouldEagerLoadModules())
395395
return; // Only pcm is needed for eager load.
396396

397+
// Preallocate memory to avoid multiple allocations
398+
CI.getFrontendOpts().ModuleMapFiles.reserve(
399+
CI.getFrontendOpts().ModuleMapFiles.size() + ClangModuleDeps.size());
400+
397401
for (const ModuleID &MID : ClangModuleDeps) {
398-
ModuleDeps *MD = ModuleDepsByID.lookup(MID);
399-
assert(MD && "Inconsistent dependency info");
400-
CI.getFrontendOpts().ModuleMapFiles.push_back(MD->ClangModuleMapFile);
402+
403+
if (ModuleDeps *MD = ModuleDepsByID.lookup(MID)) { // Single lookup
404+
assert(MD && "Inconsistent dependency info");
405+
CI.getFrontendOpts().ModuleMapFiles.emplace_back(MD->ClangModuleMapFile);
406+
}
401407
}
402408
}
403409

404410
void ModuleDepCollector::addModuleFiles(
405411
CompilerInvocation &CI, ArrayRef<ModuleID> ClangModuleDeps) const {
412+
413+
// Preallocate memory if eager load is enabled
414+
if (Service.shouldEagerLoadModules()) {
415+
CI.getFrontendOpts().ModuleFiles.reserve(
416+
CI.getFrontendOpts().ModuleFiles.size() + ClangModuleDeps.size());
417+
}
406418
for (const ModuleID &MID : ClangModuleDeps) {
407-
ModuleDeps *MD = ModuleDepsByID.lookup(MID);
408-
std::string PCMPath =
409-
Controller.lookupModuleOutput(*MD, ModuleOutputKind::ModuleFile);
410-
411-
if (Service.shouldEagerLoadModules())
412-
CI.getFrontendOpts().ModuleFiles.push_back(std::move(PCMPath));
413-
else
414-
CI.getHeaderSearchOpts().PrebuiltModuleFiles.insert(
415-
{MID.ModuleName, std::move(PCMPath)});
419+
if (ModuleDeps *MD = ModuleDepsByID.lookup(MID)) {
420+
std::string PCMPath =
421+
Controller.lookupModuleOutput(*MD, ModuleOutputKind::ModuleFile);
422+
423+
if (Service.shouldEagerLoadModules()) {
424+
CI.getFrontendOpts().ModuleFiles.emplace_back(std::move(PCMPath));
425+
} else {
426+
CI.getHeaderSearchOpts().PrebuiltModuleFiles.try_emplace(
427+
MID.ModuleName, std::move(PCMPath));
428+
}
429+
}
416430
}
417431
}
418432

419433
void ModuleDepCollector::addModuleFiles(
420434
CowCompilerInvocation &CI, ArrayRef<ModuleID> ClangModuleDeps) const {
421-
for (const ModuleID &MID : ClangModuleDeps) {
422-
ModuleDeps *MD = ModuleDepsByID.lookup(MID);
423-
std::string PCMPath =
424-
Controller.lookupModuleOutput(*MD, ModuleOutputKind::ModuleFile);
425435

426-
if (Service.shouldEagerLoadModules())
427-
CI.getMutFrontendOpts().ModuleFiles.push_back(std::move(PCMPath));
428-
else
429-
CI.getMutHeaderSearchOpts().PrebuiltModuleFiles.insert(
430-
{MID.ModuleName, std::move(PCMPath)});
436+
// Preallocation
437+
if (Service.shouldEagerLoadModules()) {
438+
CI.getMutFrontendOpts().ModuleFiles.reserve(
439+
CI.getMutFrontendOpts().ModuleFiles.size() + ClangModuleDeps.size());
440+
}
441+
for (const ModuleID &MID : ClangModuleDeps) {
442+
if (ModuleDeps *MD = ModuleDepsByID.lookup(MID)) {
443+
std::string PCMPath =
444+
Controller.lookupModuleOutput(*MD, ModuleOutputKind::ModuleFile);
445+
446+
if (EagerLoad) {
447+
CI.getMutFrontendOpts().ModuleFiles.emplace_back(std::move(PCMPath));
448+
} else {
449+
CI.getMutHeaderSearchOpts().PrebuiltModuleFiles.try_emplace(
450+
MID.ModuleName, std::move(PCMPath));
451+
}
452+
}
431453
}
432454
}
433455

0 commit comments

Comments
 (0)