[PATCH] D114837: format: Remove redundant calls to guessIsObjC to speed up clang-format on unknown file types

David Van Cleve via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 30 18:26:45 PST 2021


davidvc1 created this revision.
davidvc1 added a reviewer: curdeius.
davidvc1 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114837

Files:
  clang/lib/Format/Format.cpp


Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -2365,9 +2365,9 @@
                      << getTokenTypeName(FormatTok->getType()) << "\n");
           return true;
         }
-        if (guessIsObjC(SourceManager, Line->Children, Keywords))
-          return true;
       }
+      if (guessIsObjC(SourceManager, Line->Children, Keywords))
+        return true;
     }
     return false;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114837.390879.patch
Type: text/x-patch
Size: 516 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211201/5c3591d4/attachment-0001.bin>


More information about the cfe-commits mailing list