summaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2014-04-15 08:13:47 +0000
committerDaniel Jasper <djasper@google.com>2014-04-15 08:13:47 +0000
commitdb5783685e00d5cc5cd9b0d9a44e968e5d04590b (patch)
treecb07ab32a917ae45e99248e2fd99b462faf10ee7 /lib/Format/Format.cpp
parent52b4da7388815cac3a9d53c136121bcd22da23aa (diff)
downloadclang-db5783685e00d5cc5cd9b0d9a44e968e5d04590b.tar.gz
clang-db5783685e00d5cc5cd9b0d9a44e968e5d04590b.tar.bz2
clang-db5783685e00d5cc5cd9b0d9a44e968e5d04590b.tar.xz
clang-format: Cache results of formatting nested blocks.
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
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index b4eae19d47..2be21407f0 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -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 {