[clang] [C++20][Modules] Improve namespace look-up performance for modules. (PR #171769)
Chuanqi Xu via cfe-commits
cfe-commits at lists.llvm.org
Sun Dec 14 18:53:51 PST 2025
================
@@ -4432,14 +4432,35 @@ class ASTDeclContextNameLookupTrait
TULocalDeclsMap.insert({D->getDeclName(), DeclIDsTy{ID}});
else
Iter->second.push_back(ID);
- continue;
+ return;
}
case LookupVisibility::GenerallyVisibile:
// Generally visible decls go into the general lookup table.
break;
}
DeclIDs.push_back(ID);
+ };
+ for (NamedDecl *D : Decls) {
+ if (isa<NamespaceDecl>(D) && D->isFromASTFile()) {
+ // In ASTReader, we stored only the key declaration of a namespace decl
+ // for this TU rather than storing all of the key declarations from each
+ // imported module. If we have an external namespace decl, this is that
+ // key declaration and we need to re-expand it to write out all of the
+ // key declarations from each imported module again.
+ //
+ // See comment 'ASTReader::FindExternalVisibleDeclsByName' for details.
+ ASTReader *Chain = Writer.getChain();
+ assert(Chain && "An external namespace decl without an ASTReader");
+ assert(D == Chain->getKeyDeclaration(D) &&
+ "An external namespace decl that is not "
+ "key declaration of this TU");
----------------
ChuanqiXu9 wrote:
> Hm... Is what you're saying that, since population of StoredDeclsMap in DeclContext depends on just some ExternalASTSource rather than ASTReader, we can't make the same assumptions about its state based on this "new behavior" of ASTReader?
Yeah, and also, we may call `ASTDeclContextNameLookupTrait::getData` in future changes, then the assertion fails.
I feel more comfortable to change the condition to:
```
if (isa<NamespaceDecl>(D) && D->isFromASTFile() && D == Chain->getKeyDeclaration(D))
```
and we should use a test to make sure the regression doesn't happen.
https://github.com/llvm/llvm-project/pull/171769
More information about the cfe-commits
mailing list