[lld] afeb4a6 - [ELF] Optimize -Map. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 26 22:51:36 PST 2022


Author: Fangrui Song
Date: 2022-01-26T22:51:31-08:00
New Revision: afeb4a6628a684b268b18d5f3cbd88e58ee75b29

URL: https://github.com/llvm/llvm-project/commit/afeb4a6628a684b268b18d5f3cbd88e58ee75b29
DIFF: https://github.com/llvm/llvm-project/commit/afeb4a6628a684b268b18d5f3cbd88e58ee75b29.diff

LOG: [ELF] Optimize -Map. NFC

getVA is slow. Avoid calling it in the llvm::sort comparator.

Added: 
    

Modified: 
    lld/ELF/MapFile.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/MapFile.cpp b/lld/ELF/MapFile.cpp
index 1998192bfba6..70d0e8c65255 100644
--- a/lld/ELF/MapFile.cpp
+++ b/lld/ELF/MapFile.cpp
@@ -37,7 +37,8 @@ using namespace llvm::object;
 using namespace lld;
 using namespace lld::elf;
 
-using SymbolMapTy = DenseMap<const SectionBase *, SmallVector<Defined *, 4>>;
+using SymbolMapTy = DenseMap<const SectionBase *,
+                             SmallVector<std::pair<Defined *, uint64_t>, 0>>;
 
 static constexpr char indent8[] = "        ";          // 8 spaces
 static constexpr char indent16[] = "                "; // 16 spaces
@@ -67,7 +68,7 @@ static std::vector<Defined *> getSymbols() {
 static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
   SymbolMapTy ret;
   for (Defined *dr : syms)
-    ret[dr->section].push_back(dr);
+    ret[dr->section].emplace_back(dr, dr->getVA());
 
   // Sort symbols by address. We want to print out symbols in the
   // order in the output file rather than the order they appeared
@@ -76,12 +77,11 @@ static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
   for (auto &it : ret) {
     // Deduplicate symbols which need a canonical PLT entry/copy relocation.
     set.clear();
-    llvm::erase_if(it.second,
-                   [&](Defined *sym) { return !set.insert(sym).second; });
-
-    llvm::stable_sort(it.second, [](Defined *a, Defined *b) {
-      return a->getVA() < b->getVA();
+    llvm::erase_if(it.second, [&](std::pair<Defined *, uint64_t> a) {
+      return !set.insert(a.first).second;
     });
+
+    llvm::stable_sort(it.second, llvm::less_second());
   }
   return ret;
 }
@@ -184,7 +184,7 @@ static void writeMapFile(raw_fd_ostream &os) {
           writeHeader(os, isec->getVA(), osec->getLMA() + isec->outSecOff,
                       isec->getSize(), isec->alignment);
           os << indent8 << toString(isec) << '\n';
-          for (Symbol *sym : sectionSyms[isec])
+          for (Symbol *sym : llvm::make_first_range(sectionSyms[isec]))
             os << symStr[sym] << '\n';
         }
         continue;


        


More information about the llvm-commits mailing list