[clangd] Make advanceTo() faster on Posting Lists
authorKirill Bobyrev <kbobyrev.opensource@gmail.com>
Mon, 10 Sep 2018 07:57:28 +0000 (07:57 +0000)
committerKirill Bobyrev <kbobyrev.opensource@gmail.com>
Mon, 10 Sep 2018 07:57:28 +0000 (07:57 +0000)
If the current element is already beyond advanceTo()'s DocID, just
return instead of doing binary search. This simple optimization saves up
to 6-7% performance,

Reviewed By: ilya-biryukov

Differential Revision: https://reviews.llvm.org/D51802

llvm-svn: 341781

clang-tools-extra/clangd/index/dex/Iterator.cpp

index 3204980b397fd8972299b5fe2679216065071809..5cbd0f490afc3330b2cf594f5a0f7ebdbfd9cfd2 100644 (file)
@@ -38,7 +38,10 @@ public:
   /// or higher than the given one.
   void advanceTo(DocID ID) override {
     assert(!reachedEnd() && "DOCUMENT iterator can't advance() at the end.");
-    Index = std::lower_bound(Index, std::end(Documents), ID);
+    // If current ID is beyond requested one, iterator is already in the right
+    // state.
+    if (peek() < ID)
+      Index = std::lower_bound(Index, std::end(Documents), ID);
   }
 
   DocID peek() const override {