SF.net SVN: geany:[5625] trunk

colombanw at users.sourceforge.net colombanw at xxxxx
Thu Mar 24 22:00:18 UTC 2011


Revision: 5625
          http://geany.svn.sourceforge.net/geany/?rev=5625&view=rev
Author:   colombanw
Date:     2011-03-24 22:00:18 +0000 (Thu, 24 Mar 2011)

Log Message:
-----------
Improve usage of G_(UN)?LIKELY()

G_(UN)?LIKELY() should be only used on whole conditional expressions,
and only if the branching is very highly predictable, not if it is only
more probable.

These macros should be used with care because a wrong prediction may
be a lot worst than what a good prediction can give.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/plugins/filebrowser.c
    trunk/plugins/saveactions.c
    trunk/src/callbacks.c
    trunk/src/dialogs.c
    trunk/src/document.c
    trunk/src/document.h
    trunk/src/editor.c
    trunk/src/encodings.c
    trunk/src/filetypes.c
    trunk/src/highlighting.c
    trunk/src/log.c
    trunk/src/main.c
    trunk/src/plugins.c
    trunk/src/printing.c
    trunk/src/project.c
    trunk/src/search.c
    trunk/src/socket.c
    trunk/src/toolbar.c
    trunk/src/utils.c
    trunk/src/utils.h

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/ChangeLog	2011-03-24 22:00:18 UTC (rev 5625)
@@ -19,6 +19,12 @@
    src/search.c, src/toolbar.c, src/ui_utils.c, src/ui_utils.h:
    Make Shift-Enter in search dialog and toolbar search entries search
    backwards.
+ * plugins/filebrowser.c, plugins/saveactions.c, src/callbacks.c,
+   src/dialogs.c, src/document.c, src/document.h, src/editor.c,
+   src/encodings.c, src/filetypes.c, src/highlighting.c, src/log.c,
+   src/main.c, src/plugins.c, src/printing.c, src/project.c, src/search.c,
+   src/socket.c, src/toolbar.c, src/utils.c, src/utils.h:
+   Improve usage of G_LIKELY() and G_UNLIKELY() macros.
 
 
 2011-03-22  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>

