[MEDIUM] acl: add tree-based lookups of exact strings

Now if some ACL patterns are loaded from a file and the operation is
an exact string match, the data will be arranged in a tree, yielding
a significant performance boost on large data sets. Note that this
only works when case is sensitive.

A new dedicated function, acl_lookup_str(), has been created for this
matching. It is called for every possible input data to test and it
looks the tree up for the data. Since the keywords are loosely typed,
we would have had to add a new columns to all keywords to adjust the
function depending on the type. Instead, we just compare on the match
function. We call acl_lookup_str() when we could use acl_match_str().
The tree lookup is performed first, then the remaining patterns are
attempted if the tree returned nothing.

A quick test shows that when matching a header against a list of 52000
network names, haproxy uses 68% of one core on a core2-duo 3.2 GHz at
42000 requests per second, versus 66% without any rule, which means
only a 2% CPU increase for 52000 rules. Doing the same test without
the tree leads to 100% CPU at 6900 requests/s. Also it was possible
to run the same test at full speed with about 50 sets of 52000 rules
without any measurable performance drop.
diff --git a/src/acl.c b/src/acl.c
index ad3feaf..b474c82 100644
--- a/src/acl.c
+++ b/src/acl.c
@@ -25,6 +25,8 @@
 #include <proto/auth.h>
 #include <proto/log.h>
 
+#include <ebsttree.h>
+
 /* The capabilities of filtering hooks describe the type of information
  * available to each of them.
  */
@@ -130,6 +132,25 @@
 	return ACL_PAT_FAIL;
 }
 
+/* Lookup a string in the expression's pattern tree. The node is returned if it
+ * exists, otherwise NULL.
+ */
+void *acl_lookup_str(struct acl_test *test, struct acl_expr *expr)
+{
+	/* data are stored in a tree */
+	struct ebmb_node *node;
+	char prev;
+
+	/* we may have to force a trailing zero on the test pattern */
+	prev = test->ptr[test->len];
+	if (prev)
+		test->ptr[test->len] = '\0';
+	node = ebst_lookup(&expr->pattern_tree, test->ptr);
+	if (prev)
+		test->ptr[test->len] = prev;
+	return node;
+}
+
 /* Executes a regex. It needs to change the data. If it is marked READ_ONLY
  * then it will be allocated and duplicated in place so that others may use
  * it later on. Note that this is embarrassing because we always try to avoid
@@ -329,6 +350,23 @@
 	int len;
 
 	len  = strlen(*text);
+
+	if (pattern->flags & ACL_PAT_F_TREE_OK) {
+		/* we're allowed to put the data in a tree whose root is pointed
+		 * to by val.tree.
+		 */
+		struct ebmb_node *node;
+
+		node = calloc(1, sizeof(*node) + len + 1);
+		if (!node)
+			return 0;
+		memcpy(node->key, *text, len + 1);
+		if (ebst_insert(pattern->val.tree, node) != node)
+			free(node); /* was a duplicate */
+		pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
+		return 1;
+	}
+
 	pattern->ptr.str = strdup(*text);
 	if (!pattern->ptr.str)
 		return 0;
@@ -1224,6 +1262,12 @@
 						acl_res |= ACL_PAT_FAIL;
 				}
 				else {
+					if (expr->pattern_tree.b[EB_LEFT]) {
+						/* a tree is present, let's check what type it is */
+						if (expr->kw->match == acl_match_str)
+							acl_res |= acl_lookup_str(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
+					}
+
 					/* call the match() function for all tests on this value */
 					list_for_each_entry(pattern, &expr->patterns, list) {
 						if (acl_res == ACL_PAT_PASS)