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 <pkrempa@xxxxxxxxxx> --- src/conf/cpu_conf.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c index f98b0a0eb3..1de4c1e417 100644 --- a/src/conf/cpu_conf.c +++ b/src/conf/cpu_conf.c @@ -985,21 +985,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; } -- 2.29.2