[clang] 119a728 - [clang-format][NFC] Remove redundant calls to guessIsObjC()

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 20 00:28:33 PST 2024


Author: Owen Pan
Date: 2024-02-20T00:28:26-08:00
New Revision: 119a72866f0e143127355fe6e03f57c4f8bab1ae

URL: https://github.com/llvm/llvm-project/commit/119a72866f0e143127355fe6e03f57c4f8bab1ae
DIFF: https://github.com/llvm/llvm-project/commit/119a72866f0e143127355fe6e03f57c4f8bab1ae.diff

LOG: [clang-format][NFC] Remove redundant calls to guessIsObjC()

Running clang-format on the following input
```
int lambdas() {
  return [&] {
  return [&] {
  return [&] {
  return [&] {
  return [&] {
  return [&] {
  return [&] { return 3; } ();
  } (); } (); } (); } (); } (); } (); }
```
will finish instantly if you pass clang-format a .cpp input with this
content, but hang for tens of seconds if you pass the same via stdin
or a .h file.

Adding some debug statements showed that guessIsObjC was getting called
tens of millions of times in a manner that scales very rapidly with the
amount of nesting (if clang-format just takes a few seconds with that
input passed on stdin, try adding a couple more levels of nesting).

This change moves the recursive guessIsObjC call one level of nesting
out of an inner loop whose iterations don't affect the input to the
recursive call. This resolves the performance issue.

Authored-by: davidvc1 and Uran198

Differential Revision: https://reviews.llvm.org/D114837
Differential Revision: https://reviews.llvm.org/D47515

Added: 
    

Modified: 
    clang/lib/Format/Format.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index e67b2101f5821b..2c815128b1a59d 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2948,9 +2948,9 @@ class ObjCHeaderStyleGuesser : public TokenAnalyzer {
                      << getTokenTypeName(FormatTok->getType()) << "\n");
           return true;
         }
-        if (guessIsObjC(SourceManager, Line->Children, Keywords))
-          return true;
       }
+      if (guessIsObjC(SourceManager, Line->Children, Keywords))
+        return true;
     }
     return false;
   }


        


More information about the cfe-commits mailing list