[geany/geany] 482177: Simplify encoding menu creation loop

Colomban Wendling git-noreply at xxxxx
Fri Nov 27 22:07:07 UTC 2015


Branch:      refs/heads/master
Author:      Colomban Wendling <ban at herbesfolles.org>
Committer:   Colomban Wendling <ban at herbesfolles.org>
Date:        Fri, 27 Nov 2015 22:07:07 UTC
Commit:      48217703e13a42dcd51a799cc00417794281eaa0
             https://github.com/geany/geany/commit/48217703e13a42dcd51a799cc00417794281eaa0

Log Message:
-----------
Simplify encoding menu creation loop

Simplify and optimize creation of the sorted menus by taking advantage
of the fact they might already be partially sorted, and that we can at
least add one entry to each group in each run.

This goes from 4032 runs to 882, which while definitely not optimal for
adding 126 items, is probably totally good enough and don't warrant
duplicating the encoding array and sort it.

Though, such optimization doesn't matter as it's not what takes time in
this function, which is probably rather the widgets creation.


Modified Paths:
--------------
    src/encodings.c

Modified: src/encodings.c
46 lines changed, 24 insertions(+), 22 deletions(-)
===================================================================
@@ -440,6 +440,8 @@ void encodings_init(void)
 	{
 		GSList *group = NULL;
 		GtkWidget *submenus[GEANY_ENCODING_GROUPS_MAX];
+		gint orders[GEANY_ENCODING_GROUPS_MAX] = { 0 };
+		guint n_added = 0;
 
 		for (guint i = 0; i < GEANY_ENCODING_GROUPS_MAX; i++)
 		{
@@ -455,36 +457,36 @@ void encodings_init(void)
 			}
 		}
 
-		/** TODO can it be optimized? ATM 3782 runs at line "if (encodings[j].group ...)" */
-		for (guint i = 0; i < GEANY_ENCODING_GROUPS_MAX; i++)
+		/** TODO can it be optimized? ATM 882 runs at line "if (encodings[i].order ...)" */
+		do
 		{
-			for (gint order = 0; order < group_sizes[i]; order++)
+			for (guint i = 0; i < G_N_ELEMENTS(encodings); i++)
 			{
-				for (guint j = 0; j < GEANY_ENCODINGS_MAX; j++)
+				if (encodings[i].order == orders[encodings[i].group])
 				{
-					if (encodings[j].group == i && encodings[j].order == order)
+					GtkWidget *item;
+					gchar *label = encodings_to_string(&encodings[i]);
+
+					if (k == 0) /* Set Encoding menu */
 					{
-						GtkWidget *item;
-						gchar *label = encodings_to_string(&encodings[j]);
-
-						if (k == 0) /* Set Encoding menu */
-						{
-							item = gtk_radio_menu_item_new_with_label(group, label);
-							group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(item));
-							radio_items[j] = item;
-						}
-						else
-							item = gtk_menu_item_new_with_label(label);
-						gtk_widget_show(item);
-						gtk_container_add(GTK_CONTAINER(submenus[i]), item);
-						g_signal_connect(item, "activate", cb_func[k],
-								(gpointer) encodings[j].charset);
-						g_free(label);
-						break;
+						item = gtk_radio_menu_item_new_with_label(group, label);
+						group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(item));
+						radio_items[i] = item;
 					}
+					else
+						item = gtk_menu_item_new_with_label(label);
+					gtk_widget_show(item);
+					gtk_container_add(GTK_CONTAINER(submenus[encodings[i].group]), item);
+					g_signal_connect(item, "activate", cb_func[k],
+							(gpointer) encodings[i].charset);
+					g_free(label);
+
+					orders[encodings[i].group]++;
+					n_added++;
 				}
 			}
 		}
+		while (n_added < G_N_ELEMENTS(encodings));
 	}
 }
 



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list