From c06191a6562d68a6151561936f487a6176d8045f Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Thu, 5 Dec 2019 10:16:45 +0800 Subject: [PATCH] Transaction.c: avoid slow g_list_nth_data --- libgnucash/engine/Transaction.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libgnucash/engine/Transaction.c b/libgnucash/engine/Transaction.c index 0a22437474..477ecb0344 100644 --- a/libgnucash/engine/Transaction.c +++ b/libgnucash/engine/Transaction.c @@ -676,15 +676,25 @@ Transaction * xaccTransClone (const Transaction *from) { Transaction *to = xaccTransCloneNoKvp (from); - int i = 0; - int length = g_list_length (from->splits); + GList *lfrom, *lto; xaccTransBeginEdit (to); qof_instance_copy_kvp (QOF_INSTANCE (to), QOF_INSTANCE (from)); - g_assert (g_list_length (to->splits) == length); - for (i = 0; i < length; ++i) - xaccSplitCopyKvp (g_list_nth_data (from->splits, i), - g_list_nth_data (to->splits, i)); + g_return_val_if_fail (g_list_length (to->splits) == g_list_length (from->splits), + NULL); + + lfrom = from->splits; + lto = to->splits; + + /* lfrom and lto are known to be of equal length via above + g_return_val_if_fail */ + while (lfrom != NULL) + { + xaccSplitCopyKvp (lfrom->data, lto->data); + lfrom = lfrom->next; + lto = lto->next; + } + xaccTransCommitEdit (to); return to; }