]> xenbits.xensource.com Git - libvirt.git/commitdiff
virJSONValueCopy: Don't use virJSONValue(Object|Array)Append
authorPeter Krempa <pkrempa@redhat.com>
Fri, 12 Feb 2021 09:44:49 +0000 (10:44 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Sat, 20 Feb 2021 12:26:37 +0000 (13:26 +0100)
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 <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/virjson.c

index 80ebcb587c7ee28e853b94d81bd986d37b44a7e0..d794eed17fd62ac261995193f39a26d22a30044b 100644 (file)
@@ -1530,27 +1530,23 @@ virJSONValueCopy(const virJSONValue *in)
     switch ((virJSONType) in->type) {
     case VIR_JSON_TYPE_OBJECT:
         out = virJSONValueNewObject();
+
+        out->data.object.pairs = g_new0(virJSONObjectPair, in->data.object.npairs);
+        out->data.object.npairs = in->data.object.npairs;
+
         for (i = 0; i < in->data.object.npairs; i++) {
-            virJSONValuePtr val = NULL;
-            if (!(val = 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 = g_strdup(in->data.object.pairs[i].key);
+            out->data.object.pairs[i].value = virJSONValueCopy(in->data.object.pairs[i].value);
         }
         break;
     case VIR_JSON_TYPE_ARRAY:
         out = virJSONValueNewArray();
+
+        out->data.array.values = g_new0(virJSONValuePtr, in->data.array.nvalues);
+        out->data.array.nvalues = in->data.array.nvalues;
+
         for (i = 0; i < in->data.array.nvalues; i++) {
-            virJSONValuePtr val = NULL;
-            if (!(val = virJSONValueCopy(in->data.array.values[i])))
-                goto error;
-            if (virJSONValueArrayAppend(out, val) < 0) {
-                virJSONValueFree(val);
-                goto error;
-            }
+            out->data.array.values[i] = virJSONValueCopy(in->data.array.values[i]);
         }
         break;
 
@@ -1570,10 +1566,6 @@ virJSONValueCopy(const virJSONValue *in)
     }
 
     return out;
-
- error:
-    virJSONValueFree(out);
-    return NULL;
 }