Skip to content

Commit 6c6351e

Browse files
authored
[clang][modules] Optimize construction and usage of the submodule index (llvm#113391)
This patch avoids eagerly populating the submodule index on `Module` construction. The `StringMap` allocation shows up in my profiles of `clang-scan-deps`, while the index is not necessary most of the time. We still construct it on-demand. Moreover, this patch avoids performing qualified submodule lookup in `ASTReader` whenever we're serializing a module graph whose top-level module is unknown. This is pointless, since that's guaranteed to never find any existing submodules anyway. This speeds up `clang-scan-deps` by ~0.5% on my workload.
1 parent e0a02fd commit 6c6351e

File tree

5 files changed

+42
-29
lines changed

5 files changed

+42
-29
lines changed

clang/include/clang/Basic/Module.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class alignas(8) Module {
227227

228228
/// A mapping from the submodule name to the index into the
229229
/// \c SubModules vector at which that submodule resides.
230-
llvm::StringMap<unsigned> SubModuleIndex;
230+
mutable llvm::StringMap<unsigned> SubModuleIndex;
231231

232232
/// The AST file if this is a top-level module which has a
233233
/// corresponding serialized AST file, or null otherwise.
@@ -612,7 +612,6 @@ class alignas(8) Module {
612612
void setParent(Module *M) {
613613
assert(!Parent);
614614
Parent = M;
615-
Parent->SubModuleIndex[Name] = Parent->SubModules.size();
616615
Parent->SubModules.push_back(this);
617616
}
618617

clang/include/clang/Lex/ModuleMap.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,14 @@ class ModuleMap {
541541
///
542542
/// \param IsExplicit Whether this is an explicit submodule.
543543
///
544-
/// \returns The found or newly-created module, along with a boolean value
545-
/// that will be true if the module is newly-created.
546-
std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
547-
bool IsFramework,
548-
bool IsExplicit);
544+
/// \returns The found or newly-created module.
545+
Module *findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
546+
bool IsExplicit);
547+
/// Create new submodule, assuming it does not exist. This function can only
548+
/// be called when it is guaranteed that this submodule does not exist yet.
549+
/// The parameters have same semantics as \c ModuleMap::findOrCreateModule.
550+
Module *createModule(StringRef Name, Module *Parent, bool IsFramework,
551+
bool IsExplicit);
549552

550553
/// Create a global module fragment for a C++ module unit.
551554
///

clang/lib/Basic/Module.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ Module::Module(ModuleConstructorTag, StringRef Name,
5454
NoUndeclaredIncludes = Parent->NoUndeclaredIncludes;
5555
ModuleMapIsPrivate = Parent->ModuleMapIsPrivate;
5656

57-
Parent->SubModuleIndex[Name] = Parent->SubModules.size();
5857
Parent->SubModules.push_back(this);
5958
}
6059
}
@@ -351,11 +350,14 @@ void Module::markUnavailable(bool Unimportable) {
351350
}
352351

353352
Module *Module::findSubmodule(StringRef Name) const {
354-
llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
355-
if (Pos == SubModuleIndex.end())
356-
return nullptr;
353+
// Add new submodules into the index.
354+
for (unsigned I = SubModuleIndex.size(), E = SubModules.size(); I != E; ++I)
355+
SubModuleIndex[SubModules[I]->Name] = I;
357356

358-
return SubModules[Pos->getValue()];
357+
if (auto It = SubModuleIndex.find(Name); It != SubModuleIndex.end())
358+
return SubModules[It->second];
359+
360+
return nullptr;
359361
}
360362

361363
Module *Module::getGlobalModuleFragment() const {

clang/lib/Lex/ModuleMap.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(FileEntryRef File) {
655655
SmallString<32> NameBuf;
656656
StringRef Name = sanitizeFilenameAsIdentifier(
657657
llvm::sys::path::stem(SkippedDir.getName()), NameBuf);
658-
Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
659-
Explicit).first;
658+
Result =
659+
findOrCreateModule(Name, Result, /*IsFramework=*/false, Explicit);
660660
setInferredModuleAllowedBy(Result, UmbrellaModuleMap);
661661

662662
// Associate the module and the directory.
@@ -672,8 +672,8 @@ ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(FileEntryRef File) {
672672
SmallString<32> NameBuf;
673673
StringRef Name = sanitizeFilenameAsIdentifier(
674674
llvm::sys::path::stem(File.getName()), NameBuf);
675-
Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
676-
Explicit).first;
675+
Result =
676+
findOrCreateModule(Name, Result, /*IsFramework=*/false, Explicit);
677677
setInferredModuleAllowedBy(Result, UmbrellaModuleMap);
678678
Result->addTopHeader(File);
679679

@@ -857,15 +857,21 @@ Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
857857
return Context->findSubmodule(Name);
858858
}
859859

860-
std::pair<Module *, bool> ModuleMap::findOrCreateModule(StringRef Name,
861-
Module *Parent,
862-
bool IsFramework,
863-
bool IsExplicit) {
860+
Module *ModuleMap::findOrCreateModule(StringRef Name, Module *Parent,
861+
bool IsFramework, bool IsExplicit) {
864862
// Try to find an existing module with this name.
865863
if (Module *Sub = lookupModuleQualified(Name, Parent))
866-
return std::make_pair(Sub, false);
864+
return Sub;
867865

868866
// Create a new module with this name.
867+
return createModule(Name, Parent, IsFramework, IsExplicit);
868+
}
869+
870+
Module *ModuleMap::createModule(StringRef Name, Module *Parent,
871+
bool IsFramework, bool IsExplicit) {
872+
assert(lookupModuleQualified(Name, Parent) == nullptr &&
873+
"Creating duplicate submodule");
874+
869875
Module *Result = new (ModulesAlloc.Allocate())
870876
Module(ModuleConstructorTag{}, Name, SourceLocation(), Parent,
871877
IsFramework, IsExplicit, NumCreatedModules++);
@@ -875,7 +881,7 @@ std::pair<Module *, bool> ModuleMap::findOrCreateModule(StringRef Name,
875881
Modules[Name] = Result;
876882
ModuleScopeIDs[Result] = CurrentModuleScopeID;
877883
}
878-
return std::make_pair(Result, true);
884+
return Result;
879885
}
880886

881887
Module *ModuleMap::createGlobalModuleFragmentForModuleUnit(SourceLocation Loc,
@@ -2124,8 +2130,7 @@ void ModuleMapParser::parseModuleDecl() {
21242130
Map.createShadowedModule(ModuleName, Framework, ShadowingModule);
21252131
} else {
21262132
ActiveModule =
2127-
Map.findOrCreateModule(ModuleName, ActiveModule, Framework, Explicit)
2128-
.first;
2133+
Map.findOrCreateModule(ModuleName, ActiveModule, Framework, Explicit);
21292134
}
21302135

21312136
ActiveModule->DefinitionLoc = ModuleNameLoc;

clang/lib/Serialization/ASTReader.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5756,6 +5756,13 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
57565756
return Err;
57575757

57585758
ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5759+
bool KnowsTopLevelModule = ModMap.findModule(F.ModuleName) != nullptr;
5760+
// If we don't know the top-level module, there's no point in doing qualified
5761+
// lookup of its submodules; it won't find anything anywhere within this tree.
5762+
// Let's skip that and avoid some string lookups.
5763+
auto CreateModule = !KnowsTopLevelModule ? &ModuleMap::createModule
5764+
: &ModuleMap::findOrCreateModule;
5765+
57595766
bool First = true;
57605767
Module *CurrentModule = nullptr;
57615768
RecordData Record;
@@ -5829,11 +5836,8 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
58295836
if (Parent)
58305837
ParentModule = getSubmodule(Parent);
58315838

5832-
// Retrieve this (sub)module from the module map, creating it if
5833-
// necessary.
5834-
CurrentModule =
5835-
ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5836-
.first;
5839+
CurrentModule = std::invoke(CreateModule, &ModMap, Name, ParentModule,
5840+
IsFramework, IsExplicit);
58375841

58385842
SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
58395843
if (GlobalIndex >= SubmodulesLoaded.size() ||

0 commit comments

Comments
 (0)