[lld] r239292 - COFF: Avoid callign stable_sort.

Rui Ueyama ruiu at google.com
Mon Jun 8 01:26:29 PDT 2015


Author: ruiu
Date: Mon Jun  8 03:26:28 2015
New Revision: 239292

URL: http://llvm.org/viewvc/llvm-project?rev=239292&view=rev
Log:
COFF: Avoid callign stable_sort.

MSVC profiler reported that this stable_sort takes 7% time
when self-linking. As a result, createSection was taking 10% time.
Now createSection takes 3%. This small change actually makes
the linker a bit but perceptibly faster.

Modified:
    lld/trunk/COFF/Writer.cpp

Modified: lld/trunk/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=239292&r1=239291&r2=239292&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.cpp (original)
+++ lld/trunk/COFF/Writer.cpp Mon Jun  8 03:26:28 2015
@@ -120,30 +120,30 @@ void Writer::createSections() {
         C->printDiscardedMessage();
       continue;
     }
-    // '$' and all following characters in input section names are
-    // discarded when determining output section. So, .text$foo
-    // contributes to .text, for example. See PE/COFF spec 3.2.
-    Map[C->getSectionName().split('$').first].push_back(C);
+    Map[C->getSectionName()].push_back(C);
   }
 
   // Then create an OutputSection for each section.
+  // '$' and all following characters in input section names are
+  // discarded when determining output section. So, .text$foo
+  // contributes to .text, for example. See PE/COFF spec 3.2.
+  StringRef Name = Map.begin()->first.split('$').first;
+  auto Sec = new (CAlloc.Allocate()) OutputSection(Name, 0);
+  OutputSections.push_back(Sec);
   for (auto &P : Map) {
     StringRef SectionName = P.first;
+    StringRef Base = SectionName.split('$').first;
+    if (Base != Sec->getName()) {
+      size_t SectIdx = OutputSections.size();
+      Sec = new (CAlloc.Allocate()) OutputSection(Base, SectIdx);
+      OutputSections.push_back(Sec);
+    }
     std::vector<Chunk *> &Chunks = P.second;
-    // Input sections are ordered by their names including '$' parts,
-    // which gives you some control over the output layout.
-    std::stable_sort(Chunks.begin(), Chunks.end(),
-                     [](Chunk *A, Chunk *B) {
-                       return A->getSectionName() < B->getSectionName();
-                     });
-    size_t SectIdx = OutputSections.size();
-    auto Sec = new (CAlloc.Allocate()) OutputSection(SectionName, SectIdx);
     for (Chunk *C : Chunks) {
       C->setOutputSection(Sec);
       Sec->addChunk(C);
       Sec->addPermissions(C->getPermissions());
     }
-    OutputSections.push_back(Sec);
   }
 }
 





More information about the llvm-commits mailing list