From nobody Tue Feb 10 07:41:14 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1613152646; cv=none; d=zohomail.com; s=zohoarc; b=U+PcVjrjJD9IzPpe0ZUP4y/tH/NWnaXLPVa+TvDvLPAJ5O/mvcICY1fKwOjsQq3eqWz0aN1Zz/A9lNaMLEJgmapda0hHEpVkDKKdGoP+HBOFsmvorh74JlSa0MKvHtiEDGTD4yHjSjn+AfU1NvUcdMZz0IWcCHCVZN4j3y4NTdg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1613152646; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=uVmRkd4eTCXDzS9T/XwJHjJRnRLzbkIveR35MZphp1Q=; b=jBpU1TtbcYcc4a7noXfbp0d3a71k2WQdu9Doadvucrewox8U2H3UpBQoFoidLUvpPWAF1RogYO/JvwRjK4T8N6LtC4fz/petjeay1Ucpc6J/11F9i9YFAeLY5x3HD+o1xhAg3Kp8fFiLl9yI4LXCAdmmIXqF06nH5NpvOqfTB0s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 161315264651724.822987958562635; Fri, 12 Feb 2021 09:57:26 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-521-M-7DhPypNpaHk5xaZeS6QQ-1; Fri, 12 Feb 2021 12:57:20 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 12D1384A616; Fri, 12 Feb 2021 17:56:41 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 40A561A8F2; Fri, 12 Feb 2021 17:56:40 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B6BBC57DFC; Fri, 12 Feb 2021 17:56:39 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 11CHuZRP010868 for ; Fri, 12 Feb 2021 12:56:35 -0500 Received: by smtp.corp.redhat.com (Postfix) id DB0CA5C23D; Fri, 12 Feb 2021 17:56:35 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 33EB47DED9 for ; Fri, 12 Feb 2021 17:56:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613152645; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=uVmRkd4eTCXDzS9T/XwJHjJRnRLzbkIveR35MZphp1Q=; b=hsKxIpQqMKzDTGm1U4CU/Ul1Tc1JTaaeP6pjrHxIFWuFv7Qj3I3488aP2NTP5nHWEmB/VY T7Vs3fmcP+HmKy5bqAWxsZSPvFGpFWw95cdr1tafG50/dyEXsCclbFzK9mwmiM6yGW9aka 04lOVG3AdsVy8M44RtAZqFIJqO5YSMc= X-MC-Unique: M-7DhPypNpaHk5xaZeS6QQ-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 12/25] virJSONValueCopy: Don't use virJSONValue(Object|Array)Append Date: Fri, 12 Feb 2021 18:55:25 +0100 Message-Id: <467e2d99e8f4964bba726bf4ca03e3ef5052956d.1613152441.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" We know the exact number of keys or array members for the copied objects so we can pre-allocate the arrays rather than inserting into them in a loop incurring realloc copy penalty. Also virJSONValueCopy now can't fail since all of the functions allocating the different cases use just g_new/g_strdup internally so we can remove the NULL checks from the recursive calls. Signed-off-by: Peter Krempa --- src/util/virjson.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index 80ebcb587c..d794eed17f 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -1530,27 +1530,23 @@ virJSONValueCopy(const virJSONValue *in) switch ((virJSONType) in->type) { case VIR_JSON_TYPE_OBJECT: out =3D virJSONValueNewObject(); + + out->data.object.pairs =3D g_new0(virJSONObjectPair, in->data.obje= ct.npairs); + out->data.object.npairs =3D in->data.object.npairs; + for (i =3D 0; i < in->data.object.npairs; i++) { - virJSONValuePtr val =3D NULL; - if (!(val =3D virJSONValueCopy(in->data.object.pairs[i].value)= )) - goto error; - if (virJSONValueObjectAppend(out, in->data.object.pairs[i].key, - val) < 0) { - virJSONValueFree(val); - goto error; - } + out->data.object.pairs[i].key =3D g_strdup(in->data.object.pai= rs[i].key); + out->data.object.pairs[i].value =3D virJSONValueCopy(in->data.= object.pairs[i].value); } break; case VIR_JSON_TYPE_ARRAY: out =3D virJSONValueNewArray(); + + out->data.array.values =3D g_new0(virJSONValuePtr, in->data.array.= nvalues); + out->data.array.nvalues =3D in->data.array.nvalues; + for (i =3D 0; i < in->data.array.nvalues; i++) { - virJSONValuePtr val =3D NULL; - if (!(val =3D virJSONValueCopy(in->data.array.values[i]))) - goto error; - if (virJSONValueArrayAppend(out, val) < 0) { - virJSONValueFree(val); - goto error; - } + out->data.array.values[i] =3D virJSONValueCopy(in->data.array.= values[i]); } break; @@ -1570,10 +1566,6 @@ virJSONValueCopy(const virJSONValue *in) } return out; - - error: - virJSONValueFree(out); - return NULL; } --=20 2.29.2