From b5f5129f7d658c296a323b6e5deb3c31c4a3b3ca Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Tue, 19 Oct 2021 19:21:59 +0800 Subject: [PATCH] Shortcut g_list_length comparison against small numbers It's more efficient to test node && node->next etc when testing GList length against small numbers --- gnucash/gnome-utils/gnc-main-window.c | 6 +++--- gnucash/gnome/dialog-imap-editor.c | 4 ++-- gnucash/gnome/dialog-invoice.c | 4 ++-- gnucash/gnome/dialog-payment.c | 2 +- gnucash/gnome/dialog-price-edit-db.c | 4 ++-- gnucash/gnome/dialog-sx-editor.c | 4 ++-- gnucash/gnome/dialog-sx-editor2.c | 4 ++-- gnucash/gnome/dialog-sx-since-last-run.c | 6 ++++-- gnucash/gnome/gnc-plugin-page-account-tree.c | 4 ++-- gnucash/gnome/gnc-plugin-page-sx-list.c | 6 +++--- gnucash/register/ledger-core/gnc-ledger-display.c | 2 +- libgnucash/engine/Recurrence.c | 9 +++++---- libgnucash/engine/gncIDSearch.c | 4 +--- libgnucash/engine/qofquery.cpp | 9 +++++---- 14 files changed, 35 insertions(+), 33 deletions(-) diff --git a/gnucash/gnome-utils/gnc-main-window.c b/gnucash/gnome-utils/gnc-main-window.c index 1567e3a396..385a216d2f 100644 --- a/gnucash/gnome-utils/gnc-main-window.c +++ b/gnucash/gnome-utils/gnc-main-window.c @@ -1446,7 +1446,7 @@ gnc_main_window_delete_event (GtkWidget *window, if (already_dead) return TRUE; - if (g_list_length (active_windows) > 1) + if (active_windows && active_windows->next) { gint response; GtkWidget *dialog; @@ -1478,7 +1478,7 @@ gnc_main_window_delete_event (GtkWidget *window, return TRUE; } - if (g_list_length(active_windows) > 1) + if (active_windows && active_windows->next) return FALSE; already_dead = gnc_main_window_quit(GNC_MAIN_WINDOW(window)); @@ -3348,7 +3348,7 @@ gnc_main_window_close_page (GncPluginPage *page) /* remove the preference callbacks from the main window */ gnc_main_window_remove_prefs (window); } - if (window && g_list_length (active_windows) > 1) + if (window && active_windows && active_windows->next) gtk_widget_destroy (GTK_WIDGET(window)); } } diff --git a/gnucash/gnome/dialog-imap-editor.c b/gnucash/gnome/dialog-imap-editor.c index cd4a70bef4..966d603916 100644 --- a/gnucash/gnome/dialog-imap-editor.c +++ b/gnucash/gnome/dialog-imap-editor.c @@ -251,7 +251,7 @@ gnc_imap_dialog_delete (ImapDialog *imap_dialog) list = gtk_tree_selection_get_selected_rows (selection, &fmodel); // Make sure we have some rows selected - if (g_list_length (list) == 0) + if (list == NULL) return; // reset the invalid map total @@ -645,7 +645,7 @@ get_imap_info (ImapDialog *imap_dialog, Account *acc, const gchar *category, con else head = IMAP_FRAME; - if (g_list_length (imap_list) > 0) + if (imap_list != NULL) { PINFO("List length is %d", g_list_length (imap_list)); diff --git a/gnucash/gnome/dialog-invoice.c b/gnucash/gnome/dialog-invoice.c index c25735f01f..4faa55df1a 100644 --- a/gnucash/gnome/dialog-invoice.c +++ b/gnucash/gnome/dialog-invoice.c @@ -3237,7 +3237,7 @@ multi_post_invoice_cb (GtkWindow *dialog, GList *invoice_list, gpointer user_dat gboolean test; InvoiceWindow *iw; - if (g_list_length(invoice_list) == 0) + if (invoice_list == NULL) return; // Get the posting parameters for these invoices iw = gnc_ui_invoice_edit(dialog, invoice_list->data); @@ -3287,7 +3287,7 @@ multi_print_invoice_cb (GtkWindow *dialog, GList *invoice_list, gpointer user_da { struct multi_edit_invoice_data meid; - if (g_list_length(invoice_list) == 0) + if (invoice_list == NULL) return; meid.user_data = user_data; diff --git a/gnucash/gnome/dialog-payment.c b/gnucash/gnome/dialog-payment.c index 6a8cd48ca7..20bb58c509 100644 --- a/gnucash/gnome/dialog-payment.c +++ b/gnucash/gnome/dialog-payment.c @@ -1735,7 +1735,7 @@ static GList *select_txn_lots (GtkWindow *parent, Transaction *txn, Account **po /* If the txn has both APAR splits linked to a business lot and * splits that are not, issue a warning some will be discarded. */ - if (has_no_lot_apar_splits && (g_list_length (txn_lots) > 0)) + if (has_no_lot_apar_splits && (txn_lots != NULL)) { GtkWidget *dialog; char *split_str = g_strdup (""); diff --git a/gnucash/gnome/dialog-price-edit-db.c b/gnucash/gnome/dialog-price-edit-db.c index 70ef0078fd..62511ee8e0 100644 --- a/gnucash/gnome/dialog-price-edit-db.c +++ b/gnucash/gnome/dialog-price-edit-db.c @@ -366,7 +366,7 @@ selection_changed_cb (GtkTreeSelection *selection, gpointer data) PricesDialog *pdb_dialog = data; GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(pdb_dialog->remove_view)); GList *rows = gtk_tree_selection_get_selected_rows (selection, &model); - gboolean have_rows = (g_list_length (rows) > 0 ? TRUE : FALSE); + gboolean have_rows = (rows != NULL); change_source_flag (PRICE_REMOVE_SOURCE_COMM, have_rows, pdb_dialog); g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL); @@ -547,7 +547,7 @@ gnc_prices_dialog_add_clicked (GtkWidget *widget, gpointer data) } else if (comm_list) // selection contains price parent rows { - if (g_list_length (comm_list) == 1) // make sure it is only one parent + if (!comm_list->next) // make sure it is only one parent { price = gnc_price_create (pdb_dialog->book); gnc_price_set_commodity (price, comm_list->data); diff --git a/gnucash/gnome/dialog-sx-editor.c b/gnucash/gnome/dialog-sx-editor.c index b855046a4f..d3d5308cb0 100644 --- a/gnucash/gnome/dialog-sx-editor.c +++ b/gnucash/gnome/dialog-sx-editor.c @@ -579,7 +579,7 @@ gnc_sxed_check_endpoint (GncSxEditorDialog *sxed) g_date_clear (&nextDate, 1); gnc_frequency_save_to_recurrence (sxed->gncfreq, &schedule, &startDate); - if (g_list_length (schedule) > 0) + if (schedule != NULL) { g_date_subtract_days (&startDate, 1); recurrenceListNextInstance (schedule, &startDate, &nextDate); @@ -1766,7 +1766,7 @@ _sx_engine_event_handler (QofInstance *ent, QofEventId event_type, gpointer user book = qof_instance_get_book (QOF_INSTANCE (acct)); affected_sxes = gnc_sx_get_sxes_referencing_account (book, acct); - if (g_list_length (affected_sxes) == 0) + if (affected_sxes == NULL) return; { diff --git a/gnucash/gnome/dialog-sx-editor2.c b/gnucash/gnome/dialog-sx-editor2.c index 5a271e4a8f..6c95643ada 100644 --- a/gnucash/gnome/dialog-sx-editor2.c +++ b/gnucash/gnome/dialog-sx-editor2.c @@ -836,7 +836,7 @@ gnc_sxed_check_consistent (GncSxEditorDialog2 *sxed) g_date_clear (&nextDate, 1); gnc_frequency_save_to_recurrence (sxed->gncfreq, &schedule, &startDate); - if (g_list_length (schedule) > 0) + if (schedule != NULL) { g_date_subtract_days (&startDate, 1); recurrenceListNextInstance (schedule, &startDate, &nextDate); @@ -1704,7 +1704,7 @@ _sx_engine_event_handler (QofInstance *ent, QofEventId event_type, gpointer user book = qof_instance_get_book (QOF_INSTANCE (acct)); affected_sxes = gnc_sx_get_sxes_referencing_account (book, acct); - if (g_list_length (affected_sxes) == 0) + if (affected_sxes == NULL) return; { diff --git a/gnucash/gnome/dialog-sx-since-last-run.c b/gnucash/gnome/dialog-sx-since-last-run.c index 49992a5306..5aba06020f 100644 --- a/gnucash/gnome/dialog-sx-since-last-run.c +++ b/gnucash/gnome/dialog-sx-since-last-run.c @@ -1154,9 +1154,11 @@ dialog_response_cb (GtkDialog *dialog, gint response_id, GncSxSinceLastRunDialog // - [?] ability to create transactions { GList *unbound_variables; + gint unbound_len; unbound_variables = gnc_sx_instance_model_check_variables (app_dialog->editing_model->instances); - PINFO ("%d variables unbound", g_list_length (unbound_variables)); - if (g_list_length (unbound_variables) > 0) + unbound_len = g_list_length (unbound_variables); + PINFO ("%d variables unbound", unbound_len); + if (unbound_len > 0) { // focus first variable GncSxVariableNeeded *first_unbound; diff --git a/gnucash/gnome/gnc-plugin-page-account-tree.c b/gnucash/gnome/gnc-plugin-page-account-tree.c index 3c323480a9..1a6b7ff76b 100644 --- a/gnucash/gnome/gnc-plugin-page-account-tree.c +++ b/gnucash/gnome/gnc-plugin-page-account-tree.c @@ -593,7 +593,7 @@ gnc_plugin_page_account_tree_open (Account *account, GtkWindow *win) page_list = gnc_gobject_tracking_get_list(GNC_PLUGIN_PAGE_ACCOUNT_TREE_NAME); // If we have a window, look for account page in that window - if (g_list_length ((GList*)page_list) != 0) + if (page_list != NULL) { if (win != NULL) { @@ -1500,7 +1500,7 @@ account_subaccount (Account* account) { Account* subaccount = NULL; GList *subs = gnc_account_get_children (account); - if (g_list_length (subs) == 1) + if (subs && !subs->next) subaccount = subs->data; g_list_free (subs); return subaccount; diff --git a/gnucash/gnome/gnc-plugin-page-sx-list.c b/gnucash/gnome/gnc-plugin-page-sx-list.c index e9ce4b6853..ed8507b5cf 100644 --- a/gnucash/gnome/gnc-plugin-page-sx-list.c +++ b/gnucash/gnome/gnc-plugin-page-sx-list.c @@ -756,7 +756,7 @@ gnc_plugin_page_sx_list_cmd_edit (GtkAction *action, GncPluginPageSxList *page) selection = gtk_tree_view_get_selection (priv->tree_view); selected_paths = gtk_tree_selection_get_selected_rows (selection, &model); - if (g_list_length (selected_paths) == 0) + if (selected_paths == NULL) { g_warning ("no selection edit."); return; @@ -792,7 +792,7 @@ gnc_plugin_page_sx_list_cmd_edit2 (GtkAction *action, GncPluginPageSxList *page) selection = gtk_tree_view_get_selection (priv->tree_view); selected_paths = gtk_tree_selection_get_selected_rows (selection, &model); - if (g_list_length (selected_paths) == 0) + if (selected_paths == NULL) { g_warning ("no selection edit."); return; @@ -853,7 +853,7 @@ gnc_plugin_page_sx_list_cmd_delete (GtkAction *action, GncPluginPageSxList *page selection = gtk_tree_view_get_selection (priv->tree_view); selected_paths = gtk_tree_selection_get_selected_rows (selection, &model); - if (g_list_length (selected_paths) == 0) + if (selected_paths == NULL) { g_warning ("no selection for delete."); return; diff --git a/gnucash/register/ledger-core/gnc-ledger-display.c b/gnucash/register/ledger-core/gnc-ledger-display.c index f292c29e1a..3326ddeae5 100644 --- a/gnucash/register/ledger-core/gnc-ledger-display.c +++ b/gnucash/register/ledger-core/gnc-ledger-display.c @@ -429,7 +429,7 @@ gnc_ledger_display_gl (void) tRoot = gnc_book_get_template_root (gnc_get_current_book()); al = gnc_account_get_descendants (tRoot); - if (g_list_length (al) != 0) + if (al != NULL) xaccQueryAddAccountMatch (query, al, QOF_GUID_MATCH_NONE, QOF_QUERY_AND); g_list_free (al); diff --git a/libgnucash/engine/Recurrence.c b/libgnucash/engine/Recurrence.c index 1f8647fc3e..9e2e33e8f5 100644 --- a/libgnucash/engine/Recurrence.c +++ b/libgnucash/engine/Recurrence.c @@ -556,7 +556,7 @@ recurrenceWeekendAdjustFromString(const gchar *str) gboolean recurrenceListIsSemiMonthly(GList *recurrences) { - if (g_list_length(recurrences) != 2) + if (!(recurrences && recurrences->next && !recurrences->next->next)) return FALSE; // should be a "semi-monthly": @@ -879,9 +879,10 @@ recurrenceListCmp(GList *a, GList *b) { Recurrence *most_freq_a, *most_freq_b; - g_return_val_if_fail(g_list_length(a) != 0 && g_list_length(b) != 0, 0); - g_return_val_if_fail(g_list_length(a) != 0, -1); - g_return_val_if_fail(g_list_length(b) != 0, 1); + if (!a) + return (b ? -1 : 0); + else if (!b) + return 1; most_freq_a = (Recurrence*)g_list_nth_data(g_list_sort(a, (GCompareFunc)recurrenceCmp), 0); most_freq_b = (Recurrence*)g_list_nth_data(g_list_sort(b, (GCompareFunc)recurrenceCmp), 0); diff --git a/libgnucash/engine/gncIDSearch.c b/libgnucash/engine/gncIDSearch.c index 058474ec86..449732c87d 100644 --- a/libgnucash/engine/gncIDSearch.c +++ b/libgnucash/engine/gncIDSearch.c @@ -89,7 +89,6 @@ static void * search(QofBook * book, const gchar *id, void * object, GncSearchTy void *c; GList *result; QofQuery *q; - gint len; QofQueryPredData* string_pred_data; PINFO("Type = %d", type); @@ -123,8 +122,7 @@ static void * search(QofBook * book, const gchar *id, void * object, GncSearchTy result = qof_query_run (q); // now compare _exactly_ - len = g_list_length (result); - if (result && (len > 0)) + if (result != NULL) { result = g_list_first (result); diff --git a/libgnucash/engine/qofquery.cpp b/libgnucash/engine/qofquery.cpp index 22793bf43a..f531857f01 100644 --- a/libgnucash/engine/qofquery.cpp +++ b/libgnucash/engine/qofquery.cpp @@ -676,7 +676,7 @@ void qof_query_add_term (QofQuery *q, QofQueryParamList *param_list, qs = qof_query_create (); query_init (qs, qt); - if (qof_query_has_terms (q)) + if (q->terms != NULL) qr = qof_query_merge (q, qs, op); else qr = qof_query_merge (q, qs, QOF_QUERY_OR); @@ -701,7 +701,8 @@ void qof_query_purge_terms (QofQuery *q, QofQueryParamList *param_list) qt = static_cast(_and_->data); if (!param_list_cmp (qt->param_list, param_list)) { - if (g_list_length (static_cast(_or_->data)) == 1) + auto or_list = static_cast (_or_->data); + if (or_list && !or_list->next) { q->terms = g_list_remove_link (static_cast(q->terms), _or_); g_list_free_1 (_or_); @@ -710,7 +711,7 @@ void qof_query_purge_terms (QofQuery *q, QofQueryParamList *param_list) } else { - _or_->data = g_list_remove_link (static_cast(_or_->data), _and_); + _or_->data = g_list_remove_link (or_list, _and_); g_list_free_1 (_and_); _and_ = static_cast(_or_->data); if (!_and_) break; @@ -1137,7 +1138,7 @@ qof_query_merge(QofQuery *q1, QofQuery *q2, QofQueryOp op) * so that the first term added to an empty query doesn't screw up. */ if ((QOF_QUERY_AND == op) && - ( (0 == qof_query_has_terms (q1)) || (0 == qof_query_has_terms (q2)) )) + (q1->terms == NULL || q2->terms == NULL)) { op = QOF_QUERY_OR; }