summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Venekamp <jan@venekamp.net>2022-11-20 02:08:27 +0100
committerHauke Mehrtens <hauke@hauke-m.de>2023-03-04 19:39:32 +0100
commitae61e1cad4a14e1cf4d0e5f4638354dafa77e5d3 (patch)
treee54a6aac84c6e0eded2fb378f6d8fcf3685f6c13
parent16e8a3b1335a468c3aa6578db6ab2aa97b26fa45 (diff)
downloaduci-ae61e1cad4a14e1cf4d0e5f4638354dafa77e5d3.tar.gz
uci: optimize update section in uci_set
Optimize for the case when there is no need to update the section and the case there is no need to reallocate memory when updating a section in uci_set. Signed-off-by: Jan Venekamp <jan@venekamp.net>
-rw-r--r--list.c23
-rw-r--r--tests/shunit2/tests.d/090_cli_options8
2 files changed, 20 insertions, 11 deletions
diff --git a/list.c b/list.c
index 3e8a87c..1640213 100644
--- a/list.c
+++ b/list.c
@@ -727,14 +727,21 @@ int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
ptr->last = &ptr->o->e;
}
} else if (ptr->s && ptr->section) { /* update section */
- struct uci_section *old = ptr->s;
- ptr->s = uci_alloc_section(ptr->p, ptr->value, old->e.name, &old->e.list);
- uci_section_transfer_options(ptr->s, old);
- if (ptr->section == old->e.name)
- ptr->section = ptr->s->e.name;
- uci_free_section(old);
- ptr->s->package->n_section--;
- ptr->last = &ptr->s->e;
+ if (!strcmp(ptr->s->type, ptr->value))
+ return 0;
+
+ if (strlen(ptr->s->type) == strlen(ptr->value)) {
+ strcpy(ptr->s->type, ptr->value);
+ } else {
+ struct uci_section *old = ptr->s;
+ ptr->s = uci_alloc_section(ptr->p, ptr->value, old->e.name, &old->e.list);
+ uci_section_transfer_options(ptr->s, old);
+ if (ptr->section == old->e.name)
+ ptr->section = ptr->s->e.name;
+ uci_free_section(old);
+ ptr->s->package->n_section--;
+ ptr->last = &ptr->s->e;
+ }
} else {
UCI_THROW(ctx, UCI_ERR_INVAL);
}
diff --git a/tests/shunit2/tests.d/090_cli_options b/tests/shunit2/tests.d/090_cli_options
index 55920a2..aff8316 100644
--- a/tests/shunit2/tests.d/090_cli_options
+++ b/tests/shunit2/tests.d/090_cli_options
@@ -11,8 +11,9 @@ test_add_delta() {
# save new changes in "$new_savedir"
mkdir -p "$new_savedir"
touch "$new_savedir/delta"
- $UCI -P "$new_savedir" set delta.sec0=sectype
+ $UCI -P "$new_savedir" set delta.sec0=tmptype
$UCI -P "$new_savedir" add_list delta.sec0.li0=1
+ $UCI -P "$new_savedir" set delta.sec0=sectype
assertEquals "delta.sec0='sectype'
delta.sec0.li0+='0'" "$($UCI changes)"
@@ -22,8 +23,9 @@ delta.sec0.li0+='0'" "$($UCI changes)"
assertTrue "$?"
assertEquals "delta.sec0='sectype'
delta.sec0.li0+='0'
-delta.sec0='sectype'
-delta.sec0.li0+='1'" "$cmdoutput"
+delta.sec0='tmptype'
+delta.sec0.li0+='1'
+delta.sec0='sectype'" "$cmdoutput"
# check combined export. Order matters here.
cmdoutput="$($UCI -P "$new_savedir" export)"