[gnome-software] flatpak: Improve performance of refining an app's state



commit 95d3e065112a12d572e007a44691a6133b91c6e5
Author: Joaquim Rocha <jrocha endlessm com>
Date:   Thu Sep 21 11:45:41 2017 +0200

    flatpak: Improve performance of refining an app's state
    
    Instead of listing the installed refs and trying to match the app there,
    it instead looks for the app's installed ref directly (which lets
    Flatpak manage how to look for it and is much faster).
    
    From some measurements this makes refining apps about 2x faster.

 plugins/flatpak/gs-flatpak.c |   89 +++++++++++++++++++-----------------------
 1 files changed, 40 insertions(+), 49 deletions(-)
---
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index 3386fdd..af6834f 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -66,6 +66,21 @@ gs_flatpak_build_id (FlatpakRef *xref)
        return g_strdup (flatpak_ref_get_name (xref));
 }
 
+static FlatpakInstalledRef *
+get_installed_ref_for_app (FlatpakInstallation *installation,
+                          GsApp *app,
+                          GCancellable *cancellable,
+                          GError **error)
+{
+       return flatpak_installation_get_installed_ref (installation,
+                                                      gs_flatpak_app_get_ref_kind (app),
+                                                      gs_flatpak_app_get_ref_name (app),
+                                                      gs_flatpak_app_get_ref_arch (app),
+                                                      gs_flatpak_app_get_ref_branch (app),
+                                                      cancellable,
+                                                      error);
+}
+
 static void
 gs_plugin_refine_item_scope (GsFlatpak *self, GsApp *app)
 {
@@ -1523,29 +1538,6 @@ gs_plugin_refine_item_origin (GsFlatpak *self,
        return TRUE;
 }
 
-static gboolean
-gs_flatpak_app_matches_xref (GsFlatpak *self, GsApp *app, FlatpakRef *xref)
-{
-       g_autoptr(GsApp) app_tmp = gs_flatpak_create_app (self, xref);
-
-       /* check ID */
-       if (g_strcmp0 (gs_app_get_unique_id (app),
-           gs_app_get_unique_id (app_tmp)) == 0)
-               return TRUE;
-
-       /* do all the metadata items match? */
-       if (g_strcmp0 (gs_flatpak_app_get_ref_name (app),
-                      flatpak_ref_get_name (xref)) == 0 &&
-           g_strcmp0 (gs_flatpak_app_get_ref_arch (app),
-                      flatpak_ref_get_arch (xref)) == 0 &&
-           g_strcmp0 (gs_flatpak_app_get_ref_branch (app),
-                      flatpak_ref_get_branch (xref)) == 0)
-               return TRUE;
-
-       /* sad panda */
-       return FALSE;
-}
-
 static FlatpakRef *
 gs_flatpak_create_fake_ref (GsApp *app, GError **error)
 {
@@ -1570,9 +1562,10 @@ gs_plugin_refine_item_state (GsFlatpak *self,
                             GCancellable *cancellable,
                             GError **error)
 {
-       guint i;
        g_autoptr(GPtrArray) xrefs = NULL;
        g_autoptr(AsProfileTask) ptask = NULL;
+       g_autoptr(FlatpakInstalledRef) ref = NULL;
+       g_autoptr(GError) ref_error = NULL;
 
        /* already found */
        if (gs_app_get_state (app) != AS_APP_STATE_UNKNOWN)
@@ -1587,25 +1580,19 @@ gs_plugin_refine_item_state (GsFlatpak *self,
                                  "%s::refine-action",
                                  gs_flatpak_get_id (self));
        g_assert (ptask != NULL);
-       xrefs = flatpak_installation_list_installed_refs (self->installation,
-                                                         cancellable, error);
-       if (xrefs == NULL) {
-               gs_flatpak_error_convert (error);
-               return FALSE;
-       }
-       for (i = 0; i < xrefs->len; i++) {
-               FlatpakInstalledRef *xref = g_ptr_array_index (xrefs, i);
-
-               /* check xref is app */
-               if (!gs_flatpak_app_matches_xref (self, app, FLATPAK_REF(xref)))
-                       continue;
 
-               /* mark as installed */
+       ref = get_installed_ref_for_app (self->installation, app, cancellable,
+                                        &ref_error);
+       if (ref != NULL) {
                g_debug ("marking %s as installed with flatpak",
                         gs_app_get_id (app));
-               gs_flatpak_set_metadata_installed (self, app, xref);
+               gs_flatpak_set_metadata_installed (self, app, ref);
                if (gs_app_get_state (app) == AS_APP_STATE_UNKNOWN)
                        gs_app_set_state (app, AS_APP_STATE_INSTALLED);
+       } else if (!g_error_matches (ref_error, FLATPAK_ERROR, FLATPAK_ERROR_NOT_INSTALLED)) {
+               g_propagate_error (error, g_steal_pointer (&ref_error));
+               gs_flatpak_error_convert (error);
+               return FALSE;
        }
 
        /* ensure origin set */
@@ -1635,20 +1622,24 @@ gs_plugin_refine_item_state (GsFlatpak *self,
                                return FALSE;
                        }
                } else {
-                       g_autoptr(GPtrArray) xrefs2 = NULL;
-                       xrefs2 = flatpak_installation_list_installed_refs (installation,
-                                                                          cancellable,
-                                                                          error);
-                       if (xrefs2 == NULL) {
+                       g_autoptr(FlatpakInstalledRef) runtime_ref = NULL;
+                       g_autoptr(GError) runtime_ref_error = NULL;
+                       runtime_ref = get_installed_ref_for_app (self->installation,
+                                                                app, cancellable,
+                                                                &runtime_ref_error);
+
+                       if (runtime_ref != NULL) {
+                               g_debug ("marking runtime %s as installed in the "
+                                        "counterpart installation", gs_app_get_id (app));
+                               gs_flatpak_set_metadata_installed (self, app, runtime_ref);
+                               if (gs_app_get_state (app) == AS_APP_STATE_UNKNOWN)
+                                       gs_app_set_state (app, AS_APP_STATE_INSTALLED);
+                       } else if (!g_error_matches (runtime_ref_error, FLATPAK_ERROR,
+                                                    FLATPAK_ERROR_NOT_INSTALLED)) {
+                               g_propagate_error (error, g_steal_pointer (&runtime_ref_error));
                                gs_flatpak_error_convert (error);
                                return FALSE;
                        }
-                       for (i = 0; i < xrefs2->len; i++) {
-                               FlatpakInstalledRef *xref = g_ptr_array_index (xrefs2, i);
-                               if (!gs_flatpak_app_matches_xref (self, app, FLATPAK_REF(xref)))
-                                       continue;
-                               gs_app_set_state (app, AS_APP_STATE_INSTALLED);
-                       }
                }
        }
 


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