From: Peter Krempa Date: Fri, 5 Feb 2021 14:09:12 +0000 (+0100) Subject: qemuInteropFetchConfigs: Don't use 'virStringListAdd' to construct list X-Git-Tag: v7.1.0-rc1~176 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f060d62c757c95a597a64610e26112f333ec80c4;p=thirdparty%2Flibvirt.git qemuInteropFetchConfigs: Don't use 'virStringListAdd' to construct list 'virHashGetItems' already returns the number of entries which will be considered for addition to the list so we can allocate it to the upper bound upfront rather than growing it in a loop. This avoids the quadratic complexity of 'virStringListAdd'. Signed-off-by: Peter Krempa Reviewed-by: Michal Privoznik --- diff --git a/src/qemu/qemu_interop_config.c b/src/qemu/qemu_interop_config.c index bcaddda446..9733df7194 100644 --- a/src/qemu/qemu_interop_config.c +++ b/src/qemu/qemu_interop_config.c @@ -94,7 +94,9 @@ qemuInteropFetchConfigs(const char *name, g_autofree char *sysLocation = virFileBuildPath(QEMU_SYSTEM_LOCATION, name, NULL); g_autofree char *etcLocation = virFileBuildPath(QEMU_ETC_LOCATION, name, NULL); g_autofree virHashKeyValuePairPtr pairs = NULL; + size_t npairs; virHashKeyValuePairPtr tmp = NULL; + size_t nconfigs = 0; *configs = NULL; @@ -132,11 +134,13 @@ qemuInteropFetchConfigs(const char *name, * where each filename (as key) has the highest priority full pathname * associated with it. */ - if (virHashSize(files) == 0) + if (!(pairs = virHashGetItems(files, &npairs, true))) + return -1; + + if (npairs == 0) return 0; - if (!(pairs = virHashGetItems(files, NULL, true))) - return -1; + *configs = g_new0(char *, npairs + 1); for (tmp = pairs; tmp->key; tmp++) { const char *path = tmp->value; @@ -158,8 +162,7 @@ qemuInteropFetchConfigs(const char *name, continue; } - if (virStringListAdd(configs, path) < 0) - return -1; + (*configs)[nconfigs++] = g_strdup(path); } return 0;