-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NFC][clang-tidy]improve performance for misc-unused-using-decls check #78231
[NFC][clang-tidy]improve performance for misc-unused-using-decls check #78231
Conversation
`UnusedUsingDeclsCheck::removeFromFoundDecls` will be called with high frequency. At current time it will check every `Context`. This patch adds a cache to reduce algorithm complexitiy.
@llvm/pr-subscribers-clang-tidy Author: Congcong Cai (HerrCai0907) Changes
Full diff: https://github.com/llvm/llvm-project/pull/78231.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
index 051375263e53c3..59e487bab31195 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -117,8 +117,10 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
/*SkipTrailingWhitespaceAndNewLine=*/true));
for (const auto *UsingShadow : Using->shadows()) {
const auto *TargetDecl = UsingShadow->getTargetDecl()->getCanonicalDecl();
- if (shouldCheckDecl(TargetDecl))
+ if (shouldCheckDecl(TargetDecl)) {
Context.UsingTargetDecls.insert(TargetDecl);
+ UsingTargetDeclsCache.insert(TargetDecl);
+ }
}
if (!Context.UsingTargetDecls.empty())
Contexts.push_back(Context);
@@ -201,13 +203,16 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
if (!D)
return;
+ const Decl *CanonicalDecl = D->getCanonicalDecl();
+ if (!UsingTargetDeclsCache.contains(CanonicalDecl))
+ return;
// FIXME: Currently, we don't handle the using-decls being used in different
// scopes (such as different namespaces, different functions). Instead of
// giving an incorrect message, we mark all of them as used.
- //
- // FIXME: Use a more efficient way to find a matching context.
for (auto &Context : Contexts) {
- if (Context.UsingTargetDecls.contains(D->getCanonicalDecl()))
+ if (Context.IsUsed)
+ continue;
+ if (Context.UsingTargetDecls.contains(CanonicalDecl))
Context.IsUsed = true;
}
}
@@ -224,6 +229,7 @@ void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
}
}
Contexts.clear();
+ UsingTargetDeclsCache.clear();
}
} // namespace clang::tidy::misc
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
index 498b3ffd2678c3..7bdaf12e8aecee 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
@@ -49,6 +49,7 @@ class UnusedUsingDeclsCheck : public ClangTidyCheck {
};
std::vector<UsingDeclContext> Contexts;
+ llvm::SmallPtrSet<const Decl *, 32> UsingTargetDeclsCache;
StringRef RawStringHeaderFileExtensions;
FileExtensionsSet HeaderFileExtensions;
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
There is open issue for performance of this check.
Consider mentioning this in release notes as some generic performance improvment.
Do you have any benchmarks ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, the code looks good. Do you have any performance data after this change?
I don't run clang-tidy bench for some large project. But I do some test for some auto-generated cpp files which has lots of using, and the execution time reduce from 0.2s to 0.18s in my MAC M1 Pro. |
I have also seen this issue. But after optimization, this check is still slow. I guess registering too many pattern causes performance issue but I cannot find a good solution to optimize it. |
llvm#78231) `UnusedUsingDeclsCheck::removeFromFoundDecls` will be called with high frequency. At current time it will check every `Context`. This patch adds a cache to reduce algorithm complexity.
UnusedUsingDeclsCheck::removeFromFoundDecls
will be called with high frequency. At current time it will check everyContext
.This patch adds a cache to reduce algorithm complexity.