]> xenbits.xensource.com Git - libvirt.git/commitdiff
virResctrlMonitorGetStats: Don't use 'virStringListAdd'
authorPeter Krempa <pkrempa@redhat.com>
Fri, 5 Feb 2021 14:30:02 +0000 (15:30 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 11 Feb 2021 16:05:32 +0000 (17:05 +0100)
The iner loop copies the 'resources' array multiple times using
'virStringListAdd' which has O(n^2) complexity.

Pre-calculate the length so we can allocate the array upfront and just
copy the strings in the loop.

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

index 86b4b9d73bc6afcfb1735b0cdaaeb6da54fb6eed..ab35bccfc5815faa58d1ca47c1375041aaea244d 100644 (file)
@@ -2662,6 +2662,7 @@ virResctrlMonitorGetStats(virResctrlMonitorPtr monitor,
     char *filepath = NULL;
     struct dirent *ent = NULL;
     virResctrlMonitorStatsPtr stat = NULL;
+    size_t nresources = g_strv_length((char **) resources);
 
     if (!monitor) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -2705,6 +2706,7 @@ virResctrlMonitorGetStats(virResctrlMonitorPtr monitor,
             continue;
 
         stat = g_new0(virResctrlMonitorStats, 1);
+        stat->features = g_new0(char *, nresources + 1);
 
         /* The node ID number should be here, parsing it. */
         if (virStrToLong_uip(node_id, NULL, 0, &stat->id) < 0)
@@ -2724,8 +2726,7 @@ virResctrlMonitorGetStats(virResctrlMonitorPtr monitor,
             if (VIR_APPEND_ELEMENT(stat->vals, stat->nvals, val) < 0)
                 goto cleanup;
 
-            if (virStringListAdd(&stat->features, resources[i]) < 0)
-                goto cleanup;
+            stat->features[i] = g_strdup(resources[i]);
         }
 
         if (VIR_APPEND_ELEMENT(*stats, *nstats, stat) < 0)