Browse Source

[Project] Start optimization of userdata hashing

pull/4857/head
Vsevolod Stakhov 2 months ago
parent
commit
8238dc0a23
No account linked to committer's email address

+ 5
- 4
src/controller.c View File

@@ -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.
@@ -34,6 +34,7 @@
#include "utlist.h"
#include "libmime/lang_detection.h"
#include "mempool_vars_internal.h"
#include "lua/lua_classnames.h"
#include <math.h>

/* 60 seconds for worker's IO */
@@ -1555,7 +1556,7 @@ rspamd_controller_handle_lua_history(lua_State *L,

ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
pconn_ent = lua_newuserdata(L, sizeof(*pconn_ent));
*pconn_ent = conn_ent;
rspamd_lua_setclass(L, "rspamd{csession}", -1);
@@ -1916,7 +1917,7 @@ rspamd_controller_handle_lua(struct rspamd_http_connection_entry *conn_ent,
}

ptask = lua_newuserdata(L, sizeof(*ptask));
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
*ptask = task;

pconn = lua_newuserdata(L, sizeof(*pconn));
@@ -3541,7 +3542,7 @@ rspamd_controller_handle_lua_plugin(struct rspamd_http_connection_entry *conn_en

/* Task */
ptask = lua_newuserdata(L, sizeof(*ptask));
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
*ptask = task;

/* Connection */

+ 4
- 4
src/libmime/message.c View File

@@ -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.
@@ -994,7 +994,7 @@ rspamd_message_from_data(struct rspamd_task *task, const guchar *start,
rspamd_lua_setclass(L, "rspamd{mimepart}", -1);
*pmime = part;
ptask = lua_newuserdata(L, sizeof(struct rspamd_task *));
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
*ptask = task;

if (lua_pcall(L, 2, 2, 0) != 0) {
@@ -1447,7 +1447,7 @@ void rspamd_message_process(struct rspamd_task *task)
rspamd_lua_setclass(L, "rspamd{mimepart}", -1);
*pmime = part;
ptask = lua_newuserdata(L, sizeof(struct rspamd_task *));
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
*ptask = task;

if (lua_pcall(L, 2, 2, err_idx) != 0) {
@@ -1519,7 +1519,7 @@ void rspamd_message_process(struct rspamd_task *task)
rspamd_lua_setclass(L, "rspamd{mimepart}", -1);
*pmime = part;
ptask = lua_newuserdata(L, sizeof(struct rspamd_task *));
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
*ptask = task;

if (lua_pcall(L, 2, 0, err_idx) != 0) {

+ 2
- 2
src/libmime/scan_result.c View File

@@ -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.
@@ -566,7 +566,7 @@ rspamd_task_insert_result_full(struct rspamd_task *task,

if (!rspamd_lua_universal_pcall(L, mres->symbol_cbref,
G_STRLOC, 1, "uss", &err,
"rspamd{task}", task, symbol, mres->name ? mres->name : "default")) {
rspamd_task_classname, task, symbol, mres->name ? mres->name : "default")) {
msg_warn_task("cannot call for symbol_cbref for result %s: %e",
mres->name ? mres->name : "default", err);
g_error_free(err);

+ 3
- 2
src/libserver/protocol.c View File

@@ -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.
@@ -27,6 +27,7 @@
#include "libserver/mempool_vars_internal.h"
#include "contrib/fastutf8/fastutf8.h"
#include "task.h"
#include "lua/lua_classnames.h"
#include <math.h>

#ifdef SYS_ZSTD
@@ -1923,7 +1924,7 @@ void rspamd_protocol_write_log_pipe(struct rspamd_task *task)
if (lua_isfunction(L, -1)) {
ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
/* stack:
* -1: task
* -2: func

+ 3
- 2
src/libserver/re_cache.c View File

@@ -25,6 +25,7 @@
#include "lua/lua_common.h"
#include "libstat/stat_api.h"
#include "contrib/uthash/utlist.h"
#include "lua/lua_classnames.h"

#include "khash.h"

@@ -562,7 +563,7 @@ rspamd_re_cache_check_lua_condition(struct rspamd_task *task,

if (!rspamd_lua_universal_pcall(L, lua_cbref,
G_STRLOC, 1, "utii", &err,
"rspamd{task}", task,
rspamd_task_classname, task,
text_pos, start, end)) {
msg_warn_task("cannot call for re_cache_check_lua_condition for re %s: %e",
rspamd_regexp_get_pattern(re), err);
@@ -908,7 +909,7 @@ rspamd_re_cache_process_selector(struct rspamd_task *task,
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);

if ((ret = lua_pcall(L, 1, 1, err_idx)) != 0) {
msg_err_task("call to selector %s "

+ 3
- 2
src/libserver/task.c View File

@@ -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.
@@ -29,6 +29,7 @@
#include "libserver/cfg_file_private.h"
#include "libmime/lang_detection.h"
#include "libmime/scan_result_private.h"
#include "lua/lua_classnames.h"

#ifdef WITH_JEMALLOC
#include <jemalloc/jemalloc.h>
@@ -1616,7 +1617,7 @@ void rspamd_task_write_log(struct rspamd_task *task)
L = task->cfg->lua_state;
lua_rawgeti(L, LUA_REGISTRYINDEX, GPOINTER_TO_INT(lf->data));
ptask = lua_newuserdata(L, sizeof(*ptask));
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
*ptask = task;

if (lua_pcall(L, 1, 1, 0) != 0) {

+ 1
- 1
src/libstat/backends/redis_backend.cxx View File

@@ -218,7 +218,7 @@ gsize rspamd_redis_expand_object(const gchar *pattern,
lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->cbref_user);
ptask = (struct rspamd_task **) lua_newuserdata(L, sizeof(struct rspamd_task *));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);

if (lua_pcall(L, 1, 1, err_idx) != 0) {
msg_err_task("call to user extraction script failed: %s",

+ 5
- 5
src/libstat/backends/sqlite3_backend.c View File

@@ -1,11 +1,11 @@
/*-
* Copyright 2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -177,7 +177,7 @@ rspamd_sqlite3_get_user(struct rspamd_stat_sqlite3_db *db,
lua_rawgeti(L, LUA_REGISTRYINDEX, db->cbref_user);
ptask = lua_newuserdata(L, sizeof(struct rspamd_task *));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);

if (lua_pcall(L, 1, 1, err_idx) != 0) {
msg_err_task("call to user extraction script failed: %s",
@@ -246,7 +246,7 @@ rspamd_sqlite3_get_language(struct rspamd_stat_sqlite3_db *db,
lua_rawgeti(L, LUA_REGISTRYINDEX, db->cbref_language);
ptask = lua_newuserdata(L, sizeof(struct rspamd_task *));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);

if (lua_pcall(L, 1, 1, err_idx) != 0) {
msg_err_task("call to language extraction script failed: %s",

+ 5
- 5
src/libstat/classifiers/lua_classifier.c View File

@@ -1,11 +1,11 @@
/*-
* Copyright 2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -145,7 +145,7 @@ lua_classifier_classify(struct rspamd_classifier *cl,
lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->classify_ref);
ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
pcfg = lua_newuserdata(L, sizeof(*pcfg));
*pcfg = cl->cfg;
rspamd_lua_setclass(L, "rspamd{classifier}", -1);
@@ -200,7 +200,7 @@ lua_classifier_learn_spam(struct rspamd_classifier *cl,
lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->learn_ref);
ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
pcfg = lua_newuserdata(L, sizeof(*pcfg));
*pcfg = cl->cfg;
rspamd_lua_setclass(L, "rspamd{classifier}", -1);

+ 8
- 7
src/libstat/stat_process.c View File

@@ -1,11 +1,11 @@
/*-
* Copyright 2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -21,6 +21,7 @@
#include "libmime/images.h"
#include "libserver/html/html.h"
#include "lua/lua_common.h"
#include "lua/lua_classnames.h"
#include "libserver/mempool_vars_internal.h"
#include "utlist.h"
#include <math.h>
@@ -54,7 +55,7 @@ rspamd_stat_tokenize_parts_metadata(struct rspamd_stat_ctx *st_ctx,

ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);

if ((ret = lua_pcall(L, 1, 1, err_idx)) != 0) {
msg_err_task("call to stat_tokens lua "
@@ -210,7 +211,7 @@ rspamd_stat_classifier_is_skipped(struct rspamd_task *task,
/* Push task and two booleans: is_spam and is_unlearn */
struct rspamd_task **ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);

if (is_learn) {
lua_pushboolean(L, is_spam);
@@ -1063,7 +1064,7 @@ rspamd_stat_check_autolearn(struct rspamd_task *task)

ptask = lua_newuserdata(L, sizeof(struct rspamd_task *));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);

if (lua_pcall(L, 1, 1, err_idx) != 0) {
msg_err_task("call to autolearn script failed: "
@@ -1118,7 +1119,7 @@ rspamd_stat_check_autolearn(struct rspamd_task *task)

ptask = lua_newuserdata(L, sizeof(struct rspamd_task *));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
/* Push the whole object as well */
ucl_object_push_lua(L, obj, true);


+ 2
- 1
src/lua/CMakeLists.txt View File

@@ -34,6 +34,7 @@ SET(LUASRC ${CMAKE_CURRENT_SOURCE_DIR}/lua_common.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_spf.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_tensor.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_parsers.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_compress.c)
${CMAKE_CURRENT_SOURCE_DIR}/lua_compress.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_classnames.c)

SET(RSPAMD_LUA ${LUASRC} PARENT_SCOPE)

+ 3
- 1
src/lua/lua_common.h View File

@@ -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.
@@ -19,6 +19,7 @@

#include "config.h"


/* Lua headers do not have __cplusplus guards... */
#ifdef __cplusplus
extern "C" {
@@ -40,6 +41,7 @@ extern "C" {
#include "rspamd.h"
#include "ucl.h"
#include "lua_ucl.h"
#include "lua_classnames.h"

#ifdef __cplusplus
extern "C" {

+ 4
- 4
src/lua/lua_config.c View File

@@ -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.
@@ -1242,7 +1242,7 @@ lua_metric_symbol_callback(struct rspamd_task *task,
}

ptask = lua_newuserdata(L, sizeof(struct rspamd_task *));
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
*ptask = task;

if ((ret = lua_pcall(L, 1, LUA_MULTRET, err_idx)) != 0) {
@@ -1382,7 +1382,7 @@ lua_metric_symbol_callback_coro(struct rspamd_task *task,
}

ptask = lua_newuserdata(thread, sizeof(struct rspamd_task *));
rspamd_lua_setclass(thread, "rspamd{task}", -1);
rspamd_lua_setclass(thread, rspamd_task_classname, -1);
*ptask = task;

thread_entry->finish_callback = lua_metric_symbol_callback_return;
@@ -4773,7 +4773,7 @@ void lua_call_finish_script(struct rspamd_config_cfg_lua_script *sc,
lua_rawgeti(L, LUA_REGISTRYINDEX, sc->cbref);

ptask = lua_newuserdata(L, sizeof(struct rspamd_task *));
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
*ptask = task;

lua_thread_call(thread, 1);

+ 2
- 2
src/lua/lua_logger.c View File

@@ -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.
@@ -653,7 +653,7 @@ lua_logger_get_id(lua_State *L, gint pos, GError **err)

clsname = lua_tostring(L, -1);

if (strcmp(clsname, "rspamd{task}") == 0) {
if (strcmp(clsname, rspamd_task_classname) == 0) {
struct rspamd_task *task = lua_check_task(L, pos);

if (task) {

+ 8
- 8
src/lua/lua_task.c View File

@@ -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.
@@ -1382,7 +1382,7 @@ static const struct luaL_reg archivelib_m[] = {
struct rspamd_task *
lua_check_task(lua_State *L, gint pos)
{
void *ud = rspamd_lua_check_udata(L, pos, "rspamd{task}");
void *ud = rspamd_lua_check_udata(L, pos, rspamd_task_classname);
luaL_argcheck(L, ud != NULL, pos, "'task' expected");
return ud ? *((struct rspamd_task **) ud) : NULL;
}
@@ -1390,7 +1390,7 @@ lua_check_task(lua_State *L, gint pos)
struct rspamd_task *
lua_check_task_maybe(lua_State *L, gint pos)
{
void *ud = rspamd_lua_check_udata_maybe(L, pos, "rspamd{task}");
void *ud = rspamd_lua_check_udata_maybe(L, pos, rspamd_task_classname);

return ud ? *((struct rspamd_task **) ud) : NULL;
}
@@ -1781,7 +1781,7 @@ lua_task_load_from_file(lua_State *L)
if (res) {
ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
}
else {
if (err) {
@@ -1832,7 +1832,7 @@ lua_task_load_from_string(lua_State *L)

ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);

return 2;
}
@@ -1868,7 +1868,7 @@ lua_task_create(lua_State *L)

ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);

return 1;
}
@@ -7271,7 +7271,7 @@ luaopen_archive(lua_State *L)

void luaopen_task(lua_State *L)
{
rspamd_lua_new_class(L, "rspamd{task}", tasklib_m);
rspamd_lua_new_class(L, rspamd_task_classname, tasklib_m);
lua_pop(L, 1);

rspamd_lua_add_preload(L, "rspamd_task", lua_load_task);
@@ -7290,6 +7290,6 @@ void rspamd_lua_task_push(lua_State *L, struct rspamd_task *task)
struct rspamd_task **ptask;

ptask = lua_newuserdata(L, sizeof(gpointer));
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
*ptask = task;
}

+ 3
- 3
src/plugins/fuzzy_check.c View File

@@ -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.
@@ -1566,7 +1566,7 @@ fuzzy_rule_check_mimepart(struct rspamd_task *task,

ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);

ppart = lua_newuserdata(L, sizeof(*ppart));
*ppart = part;
@@ -3510,7 +3510,7 @@ fuzzy_process_handler(struct rspamd_http_connection_entry *conn_ent,
lua_rawgeti(L, LUA_REGISTRYINDEX, rule->learn_condition_cb);
ptask = lua_newuserdata(L, sizeof(struct rspamd_task *));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);

if (lua_pcall(L, 1, LUA_MULTRET, err_idx) != 0) {
msg_err_task("call to fuzzy learn condition failed: %s",

+ 2
- 2
src/plugins/regexp.c View File

@@ -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.
@@ -475,7 +475,7 @@ rspamd_lua_call_expression_func(struct ucl_lua_funcdata *lua_data,
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_data->idx);
/* Now we got function in top of stack */
ptask = lua_newuserdata(L, sizeof(struct rspamd_task *));
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);
*ptask = task;

/* Now push all arguments */

+ 2
- 2
src/rspamadm/lua_repl.c View File

@@ -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.
@@ -448,7 +448,7 @@ rspamadm_lua_message_handler(lua_State *L, gint argc, gchar **argv)
lua_pushvalue(L, func_idx);
ptask = lua_newuserdata(L, sizeof(*ptask));
*ptask = task;
rspamd_lua_setclass(L, "rspamd{task}", -1);
rspamd_lua_setclass(L, rspamd_task_classname, -1);


if (lua_repl_thread_call(thread, 1, argv[i], lua_thread_str_error_cb) == 0) {

Loading…
Cancel
Save