dupe: Optimize memory handling in dupe_files_add_queue_cb()
authorFrej Drejhammar <frej.drejhammar@gmail.com>
Sat, 13 Feb 2021 15:55:05 +0000 (16:55 +0100)
committerFrej Drejhammar <frej.drejhammar@gmail.com>
Sat, 27 Mar 2021 11:49:19 +0000 (12:49 +0100)
Do not traverse the lists returned by filelist_filter() while
allocating new list links with g_list_prepend(). Instead prepend the
lists to dw->add_files_queue using g_list_concat(). Although
g_list_concat() has to traverse the lists containing the new items to
find their ends, it avoids redundant memory allocation and
deallocation.

src/dupe.c

index 75e27ab..e2c5057 100644 (file)
@@ -2375,25 +2375,11 @@ static gboolean dupe_files_add_queue_cb(gpointer data)
 
                        if (filelist_read(fd, &f, &d))
                                {
-                               GList *work;
-
                                f = filelist_filter(f, FALSE);
                                d = filelist_filter(d, TRUE);
 
-                               work = f;
-                               while (work)
-                                       {
-                                       dw->add_files_queue = g_list_prepend(dw->add_files_queue, work->data);
-                                       work = work->next;
-                                       }
-                               g_list_free(f);
-                               work = d;
-                               while (work)
-                                       {
-                                       dw->add_files_queue = g_list_prepend(dw->add_files_queue, work->data);
-                                       work = work->next;
-                                       }
-                               g_list_free(d);
+                               dw->add_files_queue = g_list_concat(f, dw->add_files_queue);
+                               dw->add_files_queue = g_list_concat(d, dw->add_files_queue);
                                }
                        }
                else