]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
ng_find_matches: speed up gather...ByDepth
authorJustin Viiret <justin.viiret@intel.com>
Wed, 8 Mar 2017 23:55:22 +0000 (10:55 +1100)
committerMatthew Barr <matthew.barr@intel.com>
Wed, 26 Apr 2017 05:17:03 +0000 (15:17 +1000)
util/ng_find_matches.cpp

index 13efa05c71a3974bedb2df004b790e5e47d42eb4..80a06a15d73a5b8c8002533c107c34a99154c4d6 100644 (file)
@@ -60,8 +60,9 @@ static constexpr size_t STATE_COUNT_MAX = 15000;
 static
 vector<flat_set<NFAVertex>>
 gatherSuccessorsByDepth(const NGHolder &g, const NFAVertex &src, u32 depth) {
+    assert(depth > 0);
+
     vector<flat_set<NFAVertex>> result(depth);
-    flat_set<NFAVertex> cur, next;
 
     // populate current set of successors
     for (auto v : adjacent_vertices_range(src, g)) {
@@ -70,31 +71,28 @@ gatherSuccessorsByDepth(const NGHolder &g, const NFAVertex &src, u32 depth) {
             continue;
         }
         DEBUG_PRINTF("Node %zu depth 1\n", g[v].index);
-
-        cur.insert(v);
+        result[0].insert(v);
     }
-    result[0] = cur;
 
     for (u32 d = 1; d < depth; d++) {
         // collect all successors for all current level vertices
-        for (auto v : cur) {
+        const auto &cur = result[d - 1];
+        auto &next = result[d];
+        for (auto u : cur) {
             // don't go past special nodes
-            if (is_special(v, g)) {
+            if (is_special(u, g)) {
                 continue;
             }
 
-            for (auto succ : adjacent_vertices_range(v, g)) {
+            for (auto v : adjacent_vertices_range(u, g)) {
                 // ignore self-loops
-                if (v == succ) {
+                if (u == v) {
                     continue;
                 }
-                DEBUG_PRINTF("Node %zu depth %u\n", g[succ].index, d + 1);
-                next.insert(succ);
+                DEBUG_PRINTF("Node %zu depth %u\n", g[v].index, d + 1);
+                next.insert(v);
             }
         }
-        result[d] = next;
-        next.swap(cur);
-        next.clear();
     }
 
     return result;
@@ -103,13 +101,12 @@ gatherSuccessorsByDepth(const NGHolder &g, const NFAVertex &src, u32 depth) {
 // returns all predecessors up to a given depth in a vector of sets, indexed by
 // zero-based depth from source vertex
 static
-vector<flat_set<NFAVertex>> gatherPredecessorsByDepth(const NGHolder &g,
-                                                      NFAVertex src, u32 depth) {
-    vector<flat_set<NFAVertex>> result(depth);
-    flat_set<NFAVertex> cur, next;
-
+vector<flat_set<NFAVertex>>
+gatherPredecessorsByDepth(const NGHolder &g, NFAVertex src, u32 depth) {
     assert(depth > 0);
 
+    vector<flat_set<NFAVertex>> result(depth);
+
     // populate current set of successors
     for (auto v : inv_adjacent_vertices_range(src, g)) {
         // ignore self-loops
@@ -117,25 +114,23 @@ vector<flat_set<NFAVertex>> gatherPredecessorsByDepth(const NGHolder &g,
             continue;
         }
         DEBUG_PRINTF("Node %zu depth 1\n", g[v].index);
-        cur.insert(v);
+        result[0].insert(v);
     }
-    result[0] = cur;
 
     for (u32 d = 1; d < depth; d++) {
         // collect all successors for all current level vertices
+        const auto &cur = result[d - 1];
+        auto &next = result[d];
         for (auto v : cur) {
-            for (auto pred : inv_adjacent_vertices_range(v, g)) {
+            for (auto u : inv_adjacent_vertices_range(v, g)) {
                 // ignore self-loops
-                if (v == pred) {
+                if (v == u) {
                     continue;
                 }
-                DEBUG_PRINTF("Node %zu depth %u\n", g[pred].index, d + 1);
-                next.insert(pred);
+                DEBUG_PRINTF("Node %zu depth %u\n", g[u].index, d + 1);
+                next.insert(u);
             }
         }
-        result[d] = next;
-        next.swap(cur);
-        next.clear();
     }
 
     return result;