From 33f9a2a6f1c799f3f51c065485dfa1761053e88b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 26 Jul 2011 15:32:44 +0400 Subject: [PATCH] Fix very bad bug in a memory pool allocator. Speed up inserting symbols by hashing them. --- src/filter.c | 22 +++------------------- src/mem_pool.c | 5 +++-- src/symbols_cache.c | 9 ++++++++- src/symbols_cache.h | 3 +++ 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/filter.c b/src/filter.c index b9733076c..4bf5a5637 100644 --- a/src/filter.c +++ b/src/filter.c @@ -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++; } } diff --git a/src/mem_pool.c b/src/mem_pool.c index dac5f8a93..170aed854 100644 --- a/src/mem_pool.c +++ b/src/mem_pool.c @@ -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); diff --git a/src/symbols_cache.c b/src/symbols_cache.c index c4327b470..3684bd5e5 100644 --- a/src/symbols_cache.c +++ b/src/symbols_cache.c @@ -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; } diff --git a/src/symbols_cache.h b/src/symbols_cache.h index d84b2492b..15883f361 100644 --- a/src/symbols_cache.h +++ b/src/symbols_cache.h @@ -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; -- 2.39.5