]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Get rid of one more GHashTable
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 17 Jun 2023 14:20:27 +0000 (15:20 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 17 Jun 2023 14:20:27 +0000 (15:20 +0100)
src/libserver/task.c
src/libserver/task.h
src/lua/lua_common.h
src/lua/lua_task.c

index 1b0671a5320bed05ecf19cc2afa24df7064348df..17ab23a357a87ed1483587c56b9bcea4b4a9f8be 100644 (file)
@@ -121,7 +121,7 @@ rspamd_task_new (struct rspamd_worker *worker,
 
        new_task->queue_id = "undef";
        new_task->messages = ucl_object_typed_new (UCL_OBJECT);
-       new_task->lua_cache = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
+       kh_static_init(rspamd_task_lua_cache, &new_task->lua_cache);
 
        return new_task;
 }
@@ -178,10 +178,7 @@ void
 rspamd_task_free (struct rspamd_task *task)
 {
        struct rspamd_email_address *addr;
-       struct rspamd_lua_cached_entry *entry;
        static guint free_iters = 0;
-       GHashTableIter it;
-       gpointer k, v;
        guint i;
 
        if (task) {
@@ -247,17 +244,15 @@ rspamd_task_free (struct rspamd_task *task)
                }
 
                if (task->cfg) {
-                       if (task->lua_cache) {
-                               g_hash_table_iter_init (&it, task->lua_cache);
 
-                               while (g_hash_table_iter_next (&it, &k, &v)) {
-                                       entry = (struct rspamd_lua_cached_entry *)v;
-                                       luaL_unref (task->cfg->lua_state,
-                                                       LUA_REGISTRYINDEX, entry->ref);
-                               }
 
-                               g_hash_table_unref (task->lua_cache);
-                       }
+                       struct rspamd_lua_cached_entry entry;
+
+                       kh_foreach_value(&task->lua_cache, entry, {
+                               luaL_unref (task->cfg->lua_state,
+                                       LUA_REGISTRYINDEX, entry.ref);
+                       });
+                       kh_static_destroy(rspamd_task_lua_cache, &task->lua_cache);
 
                        if (task->cfg->full_gc_iters && (++free_iters > task->cfg->full_gc_iters)) {
                                /* Perform more expensive cleanup cycle */
index d0169d8967bd36e4837b007d0da7a1338ed993e4..3a4d241871c8f9e0c50720b28b6628e74928fa4b 100644 (file)
@@ -155,7 +155,14 @@ struct rspamd_request_header_chain {
        struct rspamd_request_header_chain *next;
 };
 
-__KHASH_TYPE (rspamd_req_headers_hash, rspamd_ftok_t *, struct rspamd_request_header_chain *)
+__KHASH_TYPE (rspamd_req_headers_hash, rspamd_ftok_t *, struct rspamd_request_header_chain *);
+
+struct rspamd_lua_cached_entry {
+       gint ref;
+       guint id;
+};
+
+KHASH_INIT(rspamd_task_lua_cache, char *, struct rspamd_lua_cached_entry, 1, kh_str_hash_func, kh_str_hash_equal);
 
 /**
  * Worker task structure
@@ -180,7 +187,7 @@ struct rspamd_task {
        struct rspamd_http_connection *http_conn;        /**< HTTP server connection                                                    */
        struct rspamd_async_session *s;                /**< async session object                                                        */
        struct rspamd_scan_result *result;            /**< Metric result                                                                        */
-       GHashTable *lua_cache;                            /**< cache of lua objects                                                     */
+       khash_t(rspamd_task_lua_cache) lua_cache;        /**< cache of lua objects                                                      */
        GPtrArray *tokens;                                /**< statistics tokens */
        GArray *meta_words;                                /**< rspamd_stat_token_t produced from meta headers
                                                                                                                (e.g. Subject) */
index 226b250a7b3a76b0032dc1a5474321780dfa014c..c0c0c37c8f9a826d4df033ca22952bdfc773e15c 100644 (file)
@@ -139,11 +139,6 @@ struct rspamd_lua_map {
        } data;
 };
 
-struct rspamd_lua_cached_entry {
-       gint ref;
-       guint id;
-};
-
 struct rspamd_lua_upstream {
        struct upstream *up;
        gint upref;
index 1e0e0a147a707f56a96ea3209d682569018d5a4b..62bda0522e21a4ac0c514248fba13622b843f1d0 100644 (file)
@@ -1420,27 +1420,24 @@ lua_task_set_cached (lua_State *L, struct rspamd_task *task, const gchar *key,
                gint pos)
 {
        LUA_TRACE_POINT;
-       struct rspamd_lua_cached_entry *entry;
+       khiter_t k;
 
        lua_pushvalue (L, pos);
 
-       entry = g_hash_table_lookup (task->lua_cache, key);
+       k = kh_get(rspamd_task_lua_cache, &task->lua_cache, (char *)key);
 
-       if (G_UNLIKELY (entry != NULL)) {
+       if (G_UNLIKELY (k != kh_end(&task->lua_cache))) {
                /* Unref previous value */
-               luaL_unref (L, LUA_REGISTRYINDEX, entry->ref);
+               luaL_unref (L, LUA_REGISTRYINDEX, kh_value(&task->lua_cache, k).ref);
        }
        else {
-               entry = rspamd_mempool_alloc (task->task_pool, sizeof (*entry));
-               g_hash_table_insert (task->lua_cache,
-                               rspamd_mempool_strdup (task->task_pool, key), entry);
-       }
-
-       entry->ref = luaL_ref (L, LUA_REGISTRYINDEX);
+               int r;
 
-       if (task->message) {
-               entry->id = GPOINTER_TO_UINT (task->message);
+               k = kh_put(rspamd_task_lua_cache, &task->lua_cache, rspamd_mempool_strdup (task->task_pool, key), &r);
        }
+
+       kh_value(&task->lua_cache, k).ref = luaL_ref (L, LUA_REGISTRYINDEX);
+       kh_value(&task->lua_cache, k).id = GPOINTER_TO_UINT (task->message);
 }
 
 
@@ -1448,13 +1445,12 @@ static gboolean
 lua_task_get_cached (lua_State *L, struct rspamd_task *task, const gchar *key)
 {
        LUA_TRACE_POINT;
-       struct rspamd_lua_cached_entry *entry;
+       khiter_t k;
 
-       entry = g_hash_table_lookup (task->lua_cache, key);
+       k = kh_get(rspamd_task_lua_cache, &task->lua_cache, (char *)key);
 
-       if (entry != NULL && (task->message &&
-                                                 entry->id == GPOINTER_TO_UINT (task->message))) {
-               lua_rawgeti (L, LUA_REGISTRYINDEX, entry->ref);
+       if (k != kh_end(&task->lua_cache) && (kh_value(&task->lua_cache, k).id == GPOINTER_TO_UINT (task->message))) {
+               lua_rawgeti (L, LUA_REGISTRYINDEX, kh_value(&task->lua_cache, k).ref);
 
                return TRUE;
        }