From 9f0a4220decbb0adaf8aa63d506cf01c55e3204c Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 14 Feb 2018 11:42:36 +0000 Subject: [PATCH] [Feature] Allow to reserve elements in libucl --- contrib/libucl/lua_ucl.c | 3 ++- contrib/libucl/ucl.h | 7 +++++++ contrib/libucl/ucl_hash.c | 22 ++++++++++++++++++++++ contrib/libucl/ucl_hash.h | 6 ++++++ contrib/libucl/ucl_util.c | 15 +++++++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/contrib/libucl/lua_ucl.c b/contrib/libucl/lua_ucl.c index 7611ecffb..52e787ee5 100644 --- a/contrib/libucl/lua_ucl.c +++ b/contrib/libucl/lua_ucl.c @@ -355,7 +355,8 @@ ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags) lua_pop (L, 1); } } - else if (is_array) { + + if (is_array) { #if LUA_VERSION_NUM >= 502 max = lua_rawlen (L, idx); #else diff --git a/contrib/libucl/ucl.h b/contrib/libucl/ucl.h index 7c069e794..8f6d6beb6 100644 --- a/contrib/libucl/ucl.h +++ b/contrib/libucl/ucl.h @@ -462,6 +462,13 @@ UCL_EXTERN ucl_object_t* ucl_object_pop_key (ucl_object_t *top, const char *key) UCL_EXTERN bool ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt, const char *key, size_t keylen, bool copy_key); +/** + * Reserve space in ucl array or object for `elt` elements + * @param obj object to reserve + * @param reserved size to reserve in an object + */ +UCL_EXTERN void ucl_object_reserve (ucl_object_t *obj, size_t reserved); + /** * Append an element to the end of array object * @param top destination object (must NOT be NULL) diff --git a/contrib/libucl/ucl_hash.c b/contrib/libucl/ucl_hash.c index 0bb8759f6..ce2a4fdb5 100644 --- a/contrib/libucl/ucl_hash.c +++ b/contrib/libucl/ucl_hash.c @@ -395,3 +395,25 @@ ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj) } } } + +void ucl_hash_reserve (ucl_hash_t *hashlin, size_t sz) +{ + if (hashlin == NULL) { + return; + } + + if (sz > hashlin->ar.m) { + kv_resize (const ucl_object_t *, hashlin->ar, sz); + + if (hashlin->caseless) { + khash_t(ucl_hash_caseless_node) *h = (khash_t( + ucl_hash_caseless_node) *) + hashlin->hash; + kh_resize (ucl_hash_caseless_node, h, sz); + } else { + khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) + hashlin->hash; + kh_resize (ucl_hash_node, h, sz); + } + } +} \ No newline at end of file diff --git a/contrib/libucl/ucl_hash.h b/contrib/libucl/ucl_hash.h index 92021e340..f474b462a 100644 --- a/contrib/libucl/ucl_hash.h +++ b/contrib/libucl/ucl_hash.h @@ -90,4 +90,10 @@ const void* ucl_hash_iterate (ucl_hash_t *hashlin, ucl_hash_iter_t *iter); */ bool ucl_hash_iter_has_next (ucl_hash_t *hashlin, ucl_hash_iter_t iter); +/** + * Reserves space in hash + * @param hashlin + */ +void ucl_hash_reserve (ucl_hash_t *hashlin, size_t sz); + #endif diff --git a/contrib/libucl/ucl_util.c b/contrib/libucl/ucl_util.c index 85b700a4d..8f5231e2f 100644 --- a/contrib/libucl/ucl_util.c +++ b/contrib/libucl/ucl_util.c @@ -2755,6 +2755,21 @@ ucl_object_new_full (ucl_type_t type, unsigned priority) return new; } +void ucl_object_reserve (ucl_object_t *obj, size_t reserved) +{ + if (obj->type == UCL_ARRAY) { + UCL_ARRAY_GET (vec, obj); + + if (vec->m < reserved) { + /* Preallocate some space for arrays */ + kv_resize (ucl_object_t *, *vec, reserved); + } + } + else if (obj->type == UCL_OBJECT) { + ucl_hash_reserve (obj->value.ov, reserved); + } +} + ucl_object_t* ucl_object_new_userdata (ucl_userdata_dtor dtor, ucl_userdata_emitter emitter, -- 2.39.5