Modified: trunk/plugins/filebrowser.c
===================================================================
--- trunk/plugins/filebrowser.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/plugins/filebrowser.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -123,7 +123,7 @@
 {
 	gsize len;
 
-	if (! NZV(base_name))
+	if (G_UNLIKELY(! NZV(base_name)))
 		return FALSE;
 
 #ifdef G_OS_WIN32

Modified: trunk/plugins/saveactions.c
===================================================================
--- trunk/plugins/saveactions.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/plugins/saveactions.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -91,7 +91,7 @@
 {
 	gchar *tmp;
 
-	if (! NZV(utf8_dir))
+	if (G_UNLIKELY(! NZV(utf8_dir)))
 		return FALSE;
 
 	tmp = utils_get_locale_from_utf8(utf8_dir);

Modified: trunk/src/callbacks.c
===================================================================
--- trunk/src/callbacks.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/callbacks.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -692,7 +692,7 @@
 {
 	GeanyDocument *doc;
 
-	if (G_UNLIKELY(main_status.opening_session_files) || G_UNLIKELY(main_status.closing_all))
+	if (G_UNLIKELY(main_status.opening_session_files || main_status.closing_all))
 		return;
 
 	if (page_num == (guint) -1 && page != NULL)
@@ -1931,7 +1931,7 @@
 	}
 
 	/* substitute the wildcard %s and run the command if it is non empty */
-	if (NZV(command))
+	if (G_LIKELY(NZV(command)))
 	{
 		utils_str_replace_all(&command, "%s", word);
 

Modified: trunk/src/dialogs.c
===================================================================
--- trunk/src/dialogs.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/dialogs.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -470,7 +470,7 @@
 	{
 		case GEANY_RESPONSE_RENAME:
 			/* rename doesn't check for empty filename or overwriting */
-			if (! NZV(new_filename))
+			if (G_UNLIKELY(! NZV(new_filename)))
 			{
 				utils_beep();
 				break;

Modified: trunk/src/document.c
===================================================================
--- trunk/src/document.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/document.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -143,7 +143,7 @@
 	{
 		GeanyDocument *doc = documents[i];
 
-		if (! doc->is_valid || G_UNLIKELY(! doc->real_path))
+		if (! doc->is_valid || ! doc->real_path)
 			continue;
 
 		if (filenamecmp(realname, doc->real_path) == 0)
@@ -191,7 +191,7 @@
 	{
 		doc = documents[i];
 
-		if (! doc->is_valid || G_UNLIKELY(doc->file_name == NULL))
+		if (! doc->is_valid || doc->file_name == NULL)
 			continue;
 
 		if (filenamecmp(utf8_filename, doc->file_name) == 0)
@@ -2953,8 +2953,8 @@
 		/* doc may be closed now */
 		ret = TRUE;
 	}
-	else if (! use_gio_filemon && /* ignore these checks when using GIO */
-			 (G_UNLIKELY(doc->priv->mtime > cur_time) || G_UNLIKELY(st.st_mtime > cur_time)))
+	else if (G_UNLIKELY(! use_gio_filemon && /* ignore these checks when using GIO */
+			 (doc->priv->mtime > cur_time || st.st_mtime > cur_time)))
 	{
 		g_warning("%s: Something is wrong with the time stamps.", G_STRFUNC);
 	}

Modified: trunk/src/document.h
===================================================================
--- trunk/src/document.h	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/document.h	2011-03-24 22:00:18 UTC (rev 5625)
@@ -141,7 +141,7 @@
  * @note This should not be used to check the result of the main API functions,
  * these only need a NULL-pointer check - @c document_get_current() != @c NULL. */
 #define DOC_VALID(doc_ptr) \
-	(G_LIKELY((doc_ptr) != NULL) && G_LIKELY((doc_ptr)->is_valid))
+	(G_LIKELY((doc_ptr) != NULL && (doc_ptr)->is_valid))
 
 /**
  *  Returns the filename of the document passed or @c GEANY_STRING_UNTITLED

Modified: trunk/src/editor.c
===================================================================
--- trunk/src/editor.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/editor.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -817,7 +817,7 @@
 	(*line)++;
 	while (*line <= lineMaxSubord)
 	{
-		if (G_UNLIKELY(force))
+		if (force)
 		{
 			if (visLevels > 0)
 				SSM(sci, SCI_SHOWLINES, *line, *line);
@@ -833,7 +833,7 @@
 			levelLine = SSM(sci, SCI_GETFOLDLEVEL, *line, 0);
 		if (levelLine & SC_FOLDLEVELHEADERFLAG)
 		{
-			if (G_UNLIKELY(force))
+			if (force)
 			{
 				if (visLevels > 1)
 					SSM(sci, SCI_SETFOLDEXPANDED, *line, 1);
@@ -1970,7 +1970,7 @@
 	GString *words;
 	const gchar **entities = symbols_get_html_entities();
 
-	if (*root != '&' || G_UNLIKELY(entities == NULL))
+	if (*root != '&' || entities == NULL)
 		return FALSE;
 
 	words = g_string_sized_new(500);
@@ -2973,7 +2973,7 @@
 		if (x < line_len && sel[x] != '\0')
 		{
 			/* use single line comment */
-			if (cc == NULL || cc[0] == '\0')
+			if (! NZV(cc))
 			{
 				single_line = TRUE;
 
@@ -3108,7 +3108,7 @@
 		while (isspace(sel[x])) x++;
 
 		/* use single line comment */
-		if (cc == NULL || cc[0] == '\0')
+		if (! NZV(cc))
 		{
 			gboolean do_continue = FALSE;
 			single_line = TRUE;
@@ -3505,7 +3505,7 @@
 			whitespace = " ";
 		}
 
-		if (G_UNLIKELY(style == SCE_D_COMMENTNESTED))
+		if (style == SCE_D_COMMENTNESTED)
 			continuation = "+"; /* for nested comments in D */
 
 		result = g_strconcat(whitespace, continuation, " ", NULL);

Modified: trunk/src/encodings.c
===================================================================
--- trunk/src/encodings.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/encodings.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -282,7 +282,7 @@
 	gchar *encoding = NULL;
 	regmatch_t pmatch[10];
 
-	if (G_UNLIKELY(! pregs_loaded) || G_UNLIKELY(buffer == NULL))
+	if (G_UNLIKELY(! pregs_loaded || buffer == NULL))
 		return NULL;
 
 	if (size > 512)

Modified: trunk/src/filetypes.c
===================================================================
--- trunk/src/filetypes.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/filetypes.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -1088,7 +1088,7 @@
 	/* default extension */
 	result = g_key_file_get_string(configh, "settings", "extension", NULL);
 	if (result == NULL) result = g_key_file_get_string(config, "settings", "extension", NULL);
-	if (G_LIKELY(result != NULL))
+	if (result != NULL)
 	{
 		setptr(filetypes[ft_id]->extension, result);
 	}
@@ -1137,7 +1137,7 @@
 	/* read context action */
 	result = g_key_file_get_string(configh, "settings", "context_action_cmd", NULL);
 	if (result == NULL) result = g_key_file_get_string(config, "settings", "context_action_cmd", NULL);
-	if (G_LIKELY(result != NULL))
+	if (result != NULL)
 	{
 		setptr(filetypes[ft_id]->context_action_cmd, result);
 	}
@@ -1274,11 +1274,11 @@
 	pft = ft->priv;
 
 	/* when reloading, proceed only if the settings were already loaded */
-	if (reload && G_UNLIKELY(! pft->keyfile_loaded))
+	if (G_UNLIKELY(reload && ! pft->keyfile_loaded))
 		return;
 
 	/* when not reloading, load the settings only once */
-	if (! reload && G_LIKELY(pft->keyfile_loaded))
+	if (G_LIKELY(! reload && pft->keyfile_loaded))
 		return;
 	pft->keyfile_loaded = TRUE;
 
@@ -1484,7 +1484,7 @@
 	*filename = NULL;
 	*line = -1;
 
-	if (!NZV(regstr))
+	if (G_UNLIKELY(! NZV(regstr)))
 		return FALSE;
 
 	if (!ft->priv->error_regex_compiled || regstr != ft->priv->last_string)

Modified: trunk/src/highlighting.c
===================================================================
--- trunk/src/highlighting.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/highlighting.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -249,7 +249,7 @@
 	gint c;
 
 	/* ignore empty strings */
-	if (!NZV(str))
+	if (G_UNLIKELY(! NZV(str)))
 		return;
 
 	c = utils_strtod(str, NULL, FALSE);

Modified: trunk/src/log.c
===================================================================
--- trunk/src/log.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/log.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -118,7 +118,7 @@
 {
 	gchar *time_str;
 
-	if (G_LIKELY(app != NULL) && app->debug_mode)
+	if (G_LIKELY(app != NULL && app->debug_mode))
 	{
 #ifdef G_OS_WIN32
 		/* On Windows g_log_default_handler() is not enough, we need to print it

Modified: trunk/src/main.c
===================================================================
--- trunk/src/main.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/main.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -331,7 +331,7 @@
 
 	g_assert(*line == -1 && *column == -1);
 
-	if (! NZV(filename))
+	if (G_UNLIKELY(! NZV(filename)))
 		return;
 
 	/* allow to open files like "test:0" */
@@ -834,7 +834,7 @@
 
 	locale_filename = utils_get_locale_from_utf8(project_prefs.session_file);
 
-	if (NZV(locale_filename))
+	if (G_LIKELY(NZV(locale_filename)))
 		project_load_file(locale_filename);
 
 	g_free(locale_filename);

Modified: trunk/src/plugins.c
===================================================================
--- trunk/src/plugins.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/plugins.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -705,7 +705,7 @@
 
 	/* read plugin name, etc. */
 	plugin_set_info(&plugin->info);
-	if (!NZV(plugin->info.name))
+	if (G_UNLIKELY(! NZV(plugin->info.name)))
 	{
 		geany_debug("No plugin name set in plugin_set_info() for \"%s\" - ignoring plugin!",
 			fname);

Modified: trunk/src/printing.c
===================================================================
--- trunk/src/printing.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/printing.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -311,7 +311,7 @@
 	g_free(data);
 
 	datetime = utils_get_date_time(printing_prefs.page_header_datefmt, &(dinfo->print_time));
-	if (NZV(datetime))
+	if (G_LIKELY(NZV(datetime)))
 	{
 		data = g_strdup_printf("<b>%s</b>", datetime);
 		pango_layout_set_markup(layout, data, -1);

Modified: trunk/src/project.c
===================================================================
--- trunk/src/project.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/project.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -684,7 +684,7 @@
 	else
 		file_name = gtk_label_get_text(GTK_LABEL(e->file_name));
 
-	if (! NZV(file_name))
+	if (G_UNLIKELY(! NZV(file_name)))
 	{
 		SHOW_ERR(_("You have specified an invalid project filename."));
 		gtk_widget_grab_focus(e->file_name);

Modified: trunk/src/search.c
===================================================================
--- trunk/src/search.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/search.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -1085,7 +1085,7 @@
 	/* clear previous search indicators */
 	editor_indicator_clear(doc->editor, GEANY_INDICATOR_SEARCH);
 
-	if (!NZV(search_text))
+	if (G_UNLIKELY(! NZV(search_text)))
 		return 0;
 
 	ttf.chrg.cpMin = 0;
@@ -1160,7 +1160,7 @@
 		search_data.flags = int_search_flags(settings.find_case_sensitive,
 			settings.find_match_whole_word, settings.find_regexp, settings.find_match_word_start);
 
-		if (search_data.text[0] == '\0')
+		if (! NZV(search_data.text))
 		{
 			fail:
 			utils_beep();
@@ -1430,7 +1430,7 @@
 		GeanyEncodingIndex enc_idx = gtk_combo_box_get_active(
 			GTK_COMBO_BOX(fif_dlg.encoding_combo));
 
-		if (!NZV(utf8_dir))
+		if (G_UNLIKELY(! NZV(utf8_dir)))
 			ui_set_statusbar(FALSE, _("Invalid directory for find in files."));
 		else if (NZV(search_text))
 		{
@@ -1908,7 +1908,7 @@
 	doc = document_get_current();
 	g_return_if_fail(doc != NULL);
 
-	if (!NZV(search_text))
+	if (G_UNLIKELY(! NZV(search_text)))
 	{
 		utils_beep();
 		return;

Modified: trunk/src/socket.c
===================================================================
--- trunk/src/socket.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/socket.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -93,9 +93,9 @@
 
 #ifdef G_OS_WIN32
 #define REMOTE_CMD_PORT		49876
-#define SOCKET_IS_VALID(s)	(G_LIKELY((s) != INVALID_SOCKET))
+#define SOCKET_IS_VALID(s)	((s) != INVALID_SOCKET)
 #else
-#define SOCKET_IS_VALID(s)	(G_LIKELY((s) >= 0))
+#define SOCKET_IS_VALID(s)	((s) >= 0)
 #define INVALID_SOCKET		(-1)
 #endif
 #define BUFFER_LENGTH 4096
@@ -468,7 +468,7 @@
 	gchar val;
 
 	sock = socket(AF_INET, SOCK_STREAM, 0);
-	if (! SOCKET_IS_VALID(sock))
+	if (G_UNLIKELY(! SOCKET_IS_VALID(sock)))
 	{
 		geany_debug("fd_open_inet(): socket() failed: %d\n", WSAGetLastError());
 		return -1;
@@ -511,7 +511,7 @@
 	struct sockaddr_in addr;
 
 	sock = socket(AF_INET, SOCK_STREAM, 0);
-	if (! SOCKET_IS_VALID(sock))
+	if (G_UNLIKELY(! SOCKET_IS_VALID(sock)))
 	{
 		geany_debug("fd_connect_inet(): socket() failed: %d\n", WSAGetLastError());
 		return -1;

Modified: trunk/src/toolbar.c
===================================================================
--- trunk/src/toolbar.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/toolbar.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -793,7 +793,7 @@
 		return;
 
 	gtk_tree_model_get(model, &iter, TB_EDITOR_COL_ACTION, &name, -1);
-	if (! NZV(name))
+	if (G_UNLIKELY(! NZV(name)))
 		return;
 
 	atom = gdk_atom_intern(tb_editor_dnd_targets[0].target, FALSE);
@@ -869,7 +869,7 @@
 
 	if (utils_str_equal(action_name, TB_EDITOR_SEPARATOR))
 		g_string_append_printf(data, "\t\t<separator/>\n");
-	else if (NZV(action_name))
+	else if (G_LIKELY(NZV(action_name)))
 		g_string_append_printf(data, "\t\t<toolitem action='%s' />\n", action_name);
 
 	g_free(action_name);

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/utils.c	2011-03-24 22:00:18 UTC (rev 5625)
@@ -423,7 +423,7 @@
 /* NULL-safe version of g_path_is_absolute(). */
 gboolean utils_is_absolute_path(const gchar *path)
 {
-	if (! NZV(path))
+	if (G_UNLIKELY(! NZV(path)))
 		return FALSE;
 
 	return g_path_is_absolute(path);
@@ -832,7 +832,7 @@
 		return default_value;
 
 	tmp = g_key_file_get_integer(config, section, key, &error);
-	if (G_UNLIKELY(error))
+	if (error)
 	{
 		g_error_free(error);
 		return default_value;
@@ -863,7 +863,7 @@
 		return default_value;
 
 	tmp = g_key_file_get_boolean(config, section, key, &error);
-	if (G_UNLIKELY(error))
+	if (error)
 	{
 		g_error_free(error);
 		return default_value;
@@ -893,7 +893,7 @@
 		return g_strdup(default_value);
 
 	tmp = g_key_file_get_string(config, section, key, NULL);
-	if (G_UNLIKELY(!tmp))
+	if (!tmp)
 	{
 		return g_strdup(default_value);
 	}
@@ -1941,7 +1941,7 @@
 	gchar *w = string;
 
 	g_return_val_if_fail(string, NULL);
-	if (!NZV(chars))
+	if (G_UNLIKELY(! NZV(chars)))
 		return string;
 
 	foreach_str(r, string)

Modified: trunk/src/utils.h
===================================================================
--- trunk/src/utils.h	2011-03-24 17:59:05 UTC (rev 5624)
+++ trunk/src/utils.h	2011-03-24 22:00:18 UTC (rev 5625)
@@ -35,7 +35,7 @@
 
 /** Returns TRUE if @a ptr points to a non-zero value. */
 #define NZV(ptr) \
-	(G_LIKELY((ptr)) && G_LIKELY((ptr)[0]))
+	((ptr) && (ptr)[0])
 
 /**
  *  Frees @a ptr (if not @c NULL), then assigns @a result to it.


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list