'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 <pkre...@redhat.com>
---
 src/qemu/qemu_interop_config.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

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;
-- 
2.29.2

Reply via email to