gnucash maint: Sorting speed-up: Cache the bool value of Transaction's is_closing property.

Christian Stimming cstim at code.gnucash.org
Mon Dec 31 08:51:31 EST 2018


Updated	 via  https://github.com/Gnucash/gnucash/commit/eb9e45bc (commit)
	from  https://github.com/Gnucash/gnucash/commit/1eed3db5 (commit)



commit eb9e45bc20531f936881d2433053716473b37828
Author: Christian Stimming <christian at cstimming.de>
Date:   Mon Dec 31 14:48:26 2018 +0100

    Sorting speed-up: Cache the bool value of Transaction's is_closing property.
    
    This value is queried on each comparison of split or txn sort function,
    which means it is called quite a lot. Avoiding the KVP lookup of this
    property gains a lot in terms of CPU cycles.

diff --git a/libgnucash/engine/Transaction.c b/libgnucash/engine/Transaction.c
index 1466834..a83bc48 100644
--- a/libgnucash/engine/Transaction.c
+++ b/libgnucash/engine/Transaction.c
@@ -273,6 +273,7 @@ gnc_transaction_init(Transaction* trans)
     trans->orig = NULL;
     trans->readonly_reason = NULL;
     trans->reason_cache_valid = FALSE;
+    trans->isClosingTxn_cached = -1;
     LEAVE (" ");
 }
 
@@ -2195,9 +2196,13 @@ xaccTransSetIsClosingTxn (Transaction *trans, gboolean is_closing)
         g_value_init (&v, G_TYPE_INT64);
         g_value_set_int64 (&v, 1);
         qof_instance_set_kvp (QOF_INSTANCE (trans), &v, 1, trans_is_closing_str);
+        trans->isClosingTxn_cached = 1;
     }
     else
+    {
         qof_instance_set_kvp (QOF_INSTANCE (trans), NULL, 1, trans_is_closing_str);
+        trans->isClosingTxn_cached = 0;
+    }
     qof_instance_set_dirty(QOF_INSTANCE(trans));
     xaccTransCommitEdit(trans);
 }
@@ -2353,12 +2358,20 @@ xaccTransGetNotes (const Transaction *trans)
 gboolean
 xaccTransGetIsClosingTxn (const Transaction *trans)
 {
-    GValue v = G_VALUE_INIT;
     if (!trans) return FALSE;
-    qof_instance_get_kvp (QOF_INSTANCE (trans), &v, 1, trans_is_closing_str);
-    if (G_VALUE_HOLDS_INT64 (&v))
-         return g_value_get_int64 (&v);
-    return FALSE;
+    if (trans->isClosingTxn_cached == -1)
+    {
+        Transaction* trans_nonconst = (Transaction*) trans;
+        GValue v = G_VALUE_INIT;
+        qof_instance_get_kvp (QOF_INSTANCE (trans), &v, 1, trans_is_closing_str);
+        if (G_VALUE_HOLDS_INT64 (&v))
+            trans_nonconst->isClosingTxn_cached = (g_value_get_int64 (&v) ? 1 : 0);
+        else
+            trans_nonconst->isClosingTxn_cached = 0;
+    }
+    return (trans->isClosingTxn_cached == 1)
+            ? TRUE
+            : FALSE;
 }
 
 /********************************************************************\
diff --git a/libgnucash/engine/TransactionP.h b/libgnucash/engine/TransactionP.h
index 3679fb4..0c29a92 100644
--- a/libgnucash/engine/TransactionP.h
+++ b/libgnucash/engine/TransactionP.h
@@ -119,6 +119,11 @@ struct transaction_s
      */
     char * readonly_reason;
     gboolean reason_cache_valid;
+
+    /* Cached bool value to indicate whether this is a closing txn. This is
+     * cached from the KVP value because it is queried a lot. Tri-state value: -1
+     * = uninitialized; 0 = FALSE, 1 = TRUE. */
+    gint isClosingTxn_cached;
 };
 
 struct _TransactionClass



Summary of changes:
 libgnucash/engine/Transaction.c  | 23 ++++++++++++++++++-----
 libgnucash/engine/TransactionP.h |  5 +++++
 2 files changed, 23 insertions(+), 5 deletions(-)



More information about the gnucash-changes mailing list