From patchwork Sun Nov 20 01:08:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Venekamp X-Patchwork-Id: 1706680 X-Patchwork-Delegate: hauke@hauke-m.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=07t5Ywam; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NFCDg28DKz23nM for ; Sun, 20 Nov 2022 12:12:35 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=KpyWnFsWMzSqbkzFMw9GiSreDNGVRE8yKTQ1lHvdI4E=; b=07t5Ywam2MajEA 8f6E81g7Q0Hid7N0lSOCKcoPZ0LUey4z6y29FkdlfqLRE3roCKH0Tm0WrwQHBNflaXzgZxs4Eby6B o6w/TwigLKRtxskQ1Lh948PPkMkKgY77fWrLQSc2Rv3utNamHnAGhz/u2sM3yVvVVuWx2Lm8ivhRy KU8/7Iz64tALcceL3BryVbjkaNkWzMLGnpTzV57lUUTP9nw+zSXG3Ue20fZLWU7P0TF05Mrot29fl RUY/c0xKhUievNOJQRAvPDSEG94csiHqsOzf9bp8QiOOzryrEQlCBpScTAYyPo6WEhrzqNlj5YkZt GsFB18q6/0Fo0Nn0ndiQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1owYqN-000pK1-7c; Sun, 20 Nov 2022 01:09:59 +0000 Received: from virt1.bvwebdesign.nl ([149.210.228.112]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1owYp4-000p5Z-65 for openwrt-devel@lists.openwrt.org; Sun, 20 Nov 2022 01:08:39 +0000 Received: from localhost.localdomain (84-31-67-158.cable.dynamic.v4.ziggo.nl [84.31.67.158]) by virt1.bvwebdesign.nl (Postfix) with ESMTPSA id 6ED1EA5ED6B for ; Sun, 20 Nov 2022 02:08:29 +0100 (CET) From: Jan Venekamp To: openwrt-devel@lists.openwrt.org Subject: [PATCH v2 8/9] uci: optimize update section in uci_set Date: Sun, 20 Nov 2022 02:08:27 +0100 Message-Id: <20221120010828.23765-9-jan@venekamp.net> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20221120010828.23765-1-jan@venekamp.net> References: <20221120010828.23765-1-jan@venekamp.net> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221119_170838_429870_45F1D370 X-CRM114-Status: GOOD ( 11.15 ) X-Spam-Score: -0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: 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 --- list.c | 23 +++++++++++++++-------- tests/shunit2/tests.d/090_cli_options | 8 +++++--- 2 files changed, 20 insertions(+), 11 deletions(-) Content analysis details: (-0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org 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 --- list.c | 23 +++++++++++++++-------- tests/shunit2/tests.d/090_cli_options | 8 +++++--- 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)"