From: Peter Krempa Date: Fri, 5 Feb 2021 14:20:44 +0000 (+0100) Subject: x86ModelParseFeatures: Don't construct list using 'virStringListAdd' X-Git-Tag: v7.1.0-rc1~174 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fb51b85ea2354ef15178ead3cfa32f3b08369ac5;p=thirdparty%2Flibvirt.git x86ModelParseFeatures: Don't construct list using 'virStringListAdd' Pre-allocate the list to the upper bound and fill it gradually. Since the data is kept long-term and the list won't be populated much shrink it to the actual size after parsing. While using 'virStringListAdd' here wouldn't be as expensive as this function is used just once, the removal will allow to remove 'virStringListAdd' altogether to discourage the antipattern it promotes. Signed-off-by: Peter Krempa Reviewed-by: Michal Privoznik --- diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c index fe423f38f9..92f945beb4 100644 --- a/src/cpu/cpu_x86.c +++ b/src/cpu/cpu_x86.c @@ -1604,11 +1604,14 @@ x86ModelParseFeatures(virCPUx86ModelPtr model, { g_autofree xmlNodePtr *nodes = NULL; size_t i; + size_t nremoved = 0; int n; if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) <= 0) return n; + model->removedFeatures = g_new0(char *, n + 1); + for (i = 0; i < n; i++) { g_autofree char *ftname = NULL; g_autofree char *removed = NULL; @@ -1640,8 +1643,7 @@ x86ModelParseFeatures(virCPUx86ModelPtr model, } if (rem == VIR_TRISTATE_BOOL_YES) { - if (virStringListAdd(&model->removedFeatures, ftname) < 0) - return -1; + model->removedFeatures[nremoved++] = g_strdup(ftname); continue; } } @@ -1650,6 +1652,8 @@ x86ModelParseFeatures(virCPUx86ModelPtr model, return -1; } + model->removedFeatures = g_renew(char *, model->removedFeatures, nremoved + 1); + return 0; }