]> xenbits.xensource.com Git - libvirt.git/commitdiff
x86ModelParseFeatures: Don't construct list using 'virStringListAdd'
authorPeter Krempa <pkrempa@redhat.com>
Fri, 5 Feb 2021 14:20:44 +0000 (15:20 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 11 Feb 2021 16:05:32 +0000 (17:05 +0100)
Pre-allocate the list to the upper bound and fill it gradually. Since
the data is kept long-term and the list won't be populated much shrink
it to the actual size after parsing.

While using 'virStringListAdd' here wouldn't be as expensive as this
function is used just once, the removal will allow to remove
'virStringListAdd' altogether to discourage the antipattern it promotes.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/cpu/cpu_x86.c

index fe423f38f92727fccf9512975d27bb95351e3bfd..92f945beb4e4ab3264b3d50475808f1d08dd0e32 100644 (file)
@@ -1604,11 +1604,14 @@ x86ModelParseFeatures(virCPUx86ModelPtr model,
 {
     g_autofree xmlNodePtr *nodes = NULL;
     size_t i;
+    size_t nremoved = 0;
     int n;
 
     if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) <= 0)
         return n;
 
+    model->removedFeatures = g_new0(char *, n + 1);
+
     for (i = 0; i < n; i++) {
         g_autofree char *ftname = NULL;
         g_autofree char *removed = NULL;
@@ -1640,8 +1643,7 @@ x86ModelParseFeatures(virCPUx86ModelPtr model,
             }
 
             if (rem == VIR_TRISTATE_BOOL_YES) {
-                if (virStringListAdd(&model->removedFeatures, ftname) < 0)
-                    return -1;
+                model->removedFeatures[nremoved++] = g_strdup(ftname);
                 continue;
             }
         }
@@ -1650,6 +1652,8 @@ x86ModelParseFeatures(virCPUx86ModelPtr model,
             return -1;
     }
 
+    model->removedFeatures = g_renew(char *, model->removedFeatures, nremoved + 1);
+
     return 0;
 }