Skip to content

Commit

Permalink
Merge branch 'speed-up-book-close' into stable #1941
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed May 20, 2024
2 parents 03622b0 + 370a06f commit 448e9ac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
13 changes: 12 additions & 1 deletion libgnucash/engine/Transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
34 changes: 12 additions & 22 deletions libgnucash/engine/qofid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,40 +302,30 @@ qof_collection_set_data (QofCollection *col, gpointer user_data)

/* =============================================================== */

struct _qofid_iterate
{
QofInstanceForeachCB fcn;
gpointer data;
};

static void
foreach_cb (gpointer item, gpointer arg)
{
struct _qofid_iterate *iter = static_cast<_qofid_iterate*>(arg);
QofInstance *ent = static_cast<QofInstance*>(item);

iter->fcn (ent, iter->data);
}

void
qof_collection_foreach (const QofCollection *col, QofInstanceForeachCB cb_func,
gpointer user_data)
qof_collection_foreach_sorted (const QofCollection *col, QofInstanceForeachCB cb_func,
gpointer user_data, GCompareFunc sort_fn)
{
struct _qofid_iterate iter;
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);
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
qof_collection_foreach (const QofCollection *col, QofInstanceForeachCB cb_func,
gpointer user_data)
{
qof_collection_foreach_sorted (col, cb_func, user_data, nullptr);
}
/* =============================================================== */
3 changes: 3 additions & 0 deletions libgnucash/engine/qofid.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ QofInstance * qof_collection_lookup_entity (const QofCollection *, const GncGUID
typedef void (*QofInstanceForeachCB) (QofInstance *, gpointer user_data);

/** 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,
gpointer user_data);

Expand Down

0 comments on commit 448e9ac

Please sign in to comment.