Skip to content

Commit fe7a6f9

Browse files
fredrik-perssonmichalvasko
authored andcommitted
validation OPTIMIZE append to the diff instead of merging a one-node diff
lyd_val_diff_add() is called once per node created or deleted by validation. It built a standalone one-node diff and merged that into the accumulating diff, which duplicated the node's whole ancestor chain on every call. lyd_diff_add() already reuses the ancestors present in the target diff, so append straight into it when the node has no entry there yet. The merge is only needed when an operation on that very node already exists, to reconcile the two. lyd_diff_add() leaves out an operation that a parent already states, so add it back to keep the diff shape unchanged.
1 parent ec49b80 commit fe7a6f9

3 files changed

Lines changed: 97 additions & 30 deletions

File tree

‎src/diff.c‎

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,49 @@ lyd_diff_dup(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node *
377377
return LY_SUCCESS;
378378
}
379379

380+
void
381+
lyd_diff_find_node(struct lyd_node *diff, const struct lyd_node *node, struct lyd_node **diff_parent,
382+
struct lyd_node **match)
383+
{
384+
struct lyd_node *siblings, *m = NULL, *dparent = NULL;
385+
const struct lyd_node *parent = NULL;
386+
387+
siblings = diff;
388+
do {
389+
/* find next node parent */
390+
parent = node;
391+
while (parent->parent && (!dparent || (parent->parent->schema != dparent->schema))) {
392+
parent = parent->parent;
393+
}
394+
395+
if (lysc_is_dup_inst_list(parent->schema)) {
396+
/* assume it never exists, we are not able to distinguish whether it does or not */
397+
m = NULL;
398+
break;
399+
}
400+
401+
/* check whether it exists in the diff */
402+
if (lyd_find_sibling_first(siblings, parent, &m)) {
403+
break;
404+
}
405+
406+
/* another parent found */
407+
dparent = m;
408+
409+
/* move down in the diff */
410+
siblings = lyd_child_no_keys(m);
411+
} while (parent != node);
412+
413+
*diff_parent = dparent;
414+
*match = (m && (parent == node)) ? m : NULL;
415+
}
416+
380417
LY_ERR
381418
lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value,
382419
const char *key, const char *value, const char *position, const char *orig_key, const char *orig_position,
383420
struct lyd_node **diff, struct lyd_node **diff_node)
384421
{
385-
struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL, *elem;
386-
const struct lyd_node *parent = NULL;
422+
struct lyd_node *dup, *match = NULL, *diff_parent = NULL, *elem;
387423
enum lyd_diff_op cur_op;
388424
struct lyd_meta *meta;
389425
ly_bool found;
@@ -410,35 +446,11 @@ lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_
410446
*diff_node = NULL;
411447
}
412448

413-
/* find the first existing parent */
414-
siblings = *diff;
415-
do {
416-
/* find next node parent */
417-
parent = node;
418-
while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
419-
parent = parent->parent;
420-
}
421-
422-
if (lysc_is_dup_inst_list(parent->schema)) {
423-
/* assume it never exists, we are not able to distinguish whether it does or not */
424-
match = NULL;
425-
break;
426-
}
427-
428-
/* check whether it exists in the diff */
429-
if (lyd_find_sibling_first(siblings, parent, &match)) {
430-
break;
431-
}
432-
433-
/* another parent found */
434-
diff_parent = match;
449+
lyd_diff_find_node(*diff, node, &diff_parent, &match);
435450

