]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Preallocate results hash tables
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 24 Jul 2018 18:48:28 +0000 (19:48 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 24 Jul 2018 18:48:28 +0000 (19:48 +0100)
src/libmime/filter.c

index e4862b0fd43bf14e3f6c7c5ada36b0957ae958b9..1c836e4a794fcf3c6b22e60d34017e59398898bb 100644 (file)
 #include <math.h>
 #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;
        }