gnucash stable: [guid.cpp] GncGUID* avoids temporary gnc::GUID, and better hash

Christopher Lam clam at code.gnucash.org
Mon Dec 8 17:16:54 EST 2025


Updated	 via  https://github.com/Gnucash/gnucash/commit/349c208a (commit)
	from  https://github.com/Gnucash/gnucash/commit/6907d364 (commit)



commit 349c208a6c68a9d57c2d0ad9eb16b3b2e337ca66
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Fri Dec 5 00:16:28 2025 +0800

    [guid.cpp] GncGUID* avoids temporary gnc::GUID, and better hash

diff --git a/libgnucash/engine/guid.cpp b/libgnucash/engine/guid.cpp
index df4cd8d77a..946f3c1d03 100644
--- a/libgnucash/engine/guid.cpp
+++ b/libgnucash/engine/guid.cpp
@@ -236,11 +236,7 @@ string_to_guid (const char * str, GncGUID * guid)
 gboolean
 guid_equal (const GncGUID *guid_1, const GncGUID *guid_2)
 {
-    if (guid_1 == guid_2) return true;
-    if (!guid_1 || !guid_2) return false;
-    gnc::GUID temp1 {*guid_1};
-    gnc::GUID temp2 {*guid_2};
-    return temp1 == temp2;
+    return guid_compare (guid_1, guid_2) == 0;
 }
 
 gint
@@ -249,15 +245,12 @@ guid_compare (const GncGUID *guid_1, const GncGUID *guid_2)
     if (guid_1 == guid_2) return 0;
     if (!guid_1) return -1;
     if (!guid_2) return 1;
-    gnc::GUID temp1 {*guid_1};
-    gnc::GUID temp2 {*guid_2};
-    if (temp1 < temp2)
-        return -1;
-    if (temp1 == temp2)
-        return 0;
-    return 1;
+    return std::memcmp (guid_1->reserved, guid_2->reserved, GUID_DATA_SIZE);
 }
 
+// returns a 32-bit hash from 32-byte guid. since guid are generated
+// randomly, this is not expected to cause hash collisions. use memcpy
+// to avoid alignment issues; memcpy likely to be optimised away.
 guint
 guid_hash_to_guint (gconstpointer ptr)
 {
@@ -266,15 +259,10 @@ guid_hash_to_guint (gconstpointer ptr)
         PERR ("received nullptr guid pointer.");
         return 0;
     }
-    GncGUID const & guid = * reinterpret_cast <GncGUID const *> (ptr);
-    gnc::GUID const & temp {guid};
-
-    guint hash {0};
-    std::for_each (temp.begin (), temp.end (), [&hash] (unsigned char a) {
-        hash <<=4;
-        hash |= a;
-    });
-    return hash;
+    const GncGUID* g = static_cast<const GncGUID*>(ptr);
+    guint rv;
+    memcpy (&rv, &g->reserved[12], sizeof (guint));
+    return rv;
 }
 
 gint



Summary of changes:
 libgnucash/engine/guid.cpp | 30 +++++++++---------------------
 1 file changed, 9 insertions(+), 21 deletions(-)



More information about the gnucash-changes mailing list