From 2787fb1237c9b13ef108ce42d19eb00b53e9c373 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Wed, 29 Apr 2026 13:56:46 -0700 Subject: [PATCH] [clang][modules] Always keep submodule index up-to-date (#194039) This partially reverts #113391, always keeping the submodule index up-to-date. This is important for a follow-up PR that will make deserialization of submodules lazier. Without this change, updating the index would deserialize submodules too eagerly. The original PR only showed 0.5% improvement in scan times, while the upcoming PR promises an order of magnitude larger improvement. --- clang/include/clang/Basic/Module.h | 3 ++- clang/lib/Basic/Module.cpp | 5 +---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/clang/include/clang/Basic/Module.h b/clang/include/clang/Basic/Module.h index 03a5d460e471..f83319db082d 100644 --- a/clang/include/clang/Basic/Module.h +++ b/clang/include/clang/Basic/Module.h @@ -352,7 +352,7 @@ private: /// A mapping from the submodule name to the index into the /// \c SubModules vector at which that submodule resides. - mutable llvm::StringMap SubModuleIndex; + llvm::StringMap SubModuleIndex; /// The AST file name and key if this is a top-level module which has a /// corresponding serialized AST file, or null otherwise. @@ -738,6 +738,7 @@ public: void setParent(Module *M) { assert(!Parent); Parent = M; + Parent->SubModuleIndex[M->Name] = Parent->SubModules.size(); Parent->SubModules.push_back(this); } diff --git a/clang/lib/Basic/Module.cpp b/clang/lib/Basic/Module.cpp index 7b34f45276db..66629baa6240 100644 --- a/clang/lib/Basic/Module.cpp +++ b/clang/lib/Basic/Module.cpp @@ -53,6 +53,7 @@ Module::Module(ModuleConstructorTag, StringRef Name, NoUndeclaredIncludes = Parent->NoUndeclaredIncludes; ModuleMapIsPrivate = Parent->ModuleMapIsPrivate; + Parent->SubModuleIndex[Name] = Parent->SubModules.size(); Parent->SubModules.push_back(this); } } @@ -348,10 +349,6 @@ void Module::markUnavailable(bool Unimportable) { } Module *Module::findSubmodule(StringRef Name) const { - // Add new submodules into the index. - for (unsigned I = SubModuleIndex.size(), E = SubModules.size(); I != E; ++I) - SubModuleIndex[SubModules[I]->Name] = I; - if (auto It = SubModuleIndex.find(Name); It != SubModuleIndex.end()) return SubModules[It->second];