================
@@ -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();
+
+  // This is called a lot, e.g. for every attribute lookup. Only walk up to the
+  // TranslationUnitDecl once per DeclContext.
+  const DeclContext *DC = getDeclContext();
+  if (!DC->CachedASTContext)
----------------
nico wrote:

That's a good suggestion, I like it!

…but I tried it and it's slower:

```
60 TUs
pr         cpu    148.4 s  instr    1514.2e9  rss   529.0 MB
erich      cpu    149.6 s  instr    1519.6e9  rss   527.7 MB
main       cpu    151.6 s  instr    1529.2e9  rss   526.0 MB
pr2        cpu    149.0 s  instr    1514.6e9  rss   527.6 MB
erich2     cpu    149.5 s  instr    1519.6e9  rss   528.0 MB
```

Every line is the benchmark run twice, and both branches are ran twice to 
hopefully account for machine drift. For good measure, I ran the whole thing a 
2nd time:

```
pr         cpu    148.3 s  instr    1515.0e9  rss   528.3 MB
erich      cpu    149.2 s  instr    1520.4e9  rss   528.8 MB
main       cpu    151.1 s  instr    1530.3e9  rss   527.5 MB
pr2        cpu    148.6 s  instr    1514.9e9  rss   528.3 MB
erich2     cpu    149.4 s  instr    1519.9e9  rss   528.3 MB
```

So that's fairly consistent. About a 1% saving (on my machine) with the patch 
as-is, and about 0.6% with the suggested variant.

https://github.com/llvm/llvm-project/pull/226612
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to