Avoid string duplication in FlatpakRef

Allocate a full ref string once per object rather than once per call.
This is similar to what we do in flatpak_dir_get_name_cached().
This commit is contained in:
Phaedrus Leeds 2020-09-17 13:49:20 -07:00 committed by Alexander Larsson
parent 929fe68f55
commit 55f6abe578
6 changed files with 39 additions and 12 deletions

View File

@ -238,7 +238,7 @@ flatpak_builtin_uninstall (int argc, char **argv, GCancellable *cancellable, GEr
for (i = 0; i < pinned->len; i++)
{
FlatpakInstalledRef *rref = g_ptr_array_index (pinned, i);
g_autofree char *ref = flatpak_ref_format_ref (FLATPAK_REF (rref));
const char *ref = flatpak_ref_format_ref_cached (FLATPAK_REF (rref));
g_print (" %s\n", ref);
}
}
@ -252,7 +252,7 @@ flatpak_builtin_uninstall (int argc, char **argv, GCancellable *cancellable, GEr
for (i = 0; i < unused->len; i++)
{
FlatpakInstalledRef *rref = g_ptr_array_index (unused, i);
g_autofree char *ref = flatpak_ref_format_ref (FLATPAK_REF (rref));
const char *ref = flatpak_ref_format_ref_cached (FLATPAK_REF (rref));
uninstall_dir_add_ref (udir, ref);
found_something_to_uninstall = TRUE;

View File

@ -1017,8 +1017,8 @@ installed_ref_compare (gconstpointer _iref_a,
{
const FlatpakInstalledRef *iref_a = *(const FlatpakInstalledRef **)_iref_a;
const FlatpakInstalledRef *iref_b = *(const FlatpakInstalledRef **)_iref_b;
g_autofree char *ref_a = flatpak_ref_format_ref (FLATPAK_REF (iref_a));
g_autofree char *ref_b = flatpak_ref_format_ref (FLATPAK_REF (iref_b));
const char *ref_a = flatpak_ref_format_ref_cached (FLATPAK_REF (iref_a));
const char *ref_b = flatpak_ref_format_ref_cached (FLATPAK_REF (iref_b));
return strcmp (ref_a, ref_b);
}
@ -1074,7 +1074,7 @@ flatpak_installation_list_installed_refs_for_update (FlatpakInstallation *self,
for (guint i = 0; i < installed_refs->len; i++)
{
FlatpakInstalledRef *installed_ref = g_ptr_array_index (installed_refs, i);
g_autofree char *ref = flatpak_ref_format_ref (FLATPAK_REF (installed_ref));
const char *ref = flatpak_ref_format_ref_cached (FLATPAK_REF (installed_ref));
/* This hash table will be used later for efficient search */
g_hash_table_insert (installed_refs_hash, g_strdup (ref), installed_ref);
@ -2266,7 +2266,7 @@ flatpak_installation_fetch_remote_size_sync (FlatpakInstallation *self,
{
g_autoptr(FlatpakDir) dir = NULL;
g_autoptr(FlatpakRemoteState) state = NULL;
g_autofree char *full_ref = flatpak_ref_format_ref (ref);
const char *full_ref = flatpak_ref_format_ref_cached (ref);
dir = flatpak_installation_get_dir (self, error);
if (dir == NULL)
@ -2306,7 +2306,7 @@ flatpak_installation_fetch_remote_metadata_sync (FlatpakInstallation *self,
{
g_autoptr(FlatpakDir) dir = NULL;
g_autoptr(FlatpakRemoteState) state = NULL;
g_autofree char *full_ref = flatpak_ref_format_ref (ref);
const char *full_ref = flatpak_ref_format_ref_cached (ref);
g_autofree char *res = NULL;
gsize len;

View File

@ -340,6 +340,32 @@ flatpak_ref_format_ref (FlatpakRef *self)
priv->arch);
}
/**
* flatpak_ref_format_ref_cached:
* @self: a #FlatpakRef
*
* Like flatpak_ref_format_ref() but this returns the same string each time
* it's called rather than allocating a new one.
*
* Returns: (transfer none): string representation
*
* Since: 1.9.1
*/
const char *
flatpak_ref_format_ref_cached (FlatpakRef *self)
{
char *full_ref;
full_ref = g_object_get_data (G_OBJECT (self), "cached-full-ref");
if (!full_ref)
{
full_ref = flatpak_ref_format_ref (self);
g_object_set_data_full (G_OBJECT (self), "cached-full-ref", full_ref, g_free);
}
return (const char *) full_ref;
}
/**
* flatpak_ref_parse:
* @ref: A string ref name, such as "app/org.test.App/x86_64/master"

View File

@ -68,6 +68,7 @@ FLATPAK_EXTERN const char * flatpak_ref_get_branch (FlatpakRef *self);
FLATPAK_EXTERN const char * flatpak_ref_get_commit (FlatpakRef *self);
FLATPAK_EXTERN FlatpakRefKind flatpak_ref_get_kind (FlatpakRef *self);
FLATPAK_EXTERN char * flatpak_ref_format_ref (FlatpakRef *self);
FLATPAK_EXTERN const char * flatpak_ref_format_ref_cached (FlatpakRef *self);
FLATPAK_EXTERN FlatpakRef * flatpak_ref_parse (const char *ref,
GError **error);
FLATPAK_EXTERN const char * flatpak_ref_get_collection_id (FlatpakRef *self);

View File

@ -1568,7 +1568,7 @@ check_for_updates (PortalFlatpakUpdateMonitor *monitor)
const char *remote_commit;
g_autoptr(GError) error = NULL;
g_autoptr(FlatpakDir) dir = NULL;
g_autofree char *ref = NULL;
const char *ref;
installation_path = update_monitor_get_installation_path (monitor);
@ -1595,7 +1595,7 @@ check_for_updates (PortalFlatpakUpdateMonitor *monitor)
if (dir == NULL)
return;
ref = flatpak_ref_format_ref (FLATPAK_REF (installed_ref));
ref = flatpak_ref_format_ref_cached (FLATPAK_REF (installed_ref));
if (flatpak_dir_ref_is_masked (dir, ref))
return; /* Never report updates for masked refs */

View File

@ -940,7 +940,7 @@ test_list_refs_in_remotes (void)
g_autofree char *repo_uri = NULL;
g_autoptr(GHashTable) ref_specs = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
NULL,
NULL);
create_multi_collection_id_repo (repo_dir);
@ -973,7 +973,7 @@ test_list_refs_in_remotes (void)
for (guint i = 0; i < refs1->len; ++i)
{
FlatpakRef *ref = g_ptr_array_index (refs1, i);
g_hash_table_add (ref_specs, flatpak_ref_format_ref (ref));
g_hash_table_add (ref_specs, flatpak_ref_format_ref_cached (ref));
}
/* Ensure that listing the refs by using a remote's URI will get us the
@ -988,7 +988,7 @@ test_list_refs_in_remotes (void)
for (guint i = 0; i < refs2->len; ++i)
{
FlatpakRef *ref = g_ptr_array_index (refs2, i);
g_autofree char *ref_spec = flatpak_ref_format_ref (ref);
const char *ref_spec = flatpak_ref_format_ref_cached (ref);
g_assert_nonnull (g_hash_table_lookup (ref_specs, ref_spec));
}
}