From 3e1b2d44a1874a0e48db88ba0e6b857269ae7aeb Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 12 Apr 2016 19:21:44 +0100 Subject: [PATCH] [Feature] Optimize rspamd_re_cache_type_from_string --- src/libserver/re_cache.c | 52 ++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/libserver/re_cache.c b/src/libserver/re_cache.c index c2e628206..db57c0db9 100644 --- a/src/libserver/re_cache.c +++ b/src/libserver/re_cache.c @@ -1137,37 +1137,53 @@ rspamd_re_cache_type_to_string (enum rspamd_re_type type) enum rspamd_re_type rspamd_re_cache_type_from_string (const char *str) { - enum rspamd_re_type ret = RSPAMD_RE_MAX; + enum rspamd_re_type ret; + guint64 h; + + /* + * To optimize this function, we apply hash to input string and + * pre-select it from the values + */ if (str != NULL) { - if (strcmp (str, "header") == 0) { + h = XXH64 (str, strlen (str), 0xdeadbabe); + + switch (h) { + case 0x298b9c8a58887d44LLU: ret = RSPAMD_RE_HEADER; - } - else if (strcmp (str, "rawheader") == 0) { + break; + case 0x467bfb5cd7ddf890LLU: ret = RSPAMD_RE_RAWHEADER; - } - else if (strcmp (str, "mime") == 0) { + break; + case 0xda081341fb600389LLU: ret = RSPAMD_RE_MIME; - } - else if (strcmp (str, "rawmime") == 0) { + break; + case 0xc35831e067a8221dLLU: ret = RSPAMD_RE_RAWMIME; - } - else if (strcmp (str, "body") == 0) { + break; + case 0xc625e13dbe636de2LLU: ret = RSPAMD_RE_BODY; - } - else if (strcmp (str, "url") == 0) { + break; + case 0x286edbe164c791d2LLU: ret = RSPAMD_RE_URL; - } - else if (strcmp (str, "allheader") == 0) { + break; + case 0x796d62205a8778c7LLU: ret = RSPAMD_RE_ALLHEADER; - } - else if (strcmp (str, "mimeheader") == 0) { + break; + case 0xa3c6c153b3b00a5eLLU: ret = RSPAMD_RE_MIMEHEADER; - } - else if (strcmp (str, "sabody") == 0) { + break; + case 0x7794501506e604e9LLU: ret = RSPAMD_RE_SABODY; + break; + default: + ret = RSPAMD_RE_MAX; + break; } } + else { + ret = RSPAMD_RE_MAX; + } return ret; } -- 2.39.5