Use pre-computed namespace GUIDs to load 0.3% faster

This should also help memory fragmentation at startup too as we were
splitting a lot of very small strings.
This commit is contained in:
Richard Hughes 2021-11-03 20:39:51 +00:00
parent 81d0216d14
commit 5e895b89c9
2 changed files with 25 additions and 9 deletions

View File

@ -809,28 +809,39 @@ fwupd_guid_from_string(const gchar *guidstr,
gchar *
fwupd_guid_hash_data(const guint8 *data, gsize datasz, FwupdGuidFlags flags)
{
const gchar *namespace_id = FWUPD_GUID_NAMESPACE_DEFAULT;
gsize digestlen = 20;
guint8 hash[20];
fwupd_guid_t uu_namespace;
fwupd_guid_t uu_new;
g_autoptr(GChecksum) csum = NULL;
const fwupd_guid_t uu_default = {0x6b,
0xa7,
0xb8,
0x10,
0x9d,
0xad,
0x11,
0xd1,
0x80,
0xb4,
0x00,
0xc0,
0x4f,
0xd4,
0x30,
0xc8};
const fwupd_guid_t uu_microso = {0x70, 0xff, 0xd8, 0x12, 0x4c, 0x7f, 0x4c, 0x7d};
const fwupd_guid_t *uu_namespace = &uu_default;
g_return_val_if_fail(namespace_id != NULL, NULL);
g_return_val_if_fail(data != NULL, NULL);
g_return_val_if_fail(datasz != 0, NULL);
/* old MS GUID */
if (flags & FWUPD_GUID_FLAG_NAMESPACE_MICROSOFT)
namespace_id = FWUPD_GUID_NAMESPACE_MICROSOFT;
/* convert the namespace to binary: hardcoded BE, not @flags */
if (!fwupd_guid_from_string(namespace_id, &uu_namespace, FWUPD_GUID_FLAG_NONE, NULL))
return NULL;
uu_namespace = &uu_microso;
/* hash the namespace and then the string */
csum = g_checksum_new(G_CHECKSUM_SHA1);
g_checksum_update(csum, (guchar *)&uu_namespace, sizeof(uu_namespace));
g_checksum_update(csum, (guchar *)uu_namespace, sizeof(*uu_namespace));
g_checksum_update(csum, (guchar *)data, (gssize)datasz);
g_checksum_get_digest(csum, hash, &digestlen);

View File

@ -689,8 +689,10 @@ fwupd_common_device_id_func(void)
static void
fwupd_common_guid_func(void)
{
const guint8 msbuf[] = "hello world!";
g_autofree gchar *guid1 = NULL;
g_autofree gchar *guid2 = NULL;
g_autofree gchar *guid3 = NULL;
g_autofree gchar *guid_be = NULL;
g_autofree gchar *guid_me = NULL;
fwupd_guid_t buf = {0x0};
@ -716,6 +718,9 @@ fwupd_common_guid_func(void)
guid2 = fwupd_guid_hash_string("8086:0406");
g_assert_cmpstr(guid2, ==, "1fbd1f2c-80f4-5d7c-a6ad-35c7b9bd5486");
guid3 = fwupd_guid_hash_data(msbuf, sizeof(msbuf), FWUPD_GUID_FLAG_NAMESPACE_MICROSOFT);
g_assert_cmpstr(guid3, ==, "6836cfac-f77a-527f-b375-4f92f01449c5");
/* round-trip BE */
ret = fwupd_guid_from_string("00112233-4455-6677-8899-aabbccddeeff",
&buf,