[PATCH 12/40] x86ModelParseFeatures: Don't construct list using 'virStringListAdd'

Peter Krempa pkrempa at redhat.com
Sat Feb 6 08:32:34 UTC 2021


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 at redhat.com>
---
 src/cpu/cpu_x86.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
index 2422e258ec..a64a971540 100644
--- a/src/cpu/cpu_x86.c
+++ b/src/cpu/cpu_x86.c
@@ -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;
 }

-- 
2.29.2




More information about the libvir-list mailing list