glib/gvariant: use g_utf8_validate() for strlen

We can get the length of the string if we provide an out argument to
g_utf8_validate(). This avoids an extra strlen() when creating GVariant
for UTF-8 strings.

This is good for nearly 7% reduction of CPU samples when building
heavily string-based GVariant using GVariantBuilder as a benchmark.
This commit is contained in:
Christian Hergert 2024-09-20 16:14:17 -07:00
parent 84b6f747cb
commit db77480d97

View File

@ -1267,11 +1267,17 @@ g_variant_new_fixed_array (const GVariantType *element_type,
GVariant * GVariant *
g_variant_new_string (const gchar *string) g_variant_new_string (const gchar *string)
{ {
g_return_val_if_fail (string != NULL, NULL); const char *endptr = NULL;
g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL);
return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING, g_return_val_if_fail (string != NULL, NULL);
string, strlen (string) + 1);
if G_LIKELY (g_utf8_validate (string, -1, &endptr))
return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING,
string, endptr - string + 1);
g_critical ("g_variant_new_string(): requires valid UTF-8");
return NULL;
} }
/** /**
@ -1299,17 +1305,25 @@ g_variant_new_string (const gchar *string)
GVariant * GVariant *
g_variant_new_take_string (gchar *string) g_variant_new_take_string (gchar *string)
{ {
GVariant *value; const char *end = NULL;
GBytes *bytes;
g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL);
bytes = g_bytes_new_take (string, strlen (string) + 1); if G_LIKELY (g_utf8_validate (string, -1, &end))
value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE); {
g_bytes_unref (bytes); GVariant *value;
GBytes *bytes;
return value; bytes = g_bytes_new_take (string, end - string + 1);
value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE);
g_bytes_unref (bytes);
return value;
}
g_critical ("g_variant_new_take_string(): requires valid UTF-8");
return NULL;
} }
/** /**