commit 63e72fc: [Minor] Get rid of one more GHashTable

Vsevolod Stakhov vsevolod at rspamd.com
Sat Jun 17 14:49:04 UTC 2023


Author: Vsevolod Stakhov
Date: 2023-06-17 15:20:27 +0100
URL: https://github.com/rspamd/rspamd/commit/63e72fc5d4f6712fb4ad41659f8189500386546d

[Minor] Get rid of one more GHashTable

---
 src/libserver/task.c | 21 ++++++++-------------
 src/libserver/task.h | 11 +++++++++--
 src/lua/lua_common.h |  5 -----
 src/lua/lua_task.c   | 30 +++++++++++++-----------------
 4 files changed, 30 insertions(+), 37 deletions(-)

diff --git a/src/libserver/task.c b/src/libserver/task.c
index 1b0671a53..17ab23a35 100644
--- a/src/libserver/task.c
+++ b/src/libserver/task.c
@@ -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 */
diff --git a/src/libserver/task.h b/src/libserver/task.h
index d0169d896..3a4d24187 100644
--- a/src/libserver/task.h
+++ b/src/libserver/task.h
@@ -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) */
diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h
index 226b250a7..c0c0c37c8 100644
--- a/src/lua/lua_common.h
+++ b/src/lua/lua_common.h
@@ -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;
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index 1e0e0a147..62bda0522 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -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;
 	}


More information about the Commits mailing list