diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-03-05 14:43:07 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-03-05 14:43:07 +0000 |
commit | 900f364e367316e80c2f21bdbadbec540f582e40 (patch) | |
tree | 7c8033b742dff3d4a51ab506208adda0b631749d | |
parent | 75327b3889c80ad0a0c95ccf4444b5c2bc148e14 (diff) | |
download | rspamd-900f364e367316e80c2f21bdbadbec540f582e40.tar.gz rspamd-900f364e367316e80c2f21bdbadbec540f582e40.zip |
[Rework] Use direct hashing instead of string hashing
-rw-r--r-- | src/lua/lua_common.c | 18 | ||||
-rw-r--r-- | src/lua/lua_common.h | 6 |
2 files changed, 13 insertions, 11 deletions
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index ef13d4119..5a2672fbc 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -1,5 +1,5 @@ /* - * Copyright 2023 Vsevolod Stakhov + * Copyright 2024 Vsevolod Stakhov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,7 +48,7 @@ lua_error_quark(void) /* * Used to map string to a pointer */ -KHASH_INIT(lua_class_set, const char *, int, 1, rspamd_str_hash, rspamd_str_equal); +KHASH_INIT(lua_class_set, int, int, 1, kh_int_hash_func, kh_int_hash_equal); struct rspamd_lua_context { lua_State *L; khash_t(lua_class_set) * classes; @@ -76,7 +76,7 @@ rspamd_lua_ctx_by_state(lua_State *L) /** * Create new class and store metatable on top of the stack (must be popped if not needed) * @param L - * @param classname name of class + * @param classname name of class, **MUST** be a static string * @param func table of class methods */ void rspamd_lua_new_class(lua_State *L, @@ -120,7 +120,7 @@ void rspamd_lua_new_class(lua_State *L, lua_pushvalue(L, -1); /* Preserves metatable */ int offset = luaL_ref(L, LUA_REGISTRYINDEX); - k = kh_put(lua_class_set, ctx->classes, classname, &r); + k = kh_put(lua_class_set, ctx->classes, GPOINTER_TO_INT(classname), &r); kh_value(ctx->classes, k) = offset; /* MT is left on stack ! */ } @@ -183,7 +183,7 @@ void rspamd_lua_setclass(lua_State *L, const gchar *classname, gint objidx) khiter_t k; struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L); - k = kh_get(lua_class_set, ctx->classes, classname); + k = kh_get(lua_class_set, ctx->classes, GPOINTER_TO_INT(classname)); g_assert(k != kh_end(ctx->classes)); lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); @@ -199,7 +199,7 @@ void rspamd_lua_class_metatable(lua_State *L, const gchar *classname) khiter_t k; struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L); - k = kh_get(lua_class_set, ctx->classes, classname); + k = kh_get(lua_class_set, ctx->classes, GPOINTER_TO_INT(classname)); g_assert(k != kh_end(ctx->classes)); lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); @@ -211,7 +211,7 @@ void rspamd_lua_add_metamethod(lua_State *L, const gchar *classname, khiter_t k; struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L); - k = kh_get(lua_class_set, ctx->classes, classname); + k = kh_get(lua_class_set, ctx->classes, GPOINTER_TO_INT(classname)); g_assert(k != kh_end(ctx->classes)); lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); @@ -1277,7 +1277,7 @@ rspamd_lua_check_class(lua_State *L, gint index, const gchar *name) if (lua_getmetatable(L, index)) { struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L); - k = kh_get(lua_class_set, ctx->classes, name); + k = kh_get(lua_class_set, ctx->classes, GPOINTER_TO_INT(name)); if (k == kh_end(ctx->classes)) { lua_pop(L, 1); @@ -1984,7 +1984,7 @@ rspamd_lua_check_udata_common(lua_State *L, gint pos, const gchar *classname, if (lua_getmetatable(L, pos)) { struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L); - k = kh_get(lua_class_set, ctx->classes, classname); + k = kh_get(lua_class_set, ctx->classes, GPOINTER_TO_INT(classname)); if (k == kh_end(ctx->classes)) { goto err; diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index b46c0bfc9..31a8c2fac 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -176,6 +176,8 @@ void rspamd_lua_new_class(lua_State *L, /** * Set class name for object at @param objidx position +* @param L + * @param classname **MUST BE STATIC**, direct address is used for comparisons! */ void rspamd_lua_setclass(lua_State *L, const gchar *classname, gint objidx); @@ -537,7 +539,7 @@ gsize lua_logger_out_type(lua_State *L, gint pos, gchar *outbuf, * Safely checks userdata to match specified class * @param L * @param pos -* @param classname +* @param classname **MUST BE STATIC**, direct address is used for comparisons! */ void *rspamd_lua_check_udata(lua_State *L, gint pos, const gchar *classname); @@ -554,7 +556,7 @@ void *rspamd_lua_check_udata(lua_State *L, gint pos, const gchar *classname); * Safely checks userdata to match specified class * @param L * @param pos -* @param classname +* @param classname **MUST BE STATIC**, direct address is used for comparisons! */ void *rspamd_lua_check_udata_maybe(lua_State *L, gint pos, const gchar *classname); |