Skip to content

Commit 3ab5150

Browse files
[Transaction.hpp] restore trans->is_closing_cached struct member
because trans->is_closing is heavily accessed during account balance calculations, and it's reasonable to cache the kvp value.
1 parent 7463b40 commit 3ab5150

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

libgnucash/engine/Transaction.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ gnc_transaction_init(Transaction* trans)
273273
trans->marker = 0;
274274
trans->orig = nullptr;
275275
trans->txn_type = TXN_TYPE_UNCACHED;
276+
trans->is_closing_cached = -1;
276277
LEAVE (" ");
277278
}
278279

@@ -2129,6 +2130,7 @@ xaccTransSetIsClosingTxn (Transaction *trans, gboolean is_closing)
21292130
{
21302131
if (!trans) return;
21312132
xaccTransBeginEdit(trans);
2133+
trans->is_closing_cached = is_closing ? 1 : 0;
21322134

21332135
if (is_closing)
21342136
{
@@ -2337,16 +2339,15 @@ xaccTransGetIsClosingTxn (const Transaction *trans)
23372339
{
23382340
if (!trans) return FALSE;
23392341

2340-
GValue v = G_VALUE_INIT;
2341-
gboolean rv;
2342-
qof_instance_get_kvp (QOF_INSTANCE (trans), &v, 1, trans_is_closing_str);
2343-
if (G_VALUE_HOLDS_INT64 (&v))
2344-
rv = (g_value_get_int64 (&v) ? 1 : 0);
2345-
else
2346-
rv = 0;
2347-
g_value_unset (&v);
2348-
2349-
return rv;
2342+
if (trans->is_closing_cached == -1)
2343+
{
2344+
auto t = const_cast<Transaction*>(trans);
2345+
GValue v = G_VALUE_INIT;
2346+
qof_instance_get_kvp (QOF_INSTANCE (trans), &v, 1, trans_is_closing_str);
2347+
t->is_closing_cached = G_VALUE_HOLDS_INT64 (&v) && g_value_get_int64 (&v) ? 1 : 0;
2348+
g_value_unset (&v);
2349+
}
2350+
return trans->is_closing_cached != 0;
23502351
}
23512352

23522353
/********************************************************************\

libgnucash/engine/TransactionP.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ struct transaction_s
116116
*/
117117
char txn_type;
118118

119+
signed char is_closing_cached;
119120
};
120121

121122
struct _TransactionClass

libgnucash/engine/test/utest-Transaction.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,15 +1778,13 @@ test_xaccTransGetReadOnly (Fixture *fixture, gconstpointer pData)
17781778
* xaccTransSetDescription C: 20 in 18 SCM: 5 in 3 Local: 2:0:0
17791779
* qofTransSetNotes Local: 0:0:0
17801780
* xaccTransSetNotes C: 5 in 5 SCM: 3 in 3 Local: 1:0:0
1781-
* xaccTransSetIsClosingTxn C: 1 Local: 0:0:0
17821781
* xaccTransGetSplit C: 57 in 24 SCM: 30 in 21 Local: 0:0:0
17831782
* xaccTransGetSplitIndex C: 7 in 2 Local: 0:0:0
17841783
* xaccTransGetSplitList C: 23 in 15 SCM: 19 in 15 Local: 2:1:0
17851784
* xaccTransCountSplits C: 17 in 9 SCM: 2 in 2 Local: 0:0:0
17861785
* xaccTransGetNum C: 15 in 12 SCM: 13 in 13 Local: 0:1:0
17871786
* xaccTransGetDescription C: 43 in 23 SCM: 9 in 9 Local: 0:2:0
17881787
* xaccTransGetNotes C: 8 in 6 SCM: 7 in 7 Local: 0:1:0
1789-
* xaccTransGetIsClosingTxn SCM: 1 Local: 0:1:0
17901788
* xaccTransGetDate C: 42 in 19 Local: 0:0:0
17911789
* xaccTransGetDatePostedTS C: 6 in 5 Local: 1:0:0
17921790
* xaccTransGetDateEnteredTS C: 1 Local: 0:0:0
@@ -1796,6 +1794,20 @@ test_xaccTransGetReadOnly (Fixture *fixture, gconstpointer pData)
17961794
* xaccTransGetDateDueTS C: 1 Local: 1:0:0
17971795
* xaccTransRetDateDueTS C: 1 SCM: 2 in 2 Local: 0:1:0
17981796
* xaccTransGetTxnType C: 3 in 2 SCM: 12 in 6 Local: 0:1:0*/
1797+
1798+
static void
1799+
test_xaccTrans_IsClosing (Fixture *fixture, gconstpointer pData)
1800+
{
1801+
auto txn = fixture->txn;
1802+
g_assert_false (xaccTransGetIsClosingTxn (txn));
1803+
1804+
xaccTransSetIsClosingTxn (txn, TRUE);
1805+
g_assert_true (xaccTransGetIsClosingTxn (txn));
1806+
1807+
xaccTransSetIsClosingTxn (txn, FALSE);
1808+
g_assert_false (xaccTransGetIsClosingTxn (txn));
1809+
}
1810+
17991811
static void
18001812
test_xaccTransGetTxnType (Fixture *fixture, gconstpointer pData)
18011813
{
@@ -2049,6 +2061,7 @@ test_suite_transaction (void)
20492061
GNC_TEST_ADD (suitename, "xaccTransRollbackEdit - Backend Errors", Fixture, NULL, setup, test_xaccTransRollbackEdit_BackendErrors, teardown);
20502062
GNC_TEST_ADD (suitename, "xaccTransOrder_num_action", Fixture, NULL, setup, test_xaccTransOrder_num_action, teardown);
20512063
GNC_TEST_ADD (suitename, "xaccTransGetTxnType", Fixture, NULL, setup, test_xaccTransGetTxnType, teardown);
2064+
GNC_TEST_ADD (suitename, "xaccTransIsClosing", Fixture, NULL, setup, test_xaccTrans_IsClosing, teardown);
20522065
GNC_TEST_ADD (suitename, "xaccTransGetreadOnly", Fixture, NULL, setup, test_xaccTransGetReadOnly, teardown);
20532066
GNC_TEST_ADD (suitename, "xaccTransSetDocLink", Fixture, NULL, setup, test_xaccTransSetDocLink, teardown);
20542067
GNC_TEST_ADD (suitename, "xaccTransVoid", Fixture, NULL, setup, test_xaccTransVoid, teardown);

0 commit comments

Comments
 (0)