[nautilus/wip/corey/fix-sort: 3/7] general: Sort NautilusFile's directly rather than through a dummy item




commit 3c6436bb198413d54e09c03f9dd9be86dd13e59d
Author: Corey Berla <corey berla me>
Date:   Tue Sep 27 08:26:21 2022 -0700

    general: Sort NautilusFile's directly rather than through a dummy item
    
    This wastes a lot of cycles while loading large folders.
    Re-work the sorter, to allow either NautilusViewItems
    or NautilusFiles directly.  This consistently saved
    about 3 seconds in a 100,000 item folder during my testing.

 src/nautilus-grid-view.c | 16 ++++++++++++++--
 src/nautilus-list-base.c |  8 +-------
 src/nautilus-list-view.c | 45 +++++++++++++++++++++++++++++++++++++++------
 3 files changed, 54 insertions(+), 15 deletions(-)
---
diff --git a/src/nautilus-grid-view.c b/src/nautilus-grid-view.c
index cbeea42de..83c14a9ea 100644
--- a/src/nautilus-grid-view.c
+++ b/src/nautilus-grid-view.c
@@ -40,8 +40,20 @@ nautilus_grid_view_sort (gconstpointer a,
     NautilusFile *file_a;
     NautilusFile *file_b;
 
-    file_a = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) a));
-    file_b = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) b));
+    if (NAUTILUS_IS_VIEW_ITEM ((gpointer) a) && NAUTILUS_IS_VIEW_ITEM ((gpointer) b))
+    {
+        file_a = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) a));
+        file_b = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) b));
+    }
+    else if (NAUTILUS_IS_FILE ((gpointer) a) && NAUTILUS_IS_FILE ((gpointer) b))
+    {
+        file_a = NAUTILUS_FILE ((gpointer) a);
+        file_b = NAUTILUS_FILE ((gpointer) b);
+    }
+    else
+    {
+        g_assert_not_reached();
+    }
 
     return nautilus_file_compare_for_sort (file_a, file_b,
                                            self->sort_type,
diff --git a/src/nautilus-list-base.c b/src/nautilus-list-base.c
index c1f85e6d1..2e2552a10 100644
--- a/src/nautilus-list-base.c
+++ b/src/nautilus-list-base.c
@@ -1307,8 +1307,6 @@ real_compare_files (NautilusFilesView *files_view,
     NautilusListBase *self = NAUTILUS_LIST_BASE (files_view);
     NautilusListBasePrivate *priv = nautilus_list_base_get_instance_private (self);
     GtkSorter *sorter;
-    g_autoptr (NautilusViewItem) item1 = NULL;
-    g_autoptr (NautilusViewItem) item2 = NULL;
 
     sorter = nautilus_view_model_get_sorter (priv->model);
     if (sorter == NULL)
@@ -1316,11 +1314,7 @@ real_compare_files (NautilusFilesView *files_view,
         return 0;
     }
 
-    /* Generate fake model items for sorter use only. */
-    item1 = nautilus_view_item_new (file1, NAUTILUS_GRID_ICON_SIZE_SMALL);
-    item2 = nautilus_view_item_new (file2, NAUTILUS_GRID_ICON_SIZE_SMALL);
-
-    return gtk_sorter_compare (sorter, item1, item2);
+    return gtk_sorter_compare (sorter, file1, file2);
 }
 
 static void
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index 2715e7e0e..8c1f46cc1 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -251,9 +251,24 @@ nautilus_list_view_sort (gconstpointer a,
                          gconstpointer b,
                          gpointer      user_data)
 {
+    NautilusFile *file_a;
+    NautilusFile *file_b;
     GQuark attribute_q = GPOINTER_TO_UINT (user_data);
-    NautilusFile *file_a = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) a));
-    NautilusFile *file_b = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) b));
+
+    if (NAUTILUS_IS_VIEW_ITEM ((gpointer) a) && NAUTILUS_IS_VIEW_ITEM ((gpointer) b))
+    {
+        file_a = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) a));
+        file_b = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) b));
+    }
+    else if (NAUTILUS_IS_FILE ((gpointer) a) && NAUTILUS_IS_FILE ((gpointer) b))
+    {
+        file_a = NAUTILUS_FILE ((gpointer) a);
+        file_b = NAUTILUS_FILE ((gpointer) b);
+    }
+    else
+    {
+        g_assert_not_reached();
+    }
 
     /* The reversed argument is FALSE because the columnview sorter handles that
      * itself and if we don't want to reverse the reverse. The directories_first
@@ -275,10 +290,28 @@ sort_directories_func (gconstpointer a,
 
     if (*directories_first)
     {
-        NautilusFile *file_a = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) a));
-        NautilusFile *file_b = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) b));
-        gboolean a_is_directory = nautilus_file_is_directory (file_a);
-        gboolean b_is_directory = nautilus_file_is_directory (file_b);
+        NautilusFile *file_a;
+        NautilusFile *file_b;
+        gboolean a_is_directory;
+        gboolean b_is_directory;
+
+        if (NAUTILUS_IS_VIEW_ITEM ((gpointer) a) && NAUTILUS_IS_VIEW_ITEM ((gpointer) b))
+        {
+            file_a = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) a));
+            file_b = nautilus_view_item_get_file (NAUTILUS_VIEW_ITEM ((gpointer) b));
+        }
+        else if (NAUTILUS_IS_FILE ((gpointer) a) && NAUTILUS_IS_FILE ((gpointer) b))
+        {
+            file_a = NAUTILUS_FILE ((gpointer) a);
+            file_b = NAUTILUS_FILE ((gpointer) b);
+        }
+        else
+        {
+            g_assert_not_reached();
+        }
+
+        a_is_directory = nautilus_file_is_directory (file_a);
+        b_is_directory = nautilus_file_is_directory (file_b);
 
         if (a_is_directory && !b_is_directory)
         {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]