From: Christian Dywan Date: Sun, 11 Dec 2011 01:57:04 +0000 (+0100) Subject: Avoid copying of strings and making tiny allocations X-Git-Url: https://spindle.queued.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c8b19b2eebf14e328955c662ad86265dad310e08;p=midori Avoid copying of strings and making tiny allocations --- diff --git a/midori/main.c b/midori/main.c index 84846ac1..614713cd 100644 --- a/midori/main.c +++ b/midori/main.c @@ -216,12 +216,10 @@ settings_save_to_file (MidoriWebSettings* settings, } g_object_get (settings, property, &string, NULL); - if (!string) - string = g_strdup (""); if (!def_string) def_string = ""; - if (strcmp (string, def_string)) - g_key_file_set_string (key_file, "settings", property, string); + if (strcmp (string ? string : "", def_string)) + g_key_file_set_string (key_file, "settings", property, string ? string : ""); g_free (string); } else if (type == G_TYPE_PARAM_INT) @@ -1405,8 +1403,7 @@ midori_load_session (gpointer data) { katze_item_set_meta_integer (item, "append", 1); katze_item_set_meta_integer (item, "dont-write-history", 1); - if (load_on_startup == MIDORI_STARTUP_DELAYED_PAGES - && katze_item_get_meta_integer (item, "delay") == -1) + if (load_on_startup == MIDORI_STARTUP_DELAYED_PAGES) katze_item_set_meta_integer (item, "delay", 1); midori_browser_add_item (browser, item); } diff --git a/midori/midori-array.c b/midori/midori-array.c index 28d08cee..1567ce50 100644 --- a/midori/midori-array.c +++ b/midori/midori-array.c @@ -796,7 +796,7 @@ katze_item_metadata_to_xbel (KatzeItem* item) const gchar* value; if (!keys) - return g_strdup (""); + return NULL; markup = g_string_new ("\n\n"); string_append_xml_element (markup, "title", katze_item_get_name (KATZE_ITEM (array))); string_append_xml_element (markup, "desc", katze_item_get_text (KATZE_ITEM (array))); - g_string_append (markup, metadata); + g_string_append (markup, metadata ? metadata : ""); KATZE_ARRAY_FOREACH_ITEM_L (item, array, list) string_append_item (markup, item); g_string_append (markup, "\n"); diff --git a/midori/midori-browser.c b/midori/midori-browser.c index a921373a..3990e830 100644 --- a/midori/midori-browser.c +++ b/midori/midori-browser.c @@ -3341,17 +3341,17 @@ midori_browser_get_uri_extension (const gchar* uri) slash = strrchr (uri, '/'); /* Huh, URI without slashes? */ if (!slash) - return g_strdup (""); + return NULL; ext_end = period = strrchr (slash, '.'); if (!period) - return g_strdup (""); + return NULL; /* Skip the period */ ext_end++; /* If *ext_end is 0 here, the URI ended with a period, so skip */ if (!*ext_end) - return g_strdup (""); + return NULL; /* Find the end of the extension */ while (*ext_end && g_ascii_isalnum (*ext_end)) @@ -5489,7 +5489,7 @@ midori_browser_size_allocate_cb (MidoriBrowser* browser, { GtkWidget* widget = GTK_WIDGET (browser); - if (gtk_widget_get_realized (widget) && !browser->alloc_timeout) + if (!browser->alloc_timeout && gtk_widget_get_realized (widget)) { gpointer last_page; diff --git a/midori/midori-view.c b/midori/midori-view.c index 90d099c3..167b5c70 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -1951,8 +1951,8 @@ gtk_widget_key_press_event_cb (WebKitWebView* web_view, if (!webkit_web_view_can_cut_clipboard (web_view) && !webkit_web_view_can_paste_clipboard (web_view)) { - gchar* text = character ? g_strdup_printf ("%c", character) : g_strdup (""); - g_signal_emit (view, signals[SEARCH_TEXT], 0, TRUE, text); + gchar* text = character ? g_strdup_printf ("%c", character) : NULL; + g_signal_emit (view, signals[SEARCH_TEXT], 0, TRUE, text ? text : ""); g_free (text); return TRUE; } @@ -3720,7 +3720,7 @@ prepare_speed_dial_html (MidoriView* view, else { g_free (file_path); - return g_strdup (""); + return NULL; } groups = g_key_file_get_groups (key_file, NULL); @@ -3774,7 +3774,7 @@ prepare_speed_dial_html (MidoriView* view, } else { - encoded = g_strdup (""); + encoded = NULL; if (load_missing) midori_view_speed_dial_get_thumb (view, groups[i], uri); } @@ -3785,7 +3785,7 @@ prepare_speed_dial_html (MidoriView* view, "" "" "
%s
\n", - slot, slot, uri, encoded, slot, title ? title : ""); + slot, slot, uri, encoded ? encoded : "", slot, title ? title : ""); g_free (title); g_free (encoded); @@ -3848,7 +3848,7 @@ midori_view_set_uri (MidoriView* view, speeddial_markup = prepare_speed_dial_html (view, TRUE); midori_view_load_alternate_string (view, - speeddial_markup, "about:blank", NULL); + speeddial_markup ? speeddial_markup : "", "about:blank", NULL); #ifdef G_ENABLE_DEBUG if (g_getenv ("MIDORI_STARTTIME") != NULL) @@ -3960,7 +3960,7 @@ midori_view_set_uri (MidoriView* view, katze_item_set_uri (view->item, view->uri); g_object_notify (G_OBJECT (view), "uri"); } - else if (katze_item_get_meta_integer (view->item, "delay") > 0) + else if (katze_item_get_meta_boolean (view->item, "delay")) { katze_assign (view->uri, g_strdup (uri)); katze_item_set_meta_integer (view->item, "delay", -1); diff --git a/midori/sokoke.c b/midori/sokoke.c index 7daa7702..39ea28eb 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -1701,7 +1701,7 @@ midori_download_prepare_tooltip_text (WebKitDownload* download) if (time_estimated > 0) { - gchar* eta; + gchar* eta = NULL; if (hours_left > 0) eta = g_strdup_printf ("%s, %s", hours_str, minutes_str); else if (minutes_left >= 10) @@ -1710,11 +1710,12 @@ midori_download_prepare_tooltip_text (WebKitDownload* download) eta = g_strdup_printf ("%s, %s", minutes_str, seconds_str); else if (seconds_left > 0) eta = g_strdup_printf ("%s", seconds_str); - else - eta = g_strdup (""); - /* i18n: Download tooltip (estimated time) : - 1 hour, 5 minutes remaning */ - g_string_append_printf (tooltip, _(" - %s remaining"), eta); - g_free (eta); + if (eta != NULL) + { + /* i18n: Download tooltip (estimated time) : - 1 hour, 5 minutes remaning */ + g_string_append_printf (tooltip, _(" - %s remaining"), eta); + g_free (eta); + } } g_free (hours_str); diff --git a/panels/midori-bookmarks.c b/panels/midori-bookmarks.c index ec6d3211..dd5291f5 100644 --- a/panels/midori-bookmarks.c +++ b/panels/midori-bookmarks.c @@ -216,9 +216,9 @@ midori_bookmarks_insert_item_db (sqlite3* db, gchar* sqlcmd; char* errmsg = NULL; KatzeItem* old_parent; - gchar* parent; - gchar* uri; - gchar* desc; + const gchar* parent; + const gchar* uri = NULL; + const gchar* desc = NULL; /* Bookmarks must have a name, import may produce invalid items */ g_return_if_fail (katze_item_get_name (item)); @@ -227,30 +227,26 @@ midori_bookmarks_insert_item_db (sqlite3* db, return; if (KATZE_ITEM_IS_BOOKMARK (item)) - uri = g_strdup (katze_item_get_uri (item)); - else - uri = g_strdup (""); + uri = katze_item_get_uri (item); if (katze_item_get_text (item)) - desc = g_strdup (katze_item_get_text (item)); - else - desc = g_strdup (""); + desc = katze_item_get_text (item); /* Use folder, otherwise fallback to parent folder */ old_parent = katze_item_get_parent (item); if (folder && *folder) - parent = g_strdup (folder); + parent = folder; else if (old_parent && katze_item_get_name (old_parent)) - parent = g_strdup (katze_item_get_name (old_parent)); + parent = katze_item_get_name (old_parent); else - parent = g_strdup (""); + parent = ""; sqlcmd = sqlite3_mprintf ( "INSERT into bookmarks (uri, title, desc, folder, toolbar, app) values" " ('%q', '%q', '%q', '%q', %d, %d)", - uri, + uri ? uri : "", katze_item_get_name (item), - desc, + desc ? desc : "", parent, katze_item_get_meta_boolean (item, "toolbar"), katze_item_get_meta_boolean (item, "app")); @@ -261,9 +257,6 @@ midori_bookmarks_insert_item_db (sqlite3* db, sqlite3_free (errmsg); } - g_free (uri); - g_free (parent); - g_free (desc); sqlite3_free (sqlcmd); } @@ -315,30 +308,31 @@ midori_bookmarks_row_changed_cb (GtkTreeModel* model, { KatzeItem* item; GtkTreeIter parent; - gchar* parent_name; + KatzeItem* new_parent = NULL; + const gchar* parent_name; gtk_tree_model_get (model, iter, 0, &item, -1); if (gtk_tree_model_iter_parent (model, &parent, iter)) { - KatzeItem* new_parent; - gtk_tree_model_get (model, &parent, 0, &new_parent, -1); /* Bookmarks must not be moved into non-folder items */ if (!KATZE_ITEM_IS_FOLDER (new_parent)) - parent_name = g_strdup (""); + parent_name = ""; else - parent_name = g_strdup (katze_item_get_name (new_parent)); - - g_object_unref (new_parent); + parent_name = katze_item_get_name (new_parent); } else - parent_name = g_strdup (""); + parent_name = ""; katze_array_remove_item (bookmarks->array, item); katze_item_set_meta_string (item, "folder", parent_name); katze_array_add_item (bookmarks->array, item); + + g_object_unref (item); + if (new_parent) + g_object_unref (new_parent); } static void