[PATCH] D150257: [clangd] Initialize clang-tidy modules only once
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed May 10 01:44:58 PDT 2023
kadircet created this revision.
kadircet added a reviewer: sammccall.
Herald added subscribers: carlosgalvezp, arphaman.
Herald added a project: All.
kadircet requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.
This is showing up on our profiles with ~100ms contribution @95th% for
buildAST latencies.
The patch is unlikely to address it all, but should help with some low-hanging
fruit.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D150257
Files:
clang-tools-extra/clangd/ParsedAST.cpp
clang-tools-extra/clangd/TidyProvider.cpp
Index: clang-tools-extra/clangd/TidyProvider.cpp
===================================================================
--- clang-tools-extra/clangd/TidyProvider.cpp
+++ clang-tools-extra/clangd/TidyProvider.cpp
@@ -8,6 +8,7 @@
#include "TidyProvider.h"
#include "../clang-tidy/ClangTidyModuleRegistry.h"
+#include "../clang-tidy/ClangTidyOptions.h"
#include "Config.h"
#include "support/FileCache.h"
#include "support/Logger.h"
@@ -283,11 +284,18 @@
tidy::ClangTidyOptions getTidyOptionsForFile(TidyProviderRef Provider,
llvm::StringRef Filename) {
- tidy::ClangTidyOptions Opts = tidy::ClangTidyOptions::getDefaults();
- Opts.Checks->clear();
- if (Provider)
- Provider(Opts, Filename);
- return Opts;
+ // getDefaults instantiates all check factories, which are registered at link
+ // time. So cache the results once.
+ static const auto Opts = [] {
+ auto Opts = tidy::ClangTidyOptions::getDefaults();
+ Opts.Checks->clear();
+ return Opts;
+ }();
+ if (!Provider)
+ return Opts;
+ auto NewOpts = Opts;
+ Provider(NewOpts, Filename);
+ return NewOpts;
}
bool isRegisteredTidyCheck(llvm::StringRef Check) {
Index: clang-tools-extra/clangd/ParsedAST.cpp
===================================================================
--- clang-tools-extra/clangd/ParsedAST.cpp
+++ clang-tools-extra/clangd/ParsedAST.cpp
@@ -9,6 +9,7 @@
#include "ParsedAST.h"
#include "../clang-tidy/ClangTidyCheck.h"
#include "../clang-tidy/ClangTidyDiagnosticConsumer.h"
+#include "../clang-tidy/ClangTidyModule.h"
#include "../clang-tidy/ClangTidyModuleRegistry.h"
#include "AST.h"
#include "Compiler.h"
@@ -25,7 +26,6 @@
#include "TidyProvider.h"
#include "clang-include-cleaner/Record.h"
#include "index/CanonicalIncludes.h"
-#include "index/Index.h"
#include "index/Symbol.h"
#include "support/Logger.h"
#include "support/Trace.h"
@@ -50,7 +50,6 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
-#include <algorithm>
#include <memory>
#include <optional>
#include <vector>
@@ -476,9 +475,12 @@
// diagnostics.
if (PreserveDiags) {
trace::Span Tracer("ClangTidyInit");
- tidy::ClangTidyCheckFactories CTFactories;
- for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
- E.instantiate()->addCheckFactories(CTFactories);
+ static const tidy::ClangTidyCheckFactories CTFactories = [] {
+ tidy::ClangTidyCheckFactories CTFactories;
+ for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
+ E.instantiate()->addCheckFactories(CTFactories);
+ return CTFactories;
+ }();
CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>(
tidy::ClangTidyGlobalOptions(), ClangTidyOpts));
CTContext->setDiagnosticsEngine(&Clang->getDiagnostics());
@@ -610,10 +612,8 @@
Macros = Patch->mainFileMacros();
Marks = Patch->marks();
}
- auto& PP = Clang->getPreprocessor();
- PP.addPPCallbacks(
- std::make_unique<CollectMainFileMacros>(
- PP, Macros));
+ auto &PP = Clang->getPreprocessor();
+ PP.addPPCallbacks(std::make_unique<CollectMainFileMacros>(PP, Macros));
PP.addPPCallbacks(
collectPragmaMarksCallback(Clang->getSourceManager(), Marks));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150257.520934.patch
Type: text/x-patch
Size: 3298 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230510/e77ab750/attachment.bin>
More information about the cfe-commits
mailing list