436-
/* move down in the diff */
437-
siblings = lyd_child_no_keys(match);
438-
} while (parent != node);
439-
440-
if (match && (parent == node)) {
451+
if (match) {
441452
/* special case when there is already an operation on our descendant */
453+
diff_parent = match;
442454
assert(!lyd_diff_get_op(diff_parent, &cur_op, NULL));
443455

444456
/* move it to the end where it is expected (matters for user-ordered lists) */
@@ -525,6 +537,26 @@ lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_
525537
return LY_SUCCESS;
526538
}
527539

540+
LY_ERR
541+
lyd_diff_add_explicit_op(const struct lyd_node *node, enum lyd_diff_op op, const char *key, const char *value,
542+
const char *position, struct lyd_node **diff)
543+
{
544+
struct lyd_node *dup = NULL;
545+
struct lyd_meta *meta;
546+
547+
LY_CHECK_RET(lyd_diff_add(node, op, NULL, NULL, key, value, position, NULL, NULL, diff, &dup));
548+
549+
/* ::lyd_diff_add() omits an operation a parent already states, a validation diff states it
550+
* on every node */
551+
lyd_diff_find_meta(dup, "operation", &meta, NULL);
552+
if (!meta) {
553+
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:operation", lyd_diff_op2str(op),
554+
LYD_NEW_VAL_STORE_ONLY, NULL));
555+
}
556+
557+
return LY_SUCCESS;
558+
}
559+
528560
/**
529561
* @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
530562
*

‎src/diff.h‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ enum lyd_diff_op {
4040
LYD_DIFF_OP_NONE /**< No change of an existing inner node or default flag change of a term node. */
4141
};
4242

43+
/**
44+
* @brief Find a node and its deepest existing ancestor in a diff.
45+
*
46+
* @param[in] diff Diff to search (first sibling).
47+
* @param[in] node Data node to look for.
48+
* @param[out] diff_parent Deepest ancestor of @p node in @p diff, NULL if none.
49+
* @param[out] match Diff node of @p node, NULL if not present.
50+
*/
51+
void lyd_diff_find_node(struct lyd_node *diff, const struct lyd_node *node, struct lyd_node **diff_parent,
52+
struct lyd_node **match);
53+
4354
/**
4455
* @brief Add a new change into diff.
4556
*
@@ -60,4 +71,20 @@ LIBYANG_API_DECL LY_ERR lyd_diff_add(const struct lyd_node *node, enum lyd_diff_
6071
const char *orig_value, const char *key, const char *value, const char *position, const char *orig_key,
6172
const char *orig_position, struct lyd_node **diff, struct lyd_node **diff_node);
6273

74+
/**
75+
* @brief Add a new change into diff, always stating the operation on the added node.
76+
*
77+
* Unlike ::lyd_diff_add(), sets the operation metadata even when a parent already states it.
78+
*
79+
* @param[in] node Node (subtree) to add into diff.
80+
* @param[in] op Operation to set.
81+
* @param[in] key Key metadata to set.
82+
* @param[in] value Value metadata to set.
83+
* @param[in] position Position metadata to set.
84+
* @param[in,out] diff Diff to append to.
85+
* @return LY_ERR value.
86+
*/
87+
LY_ERR lyd_diff_add_explicit_op(const struct lyd_node *node, enum lyd_diff_op op, const char *key,
88+
const char *value, const char *position, struct lyd_node **diff);
89+
6390
#endif /* LY_DIFF_H_ */

‎src/validation.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ LY_ERR
188188
lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node **diff)
189189
{
190190
LY_ERR ret = LY_SUCCESS;
191-
struct lyd_node *new_diff = NULL;
191+
struct lyd_node *new_diff = NULL, *diff_parent, *match;
192192
const struct lyd_node *prev_inst;
193193
char *key = NULL, *value = NULL, *position = NULL;
194194
size_t buflen = 0, bufused = 0;
@@ -240,6 +240,14 @@ lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_no
240240
}
241241
}
242242

243+
/* appending reuses the ancestors already in the diff, unlike building a one-node diff and
244+
* merging it; merge only to reconcile an operation already recorded on this node */
245+
lyd_diff_find_node(*diff, node, &diff_parent, &match);
246+
if (!match) {
247+
ret = lyd_diff_add_explicit_op(node, op, key, value, position, diff);
248+
goto cleanup;
249+
}
250+
243251
/* create new diff tree */
244252
LY_CHECK_GOTO(ret = lyd_diff_add(node, op, NULL, NULL, key, value, position, NULL, NULL, &new_diff, NULL), cleanup);
245253

0 commit comments

Comments
 (0)