From 9f36c7e704ea7a604a7962452b15dc8646659744 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Wed, 11 Apr 2018 08:13:07 +0000 Subject: [PATCH] [Tooling] Optimize memory usage in InMemoryToolResults. Avoid storing duplicated "std::string"s. clangd's global-symbol-builder takes 20+GB memory running across LLVM repository. With this patch, the used memory is ~10GB (running on 48 threads, most of meory are AST-related). Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D45479 llvm-svn: 329784 --- clang/include/clang/Tooling/Execution.h | 17 ++++++++++++++--- clang/lib/Tooling/AllTUsExecution.cpp | 3 ++- clang/lib/Tooling/Execution.cpp | 13 +++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Tooling/Execution.h b/clang/include/clang/Tooling/Execution.h index 1a44c478..a3036bd 100644 --- a/clang/include/clang/Tooling/Execution.h +++ b/clang/include/clang/Tooling/Execution.h @@ -32,6 +32,7 @@ #include "clang/Tooling/Tooling.h" #include "llvm/Support/Error.h" #include "llvm/Support/Registry.h" +#include "llvm/Support/StringSaver.h" namespace clang { namespace tooling { @@ -45,20 +46,30 @@ class ToolResults { public: virtual ~ToolResults() = default; virtual void addResult(StringRef Key, StringRef Value) = 0; - virtual std::vector> AllKVResults() = 0; + virtual std::vector> + AllKVResults() = 0; virtual void forEachResult( llvm::function_ref Callback) = 0; }; +/// \brief Stores the key-value results in memory. It maintains the lifetime of +/// the result. Clang tools using this class are expected to generate a small +/// set of different results, or a large set of duplicated results. class InMemoryToolResults : public ToolResults { public: + InMemoryToolResults() : StringsPool(Arena) {} void addResult(StringRef Key, StringRef Value) override; - std::vector> AllKVResults() override; + std::vector> + AllKVResults() override; void forEachResult(llvm::function_ref Callback) override; private: - std::vector> KVResults; + llvm::BumpPtrAllocator Arena; + llvm::StringSaver StringsPool; + llvm::DenseSet Strings; + + std::vector> KVResults; }; /// \brief The context of an execution, including the information about diff --git a/clang/lib/Tooling/AllTUsExecution.cpp b/clang/lib/Tooling/AllTUsExecution.cpp index 0c0854d..b761556 100644 --- a/clang/lib/Tooling/AllTUsExecution.cpp +++ b/clang/lib/Tooling/AllTUsExecution.cpp @@ -36,7 +36,8 @@ public: Results.addResult(Key, Value); } - std::vector> AllKVResults() override { + std::vector> + AllKVResults() override { return Results.AllKVResults(); } diff --git a/clang/lib/Tooling/Execution.cpp b/clang/lib/Tooling/Execution.cpp index ff68f85..5d6559f 100644 --- a/clang/lib/Tooling/Execution.cpp +++ b/clang/lib/Tooling/Execution.cpp @@ -21,10 +21,19 @@ static llvm::cl::opt llvm::cl::init("standalone")); void InMemoryToolResults::addResult(StringRef Key, StringRef Value) { - KVResults.push_back({Key.str(), Value.str()}); + auto Intern = [&](StringRef &V) { + auto R = Strings.insert(V); + if (R.second) { // A new entry, create a new string copy. + *R.first = StringsPool.save(V); + } + V = *R.first; + }; + Intern(Key); + Intern(Value); + KVResults.push_back({Key, Value}); } -std::vector> +std::vector> InMemoryToolResults::AllKVResults() { return KVResults; } -- 2.7.4