[lld] r284594 - Add a faster binary search.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 19 07:17:37 PDT 2016


Author: rafael
Date: Wed Oct 19 09:17:36 2016
New Revision: 284594

URL: http://llvm.org/viewvc/llvm-project?rev=284594&view=rev
Log:
Add a faster binary search.

Even with the hash table cache, binary search was still pretty
hot. This can be made even faster with prefetching.

Idea from http://cglab.ca/~morin/misc/arraylayout-v2/

I will suggest moving this to llvm.

Modified:
    lld/trunk/ELF/InputSection.cpp

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=284594&r1=284593&r2=284594&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Wed Oct 19 09:17:36 2016
@@ -651,6 +651,19 @@ SectionPiece *MergeInputSection<ELFT>::g
   return const_cast<SectionPiece *>(This->getSectionPiece(Offset));
 }
 
+template <class It, class T, class Compare>
+static It fastUpperBound(It First, It Last, const T &Value, Compare Comp) {
+  size_t Size = std::distance(First, Last);
+  assert(Size != 0);
+  while (Size != 1) {
+    size_t H = Size / 2;
+    const It MI = First + H;
+    Size -= H;
+    First = Comp(Value, *MI) ? First : First + H;
+  }
+  return Comp(Value, *First) ? First : First + 1;
+}
+
 template <class ELFT>
 const SectionPiece *
 MergeInputSection<ELFT>::getSectionPiece(uintX_t Offset) const {
@@ -659,7 +672,7 @@ MergeInputSection<ELFT>::getSectionPiece
     fatal(getName(this) + ": entry is past the end of the section");
 
   // Find the element this offset points to.
-  auto I = std::upper_bound(
+  auto I = fastUpperBound(
       Pieces.begin(), Pieces.end(), Offset,
       [](const uintX_t &A, const SectionPiece &B) { return A < B.InputOff; });
   --I;




More information about the llvm-commits mailing list