From e54e9d808677f4b8fb21a546cc5b1232c492cc48 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sun, 12 May 2024 23:58:44 +0800 Subject: [PATCH] [account.cpp] use HashTable for splits reduces xml loading time from 3.5s to 3.1s, i.e. 11% improvement --- libgnucash/engine/Account.cpp | 12 +++++++----- libgnucash/engine/AccountP.hpp | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index 1290d1157c..1fcd9928d9 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -333,6 +333,7 @@ gnc_account_init(Account* acc) priv->include_sub_account_balances = {}; new (&priv->splits) SplitsVec (); + priv->splits_hash = g_hash_table_new (g_direct_hash, g_direct_equal); priv->sort_dirty = FALSE; } @@ -1470,6 +1471,7 @@ xaccFreeAccount (Account *acc) priv->balance_dirty = FALSE; priv->sort_dirty = FALSE; priv->splits.~SplitsVec(); + g_hash_table_destroy (priv->splits_hash); /* qof_instance_release (&acc->inst); */ g_object_unref(acc); @@ -1556,6 +1558,7 @@ xaccAccountCommitEdit (Account *acc) else { priv->splits.clear(); + g_hash_table_remove_all (priv->splits_hash); } /* It turns out there's a case where this assertion does not hold: @@ -1966,8 +1969,8 @@ gnc_account_insert_split (Account *acc, Split *s) g_return_val_if_fail(GNC_IS_SPLIT(s), FALSE); priv = GET_PRIVATE(acc); - if (std::find (priv->splits.begin(), priv->splits.end(), s) != priv->splits.end()) - return FALSE; + if (!g_hash_table_add (priv->splits_hash, s)) + return false; priv->splits.push_back (s); @@ -1997,10 +2000,9 @@ gnc_account_remove_split (Account *acc, Split *s) priv = GET_PRIVATE(acc); + if (!g_hash_table_remove (priv->splits_hash, s)) + return false; auto it = std::remove (priv->splits.begin(), priv->splits.end(), s); - if (it == priv->splits.end()) - return FALSE; - priv->splits.erase (it, priv->splits.end()); //FIXME: find better event type qof_event_gen(&acc->inst, QOF_EVENT_MODIFY, nullptr); diff --git a/libgnucash/engine/AccountP.hpp b/libgnucash/engine/AccountP.hpp index abd43f69c8..d71f4ef313 100644 --- a/libgnucash/engine/AccountP.hpp +++ b/libgnucash/engine/AccountP.hpp @@ -120,6 +120,7 @@ typedef struct AccountPrivate gboolean balance_dirty; /* balances in splits incorrect */ std::vector splits; /* list of split pointers */ + GHashTable* splits_hash; gboolean sort_dirty; /* sort order of splits is bad */ LotList *lots; /* list of lot pointers */