]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemuInteropFetchConfigs: Don't use 'virStringListAdd' to construct list
authorPeter Krempa <pkrempa@redhat.com>
Fri, 5 Feb 2021 14:09:12 +0000 (15:09 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 11 Feb 2021 16:05:32 +0000 (17:05 +0100)
'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 <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/qemu/qemu_interop_config.c

index bcaddda446a8328f6d3cdb2fe36d838736f1a7af..9733df7194a77c62902080a8b532e17b18f5d6d4 100644 (file)
@@ -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;