From 65daeed69c7cf8e8b95935a1d1c91fe086caea9e Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 24 Jul 2018 19:48:28 +0100 Subject: [PATCH] [Minor] Preallocate results hash tables --- src/libmime/filter.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/libmime/filter.c b/src/libmime/filter.c index e4862b0fd..1c836e4a7 100644 --- a/src/libmime/filter.c +++ b/src/libmime/filter.c @@ -22,12 +22,17 @@ #include #include "contrib/uthash/utlist.h" +/* Average symbols count to optimize hash allocation */ +static struct rspamd_counter_data symbols_count; + static void rspamd_metric_result_dtor (gpointer d) { struct rspamd_metric_result *r = (struct rspamd_metric_result *)d; struct rspamd_symbol_result sres; + rspamd_set_counter_ema (&symbols_count, kh_size (r->symbols), 0.5); + kh_foreach_value (r->symbols, sres, { if (sres.options) { kh_destroy (rspamd_options_hash, sres.options); @@ -56,6 +61,16 @@ rspamd_create_metric_result (struct rspamd_task *task) metric_res->grow_factor = 0; metric_res->score = 0; + /* Optimize allocation */ + kh_resize (rspamd_symbols_group_hash, metric_res->sym_groups, 4); + + if (symbols_count.mean > 4) { + kh_resize (rspamd_symbols_hash, metric_res->symbols, symbols_count.mean); + } + else { + kh_resize (rspamd_symbols_hash, metric_res->symbols, 4); + } + for (i = 0; i < METRIC_ACTION_MAX; i++) { metric_res->actions_limits[i] = task->cfg->actions[i].score; } -- 2.39.5