gobject: Read the toggle reference state only after we've update the references

We were reading if an object has toggle references even if this was not
really relevant for the current object state, as we only need to notify
when going from 2 to 1 references, so first ensure that this is the case
and then check if we have toggle references enabled in the object.

This is a micro-optimization, for the way flags are defined, but still
an operation we can avoid in most cases.
This commit is contained in:
Marco Trevisan (Treviño) 2022-11-29 21:36:48 +01:00
parent 65303537b0
commit c0360f626c

View File

@ -3794,7 +3794,6 @@ g_object_unref (gpointer _object)
while (old_ref > 1) while (old_ref > 1)
{ {
/* valid if last 2 refs are owned by this call to unref and the toggle_ref */ /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
if (!g_atomic_int_compare_and_exchange_full ((int *)&object->ref_count, if (!g_atomic_int_compare_and_exchange_full ((int *)&object->ref_count,
old_ref, old_ref - 1, old_ref, old_ref - 1,
@ -3804,8 +3803,11 @@ g_object_unref (gpointer _object)
TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref)); TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
/* if we went from 2->1 we need to notify toggle refs if any */ /* if we went from 2->1 we need to notify toggle refs if any */
if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */ if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
toggle_refs_notify (object, TRUE); {
/* The last ref being held in this case is owned by the toggle_ref */
toggle_refs_notify (object, TRUE);
}
return; return;
} }