[PATCH] D51676: [clangd] NFC: Use TopN instead of std::priority_queue

Kirill Bobyrev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 6 06:16:39 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL341544: [clangd] NFC: Use TopN instead of std::priority_queue (authored by omtcyfz, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D51676?vs=164200&id=164202#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D51676

Files:
  clang-tools-extra/trunk/clangd/index/MemIndex.cpp


Index: clang-tools-extra/trunk/clangd/index/MemIndex.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/index/MemIndex.cpp
+++ clang-tools-extra/trunk/clangd/index/MemIndex.cpp
@@ -10,7 +10,7 @@
 #include "MemIndex.h"
 #include "../FuzzyMatch.h"
 #include "../Logger.h"
-#include <queue>
+#include "../Quality.h"
 
 namespace clang {
 namespace clangd {
@@ -26,7 +26,7 @@
   assert(!StringRef(Req.Query).contains("::") &&
          "There must be no :: in query.");
 
-  std::priority_queue<std::pair<float, const Symbol *>> Top;
+  TopN<std::pair<float, const Symbol *>> Top(Req.MaxCandidateCount);
   FuzzyMatcher Filter(Req.Query);
   bool More = false;
   for (const auto Pair : Index) {
@@ -38,16 +38,12 @@
     if (Req.RestrictForCodeCompletion && !Sym->IsIndexedForCodeCompletion)
       continue;
 
-    if (auto Score = Filter.match(Sym->Name)) {
-      Top.emplace(-*Score * quality(*Sym), Sym);
-      if (Top.size() > Req.MaxCandidateCount) {
-        More = true;
-        Top.pop();
-      }
-    }
+    if (auto Score = Filter.match(Sym->Name))
+      if (Top.push({*Score * quality(*Sym), Sym}))
+        More = true; // An element with smallest score was discarded.
   }
-  for (; !Top.empty(); Top.pop())
-    Callback(*Top.top().second);
+  for (const auto &Item : std::move(Top).items())
+    Callback(*Item.second);
   return More;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51676.164202.patch
Type: text/x-patch
Size: 1414 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180906/44e11709/attachment.bin>


More information about the cfe-commits mailing list