From 747486a9656da913e578fad6f24f897f95e66bf6 Mon Sep 17 00:00:00 2001 From: Ondrej Kusnirik Date: Mon, 19 Aug 2024 10:52:44 +0200 Subject: [PATCH] tree data OPTIMIZE duplication of a hash table Allocate the exact amount of memory for a new hash table as was allocated for the old one at once. --- src/tree_data.c | 5 +++++ src/tree_data_hash.c | 2 +- src/tree_data_internal.h | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/tree_data.c b/src/tree_data.c index e1463ea25..5bddf5f9c 100644 --- a/src/tree_data.c +++ b/src/tree_data.c @@ -2148,6 +2148,11 @@ lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_ struct lyd_node *child; if (options & LYD_DUP_RECURSIVE) { + /* create a hash table with the size of the previous hash table (duplicate) */ + if (orig->children_ht) { + ((struct lyd_node_inner *)dup)->children_ht = lyht_new(orig->children_ht->size, sizeof(struct lyd_node *), lyd_hash_table_val_equal, NULL, 1); + } + /* duplicate all the children */ LY_LIST_FOR(orig->child, child) { LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, LYD_INSERT_NODE_LAST, NULL, options, NULL), error); diff --git a/src/tree_data_hash.c b/src/tree_data_hash.c index ce237b1e5..9be655ca4 100644 --- a/src/tree_data_hash.c +++ b/src/tree_data_hash.c @@ -84,7 +84,7 @@ lyd_hash(struct lyd_node *node) * * Implementation of ::lyht_value_equal_cb. */ -static ly_bool +ly_bool lyd_hash_table_val_equal(void *val1_p, void *val2_p, ly_bool mod, void *UNUSED(cb_data)) { struct lyd_node *val1, *val2; diff --git a/src/tree_data_internal.h b/src/tree_data_internal.h index 2bfa66de7..b3a027c58 100644 --- a/src/tree_data_internal.h +++ b/src/tree_data_internal.h @@ -16,6 +16,7 @@ #ifndef LY_TREE_DATA_INTERNAL_H_ #define LY_TREE_DATA_INTERNAL_H_ +#include "compat.h" #include "log.h" #include "plugins_types.h" #include "tree_data.h" @@ -600,6 +601,13 @@ LY_ERR ly_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, */ LY_ERR lyd_hash(struct lyd_node *node); +/** + * @brief Compare callback for values in hash table. + * + * Implementation of ::lyht_value_equal_cb. + */ +ly_bool lyd_hash_table_val_equal(void *val1_p, void *val2_p, ly_bool mod, void *cb_data); + /** * @brief Insert hash of the node into the hash table of its parent. *