From e59c6d0276832bf8201561f71718daeca2465360 Mon Sep 17 00:00:00 2001 From: Colomban Wendling Date: Wed, 15 Nov 2023 14:45:37 +0100 Subject: Optimize mate_panel_applet_settings_get_g*list() - Build the list in reverse order, then reverse the result. This is useful because GS?List are list nodes, not containers of nodes, and thus don't contain a pointer to the list's end, meaning to append one has to walk the entire list to find the end each time. To avoid this we use the common idiom of prepending to the list (which is cheap, as it's adding a node before the given one), and then reversing the resulting list to get back the original order. - Avoid unnecessary memory copy by stealing the GStrv's members. We get the array as a copy, so we can simply steal the members and free the container array only, saving a copy for each member. --- libmate-panel-applet/mate-panel-applet-gsettings.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libmate-panel-applet/mate-panel-applet-gsettings.c b/libmate-panel-applet/mate-panel-applet-gsettings.c index 4679dc6d..a4bd3db1 100644 --- a/libmate-panel-applet/mate-panel-applet-gsettings.c +++ b/libmate-panel-applet/mate-panel-applet-gsettings.c @@ -134,15 +134,15 @@ mate_panel_applet_settings_get_glist (GSettings *settings, gchar *key) { gchar **array; GList *list = NULL; - gint i; + array = g_settings_get_strv (settings, key); if (array != NULL) { - for (i = 0; array[i]; i++) { - list = g_list_append (list, g_strdup (array[i])); + for (gint i = 0; array[i]; i++) { + list = g_list_prepend (list, array[i]); } + g_free (array); } - g_strfreev (array); - return list; + return g_list_reverse (list); } void @@ -163,15 +163,15 @@ mate_panel_applet_settings_get_gslist (GSettings *settings, gchar *key) { gchar **array; GSList *list = NULL; - gint i; + array = g_settings_get_strv (settings, key); if (array != NULL) { - for (i = 0; array[i]; i++) { - list = g_slist_append (list, g_strdup (array[i])); + for (gint i = 0; array[i]; i++) { + list = g_slist_prepend (list, array[i]); } + g_free (array); } - g_strfreev (array); - return list; + return g_slist_reverse (list); } void -- cgit v1.2.1