[clang] [clang] Speed up Decl::getASTContext() (PR #226612)
Nico Weber via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 4 11:14:52 PDT 2026
================
@@ -548,7 +548,15 @@ TranslationUnitDecl *Decl::getTranslationUnitDecl() {
}
ASTContext &Decl::getASTContext() const {
- return getTranslationUnitDecl()->getASTContext();
+ if (const auto *TUD = dyn_cast<TranslationUnitDecl>(this))
+ return TUD->getASTContext();
----------------
nico wrote:
I could! But checking `this`'s type is very cheap, and then we don't have to `getDeclContext()` for TranslationUnitDecls. Looking up things globally is presumably common and it's nice for this to be fast.
But to be honest, I haven't measured how just
```
ASTContext &Decl::getASTContext() const {
if (!DC->CachedASTContext)
DC->CachedASTContext = &getTranslationUnitDecl()->getASTContext();
return *DC->CachedASTContext;
}
```
would do. I should probably measure it. I'll do so and report back.
https://github.com/llvm/llvm-project/pull/226612
More information about the cfe-commits
mailing list