Merge branch 'speed-up-book-close' into stable #1941

This commit is contained in:
Christopher Lam 2024-05-20 08:05:29 +08:00
commit 448e9ac255
3 changed files with 31 additions and 27 deletions

View File

@ -3070,6 +3070,12 @@ destroy_tx_on_book_close(QofInstance *ent, gpointer data)
xaccTransDestroy(tx); 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 /** Handles book end - frees all transactions from the book
* *
* @param book Book being closed * @param book Book being closed
@ -3080,7 +3086,12 @@ gnc_transaction_book_end(QofBook* book)
QofCollection *col; QofCollection *col;
col = qof_book_get_collection(book, GNC_ID_TRANS); 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 #ifdef _MSC_VER

View File

@ -302,40 +302,30 @@ qof_collection_set_data (QofCollection *col, gpointer user_data)
/* =============================================================== */ /* =============================================================== */
struct _qofid_iterate void
qof_collection_foreach_sorted (const QofCollection *col, QofInstanceForeachCB cb_func,
gpointer user_data, GCompareFunc sort_fn)
{ {
QofInstanceForeachCB fcn; GList *entries;
gpointer data;
};
static void g_return_if_fail (col);
foreach_cb (gpointer item, gpointer arg) g_return_if_fail (cb_func);
{
struct _qofid_iterate *iter = static_cast<_qofid_iterate*>(arg);
QofInstance *ent = static_cast<QofInstance*>(item);
iter->fcn (ent, iter->data); PINFO("Hash Table size of %s before is %d", col->e_type, g_hash_table_size(col->hash_of_entities));
entries = g_hash_table_get_values (col->hash_of_entities);
if (sort_fn)
entries = g_list_sort (entries, sort_fn);
g_list_foreach (entries, (GFunc)cb_func, user_data);
g_list_free (entries);
PINFO("Hash Table size of %s after is %d", col->e_type, g_hash_table_size(col->hash_of_entities));
} }
void void
qof_collection_foreach (const QofCollection *col, QofInstanceForeachCB cb_func, qof_collection_foreach (const QofCollection *col, QofInstanceForeachCB cb_func,
gpointer user_data) gpointer user_data)
{ {
struct _qofid_iterate iter; qof_collection_foreach_sorted (col, cb_func, user_data, nullptr);
GList *entries;
g_return_if_fail (col);
g_return_if_fail (cb_func);
iter.fcn = cb_func;
iter.data = user_data;
PINFO("Hash Table size of %s before is %d", col->e_type, g_hash_table_size(col->hash_of_entities));
entries = g_hash_table_get_values (col->hash_of_entities);
g_list_foreach (entries, foreach_cb, &iter);
g_list_free (entries);
PINFO("Hash Table size of %s after is %d", col->e_type, g_hash_table_size(col->hash_of_entities));
} }
/* =============================================================== */ /* =============================================================== */

View File

@ -146,6 +146,9 @@ QofInstance * qof_collection_lookup_entity (const QofCollection *, const GncGUID
typedef void (*QofInstanceForeachCB) (QofInstance *, gpointer user_data); typedef void (*QofInstanceForeachCB) (QofInstance *, gpointer user_data);
/** Call the callback for each entity in the collection. */ /** Call the callback for each entity in the collection. */
void qof_collection_foreach_sorted (const QofCollection *col, QofInstanceForeachCB cb_func,
gpointer user_data, GCompareFunc sort_fn);
void qof_collection_foreach (const QofCollection *, QofInstanceForeachCB, void qof_collection_foreach (const QofCollection *, QofInstanceForeachCB,
gpointer user_data); gpointer user_data);