]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
rose: linear scan for lookaround during build
authorJustin Viiret <justin.viiret@intel.com>
Fri, 27 May 2016 06:51:41 +0000 (16:51 +1000)
committerMatthew Barr <matthew.barr@intel.com>
Fri, 8 Jul 2016 00:46:54 +0000 (10:46 +1000)
This allows us to reuse more lookaround entries in the bytecode.

src/rose/rose_build_bytecode.cpp

index dac2e79ce93cd7d91107d588f2be9d3d541ea7b9..578c4b4a3ca7b47c87254118ee7ab2125f50198f 100644 (file)
@@ -2434,6 +2434,33 @@ bool onlyAtEod(const RoseBuildImpl &tbi, RoseVertex v) {
     return true;
 }
 
+static
+u32 addLookaround(build_context &bc, const vector<LookEntry> &look) {
+    // Check the cache.
+    auto it = bc.lookaround_cache.find(look);
+    if (it != bc.lookaround_cache.end()) {
+        DEBUG_PRINTF("reusing look at idx %zu\n", it->second);
+        return verify_u32(it->second);
+    }
+
+    // Linear scan for sequence.
+    auto seq_it = search(begin(bc.lookaround), end(bc.lookaround), begin(look),
+                         end(look));
+    if (seq_it != end(bc.lookaround)) {
+        size_t idx = distance(begin(bc.lookaround), seq_it);
+        DEBUG_PRINTF("linear scan found look at idx %zu\n", idx);
+        bc.lookaround_cache.emplace(look, idx);
+        return verify_u32(idx);
+    }
+
+    // New sequence.
+    size_t idx = bc.lookaround.size();
+    bc.lookaround_cache.emplace(look, idx);
+    insert(&bc.lookaround, bc.lookaround.end(), look);
+    DEBUG_PRINTF("adding look at idx %zu\n", idx);
+    return verify_u32(idx);
+}
+
 static
 void makeRoleLookaround(RoseBuildImpl &build, build_context &bc, RoseVertex v,
                         vector<RoseInstruction> &program) {
@@ -2460,18 +2487,7 @@ void makeRoleLookaround(RoseBuildImpl &build, build_context &bc, RoseVertex v,
     }
 
     DEBUG_PRINTF("role has lookaround\n");
-    u32 look_idx;
-    auto it = bc.lookaround_cache.find(look);
-    if (it != bc.lookaround_cache.end()) {
-        DEBUG_PRINTF("reusing look at idx %zu\n", it->second);
-        look_idx = verify_u32(it->second);
-    } else {
-        size_t idx = bc.lookaround.size();
-        bc.lookaround_cache.emplace(look, idx);
-        insert(&bc.lookaround, bc.lookaround.end(), look);
-        DEBUG_PRINTF("adding look at idx %zu\n", idx);
-        look_idx = verify_u32(idx);
-    }
+    u32 look_idx = addLookaround(bc, look);
     u32 look_count = verify_u32(look.size());
 
     auto ri = RoseInstruction(ROSE_INSTR_CHECK_LOOKAROUND,