]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Use lua_createtable when size is known
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 5 Oct 2016 09:50:59 +0000 (10:50 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 5 Oct 2016 09:50:59 +0000 (10:50 +0100)
src/lua/lua_ip.c
src/lua/lua_regexp.c
src/lua/lua_task.c
src/lua/lua_url.c
src/lua/lua_util.c

index 97f75dddac84e35e2db96803fefeca854181b9a4..0ebbf20039eaccdbca6daa3491a6b5cfbb248f4c 100644 (file)
@@ -223,8 +223,8 @@ lua_ip_to_table (lua_State *L)
        guint8 *ptr;
 
        if (ip != NULL && ip->addr) {
-               lua_newtable (L);
                ptr = rspamd_inet_address_get_hash_key (ip->addr, &max);
+               lua_createtable (L, max, 0);
 
                for (i = 1; i <= max; i++, ptr++) {
                        lua_pushnumber (L, *ptr);
@@ -248,9 +248,9 @@ lua_ip_str_octets (lua_State *L)
        char numbuf[8];
 
        if (ip != NULL && ip->addr) {
-               lua_newtable (L);
                af = rspamd_inet_address_get_af (ip->addr);
                ptr = rspamd_inet_address_get_hash_key (ip->addr, &max);
+               lua_createtable (L, max * 2, 0);
 
                for (i = 1; i <= max; i++, ptr++) {
                        if (af == AF_INET) {
@@ -288,9 +288,9 @@ lua_ip_inversed_str_octets (lua_State *L)
        gint af;
 
        if (ip != NULL && ip->addr) {
-               lua_newtable (L);
                ptr = rspamd_inet_address_get_hash_key (ip->addr, &max);
                af = rspamd_inet_address_get_af (ip->addr);
+               lua_createtable (L, max * 2, 0);
 
                ptr += max - 1;
                for (i = 1; i <= max; i++, ptr--) {
index e92c8530b94db8e6c8c85b47508cf89f0213431c..80b7521723fd6a3a1527861285e174114f0c688d 100644 (file)
@@ -419,7 +419,7 @@ lua_regexp_search (lua_State *L)
                                        captures)) {
 
                                if (capture) {
-                                       lua_newtable (L);
+                                       lua_createtable (L, captures->len, 0);
 
                                        for (capn = 0; capn < captures->len; capn ++) {
                                                cap = &g_array_index (captures, struct rspamd_re_capture,
index 8da6d3f16037c8cd6bf3a67b4dd1af64821c48c0..4706d69a06a84fc63b756a0c789e4c204fd7745a 100644 (file)
@@ -1116,13 +1116,20 @@ lua_task_get_urls (lua_State * L)
        struct rspamd_task *task = lua_check_task (L, 1);
        struct lua_tree_cb_data cb;
        gboolean need_emails = FALSE;
+       gsize sz;
 
        if (task) {
                if (lua_gettop (L) >= 2) {
                        need_emails = lua_toboolean (L, 2);
                }
 
-               lua_newtable (L);
+               sz = g_hash_table_size (task->urls);
+
+               if (need_emails) {
+                       sz += g_hash_table_size (task->emails);
+               }
+
+               lua_createtable (L, sz, 0);
                cb.i = 1;
                cb.L = L;
                g_hash_table_foreach (task->urls, lua_tree_url_callback, &cb);
@@ -1222,7 +1229,7 @@ lua_task_get_emails (lua_State * L)
        struct lua_tree_cb_data cb;
 
        if (task) {
-               lua_newtable (L);
+               lua_createtable (L, g_hash_table_size (task->emails), 0);
                cb.i = 1;
                cb.L = L;
                g_hash_table_foreach (task->emails, lua_tree_url_callback, &cb);
@@ -1242,7 +1249,7 @@ lua_task_get_text_parts (lua_State * L)
        struct rspamd_mime_text_part *part, **ppart;
 
        if (task != NULL) {
-               lua_newtable (L);
+               lua_createtable (L, task->text_parts->len, 0);
 
                for (i = 0; i < task->text_parts->len; i ++) {
                        part = g_ptr_array_index (task->text_parts, i);
@@ -1268,7 +1275,7 @@ lua_task_get_parts (lua_State * L)
        struct rspamd_mime_part *part, **ppart;
 
        if (task != NULL) {
-               lua_newtable (L);
+               lua_createtable (L, task->parts->len, 0);
 
                for (i = 0; i < task->parts->len; i ++) {
                        part = g_ptr_array_index (task->parts, i);
@@ -2218,7 +2225,7 @@ lua_task_get_archives (lua_State *L)
        struct rspamd_archive **parch;
 
        if (task) {
-               lua_newtable (L);
+               lua_createtable (L, task->parts->len, 0);
 
                for (i = 0; i < task->parts->len; i ++) {
                        part = g_ptr_array_index (task->parts, i);
@@ -2276,11 +2283,13 @@ lua_push_symbol_result (lua_State *L,
                                opt = s->options;
                                lua_pushstring (L, "options");
                                lua_newtable (L);
+
                                while (opt) {
                                        lua_pushstring (L, opt->data);
                                        lua_rawseti (L, -2, j++);
                                        opt = g_list_next (opt);
                                }
+
                                lua_settable (L, -3);
                        }
 
@@ -2589,7 +2598,7 @@ lua_task_get_timeval (lua_State *L)
        struct rspamd_task *task = lua_check_task (L, 1);
 
        if (task != NULL) {
-               lua_newtable (L);
+               lua_createtable (L, 0, 2);
                lua_pushstring (L, "tv_sec");
                lua_pushnumber (L, (lua_Number)task->tv.tv_sec);
                lua_settable (L, -3);
@@ -2720,7 +2729,7 @@ lua_task_get_flags (lua_State *L)
        guint flags, bit, i;
 
        if (task) {
-               lua_newtable (L);
+               lua_createtable (L, 8, 0);
 
                flags = task->flags;
 
@@ -3088,7 +3097,7 @@ lua_task_get_metric_score (lua_State *L)
        if (task && metric_name) {
                if ((metric_res =
                        g_hash_table_lookup (task->results, metric_name)) != NULL) {
-                       lua_newtable (L);
+                       lua_createtable (L, 2, 0);
                        lua_pushnumber (L, metric_res->score);
                        rs = rspamd_task_get_required_score (task, metric_res);
                        lua_rawseti (L, -2, 1);
index ad77fcde2a13b67076f6dfbd24781ca4870e1c5d..2fd34664b256c2931ce3d857507778112a00778d 100644 (file)
@@ -376,7 +376,7 @@ lua_url_to_table (lua_State *L)
 
        if (url != NULL) {
                u = url->url;
-               lua_newtable (L);
+               lua_createtable (L, 0, 12);
                lua_pushstring (L, "url");
                lua_pushlstring (L, u->string, u->urllen);
                lua_settable (L, -3);
index b3c30ab296d7f5abb3eb15d52fd56f733f351c6f..9c37bc8617f6fa94c303512b619a840df646f030 100644 (file)
@@ -873,7 +873,7 @@ lua_util_tokenize_text (lua_State *L)
                lua_pushnil (L);
        }
        else {
-               lua_newtable (L);
+               lua_createtable (L, res->len, 0);
 
                for (i = 0; i < res->len; i ++) {
                        w = &g_array_index (res, rspamd_ftok_t, i);
@@ -992,12 +992,12 @@ lua_util_parse_addr (lua_State *L)
                }
                else {
                        cnt = internet_address_list_length (ia);
-                       lua_newtable (L);
+                       lua_createtable (L, cnt, 0);
 
                        for (i = 0; i < cnt; i ++) {
                                addr = internet_address_list_get_address (ia, i);
 
-                               lua_newtable (L);
+                               lua_createtable (L, 0, 2);
                                lua_pushstring (L, "name");
                                lua_pushstring (L, internet_address_get_name (addr));
                                lua_settable (L, -3);
@@ -1147,7 +1147,7 @@ lua_util_glob (lua_State *L)
                }
        }
 
-       lua_newtable (L);
+       lua_createtable (L, gl.gl_pathc, 0);
        /* Push results */
        for (i = 0; i < (gint)gl.gl_pathc; i ++) {
                lua_pushstring (L, gl.gl_pathv[i]);