Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize the duplication of a hash table and fix comment #2288

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ lyht_free(struct ly_ht *ht, void (*val_free)(void *val_p))
*
* @param[in] ht Hash table to resize.
* @param[in] operation Operation to perform. 1 to enlarge, -1 to shrink, 0 to only rehash all records.
* @param[in] check Whether to check if the value has already been inserted or not.
* @return LY_ERR value.
*/
static LY_ERR
Expand Down
5 changes: 5 additions & 0 deletions src/tree_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/tree_data_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/tree_data_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
*
Expand Down
Loading