[PATCH 14/40] virResctrlMonitorGetStats: Don't use 'virStringListAdd'

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


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 at redhat.com>
---
 src/util/virresctrl.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
index 86b4b9d73b..ab35bccfc5 100644
--- a/src/util/virresctrl.c
+++ b/src/util/virresctrl.c
@@ -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)
-- 
2.29.2




More information about the libvir-list mailing list