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.

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@5625 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
Colomban Wendling 2011-03-24 22:00:18 +00:00
parent ff7d6b9cad
commit 7698bf60a7
21 changed files with 51 additions and 45 deletions

View File

@ -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>

View File

@ -123,7 +123,7 @@ static gboolean check_hidden(const gchar *filename, const gchar *base_name)
{
gsize len;
if (! NZV(base_name))
if (G_UNLIKELY(! NZV(base_name)))
return FALSE;
#ifdef G_OS_WIN32

View File

@ -91,7 +91,7 @@ static gboolean backupcopy_set_backup_dir(const gchar *utf8_dir)
{
gchar *tmp;
if (! NZV(utf8_dir))
if (G_UNLIKELY(! NZV(utf8_dir)))
return FALSE;
tmp = utils_get_locale_from_utf8(utf8_dir);

View File

@ -692,7 +692,7 @@ on_notebook1_switch_page_after (GtkNotebook *notebook,
{
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 @@ on_context_action1_activate (GtkMenuItem *menuitem,
}
/* 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);

View File

@ -470,7 +470,7 @@ on_file_save_dialog_response (GtkDialog *dialog,
{
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;

View File

@ -143,7 +143,7 @@ GeanyDocument* document_find_by_real_path(const gchar *realname)
{
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 @@ GeanyDocument *document_find_by_filename(const gchar *utf8_filename)
{
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 @@ gboolean document_check_disk_status(GeanyDocument *doc, gboolean force)
/* 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);
}

View File

@ -141,7 +141,7 @@ extern GPtrArray *documents_array;
* @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

View File

@ -817,7 +817,7 @@ static void expand(ScintillaObject *sci, gint *line, gboolean doExpand,
(*line)++;
while (*line <= lineMaxSubord)
{
if (G_UNLIKELY(force))
if (force)
{
if (visLevels > 0)
SSM(sci, SCI_SHOWLINES, *line, *line);
@ -833,7 +833,7 @@ static void expand(ScintillaObject *sci, gint *line, gboolean doExpand,
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 @@ autocomplete_html(ScintillaObject *sci, const gchar *root, gsize rootlen)
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 @@ gint editor_do_uncomment(GeanyEditor *editor, gint line, gboolean toggle)
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 @@ void editor_do_comment_toggle(GeanyEditor *editor)
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 @@ static void auto_multiline(GeanyEditor *editor, gint cur_line)
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);

View File

@ -282,7 +282,7 @@ static gchar *regex_match(regex_t *preg, const gchar *buffer, gsize size)
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)

View File

@ -1088,7 +1088,7 @@ static void load_settings(gint ft_id, GKeyFile *config, GKeyFile *configh)
/* 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 @@ static void load_settings(gint ft_id, GKeyFile *config, GKeyFile *configh)
/* 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 @@ void filetypes_load_config(gint ft_id, gboolean reload)
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 @@ gboolean filetypes_parse_error_message(GeanyFiletype *ft, const gchar *message,
*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)

View File

@ -249,7 +249,7 @@ static void parse_color(const gchar *str, gint *clr)
gint c;
/* ignore empty strings */
if (!NZV(str))
if (G_UNLIKELY(! NZV(str)))
return;
c = utils_strtod(str, NULL, FALSE);

View File

@ -118,7 +118,7 @@ static void handler_log(const gchar *domain, GLogLevelFlags level, const gchar *
{
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

View File

@ -331,7 +331,7 @@ static void get_line_and_column_from_filename(gchar *filename, gint *line, gint
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 @@ static void load_session_project_file(void)
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);

View File

@ -705,7 +705,7 @@ plugin_new(const gchar *fname, gboolean init_plugin, gboolean add_to_list)
/* 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);

View File

@ -311,7 +311,7 @@ static void add_page_header(DocInfo *dinfo, cairo_t *cr, gint width, gint page_n
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);

View File

@ -684,7 +684,7 @@ static gboolean update_config(const PropertyDialogElements *e, gboolean new_proj
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);

View File

@ -1085,7 +1085,7 @@ gint search_mark_all(GeanyDocument *doc, const gchar *search_text, gint flags)
/* 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 @@ on_find_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
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 @@ on_find_in_files_dialog_response(GtkDialog *dialog, gint response,
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 @@ void search_find_usage(const gchar *search_text, gint flags, gboolean in_session
doc = document_get_current();
g_return_if_fail(doc != NULL);
if (!NZV(search_text))
if (G_UNLIKELY(! NZV(search_text)))
{
utils_beep();
return;

View File

@ -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 @@ static gint socket_fd_open_inet(gushort port)
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 @@ static gint socket_fd_connect_inet(gushort port)
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;

View File

@ -793,7 +793,7 @@ static void tb_editor_drag_data_get_cb(GtkWidget *widget, GdkDragContext *contex
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 @@ static gboolean tb_editor_foreach_used(GtkTreeModel *model, GtkTreePath *path,
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);

View File

@ -423,7 +423,7 @@ gboolean utils_atob(const gchar *str)
/* 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 @@ gint utils_get_setting_integer(GKeyFile *config, const gchar *section, const gch
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 @@ gboolean utils_get_setting_boolean(GKeyFile *config, const gchar *section, const
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 @@ gchar *utils_get_setting_string(GKeyFile *config, const gchar *section, const gc
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 *utils_str_remove_chars(gchar *string, const gchar *chars)
gchar *w = string;
g_return_val_if_fail(string, NULL);
if (!NZV(chars))
if (G_UNLIKELY(! NZV(chars)))
return string;
foreach_str(r, string)

View File

@ -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.