From fe2cdf42c700618d2549ae48a6ada26dd5d3c31e Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sun, 19 May 2024 19:31:54 +0800 Subject: [PATCH] [transaction.cpp] speed up book close --- libgnucash/engine/Transaction.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libgnucash/engine/Transaction.cpp b/libgnucash/engine/Transaction.cpp index dc6d5186bd..d50dce9073 100644 --- a/libgnucash/engine/Transaction.cpp +++ b/libgnucash/engine/Transaction.cpp @@ -3070,6 +3070,12 @@ destroy_tx_on_book_close(QofInstance *ent, gpointer data) xaccTransDestroy(tx); } +static int +trans_reverse_order (const Transaction* a, const Transaction* b) +{ + return xaccTransOrder (b, a); +} + /** Handles book end - frees all transactions from the book * * @param book Book being closed @@ -3080,7 +3086,12 @@ gnc_transaction_book_end(QofBook* book) QofCollection *col; col = qof_book_get_collection(book, GNC_ID_TRANS); - qof_collection_foreach(col, destroy_tx_on_book_close, nullptr); + + // destroy all transactions from latest to earliest, because + // accounts' splits are stored chronologically and removing from + // the end is faster than from the middle. + qof_collection_foreach_sorted (col, destroy_tx_on_book_close, nullptr, + (GCompareFunc)trans_reverse_order); } #ifdef _MSC_VER