[Symbolize] Replace map<SymbolDesc, StringRef> with sorted vector
authorFangrui Song <maskray@google.com>
Fri, 5 Apr 2019 12:52:04 +0000 (12:52 +0000)
committerFangrui Song <maskray@google.com>
Fri, 5 Apr 2019 12:52:04 +0000 (12:52 +0000)
llvm-svn: 357758

llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp
llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.h

index 889b0c1..a92fe12 100644 (file)
@@ -78,6 +78,14 @@ SymbolizableObjectFile::create(object::ObjectFile *Obj,
       if (auto EC = res->addCoffExportSymbols(CoffObj))
         return EC;
   }
+
+  std::vector<std::pair<SymbolDesc, StringRef>> &Fs = res->Functions,
+                                                &Os = res->Objects;
+  llvm::sort(Fs);
+  Fs.erase(std::unique(Fs.begin(), Fs.end()), Fs.end());
+  llvm::sort(Os);
+  Os.erase(std::unique(Os.begin(), Os.end()), Os.end());
+
   return std::move(res);
 }
 
@@ -127,7 +135,7 @@ std::error_code SymbolizableObjectFile::addCoffExportSymbols(
     uint64_t SymbolStart = ImageBase + Export.Offset;
     uint64_t SymbolSize = NextOffset - Export.Offset;
     SymbolDesc SD = {SymbolStart, SymbolSize};
-    Functions.insert(std::make_pair(SD, Export.Name));
+    Functions.emplace_back(SD, Export.Name);
   }
   return std::error_code();
 }
@@ -174,7 +182,7 @@ std::error_code SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
   // with same address size. Make sure we choose the correct one.
   auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
   SymbolDesc SD = { SymbolAddress, SymbolSize };
-  M.insert(std::make_pair(SD, SymbolName));
+  M.emplace_back(SD, SymbolName);
   return std::error_code();
 }
 
@@ -195,15 +203,13 @@ bool SymbolizableObjectFile::getNameFromSymbolTable(SymbolRef::Type Type,
                                                     std::string &Name,
                                                     uint64_t &Addr,
                                                     uint64_t &Size) const {
-  const auto &SymbolMap = Type == SymbolRef::ST_Function ? Functions : Objects;
-  if (SymbolMap.empty())
-    return false;
-  SymbolDesc SD = {Address, UINT64_C(-1)};
+  const auto &Symbols = Type == SymbolRef::ST_Function ? Functions : Objects;
+  std::pair<SymbolDesc, StringRef> SD{{Address, UINT64_C(-1)}, StringRef()};
   // SymbolDescs are sorted by (Addr,Size), if several SymbolDescs share the
   // same Addr, pick the one with the largest Size. This helps us avoid symbols
   // with no size information (Size=0).
-  auto SymbolIterator = SymbolMap.upper_bound(SD);
-  if (SymbolIterator == SymbolMap.begin())
+  auto SymbolIterator = llvm::upper_bound(Symbols, SD);
+  if (SymbolIterator == Symbols.begin())
     return false;
   --SymbolIterator;
   if (SymbolIterator->first.Size != 0 &&
index d811dc2..ed2f89b 100644 (file)
@@ -75,12 +75,15 @@ private:
     // the following symbol.
     uint64_t Size;
 
+    bool operator==(const SymbolDesc &RHS) const {
+      return Addr == RHS.Addr && Size == RHS.Size;
+    }
     bool operator<(const SymbolDesc &RHS) const {
       return Addr != RHS.Addr ? Addr < RHS.Addr : Size < RHS.Size;
     }
   };
-  std::map<SymbolDesc, StringRef> Functions;
-  std::map<SymbolDesc, StringRef> Objects;
+  std::vector<std::pair<SymbolDesc, StringRef>> Functions;
+  std::vector<std::pair<SymbolDesc, StringRef>> Objects;
 
   SymbolizableObjectFile(object::ObjectFile *Obj,
                          std::unique_ptr<DIContext> DICtx);