[clang] [StaticAnalyzer] Avoid repeated hash lookups (NFC) (PR #111272)

Kazu Hirata via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 5 19:55:29 PDT 2024


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/111272

None

>From 9a5ad987040ed16478cd119814dc99ebc3d9d177 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 5 Oct 2024 10:24:52 -0700
Subject: [PATCH] [StaticAnalyzer] Avoid repeated hash lookups (NFC)

---
 .../StaticAnalyzer/Core/CheckerManager.cpp    | 26 ++++++++-----------
 1 file changed, 11 insertions(+), 15 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Core/CheckerManager.cpp b/clang/lib/StaticAnalyzer/Core/CheckerManager.cpp
index ef617f5715c370..9f7a0fcc2edb36 100644
--- a/clang/lib/StaticAnalyzer/Core/CheckerManager.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CheckerManager.cpp
@@ -66,13 +66,10 @@ void CheckerManager::runCheckersOnASTDecl(const Decl *D, AnalysisManager& mgr,
   assert(D);
 
   unsigned DeclKind = D->getKind();
-  CachedDeclCheckers *checkers = nullptr;
-  CachedDeclCheckersMapTy::iterator CCI = CachedDeclCheckersMap.find(DeclKind);
-  if (CCI != CachedDeclCheckersMap.end()) {
-    checkers = &(CCI->second);
-  } else {
+  auto [CCI, Inserted] = CachedDeclCheckersMap.try_emplace(DeclKind);
+  CachedDeclCheckers *checkers = &(CCI->second);
+  if (Inserted) {
     // Find the checkers that should run for this Decl and cache them.
-    checkers = &CachedDeclCheckersMap[DeclKind];
     for (const auto &info : DeclCheckers)
       if (info.IsForDeclFn(D))
         checkers->push_back(info.CheckFn);
@@ -896,14 +893,13 @@ CheckerManager::getCachedStmtCheckersFor(const Stmt *S, bool isPreVisit) {
   assert(S);
 
   unsigned Key = (S->getStmtClass() << 1) | unsigned(isPreVisit);
-  CachedStmtCheckersMapTy::iterator CCI = CachedStmtCheckersMap.find(Key);
-  if (CCI != CachedStmtCheckersMap.end())
-    return CCI->second;
-
-  // Find the checkers that should run for this Stmt and cache them.
-  CachedStmtCheckers &Checkers = CachedStmtCheckersMap[Key];
-  for (const auto &Info : StmtCheckers)
-    if (Info.IsPreVisit == isPreVisit && Info.IsForStmtFn(S))
-      Checkers.push_back(Info.CheckFn);
+  auto [CCI, Inserted] = CachedStmtCheckersMap.try_emplace(Key);
+  CachedStmtCheckers &Checkers = CCI->second;
+  if (Inserted) {
+    // Find the checkers that should run for this Stmt and cache them.
+    for (const auto &Info : StmtCheckers)
+      if (Info.IsPreVisit == isPreVisit && Info.IsForStmtFn(S))
+        Checkers.push_back(Info.CheckFn);
+  }
   return Checkers;
 }



More information about the cfe-commits mailing list