[PATCH] D44973: Use OffsetMap in getSectionPiece

Rafael Avila de Espindola via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 27 22:58:53 PDT 2018


espindola created this revision.
espindola added a reviewer: ruiu.
Herald added subscribers: arichardson, emaste.

OffsetMap maps to a SectionPiece index, but we were not taking advantage of that in getSectionPiece.

With this patch both getOffset and getSectionPiece use OffsetMap and the binary search is moved to findSectionPiece.

This is a measurable win (up to 2,2%) in most tests.


https://reviews.llvm.org/D44973

Files:
  ELF/InputSection.cpp


Index: ELF/InputSection.cpp
===================================================================
--- ELF/InputSection.cpp
+++ ELF/InputSection.cpp
@@ -939,18 +939,29 @@
 }
 
 // Do binary search to get a section piece at a given input offset.
-SectionPiece *MergeInputSection::getSectionPiece(uint64_t Offset) {
-  if (Data.size() <= Offset)
-    fatal(toString(this) + ": entry is past the end of the section");
+static SectionPiece *findSectionPiece(MergeInputSection *Sec, uint64_t Offset) {
+  if (Sec->Data.size() <= Offset)
+    fatal(toString(Sec) + ": entry is past the end of the section");
 
   // Find the element this offset points to.
   auto I = fastUpperBound(
-      Pieces.begin(), Pieces.end(), Offset,
+      Sec->Pieces.begin(), Sec->Pieces.end(), Offset,
       [](const uint64_t &A, const SectionPiece &B) { return A < B.InputOff; });
   --I;
   return &*I;
 }
 
+SectionPiece *MergeInputSection::getSectionPiece(uint64_t Offset) {
+  // Find a piece starting at a given offset.
+  auto It = OffsetMap.find(Offset);
+  if (It != OffsetMap.end())
+    return &Pieces[It->second];
+
+  // If Offset is not at beginning of a section piece, it is not in the map.
+  // In that case we need to search from the original section piece vector.
+  return findSectionPiece(this, Offset);
+}
+
 // Returns the offset in an output section for a given input offset.
 // Because contents of a mergeable section is not contiguous in output,
 // it is not just an addition to a base output offset.
@@ -965,7 +976,8 @@
 
   // If Offset is not at beginning of a section piece, it is not in the map.
   // In that case we need to search from the original section piece vector.
-  const SectionPiece &Piece = *getSectionPiece(Offset);
+  const SectionPiece &Piece =
+      *findSectionPiece(const_cast<MergeInputSection *>(this), Offset);
   if (!Piece.Live)
     return 0;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44973.140043.patch
Type: text/x-patch
Size: 1878 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180328/4cc6dac5/attachment.bin>


More information about the llvm-commits mailing list