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 <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2021-02-05 15:13:28 +01:00
parent f060d62c75
commit dad3827d6a

View File

@ -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;
}