Blame 0001-Revert-packagekit-Avoid-600000-allocations-when-comp.patch

d856322
From b3a50ee2d6b93980d1808599ba003e9afc4feae5 Mon Sep 17 00:00:00 2001
d856322
From: Kalev Lember <klember@redhat.com>
d856322
Date: Mon, 14 Sep 2020 12:07:30 +0200
d856322
Subject: [PATCH] Revert "packagekit: Avoid 600000 allocations when comparing
d856322
 package IDs"
d856322
d856322
This broke packagekit updates.
d856322
d856322
https://gitlab.gnome.org/GNOME/gnome-software/-/issues/1061
d856322
https://bodhi.fedoraproject.org/updates/FEDORA-2020-7f57486c95#comment-1621958
d856322
d856322
This reverts commit 955570e4a5d737a9a4f85860fd7e483158e130c4.
d856322
---
d856322
 .../packagekit/gs-plugin-packagekit-refine.c  |  12 +-
d856322
 .../gs-plugin-packagekit-url-to-app.c         |   6 +-
d856322
 plugins/packagekit/packagekit-common.c        | 149 ++++++------------
d856322
 plugins/packagekit/packagekit-common.h        |   3 +-
d856322
 4 files changed, 54 insertions(+), 116 deletions(-)
d856322
d856322
diff --git a/plugins/packagekit/gs-plugin-packagekit-refine.c b/plugins/packagekit/gs-plugin-packagekit-refine.c
d856322
index 68f7eb64..813390b1 100644
d856322
--- a/plugins/packagekit/gs-plugin-packagekit-refine.c
d856322
+++ b/plugins/packagekit/gs-plugin-packagekit-refine.c
d856322
@@ -345,7 +345,6 @@ gs_plugin_packagekit_refine_details2 (GsPlugin *plugin,
d856322
 	g_autoptr(GPtrArray) array = NULL;
d856322
 	g_autoptr(GPtrArray) package_ids = NULL;
d856322
 	g_autoptr(PkResults) results = NULL;
d856322
-	g_autoptr(GHashTable) details_collection = NULL;
d856322
 
d856322
 	package_ids = g_ptr_array_new_with_free_func (g_free);
d856322
 	for (i = 0; i < gs_app_list_length (list); i++) {
d856322
@@ -375,19 +374,12 @@ gs_plugin_packagekit_refine_details2 (GsPlugin *plugin,
d856322
 		return FALSE;
d856322
 	}
d856322
 
d856322
-	/* get the results and copy them into a hash table for fast lookups:
d856322
-	 * there are typically 400 to 700 elements in @array, and 100 to 200
d856322
-	 * elements in @list, each with 1 or 2 source IDs to look up (but
d856322
-	 * sometimes 200) */
d856322
-	array = pk_results_get_details_array (results);
d856322
-	details_collection = gs_plugin_packagekit_details_array_to_hash (array);
d856322
-
d856322
 	/* set the update details for the update */
d856322
+	array = pk_results_get_details_array (results);
d856322
 	for (i = 0; i < gs_app_list_length (list); i++) {
d856322
 		app = gs_app_list_index (list, i);
d856322
-		gs_plugin_packagekit_refine_details_app (plugin, details_collection, app);
d856322
+		gs_plugin_packagekit_refine_details_app (plugin, array, app);
d856322
 	}
d856322
-
d856322
 	return TRUE;
d856322
 }
d856322
 
d856322
diff --git a/plugins/packagekit/gs-plugin-packagekit-url-to-app.c b/plugins/packagekit/gs-plugin-packagekit-url-to-app.c
d856322
index 04189204..7f566c72 100644
d856322
--- a/plugins/packagekit/gs-plugin-packagekit-url-to-app.c
d856322
+++ b/plugins/packagekit/gs-plugin-packagekit-url-to-app.c
d856322
@@ -106,15 +106,11 @@ gs_plugin_url_to_app (GsPlugin *plugin,
d856322
 	details = pk_results_get_details_array (results);
d856322
 
d856322
 	if (packages->len >= 1) {
d856322
-		g_autoptr(GHashTable) details_collection = NULL;
d856322
-
d856322
 		if (gs_app_get_local_file (app) != NULL)
d856322
 			return TRUE;
d856322
 
d856322
-		details_collection = gs_plugin_packagekit_details_array_to_hash (details);
d856322
-
d856322
 		gs_plugin_packagekit_resolve_packages_app (plugin, packages, app);
d856322
-		gs_plugin_packagekit_refine_details_app (plugin, details_collection, app);
d856322
+		gs_plugin_packagekit_refine_details_app (plugin, details, app);
d856322
 
d856322
 		gs_app_list_add (list, app);
d856322
 	} else {
d856322
diff --git a/plugins/packagekit/packagekit-common.c b/plugins/packagekit/packagekit-common.c
d856322
index 495960dd..9367f5bf 100644
d856322
--- a/plugins/packagekit/packagekit-common.c
d856322
+++ b/plugins/packagekit/packagekit-common.c
d856322
@@ -388,127 +388,78 @@ gs_plugin_packagekit_set_metadata_from_package (GsPlugin *plugin,
d856322
 			    pk_package_get_summary (package));
d856322
 }
d856322
 
d856322
-/* Hash functions which compare PkPackageIds on NAME, VERSION and ARCH, but not DATA.
d856322
- * This is because some backends do not append the origin.
d856322
+/*
d856322
+ * gs_pk_compare_ids:
d856322
  *
d856322
- * Borrowing some implementation details from pk-package-id.c, a package
d856322
- * ID is a semicolon-separated list of NAME;[VERSION];[ARCH];[DATA],
d856322
- * so a comparison which ignores DATA is just a strncmp() up to and
d856322
- * including the final semicolon.
d856322
- *
d856322
- * Doing it this way means zero allocations, which allows the hash and
d856322
- * equality functions to be fast. This is important when dealing with
d856322
- * large refine() package lists.
d856322
- *
d856322
- * The hash and equality functions assume that the IDs they are passed are
d856322
- * valid. */
d856322
-static guint
d856322
-package_id_hash (gconstpointer key)
d856322
-{
d856322
-	const gchar *package_id = key;
d856322
-	gchar *no_data;
d856322
-	gsize i, last_semicolon = 0;
d856322
-
d856322
-	/* find the last semicolon, which starts the DATA section */
d856322
-	for (i = 0; package_id[i] != '\0'; i++) {
d856322
-		if (package_id[i] == ';')
d856322
-			last_semicolon = i;
d856322
-	}
d856322
-
d856322
-	/* exit early if the DATA section was empty */
d856322
-	if (last_semicolon + 1 == i)
d856322
-		return g_str_hash (package_id);
d856322
-
d856322
-	/* extract up to (and including) the last semicolon into a local string */
d856322
-	no_data = g_alloca (last_semicolon + 2);
d856322
-	memcpy (no_data, package_id, last_semicolon + 1);
d856322
-	no_data[last_semicolon + 1] = '\0';
d856322
-
d856322
-	return g_str_hash (no_data);
d856322
-}
d856322
-
d856322
+ * Do not compare the repo. Some backends do not append the origin.
d856322
+ */
d856322
 static gboolean
d856322
-package_id_equal (gconstpointer a,
d856322
-                  gconstpointer b)
d856322
+gs_pk_compare_ids (const gchar *package_id1, const gchar *package_id2)
d856322
 {
d856322
-	const gchar *package_id_a = a;
d856322
-	const gchar *package_id_b = b;
d856322
-	gsize n_semicolons = 0;
d856322
-
d856322
-	/* compare up to and including the last semicolon */
d856322
-	for (gsize i = 0; package_id_a[i] != '\0' && package_id_b[i] != '\0'; i++) {
d856322
-		if (package_id_a[i] != package_id_b[i])
d856322
-			return FALSE;
d856322
-		if (package_id_a[i] == ';')
d856322
-			n_semicolons++;
d856322
-		if (n_semicolons == 4)
d856322
-			return TRUE;
d856322
-	}
d856322
+	gboolean ret;
d856322
+	g_auto(GStrv) split1 = NULL;
d856322
+	g_auto(GStrv) split2 = NULL;
d856322
 
d856322
-	return FALSE;
d856322
+	split1 = pk_package_id_split (package_id1);
d856322
+	if (split1 == NULL)
d856322
+		return FALSE;
d856322
+	split2 = pk_package_id_split (package_id2);
d856322
+	if (split2 == NULL)
d856322
+		return FALSE;
d856322
+	ret = (g_strcmp0 (split1[PK_PACKAGE_ID_NAME],
d856322
+			  split2[PK_PACKAGE_ID_NAME]) == 0 &&
d856322
+	       g_strcmp0 (split1[PK_PACKAGE_ID_VERSION],
d856322
+			  split2[PK_PACKAGE_ID_VERSION]) == 0 &&
d856322
+	       g_strcmp0 (split1[PK_PACKAGE_ID_ARCH],
d856322
+			  split2[PK_PACKAGE_ID_ARCH]) == 0);
d856322
+	return ret;
d856322
 }
d856322
 
d856322
-GHashTable *
d856322
-gs_plugin_packagekit_details_array_to_hash (GPtrArray *array)
d856322
-{
d856322
-	g_autoptr(GHashTable) details_collection = NULL;
d856322
-
d856322
-	details_collection = g_hash_table_new_full (package_id_hash, package_id_equal,
d856322
-						    NULL, NULL);
d856322
-
d856322
-	for (gsize i = 0; i < array->len; i++) {
d856322
-		PkDetails *details = g_ptr_array_index (array, i);
d856322
-		g_hash_table_insert (details_collection,
d856322
-				     pk_details_get_package_id (details),
d856322
-				     details);
d856322
-	}
d856322
-
d856322
-	return g_steal_pointer (&details_collection);
d856322
-}
d856322
 
d856322
 void
d856322
 gs_plugin_packagekit_refine_details_app (GsPlugin *plugin,
d856322
-					 GHashTable *details_collection,
d856322
+					 GPtrArray *array,
d856322
 					 GsApp *app)
d856322
 {
d856322
 	GPtrArray *source_ids;
d856322
 	PkDetails *details;
d856322
 	const gchar *package_id;
d856322
+	guint i;
d856322
 	guint j;
d856322
 	guint64 size = 0;
d856322
 
d856322
-	/* @source_ids can have as many as 200 elements (google-noto); typically
d856322
-	 * it has 1 or 2
d856322
-	 *
d856322
-	 * @details_collection is typically a large list of apps in the
d856322
-	 * repository, on the order of 400 or 700 apps */
d856322
 	source_ids = gs_app_get_source_ids (app);
d856322
 	for (j = 0; j < source_ids->len; j++) {
d856322
 		package_id = g_ptr_array_index (source_ids, j);
d856322
-		details = g_hash_table_lookup (details_collection, package_id);
d856322
-		if (details == NULL)
d856322
-			continue;
d856322
-
d856322
-		if (gs_app_get_license (app) == NULL) {
d856322
-			g_autofree gchar *license_spdx = NULL;
d856322
-			license_spdx = as_utils_license_to_spdx (pk_details_get_license (details));
d856322
-			if (license_spdx != NULL) {
d856322
-				gs_app_set_license (app,
d856322
-						    GS_APP_QUALITY_LOWEST,
d856322
-						    license_spdx);
d856322
+		for (i = 0; i < array->len; i++) {
d856322
+			/* right package? */
d856322
+			details = g_ptr_array_index (array, i);
d856322
+			if (!gs_pk_compare_ids (package_id,
d856322
+						pk_details_get_package_id (details))) {
d856322
+				continue;
d856322
 			}
d856322
+			if (gs_app_get_license (app) == NULL) {
d856322
+				g_autofree gchar *license_spdx = NULL;
d856322
+				license_spdx = as_utils_license_to_spdx (pk_details_get_license (details));
d856322
+				if (license_spdx != NULL) {
d856322
+					gs_app_set_license (app,
d856322
+							    GS_APP_QUALITY_LOWEST,
d856322
+							    license_spdx);
d856322
+				}
d856322
+			}
d856322
+			if (gs_app_get_url (app, AS_URL_KIND_HOMEPAGE) == NULL) {
d856322
+				gs_app_set_url (app,
d856322
+						AS_URL_KIND_HOMEPAGE,
d856322
+						pk_details_get_url (details));
d856322
+			}
d856322
+			if (gs_app_get_description (app) == NULL) {
d856322
+				gs_app_set_description (app,
d856322
+				                        GS_APP_QUALITY_LOWEST,
d856322
+				                        pk_details_get_description (details));
d856322
+			}
d856322
+			size += pk_details_get_size (details);
d856322
+			break;
d856322
 		}
d856322
-		if (gs_app_get_url (app, AS_URL_KIND_HOMEPAGE) == NULL) {
d856322
-			gs_app_set_url (app,
d856322
-					AS_URL_KIND_HOMEPAGE,
d856322
-					pk_details_get_url (details));
d856322
-		}
d856322
-		if (gs_app_get_description (app) == NULL) {
d856322
-			gs_app_set_description (app,
d856322
-			                        GS_APP_QUALITY_LOWEST,
d856322
-			                        pk_details_get_description (details));
d856322
-		}
d856322
-		size += pk_details_get_size (details);
d856322
 	}
d856322
 
d856322
 	/* the size is the size of all sources */
d856322
diff --git a/plugins/packagekit/packagekit-common.h b/plugins/packagekit/packagekit-common.h
d856322
index 9f523684..0742ea3a 100644
d856322
--- a/plugins/packagekit/packagekit-common.h
d856322
+++ b/plugins/packagekit/packagekit-common.h
d856322
@@ -30,9 +30,8 @@ void		gs_plugin_packagekit_resolve_packages_app	(GsPlugin *plugin,
d856322
 void		gs_plugin_packagekit_set_metadata_from_package	(GsPlugin *plugin,
d856322
 								 GsApp *app,
d856322
 								 PkPackage *package);
d856322
-GHashTable *	gs_plugin_packagekit_details_array_to_hash	(GPtrArray *array);
d856322
 void		gs_plugin_packagekit_refine_details_app		(GsPlugin *plugin,
d856322
-								 GHashTable *details_collection,
d856322
+								 GPtrArray *array,
d856322
 								 GsApp *app);
d856322
 void		gs_plugin_packagekit_set_packaging_format	(GsPlugin *plugin,
d856322
 								 GsApp *app);
d856322
-- 
d856322
2.26.2
d856322