[PATCH] D54442: [llvm-exegesis] Optimize ToProcess in dbScan

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 12 13:38:06 PST 2018


MaskRay updated this revision to Diff 173753.
MaskRay added a comment.

.


Repository:
  rL LLVM

https://reviews.llvm.org/D54442

Files:
  tools/llvm-exegesis/lib/Clustering.cpp


Index: tools/llvm-exegesis/lib/Clustering.cpp
===================================================================
--- tools/llvm-exegesis/lib/Clustering.cpp
+++ tools/llvm-exegesis/lib/Clustering.cpp
@@ -102,7 +102,10 @@
 }
 
 void InstructionBenchmarkClustering::dbScan(const size_t MinPts) {
-  for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
+  const size_t NumPoints = Points_.size();
+  std::vector<size_t> ToProcess(NumPoints);
+  std::vector<char> Added(NumPoints);
+  for (size_t P = 0; P < NumPoints; ++P) {
     if (!ClusterIdForPoint_[P].isUndef())
       continue; // Previously processed in inner loop.
     const auto Neighbors = rangeQuery(P);
@@ -118,31 +121,37 @@
     Cluster &CurrentCluster = Clusters_.back();
     ClusterIdForPoint_[P] = CurrentCluster.Id; /* Label initial point */
     CurrentCluster.PointIndices.push_back(P);
+    Added[P] = 1;
 
     // Process P's neighbors.
-    std::unordered_set<size_t> ToProcess(Neighbors.begin(), Neighbors.end());
-    while (!ToProcess.empty()) {
+    size_t Head = 0;
+    for (size_t Q : Neighbors)
+      if (!Added[Q]) {
+        ToProcess[Head++] = Q;
+        Added[Q] = 1;
+      }
+    for (size_t Tail = 0; Tail < Head; ++Tail) {
       // Retrieve a point from the set.
-      const size_t Q = *ToProcess.begin();
-      ToProcess.erase(Q);
+      size_t Q = ToProcess[Tail];
 
       if (ClusterIdForPoint_[Q].isNoise()) {
         // Change noise point to border point.
         ClusterIdForPoint_[Q] = CurrentCluster.Id;
         CurrentCluster.PointIndices.push_back(Q);
         continue;
       }
-      if (!ClusterIdForPoint_[Q].isUndef()) {
-        continue; // Previously processed.
-      }
+      assert(ClusterIdForPoint_[Q].isUndef());
       // Add Q to the current custer.
       ClusterIdForPoint_[Q] = CurrentCluster.Id;
       CurrentCluster.PointIndices.push_back(Q);
       // And extend to the neighbors of Q if the region is dense enough.
-      const auto Neighbors = rangeQuery(Q);
-      if (Neighbors.size() + 1 >= MinPts) {
-        ToProcess.insert(Neighbors.begin(), Neighbors.end());
-      }
+      const std::vector<size_t> Neighbors = rangeQuery(Q);
+      if (Neighbors.size() + 1 >= MinPts)
+        for (size_t P : Neighbors)
+          if (!Added[P]) {
+            ToProcess[Head++] = P;
+            Added[P] = 1;
+          }
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54442.173753.patch
Type: text/x-patch
Size: 2382 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181112/34578792/attachment.bin>


More information about the llvm-commits mailing list