gvarianttype: avoid walking type string twice to hash

We already need to walk the string to determine the length of it, so just
hash the string at the same time.
This commit is contained in:
Christian Hergert 2024-09-20 10:32:09 -07:00
parent 9879b7152a
commit 72f217e25a
2 changed files with 33 additions and 12 deletions

View File

@ -64,4 +64,36 @@ _g_variant_type_equal (const GVariantType *type1,
return TRUE;
}
static inline guint
_g_variant_type_hash (gconstpointer type)
{
const gchar *type_string = type;
guint value = 0;
gsize index = 0;
int brackets = 0;
do
{
value = (value << 5) - value + type_string[index];
while (type_string[index] == 'a' || type_string[index] == 'm')
{
index++;
value = (value << 5) - value + type_string[index];
}
if (type_string[index] == '(' || type_string[index] == '{')
brackets++;
else if (type_string[index] == ')' || type_string[index] == '}')
brackets--;
index++;
}
while (brackets);
return value;
}
G_END_DECLS

View File

@ -777,20 +777,9 @@ g_variant_type_is_variant (const GVariantType *type)
guint
g_variant_type_hash (gconstpointer type)
{
const gchar *type_string;
guint value = 0;
gsize length;
gsize i;
g_return_val_if_fail (g_variant_type_check (type), 0);
type_string = g_variant_type_peek_string (type);
length = g_variant_type_get_string_length (type);
for (i = 0; i < length; i++)
value = (value << 5) - value + type_string[i];
return value;
return _g_variant_type_hash (type);
}
/**