]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Improve allocation policy when interacting with Lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 14 Feb 2018 14:44:17 +0000 (14:44 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 14 Feb 2018 14:45:25 +0000 (14:45 +0000)
contrib/libucl/lua_ucl.c
contrib/libucl/ucl_hash.c

index 52e787ee5a68ff7818adb5abf77b45398054c04e..15d41471e41976996e8957ba381e5e51c8dce19d 100644 (file)
@@ -301,7 +301,7 @@ ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags)
        size_t keylen;
        const char *k;
        bool is_array = true, is_implicit = false, found_mt = false;
-       int max = INT_MIN;
+       size_t max = 0, nelts = 0;
 
        if (idx < 0) {
                /* For negative indicies we want to invert them */
@@ -320,10 +320,22 @@ ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags)
                        } else if (strcmp (classname, UCL_ARRAY_TYPE_META) == 0) {
                                is_array = true;
                                found_mt = true;
+#if LUA_VERSION_NUM >= 502
+                               max = lua_rawlen (L, idx);
+#else
+                               max = lua_objlen (L, idx);
+#endif
+                               nelts = max;
                        } else if (strcmp (classname, UCL_IMPL_ARRAY_TYPE_META) == 0) {
                                is_array = true;
                                is_implicit = true;
                                found_mt = true;
+#if LUA_VERSION_NUM >= 502
+                               max = lua_rawlen (L, idx);
+#else
+                               max = lua_objlen (L, idx);
+#endif
+                               nelts = max;
                        }
                }
 
@@ -342,34 +354,24 @@ ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags)
                                        }
                                } else {
                                        /* Keys are not integer */
-                                       lua_pop (L, 2);
                                        is_array = false;
-                                       break;
                                }
                        } else {
                                /* Keys are not numeric */
-                               lua_pop (L, 2);
                                is_array = false;
-                               break;
                        }
                        lua_pop (L, 1);
+                       nelts ++;
                }
        }
 
-       if (is_array) {
-#if LUA_VERSION_NUM >= 502
-               max = lua_rawlen (L, idx);
-#else
-               max = lua_objlen (L, idx);
-#endif
-       }
-
        /* Table iterate */
        if (is_array) {
                int i;
 
                if (!is_implicit) {
                        top = ucl_object_typed_new (UCL_ARRAY);
+                       ucl_object_reserve (top, nelts);
                }
                else {
                        top = NULL;
@@ -395,15 +397,18 @@ ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags)
        else {
                lua_pushnil (L);
                top = ucl_object_typed_new (UCL_OBJECT);
+               ucl_object_reserve (top, nelts);
+
                while (lua_next (L, idx) != 0) {
                        /* copy key to avoid modifications */
-                       k = lua_tolstring (L, -2, &keylen);
-                       obj = ucl_object_lua_fromelt (L, lua_gettop (L), flags);
+                       lua_pushvalue (L, -2);
+                       k = lua_tolstring (L, -1, &keylen);
+                       obj = ucl_object_lua_fromelt (L, lua_gettop (L) - 1, flags);
 
                        if (obj != NULL) {
                                ucl_object_insert_key (top, obj, k, keylen, true);
                        }
-                       lua_pop (L, 1);
+                       lua_pop (L, 2);
                }
        }
 
index 92c8b3933c06bff998afc1edee63d9769f06d1e0..352ae3a2bba4a1fa07607b58134f7618fca104eb 100644 (file)
@@ -424,11 +424,11 @@ void ucl_hash_reserve (ucl_hash_t *hashlin, size_t sz)
                        khash_t(ucl_hash_caseless_node) *h = (khash_t(
                                        ucl_hash_caseless_node) *)
                                        hashlin->hash;
-                       kh_resize (ucl_hash_caseless_node, h, sz);
+                       kh_resize (ucl_hash_caseless_node, h, sz * 2);
                } else {
                        khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *)
                                        hashlin->hash;
-                       kh_resize (ucl_hash_node, h, sz);
+                       kh_resize (ucl_hash_node, h, sz * 2);
                }
        }
 }
\ No newline at end of file