From 3728212eeea755b5a71e38a9e8dbd832ba3b4b19 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 27 Mar 2015 00:02:48 +0000 Subject: [PATCH] More to priorities optimization. --- src/libutil/expression.c | 20 ++++++++++++++++---- src/libutil/expression.h | 2 ++ test/lua/unit/expressions.lua | 12 ++++++------ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/libutil/expression.c b/src/libutil/expression.c index a668371cc..c18783d79 100644 --- a/src/libutil/expression.c +++ b/src/libutil/expression.c @@ -392,13 +392,14 @@ rspamd_ast_priority_traverse (GNode *node, gpointer d) if (elt->type == ELT_LIMIT) { /* Always push limit first */ - elt->priority = G_MAXINT; + elt->priority = 0; } else { - elt->priority = 0; + elt->priority = RSPAMD_EXPRESSION_MAX_PRIORITY; if (expr->subr->priority != NULL) { - elt->priority = expr->subr->priority (elt->p.atom); + elt->priority = RSPAMD_EXPRESSION_MAX_PRIORITY - + expr->subr->priority (elt->p.atom); } } } @@ -411,7 +412,7 @@ rspamd_ast_priority_cmp (GNode *a, GNode *b) { struct rspamd_expression_elt *ea = a->data, *eb = b->data; - return eb->priority - ea->priority; + return ea->priority - eb->priority; } static gboolean @@ -991,6 +992,8 @@ static gboolean rspamd_ast_string_traverse (GNode *n, gpointer d) { GString *res = d; + gint cnt; + GNode *cur; struct rspamd_expression_elt *elt = n->data; const char *op_str = NULL; @@ -1034,6 +1037,15 @@ rspamd_ast_string_traverse (GNode *n, gpointer d) break; } g_string_append (res, op_str); + + if (n->children) { + LL_COUNT(n->children, cur, cnt); + + if (cnt > 2) { + /* Print n-ary of the operator */ + g_string_append_printf (res, "(%d)", cnt); + } + } } g_string_append_c (res, ' '); diff --git a/src/libutil/expression.h b/src/libutil/expression.h index 8605bbbda..924dbb45b 100644 --- a/src/libutil/expression.h +++ b/src/libutil/expression.h @@ -28,6 +28,8 @@ #include "config.h" #include "mem_pool.h" +#define RSPAMD_EXPRESSION_MAX_PRIORITY 1024 + typedef struct rspamd_expression_atom_s { /* Opaque userdata */ gpointer data; diff --git a/test/lua/unit/expressions.lua b/test/lua/unit/expressions.lua index 7bf3806b9..d6deaefd7 100644 --- a/test/lua/unit/expressions.lua +++ b/test/lua/unit/expressions.lua @@ -25,22 +25,22 @@ context("Rspamd expressions", function() local pool = rspamd_mempool.create() local cases = { - {'A & B | !C', 'A B & C ! |'}, + {'A & B | !C', 'C ! A B & |'}, {'A & (B | !C)', 'A B C ! | &'}, -- Unbalanced braces {'(((A))', nil}, -- Balanced braces {'(((A)))', 'A'}, -- Plus and comparision operators - {'A + B + C + D > 2', 'A B C D + + + 2 >'}, + {'A + B + C + D > 2', '2 A B C D +(4) >'}, -- Plus and logic operators - {'((A + B + C + D) > 2) & D', 'A B C D + + + 2 > D &'}, + {'((A + B + C + D) > 2) & D', 'D 2 A B C D +(4) > &'}, -- Associativity - {'A | B | C & D & E', 'A B C D E & & | |'}, + {'A | B | C & D & E', 'A B C D E &(3) |(3)'}, -- More associativity - {'1 | 0 & 0 | 0', '1 0 0 & 0 | |'}, + {'1 | 0 & 0 | 0', '1 0 0 & 0 |(3)'}, -- Extra space - {'A & B | ! C', 'A B & C ! |'}, + {'A & B | ! C', 'C ! A B & |'}, } for _,c in ipairs(cases) do local expr,err = rspamd_expression.create(c[1], -- 2.39.5