[PATCH] D38129: [ELF] - Speedup -r and --emit-relocs

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 25 05:05:20 PDT 2017


grimar updated this revision to Diff 116505.
grimar added a comment.

- Addressed review comments.


https://reviews.llvm.org/D38129

Files:
  ELF/InputSection.cpp
  ELF/SyntheticSections.cpp
  ELF/SyntheticSections.h


Index: ELF/SyntheticSections.h
===================================================================
--- ELF/SyntheticSections.h
+++ ELF/SyntheticSections.h
@@ -418,6 +418,10 @@
   std::vector<SymbolTableEntry> Symbols;
 
   StringTableSection &StrTabSec;
+
+  llvm::once_flag InitSearchTableFlag;
+  llvm::DenseMap<SymbolBody *, size_t> SymbolToIndexMap;
+  llvm::DenseMap<OutputSection *, size_t> SectionToIndexMap;
 };
 
 template <class ELFT>
Index: ELF/SyntheticSections.cpp
===================================================================
--- ELF/SyntheticSections.cpp
+++ ELF/SyntheticSections.cpp
@@ -1355,18 +1355,27 @@
 }
 
 size_t SymbolTableBaseSection::getSymbolIndex(SymbolBody *Body) {
-  auto I = llvm::find_if(Symbols, [&](const SymbolTableEntry &E) {
-    if (E.Symbol == Body)
-      return true;
-    // This is used for -r, so we have to handle multiple section
-    // symbols being combined.
-    if (Body->Type == STT_SECTION && E.Symbol->Type == STT_SECTION)
-      return Body->getOutputSection() == E.Symbol->getOutputSection();
-    return false;
+  // Initialize symbol search table lazily. This is used only when -r or
+  // --emit-relocs is given and allows to find symbol table index without
+  // linear search what improves perfomance drastically in some cases.
+  llvm::call_once(InitSearchTableFlag, [&] {
+    SymbolToIndexMap.reserve(Symbols.size());
+    size_t I = 0;
+    for (const SymbolTableEntry &E : Symbols) {
+      if (E.Symbol->Type == STT_SECTION)
+        SectionToIndexMap[E.Symbol->getOutputSection()] = ++I;
+      else
+        SymbolToIndexMap[E.Symbol] = ++I;
+    }
   });
-  if (I == Symbols.end())
-    return 0;
-  return I - Symbols.begin() + 1;
+
+  // Handling STT_SECTION is special. We ignore section symbols when reading
+  // files and create a section symbol for each output section instead to merge
+  // them. That means we should distinguish STT_SECTION symbols by output
+  // sections they belongs to.
+  if (Body->Type == STT_SECTION)
+    return SectionToIndexMap.lookup(Body->getOutputSection());
+  return SymbolToIndexMap.lookup(Body);
 }
 
 template <class ELFT>
Index: ELF/InputSection.cpp
===================================================================
--- ELF/InputSection.cpp
+++ ELF/InputSection.cpp
@@ -384,11 +384,6 @@
 template <class ELFT, class RelTy>
 void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
   InputSectionBase *RelocatedSection = getRelocatedSection();
-
-  // Loop is slow and have complexity O(N*M), where N - amount of
-  // relocations and M - amount of symbols in symbol table.
-  // That happens because getSymbolIndex(...) call below performs
-  // simple linear search.
   for (const RelTy &Rel : Rels) {
     uint32_t Type = Rel.getType(Config->IsMips64EL);
     SymbolBody &Body = this->getFile<ELFT>()->getRelocTargetSym(Rel);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38129.116505.patch
Type: text/x-patch
Size: 2870 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170925/3c0c6887/attachment.bin>


More information about the llvm-commits mailing list