[PATCH] D46145: Use a buffer when allocating relocations

Rafael Avila de Espindola via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 27 08:38:01 PDT 2018


espindola updated this revision to Diff 144350.
espindola added a comment.

correct patch .


https://reviews.llvm.org/D46145

Files:
  ELF/Writer.cpp


Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -859,16 +859,29 @@
 
 template <class ELFT>
 void Writer<ELFT>::forEachRelSec(std::function<void(InputSectionBase &)> Fn) {
+  std::vector<Relocation> Buffer;
+
+  // We allocate a lot of relocations. By using a single buffer for
+  // all sections we ensure that the final vector in each section has
+  // the exact size.
+  auto WithBuffer = [&](InputSectionBase &Sec) {
+    swap(Sec.Relocations, Buffer);
+    Fn(Sec);
+    swap(Sec.Relocations, Buffer);
+    Sec.Relocations.insert(Sec.Relocations.end(), Buffer.begin(), Buffer.end());
+    Buffer.clear();
+  };
+
   // Scan all relocations. Each relocation goes through a series
   // of tests to determine if it needs special treatment, such as
   // creating GOT, PLT, copy relocations, etc.
   // Note that relocations for non-alloc sections are directly
   // processed by InputSection::relocateNonAlloc.
   for (InputSectionBase *IS : InputSections)
     if (IS->Live && isa<InputSection>(IS) && (IS->Flags & SHF_ALLOC))
-      Fn(*IS);
+      WithBuffer(*IS);
   for (EhInputSection *ES : InX::EhFrame->Sections)
-    Fn(*ES);
+    WithBuffer(*ES);
 }
 
 // This function generates assignments for predefined symbols (e.g. _end or


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46145.144350.patch
Type: text/x-patch
Size: 1325 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180427/d8e7ef82/attachment.bin>


More information about the llvm-commits mailing list