[nautilus/wip/antoniof/metadata-lists-optimization: 1/3] file: Get metadata list as array




commit 0fdebf3842f2746c45db03ed2cd52905c95c5f4d
Author: António Fernandes <antoniof gnome org>
Date:   Mon Jul 26 12:55:05 2021 +0100

    file: Get metadata list as array
    
    Metadata lists are attributes of type G_FILE_ATTRIBUTE_TYPE_STRINGV.
    In other words, they are gchar** (arrays of strings).
    
    Our NautilusFile API exports them as GList instead, but most calls
    happen in NautilusListView, which consumes arrays of strings. So,
    the conversion from array to list and back to array is wasteful.
    
    Let's start with the getter, making it return a string array isntead.
    For emblem keywords, we actually need a GList (for sorting), so make
    the conversion there after the getter call.
    
    The setter part is due in the next commit.

 src/nautilus-file.c      | 30 +++++++++++++---------------
 src/nautilus-file.h      |  2 +-
 src/nautilus-list-view.c | 51 +++++++++---------------------------------------
 3 files changed, 24 insertions(+), 59 deletions(-)
---
diff --git a/src/nautilus-file.c b/src/nautilus-file.c
index c14356d8f..b6394f569 100644
--- a/src/nautilus-file.c
+++ b/src/nautilus-file.c
@@ -4138,14 +4138,12 @@ nautilus_file_get_metadata (NautilusFile *file,
     return g_strdup (default_metadata);
 }
 
