]> granicus.if.org Git - clang/commitdiff
clang-format: Cache results of formatting nested blocks.
authorDaniel Jasper <djasper@google.com>
Tue, 15 Apr 2014 08:13:47 +0000 (08:13 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 15 Apr 2014 08:13:47 +0000 (08:13 +0000)
This somewhat improves the performance problem reported in
llvm.org/PR18761. No other behavior changes intended.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@206258 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp

index b4eae19d47d376808dcfc706022abde458398ed2..2be21407f0c454964f765cf77197e2891e86df91 100644 (file)
@@ -721,6 +721,13 @@ public:
 
   unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun,
                   int AdditionalIndent = 0, bool FixBadIndentation = false) {
+    // Try to look up already computed penalty in DryRun-mode.
+    std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned> CacheKey{
+        &Lines, AdditionalIndent};
+    auto CacheIt = PenaltyCache.find(CacheKey);
+    if (DryRun && CacheIt != PenaltyCache.end())
+      return CacheIt->second;
+
     assert(!Lines.empty());
     unsigned Penalty = 0;
     std::vector<int> IndentForLevel;
@@ -844,6 +851,7 @@ public:
       }
       PreviousLine = *I;
     }
+    PenaltyCache[CacheKey] = Penalty;
     return Penalty;
   }
 
@@ -1149,6 +1157,12 @@ private:
   LineJoiner Joiner;
 
   llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
+
+  // Cache to store the penalty of formatting a vector of AnnotatedLines
+  // starting from a specific additional offset. Improves performance if there
+  // are many nested blocks.
+  std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>,
+           unsigned> PenaltyCache;
 };
 
 class FormatTokenLexer {