From: Peter Krempa Date: Fri, 5 Feb 2021 14:13:28 +0000 (+0100) Subject: virCPUDefCheckFeatures: Don't use 'virStringListAdd' to construct list X-Git-Tag: v7.1.0-rc1~175 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dad3827d6ac58a068c508e237c4a26249bcc59d8;p=thirdparty%2Flibvirt.git virCPUDefCheckFeatures: Don't use 'virStringListAdd' to construct list We already know the upper bound of items we might need so we can allocate the array upfront and avoid the quadratic complexity of 'virStringListAdd'. In this instance the returned data is kept only temporarily so a potential unused space due to filtered-out entries doesn't impose a long-term burden on memory. Signed-off-by: Peter Krempa Reviewed-by: Michal Privoznik --- diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c index eb4bfbbcfa..8d800bd80d 100644 --- a/src/conf/cpu_conf.c +++ b/src/conf/cpu_conf.c @@ -990,21 +990,21 @@ virCPUDefCheckFeatures(virCPUDefPtr cpu, void *opaque, char ***features) { - g_auto(GStrv) list = NULL; size_t n = 0; size_t i; *features = NULL; + if (cpu->nfeatures == 0) + return 0; + + *features = g_new0(char *, cpu->nfeatures + 1); + for (i = 0; i < cpu->nfeatures; i++) { - if (filter(cpu->features[i].name, cpu->features[i].policy, opaque)) { - if (virStringListAdd(&list, cpu->features[i].name) < 0) - return -1; - n++; - } + if (filter(cpu->features[i].name, cpu->features[i].policy, opaque)) + (*features)[n++] = g_strdup(cpu->features[i].name); } - *features = g_steal_pointer(&list); return n; }