[PATCH] D55248: [ELF] Simplify getSectionPiece

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 3 20:37:39 PST 2018


MaskRay created this revision.
MaskRay added a reviewer: ruiu.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.

Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D55248

Files:
  ELF/InputSection.cpp


Index: ELF/InputSection.cpp
===================================================================
--- ELF/InputSection.cpp
+++ ELF/InputSection.cpp
@@ -1223,18 +1223,11 @@
 
   // If Offset is not at beginning of a section piece, it is not in the map.
   // In that case we need to  do a binary search of the original section piece vector.
-  size_t Size = Pieces.size();
-  size_t Idx = 0;
-
-  while (Size != 1) {
-    size_t H = Size / 2;
-    Size -= H;
-    if (Pieces[Idx + H].InputOff <= Offset)
-      Idx += H;
-  }
-  if (Offset < Pieces[Idx].InputOff)
-    --Idx;
-  return &Pieces[Idx];
+  size_t I = upper_bound(Pieces, Offset, [](uint64_t Offset, SectionPiece P) {
+    return Offset < P.InputOff;
+  }) - Pieces.begin();
+  assert(Pieces[0] == 0 && I > 0);
+  return &Pieces[I - 1];
 }
 
 // Returns the offset in an output section for a given input offset.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55248.176543.patch
Type: text/x-patch
Size: 871 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181204/76ed57a0/attachment.bin>


More information about the llvm-commits mailing list