Skip to content
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

Merged
merged 1 commit into from
Jan 16, 2024

Conversation

HerrCai0907
Copy link
Contributor

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 every `Context`.
This patch adds a cache to reduce algorithm complexitiy.
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 16, 2024

@llvm/pr-subscribers-clang-tidy

Author: Congcong Cai (HerrCai0907)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/78231.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp (+10-4)
  • (modified) clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h (+1)
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;

@HerrCai0907 HerrCai0907 requested review from hokein, alexfh, kazutakahirata and PiotrZSL and removed request for kazutakahirata January 16, 2024 05:07
Copy link
Member

@PiotrZSL PiotrZSL left a 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 ?

Copy link
Collaborator

@hokein hokein left a 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?

@HerrCai0907
Copy link
Contributor Author

HerrCai0907 commented Jan 16, 2024

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.

@HerrCai0907 HerrCai0907 merged commit dca6f60 into main Jan 16, 2024
5 checks passed
@HerrCai0907 HerrCai0907 deleted the users/ccc/improve-pref-UnusedUsingDeclsCheck branch January 16, 2024 09:45
@HerrCai0907
Copy link
Contributor Author

LGTM. There is open issue for performance of this check. Consider mentioning this in release notes as some generic performance improvement.

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.

justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants