[PATCH] D45479: [Tooling] Optimize memory usage in InMemoryToolResults.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 10 07:22:57 PDT 2018


hokein updated this revision to Diff 141842.
hokein edited the summary of this revision.
hokein added a comment.

Add a comment.


Repository:
  rC Clang

https://reviews.llvm.org/D45479

Files:
  include/clang/Tooling/Execution.h
  lib/Tooling/AllTUsExecution.cpp
  lib/Tooling/Execution.cpp


Index: lib/Tooling/Execution.cpp
===================================================================
--- lib/Tooling/Execution.cpp
+++ lib/Tooling/Execution.cpp
@@ -21,10 +21,19 @@
                  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::pair<std::string, std::string>>
+std::vector<std::pair<llvm::StringRef, llvm::StringRef>>
 InMemoryToolResults::AllKVResults() {
   return KVResults;
 }
Index: lib/Tooling/AllTUsExecution.cpp
===================================================================
--- lib/Tooling/AllTUsExecution.cpp
+++ lib/Tooling/AllTUsExecution.cpp
@@ -36,7 +36,8 @@
     Results.addResult(Key, Value);
   }
 
-  std::vector<std::pair<std::string, std::string>> AllKVResults() override {
+  std::vector<std::pair<llvm::StringRef, llvm::StringRef>>
+  AllKVResults() override {
     return Results.AllKVResults();
   }
 
Index: include/clang/Tooling/Execution.h
===================================================================
--- include/clang/Tooling/Execution.h
+++ 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 @@
 public:
   virtual ~ToolResults() = default;
   virtual void addResult(StringRef Key, StringRef Value) = 0;
-  virtual std::vector<std::pair<std::string, std::string>> AllKVResults() = 0;
+  virtual std::vector<std::pair<llvm::StringRef, llvm::StringRef>>
+  AllKVResults() = 0;
   virtual void forEachResult(
       llvm::function_ref<void(StringRef Key, StringRef Value)> 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<std::pair<std::string, std::string>> AllKVResults() override;
+  std::vector<std::pair<llvm::StringRef, llvm::StringRef>>
+  AllKVResults() override;
   void forEachResult(llvm::function_ref<void(StringRef Key, StringRef Value)>
                          Callback) override;
 
 private:
-  std::vector<std::pair<std::string, std::string>> KVResults;
+  llvm::BumpPtrAllocator Arena;
+  llvm::StringSaver StringsPool;
+  llvm::DenseSet<llvm::StringRef> Strings;
+
+  std::vector<std::pair<llvm::StringRef, llvm::StringRef>> KVResults;
 };
 
 /// \brief The context of an execution, including the information about


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45479.141842.patch
Type: text/x-patch
Size: 3085 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180410/3bdb08f9/attachment-0001.bin>


More information about the cfe-commits mailing list