[PATCH] D117529: [clangd][NFC] Cache ClangTidy check globs to speed up createChecks

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 19 06:18:30 PST 2022


njames93 updated this revision to Diff 401209.
njames93 added a comment.

Remove now unnecessary changes in ClangTidyModule.
Fix Cache miss reporting


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117529/new/

https://reviews.llvm.org/D117529

Files:
  clang-tools-extra/clangd/ParsedAST.cpp


Index: clang-tools-extra/clangd/ParsedAST.cpp
===================================================================
--- clang-tools-extra/clangd/ParsedAST.cpp
+++ clang-tools-extra/clangd/ParsedAST.cpp
@@ -10,6 +10,7 @@
 #include "../clang-tidy/ClangTidyCheck.h"
 #include "../clang-tidy/ClangTidyDiagnosticConsumer.h"
 #include "../clang-tidy/ClangTidyModuleRegistry.h"
+#include "../clang-tidy/GlobList.h"
 #include "AST.h"
 #include "Compiler.h"
 #include "Config.h"
@@ -282,6 +283,41 @@
   }
 }
 
+std::vector<std::unique_ptr<tidy::ClangTidyCheck>>
+buildChecks(tidy::ClangTidyContext *Context) {
+  using FactoryFunc = std::function<std::unique_ptr<tidy::ClangTidyCheck>(
+      llvm::StringRef, tidy::ClangTidyContext *)>;
+  const llvm::Optional<std::string> &CheckGlob = Context->getOptions().Checks;
+  if (!CheckGlob || CheckGlob->empty())
+    return {};
+  thread_local std::pair<std::string,
+                         std::vector<std::pair<std::string, FactoryFunc>>>
+      Cached;
+  static std::atomic<size_t> CacheMiss, CacheAccess;
+  ++CacheAccess;
+  if (*CheckGlob != Cached.first) {
+    ++CacheMiss;
+    vlog("ClangTidyCheck factory cache miss '{0:P2}%'\n"
+         "Rebuilding ClangTidyChecks factory with new glob '{1}'",
+         static_cast<double>(CacheMiss) / CacheAccess, *CheckGlob);
+    Cached.first.assign(CheckGlob->data(), CheckGlob->size());
+    Cached.second.clear();
+    tidy::ClangTidyCheckFactories CTFactories;
+    for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
+      E.instantiate()->addCheckFactories(CTFactories);
+    tidy::GlobList Glob(*CheckGlob);
+    for (const auto &Check : CTFactories) {
+      if (Glob.contains(Check.getKey()))
+        Cached.second.emplace_back(Check.getKey(), Check.getValue());
+    }
+  }
+  std::vector<std::unique_ptr<tidy::ClangTidyCheck>> Result;
+  Result.reserve(Cached.second.size());
+  for (auto &Factory : Cached.second) {
+    Result.push_back(Factory.second(Factory.first, Context));
+  }
+  return Result;
+}
 } // namespace
 
 llvm::Optional<ParsedAST>
@@ -410,15 +446,12 @@
   // diagnostics.
   if (PreserveDiags) {
     trace::Span Tracer("ClangTidyInit");
-    tidy::ClangTidyCheckFactories CTFactories;
-    for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
-      E.instantiate()->addCheckFactories(CTFactories);
     CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>(
         tidy::ClangTidyGlobalOptions(), ClangTidyOpts));
     CTContext->setDiagnosticsEngine(&Clang->getDiagnostics());
     CTContext->setASTContext(&Clang->getASTContext());
     CTContext->setCurrentFile(Filename);
-    CTChecks = CTFactories.createChecks(CTContext.getPointer());
+    CTChecks = buildChecks(CTContext.getPointer());
     llvm::erase_if(CTChecks, [&](const auto &Check) {
       return !Check->isLanguageVersionSupported(CTContext->getLangOpts());
     });


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117529.401209.patch
Type: text/x-patch
Size: 2895 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220119/2cf1538d/attachment-0001.bin>


More information about the cfe-commits mailing list