From 349c208a6c68a9d57c2d0ad9eb16b3b2e337ca66 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Fri, 5 Dec 2025 00:16:28 +0800 Subject: [PATCH] [guid.cpp] GncGUID* avoids temporary gnc::GUID, and better hash --- libgnucash/engine/guid.cpp | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) 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 (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(ptr); + guint rv; + memcpy (&rv, &g->reserved[12], sizeof (guint)); + return rv; } gint