From: Peter Krempa Date: Fri, 5 Feb 2021 14:30:02 +0000 (+0100) Subject: virResctrlMonitorGetStats: Don't use 'virStringListAdd' X-Git-Tag: v7.1.0-rc1~172 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b297714793f702cd7e42b46dc05e2cf66921ef5f;p=thirdparty%2Flibvirt.git virResctrlMonitorGetStats: Don't use 'virStringListAdd' 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 Reviewed-by: Michal Privoznik --- 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)