-GList *
+gchar **
 nautilus_file_get_metadata_list (NautilusFile *file,
                                  const char   *key)
 {
-    GList *res;
     guint id;
     char **value;
-    int i;
 
     g_return_val_if_fail (key != NULL, NULL);
     g_return_val_if_fail (key[0] != '\0', NULL);
@@ -4163,17 +4161,7 @@ nautilus_file_get_metadata_list (NautilusFile *file,
 
     value = g_hash_table_lookup (file->details->metadata, GUINT_TO_POINTER (id));
 
-    if (value)
-    {
-        res = NULL;
-        for (i = 0; value[i] != NULL; i++)
-        {
-            res = g_list_prepend (res, g_strdup (value[i]));
-        }
-        return g_list_reverse (res);
-    }
-
-    return NULL;
+    return g_strdupv (value);
 }
 
 void
@@ -4935,7 +4923,9 @@ clean_up_metadata_keywords (NautilusFile  *file,
 static GList *
 nautilus_file_get_keywords (NautilusFile *file)
 {
-    GList *keywords, *metadata_keywords;
+    GList *keywords;
+    gchar **metadata_strv;
+    GList *metadata_keywords = NULL;
 
     if (file == NULL)
     {
@@ -4947,7 +4937,15 @@ nautilus_file_get_keywords (NautilusFile *file)
     keywords = g_list_copy_deep (file->details->extension_emblems, (GCopyFunc) g_strdup, NULL);
     keywords = g_list_concat (keywords, g_list_copy_deep (file->details->pending_extension_emblems, 
(GCopyFunc) g_strdup, NULL));
 
-    metadata_keywords = nautilus_file_get_metadata_list (file, NAUTILUS_METADATA_KEY_EMBLEMS);
+    metadata_strv = nautilus_file_get_metadata_list (file, NAUTILUS_METADATA_KEY_EMBLEMS);
+    /* Convert array to list */
+    for (gint i = 0; metadata_strv[i] != NULL; i++)
+    {
+        metadata_keywords = g_list_prepend (metadata_keywords, metadata_strv[i]);
+    }
+    /* Free only the container array. The strings are owned by the list now. */
+    g_free (metadata_strv);
+
     clean_up_metadata_keywords (file, &metadata_keywords);
     keywords = g_list_concat (keywords, metadata_keywords);
 
diff --git a/src/nautilus-file.h b/src/nautilus-file.h
index 6946be5f8..939033683 100644
--- a/src/nautilus-file.h
+++ b/src/nautilus-file.h
@@ -372,7 +372,7 @@ gboolean                nautilus_file_is_not_yet_confirmed              (Nautilu
 char *                  nautilus_file_get_metadata                      (NautilusFile                   
*file,
                                                                         const char                     *key,
                                                                         const char                     
*default_metadata);
-GList *                 nautilus_file_get_metadata_list                 (NautilusFile                   
*file,
+gchar **                nautilus_file_get_metadata_list                 (NautilusFile                   
*file,
                                                                         const char                     *key);
 void                    nautilus_file_set_metadata                      (NautilusFile                   
*file,
                                                                         const char                     *key,
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index 0ee93f640..e342ee176 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -2476,34 +2476,13 @@ get_default_visible_columns (NautilusListView *list_view)
                                 NAUTILUS_PREFERENCES_LIST_VIEW_DEFAULT_VISIBLE_COLUMNS);
 }
 
-static GList *
-get_default_visible_columns_as_list (NautilusListView *list_view)
-{
-    GList *res = NULL;
-    gint i = 0;
-    gchar **array;
-
-    array = get_default_visible_columns (list_view);
-
-    while (array[i] != NULL)
-    {
-        res = g_list_prepend (res, array[i]);
-        i++;
-    }
-
-    g_free (array);
-
-    return res;
-}
-
 static char **
 get_visible_columns (NautilusListView *list_view)
 {
     NautilusFile *file;
-    g_autoptr (GList) visible_columns = NULL;
+    g_autofree gchar **visible_columns = NULL;
     g_autoptr (GFile) location = NULL;
     GPtrArray *res;
-    GList *l;
     g_autofree gchar *uri = NULL;
     gboolean can_star_current_directory;
     gboolean is_starred;
@@ -2520,16 +2499,16 @@ get_visible_columns (NautilusListView *list_view)
                                                        NAUTILUS_METADATA_KEY_LIST_VIEW_VISIBLE_COLUMNS);
     if (visible_columns == NULL)
     {
-        visible_columns = get_default_visible_columns_as_list (list_view);
+        visible_columns = get_default_visible_columns (list_view);
     }
 
     res = g_ptr_array_new ();
-    for (l = visible_columns; l != NULL; l = l->next)
+    for (gint i = 0; visible_columns[i] != NULL; i++)
     {
-        if (g_strcmp0 (l->data, "starred") != 0 ||
-            (g_strcmp0 (l->data, "starred") == 0 && (can_star_current_directory || is_starred)))
+        if (g_strcmp0 (visible_columns[i], "starred") != 0 ||
+            (g_strcmp0 (visible_columns[i], "starred") == 0 && (can_star_current_directory || is_starred)))
         {
-            g_ptr_array_add (res, l->data);
+            g_ptr_array_add (res, visible_columns[i]);
         }
     }
 
@@ -2570,7 +2549,7 @@ static char **
 get_column_order (NautilusListView *list_view)
 {
     NautilusFile *file;
-    GList *column_order;
+    gchar **column_order;
 
     file = nautilus_files_view_get_directory_as_file (NAUTILUS_FILES_VIEW (list_view));
 
@@ -2578,21 +2557,9 @@ get_column_order (NautilusListView *list_view)
                        (file,
                        NAUTILUS_METADATA_KEY_LIST_VIEW_COLUMN_ORDER);
 
-    if (column_order)
+    if (column_order != NULL)
     {
-        GPtrArray *res;
-        GList *l;
-
-        res = g_ptr_array_new ();
-        for (l = column_order; l != NULL; l = l->next)
-        {
-            g_ptr_array_add (res, l->data);
-        }
-        g_ptr_array_add (res, NULL);
-
-        g_list_free (column_order);
-
-        return (char **) g_ptr_array_free (res, FALSE);
+        return column_order;
     }
 
     return get_default_column_order (list_view);


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