[PATCH 09/40] virHookCall: Don't use 'virStringListAdd' to construct list in loop

Peter Krempa pkrempa at redhat.com
Sat Feb 6 08:32:31 UTC 2021


'virStringListAdd' calculates the string list length on every invocation
so constructing a string list using it results in O(n^2) complexity.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/virhook.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/util/virhook.c b/src/util/virhook.c
index 05d46f259e..e4e1945225 100644
--- a/src/util/virhook.c
+++ b/src/util/virhook.c
@@ -34,6 +34,7 @@
 #include "configmake.h"
 #include "vircommand.h"
 #include "virstring.h"
+#include "virglibutil.h"

 #define VIR_FROM_THIS VIR_FROM_HOOK

@@ -343,11 +344,11 @@ virHookCall(int driver,
     struct dirent *entry;
     g_autofree char *path = NULL;
     g_autofree char *dir_path = NULL;
-    g_auto(GStrv) entries = NULL;
+    g_autoptr(virGSListString) entries = NULL;
     const char *drvstr;
     const char *opstr;
     const char *subopstr;
-    size_t i, nentries;
+    GSList *next;

     if (output)
         *output = NULL;
@@ -433,7 +434,7 @@ virHookCall(int driver,
         if (!virFileIsExecutable(entry_path))
             continue;

-        virStringListAdd(&entries, entry_path);
+        entries = g_slist_prepend(entries, g_steal_pointer(&entry_path));
     }

     if (ret < 0)
@@ -442,18 +443,18 @@ virHookCall(int driver,
     if (!entries)
         return script_ret;

-    nentries = virStringListLength((const char **)entries);
-    qsort(entries, nentries, sizeof(*entries), virStringSortCompare);
+    entries = g_slist_sort(entries, (GCompareFunc) strcmp);

-    for (i = 0; i < nentries; i++) {
+    for (next = entries; next; next = next->next) {
         int entry_ret;
         const char *entry_input;
         g_autofree char *entry_output = NULL;
+        const char *filename = next->data;

         /* Get input from previous output */
         entry_input = (!script_ret && output &&
                        !virStringIsEmpty(*output)) ? *output : input;
-        entry_ret = virRunScript(entries[i], id, opstr,
+        entry_ret = virRunScript(filename, id, opstr,
                                  subopstr, extra, entry_input,
                                  (output) ? &entry_output : NULL);
         if (entry_ret < script_ret)
-- 
2.29.2




More information about the libvir-list mailing list