summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Venekamp <jan@venekamp.net>2022-11-20 02:08:22 +0100
committerHauke Mehrtens <hauke@hauke-m.de>2023-03-04 19:39:32 +0100
commit7e01d66d7bec5f9e3694dcad25c472327a0ff352 (patch)
tree0c51a3bf2ea18efd0448f6191a22d6f193204c53
parentb7ceda9a2fbdd0224c7bf742524ddc8c19c180e9 (diff)
downloaduci-7e01d66d7bec5f9e3694dcad25c472327a0ff352.tar.gz
uci: optimize update option in uci_set
Optimize for the case when there is no need to reallocate memory when updating an option in uci_set. Signed-off-by: Jan Venekamp <jan@venekamp.net>
-rw-r--r--list.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/list.c b/list.c
index a8f2a2c..5148dfd 100644
--- a/list.c
+++ b/list.c
@@ -725,17 +725,19 @@ int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
ptr->last = &ptr->s->e;
} else if (ptr->o && ptr->option) { /* update option */
- struct uci_option *old = ptr->o;
-
- if ((ptr->o->type == UCI_TYPE_STRING) &&
- !strcmp(ptr->o->v.string, ptr->value))
+ if (ptr->o->type == UCI_TYPE_STRING && !strcmp(ptr->o->v.string, ptr->value))
return 0;
- ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value, &old->e.list);
- if (ptr->option == old->e.name)
- ptr->option = ptr->o->e.name;
- uci_free_option(old);
- ptr->last = &ptr->o->e;
+ if (ptr->o->type == UCI_TYPE_STRING && strlen(ptr->o->v.string) == strlen(ptr->value)) {
+ strcpy(ptr->o->v.string, ptr->value);
+ } else {
+ struct uci_option *old = ptr->o;
+ ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value, &old->e.list);
+ if (ptr->option == old->e.name)
+ ptr->option = ptr->o->e.name;
+ uci_free_option(old);
+ ptr->last = &ptr->o->e;
+ }
} else if (ptr->s && ptr->section) { /* update section */
char *s = uci_strdup(ctx, ptr->value);