Browse code

libclamav/matcher-ac.c: micro-optimization (bb#843), thanks to Edwin

git-svn: trunk@4334

Tomasz Kojm authored on 2008/11/05 04:23:35
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Tue Nov  4 20:50:45 CET 2008 (tk)
2
+---------------------------------
3
+ * libclamav/matcher-ac.c: micro-optimization (bb#843), thanks to Edwin
4
+
1 5
 Tue Nov  4 20:47:14 CET 2008 (acab)
2 6
 -----------------------------------
3 7
  * libclamav: mempool de-uglify
... ...
@@ -258,8 +258,13 @@ static int ac_maketrans(struct cli_matcher *root)
258 258
     }
259 259
 
260 260
     while((node = bfs_dequeue(&bfs, &bfs_last))) {
261
-	if(node->leaf)
261
+	if(node->leaf) {
262
+	    struct cli_ac_node *failtarget = node->fail;
263
+	    while(failtarget->leaf)
264
+		failtarget = failtarget->fail;
265
+	    node->fail = failtarget;
262 266
 	    continue;
267
+	}
263 268
 
264 269
 	for(i = 0; i < 256; i++) {
265 270
 	    child = node->trans[i];
... ...
@@ -289,6 +294,31 @@ static int ac_maketrans(struct cli_matcher *root)
289 289
 	}
290 290
     }
291 291
 
292
+    bfs = bfs_last = NULL;
293
+    for(i = 0; i < 256; i++) {
294
+	node = ac_root->trans[i];
295
+	if(node != ac_root) {
296
+	    if((ret = bfs_enqueue(&bfs, &bfs_last, node)))
297
+		return ret;
298
+	}
299
+    }
300
+    while((node = bfs_dequeue(&bfs, &bfs_last))) {
301
+	if(node->leaf)
302
+	    continue;
303
+	for(i = 0; i < 256; i++) {
304
+	    child = node->trans[i];
305
+	    if(!child) {
306
+		struct cli_ac_node *failtarget = node->fail;
307
+		while(failtarget->leaf || !failtarget->trans[i])
308
+		    failtarget = failtarget->fail;
309
+		node->trans[i] = failtarget->trans[i];
310
+	    } else {
311
+		if((ret = bfs_enqueue(&bfs, &bfs_last, child)) != 0)
312
+		    return ret;
313
+	    }
314
+	}
315
+    }
316
+
292 317
     return CL_SUCCESS;
293 318
 }
294 319
 
... ...
@@ -875,7 +905,7 @@ int cli_ac_scanbuff(const unsigned char *buffer, uint32_t length, const char **v
875 875
 
876 876
     for(i = 0; i < length; i++)  {
877 877
 
878
-	while(current->leaf || !current->trans[buffer[i]])
878
+	if(current->leaf)
879 879
 	    current = current->fail;
880 880
 
881 881
 	current = current->trans[buffer[i]];