From fb51b85ea2354ef15178ead3cfa32f3b08369ac5 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 5 Feb 2021 15:20:44 +0100 Subject: [PATCH] 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 --- src/cpu/cpu_x86.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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; } -- 2.47.2