[gnome-software: 10/72] snap: Combine snap queries into a single D-Bus call




commit c7a608a4d96042716ede500f64361b919c758913
Author: Philip Withnall <pwithnall endlessos org>
Date:   Fri Oct 22 13:29:20 2021 +0100

    snap: Combine snap queries into a single D-Bus call
    
    Rather than querying N times for N snaps, query once. This should speed
    things up, as D-Bus round trips are more expensive than local processing
    of the result list.
    
    Not tested.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #1472

 plugins/snap/gs-plugin-snap.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
---
diff --git a/plugins/snap/gs-plugin-snap.c b/plugins/snap/gs-plugin-snap.c
index aae23c84c..5f463a51e 100644
--- a/plugins/snap/gs-plugin-snap.c
+++ b/plugins/snap/gs-plugin-snap.c
@@ -1111,6 +1111,19 @@ gs_snap_get_app_directory_size (const gchar *snap_name,
        return gs_utils_get_file_size (filename, is_cache_size ? NULL : gs_snap_file_size_include_cb, NULL, 
cancellable);
 }
 
+static SnapdSnap *
+find_snap_in_array (GPtrArray   *snaps,
+                    const gchar *snap_name)
+{
+       for (guint i = 0; i < snaps->len; i++) {
+               SnapdSnap *snap = SNAPD_SNAP (snaps->pdata[i]);
+               if (g_strcmp0 (snapd_snap_get_name (snap), snap_name) == 0)
+                       return snap;
+       }
+
+       return NULL;
+}
+
 gboolean
 gs_plugin_refine (GsPlugin             *plugin,
                  GsAppList            *list,
@@ -1120,11 +1133,27 @@ gs_plugin_refine (GsPlugin             *plugin,
 {
        GsPluginSnap *self = GS_PLUGIN_SNAP (plugin);
        g_autoptr(SnapdClient) client = NULL;
+       g_autoptr(GPtrArray) snap_names = g_ptr_array_new_with_free_func (NULL);
+       g_autoptr(GPtrArray) local_snaps = NULL;
 
        client = get_client (self, error);
        if (client == NULL)
                return FALSE;
 
+       /* Get information from locally installed snaps */
+       for (guint i = 0; i < gs_app_list_length (list); i++) {
+               GsApp *app = gs_app_list_index (list, i);
+
+               if (!gs_app_has_management_plugin (app, plugin))
+                       continue;
+
+               g_ptr_array_add (snap_names, (gpointer) gs_app_get_metadata_item (app, "snap::name"));
+       }
+
+       g_ptr_array_add (snap_names, NULL);  /* NULL terminator */
+
+       local_snaps = snapd_client_get_snaps_sync (client, SNAPD_GET_SNAPS_FLAGS_NONE, (gchar **) 
snap_names->pdata, cancellable, NULL);
+
        for (guint i = 0; i < gs_app_list_length (list); i++) {
                GsApp *app = gs_app_list_index (list, i);
                const gchar *snap_name, *name, *website, *contact, *version;
@@ -1148,7 +1177,7 @@ gs_plugin_refine (GsPlugin             *plugin,
                channel = g_strdup (gs_app_get_branch (app));
 
                /* get information from locally installed snaps and information we already have */
-               local_snap = snapd_client_get_snap_sync (client, snap_name, cancellable, NULL);
+               local_snap = find_snap_in_array (local_snaps, snap_name);
                store_snap = store_snap_cache_lookup (self, snap_name, FALSE);
                if (store_snap != NULL)
                        store_channel = expand_channel_name (snapd_snap_get_channel (store_snap));


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