]> source.dussan.org Git - rspamd.git/commitdiff
Fix very bad bug in a memory pool allocator.
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 26 Jul 2011 11:32:44 +0000 (15:32 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 26 Jul 2011 11:32:44 +0000 (15:32 +0400)
Speed up inserting symbols by hashing them.

src/filter.c
src/mem_pool.c
src/symbols_cache.c
src/symbols_cache.h

index b9733076c527301c04f95a2b545ee4960f70a198..4bf5a563750d6f650acc95907dcc2fd03b5b8a65 100644 (file)
@@ -163,25 +163,9 @@ insert_result_common (struct worker_task *task, const gchar *symbol, double flag
 
        /* Process cache item */
        if (task->cfg->cache) {
-               cur = task->cfg->cache->static_items;
-               while (cur)
-               {
-                       item = cur->data;
-
-                       if (strcmp (item->s->symbol, symbol) == 0) {
-                               item->s->frequency++;
-                       }
-                       cur = g_list_next (cur);
-               }
-               cur = task->cfg->cache->negative_items;
-               while (cur)
-               {
-                       item = cur->data;
-
-                       if (strcmp (item->s->symbol, symbol) == 0) {
-                               item->s->frequency++;
-                       }
-                       cur = g_list_next (cur);
+               item = g_hash_table_lookup (task->cfg->cache->items_by_symbol, symbol);
+               if (item != NULL) {
+                       item->s->frequency++;
                }
        }
 
index dac5f8a93c761ec091f87aafd0e7a4bb702523a2..170aed854cbb165f17aa9033f3cdf0a2be63fc18 100644 (file)
@@ -204,6 +204,7 @@ memory_pool_alloc (memory_pool_t * pool, gsize size)
 {
        guint8                         *tmp;
        struct _pool_chain             *new, *cur;
+       gsize                           free;
 
        if (pool) {
 #ifdef MEMORY_GREEDY
@@ -212,10 +213,10 @@ memory_pool_alloc (memory_pool_t * pool, gsize size)
                cur = pool->cur_pool;
 #endif
                /* Find free space in pool chain */
-               while (pool_chain_free (cur) < size && cur->next) {
+               while ((free = pool_chain_free (cur)) < size && cur->next) {
                        cur = cur->next;
                }
-               if (cur->next == NULL) {
+               if (free < size && cur->next == NULL) {
                        /* Allocate new pool */
                        if (cur->len >= size) {
                                new = pool_chain_new (cur->len);
index c4327b470b1239006a2504ad43a0942f7646f39e..3684bd5e5f7fd5111d71985816ad78114b78493b 100644 (file)
@@ -120,12 +120,14 @@ post_cache_init (struct symbols_cache *cache)
        while (cur) {
                item = cur->data;
                total_frequency += item->s->frequency;
+               g_hash_table_insert (cache->items_by_symbol, item->s->symbol, item);
                cur = g_list_next (cur);
        }
        cur = g_list_first (cache->static_items);
        while (cur) {
                item = cur->data;
                total_frequency += item->s->frequency;
+               g_hash_table_insert (cache->items_by_symbol, item->s->symbol, item);
                cur = g_list_next (cur);
        }
 
@@ -388,6 +390,8 @@ register_dynamic_symbol (memory_pool_t *dynamic_pool, struct symbols_cache **cac
        msg_debug ("used items: %d, added symbol: %s", (*cache)->used_items, name);
        set_counter (item->s->symbol, 0);
        
+       g_hash_table_insert (pcache->items_by_symbol, item->s->symbol, item);
+
        if (networks == NULL) {
                pcache->dynamic_items = g_list_prepend (pcache->dynamic_items, item);
        }
@@ -497,7 +501,7 @@ free_cache (gpointer arg)
        if (cache->negative_dynamic_map) {
                radix_tree_free (cache->negative_dynamic_map);
        }
-
+       g_hash_table_destroy (cache->items_by_symbol);
        memory_pool_delete (cache->static_pool);
 
        g_free (cache);
@@ -618,7 +622,10 @@ init_symbols_cache (memory_pool_t * pool, struct symbols_cache *cache, struct co
        }
        /* MMap cache file and copy saved_cache structures */
        res = mmap_cache_file (cache, fd, pool);
+       cache->items_by_symbol = g_hash_table_new (g_str_hash, g_str_equal);
+
        memory_pool_add_destructor (pool, (pool_destruct_func)free_cache, cache);
+
        return res;
 }
 
index d84b2492b0308612f445750fc9613d057e37b3b7..15883f361666fbf7cf4d1095ff6341c22dd86403 100644 (file)
@@ -60,6 +60,9 @@ struct symbols_cache {
        /* Common dynamic rules */
        GList *dynamic_items;
 
+       /* Hash table for fast access */
+       GHashTable *items_by_symbol;
+
        memory_pool_t *static_pool;
 
        guint cur_items;