[clang-tools-extra] r340161 - [clangd] Simplify the code using UniqueStringSaver, NFC.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 20 02:47:12 PDT 2018


Author: hokein
Date: Mon Aug 20 02:47:12 2018
New Revision: 340161

URL: http://llvm.org/viewvc/llvm-project?rev=340161&view=rev
Log:
[clangd] Simplify the code using UniqueStringSaver, NFC.

Modified:
    clang-tools-extra/trunk/clangd/index/Index.cpp
    clang-tools-extra/trunk/clangd/index/Index.h

Modified: clang-tools-extra/trunk/clangd/index/Index.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/Index.cpp?rev=340161&r1=340160&r2=340161&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/Index.cpp (original)
+++ clang-tools-extra/trunk/clangd/index/Index.cpp Mon Aug 20 02:47:12 2018
@@ -77,16 +77,10 @@ SymbolSlab::const_iterator SymbolSlab::f
 }
 
 // Copy the underlying data of the symbol into the owned arena.
-static void own(Symbol &S, DenseSet<StringRef> &Strings,
+static void own(Symbol &S, llvm::UniqueStringSaver &Strings,
                 BumpPtrAllocator &Arena) {
   // Intern replaces V with a reference to the same string owned by the arena.
-  auto Intern = [&](StringRef &V) {
-    auto R = Strings.insert(V);
-    if (R.second) { // New entry added to the table, copy the string.
-      *R.first = V.copy(Arena);
-    }
-    V = *R.first;
-  };
+  auto Intern = [&](StringRef &V) { V = Strings.save(V); };
 
   // We need to copy every StringRef field onto the arena.
   Intern(S.Name);
@@ -114,10 +108,10 @@ void SymbolSlab::Builder::insert(const S
   auto R = SymbolIndex.try_emplace(S.ID, Symbols.size());
   if (R.second) {
     Symbols.push_back(S);
-    own(Symbols.back(), Strings, Arena);
+    own(Symbols.back(), UniqueStrings, Arena);
   } else {
     auto &Copy = Symbols[R.first->second] = S;
-    own(Copy, Strings, Arena);
+    own(Copy, UniqueStrings, Arena);
   }
 }
 
@@ -128,7 +122,7 @@ SymbolSlab SymbolSlab::Builder::build()
             [](const Symbol &L, const Symbol &R) { return L.ID < R.ID; });
   // We may have unused strings from overwritten symbols. Build a new arena.
   BumpPtrAllocator NewArena;
-  DenseSet<StringRef> Strings;
+  llvm::UniqueStringSaver Strings(NewArena);
   for (auto &S : Symbols)
     own(S, Strings, NewArena);
   return SymbolSlab(std::move(NewArena), std::move(Symbols));

Modified: clang-tools-extra/trunk/clangd/index/Index.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/Index.h?rev=340161&r1=340160&r2=340161&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/Index.h (original)
+++ clang-tools-extra/trunk/clangd/index/Index.h Mon Aug 20 02:47:12 2018
@@ -17,6 +17,7 @@
 #include "llvm/ADT/Hashing.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/StringSaver.h"
 #include <array>
 #include <string>
 
@@ -257,6 +258,8 @@ public:
   // The frozen SymbolSlab will use less memory.
   class Builder {
   public:
+    Builder() : UniqueStrings(Arena) {}
+
     // Adds a symbol, overwriting any existing one with the same ID.
     // This is a deep copy: underlying strings will be owned by the slab.
     void insert(const Symbol &S);
@@ -273,7 +276,7 @@ public:
   private:
     llvm::BumpPtrAllocator Arena;
     // Intern table for strings. Contents are on the arena.
-    llvm::DenseSet<llvm::StringRef> Strings;
+    llvm::UniqueStringSaver UniqueStrings;
     std::vector<Symbol> Symbols;
     // Values are indices into Symbols vector.
     llvm::DenseMap<SymbolID, size_t> SymbolIndex;




More information about the cfe-commits mailing list