gvariant: Remove redundant g_variant_serialised_n_children() calls

These functions were previously calling
`g_variant_serialised_n_children()` twice just to validate the input, in
the case that the input was a serialised variant.

That’s not necessary, and checking the number of children in a
serialised variant is not necessarily cheap.

Move the checks around so that the number of children is only checked
once on each code path. This doesn’t introduce any functional changes.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2022-12-29 18:53:35 +00:00
parent a1213e343e
commit fa17060027

View File

@ -1138,11 +1138,13 @@ GVariant *
g_variant_get_child_value (GVariant *value,
gsize index_)
{
g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
g_return_val_if_fail (value->depth < G_MAXSIZE, NULL);
if (~g_atomic_int_get (&value->state) & STATE_SERIALISED)
{
/* g_variant_serialised_get_child() does its own checks on index_ */
g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
g_variant_lock (value);
if (~value->state & STATE_SERIALISED)
@ -1233,11 +1235,13 @@ GVariant *
g_variant_maybe_get_child_value (GVariant *value,
gsize index_)
{
g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
g_return_val_if_fail (value->depth < G_MAXSIZE, NULL);
if (~g_atomic_int_get (&value->state) & STATE_SERIALISED)
{
/* g_variant_serialised_get_child() does its own checks on index_ */
g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
g_variant_lock (value);
if (~value->state & STATE_SERIALISED)