Merge branch 'gtype-speedups' into 'main'

gtype: small optimization

See merge request GNOME/glib!2672
This commit is contained in:
Matthias Clasen 2022-05-20 13:43:17 +00:00
commit 0a3a0894f9
3 changed files with 27 additions and 8 deletions

View File

@ -1858,13 +1858,13 @@ g_type_create_instance (GType type)
guint i;
node = lookup_type_node_I (type);
if (!node || !node->is_instantiatable)
if (G_UNLIKELY (!node || !node->is_instantiatable))
{
g_error ("cannot create new instance of invalid (non-instantiatable) type '%s'",
type_descriptive_name_I (type));
}
/* G_TYPE_IS_ABSTRACT() is an external call: _U */
if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type))
if (G_UNLIKELY (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (type)))
{
g_error ("cannot create instance of abstract (non-instantiatable) type '%s'",
type_descriptive_name_I (type));
@ -1893,7 +1893,7 @@ g_type_create_instance (GType type)
ivar_size = node->data->instance.instance_size;
#ifdef ENABLE_VALGRIND
if (private_size && RUNNING_ON_VALGRIND)
if (G_UNLIKELY (private_size && RUNNING_ON_VALGRIND))
{
private_size += ALIGN_STRUCT (1);
@ -1963,14 +1963,14 @@ g_type_free_instance (GTypeInstance *instance)
class = instance->g_class;
node = lookup_type_node_I (class->g_type);
if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
if (G_UNLIKELY (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class))
{
g_warning ("cannot free instance of invalid (non-instantiatable) type '%s'",
type_descriptive_name_I (class->g_type));
return;
}
/* G_TYPE_IS_ABSTRACT() is an external call: _U */
if (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
if (G_UNLIKELY (!node->mutatable_check_cache && G_TYPE_IS_ABSTRACT (NODE_TYPE (node))))
{
g_warning ("cannot free instance of abstract (non-instantiatable) type '%s'",
NODE_NAME (node));
@ -1990,7 +1990,7 @@ g_type_free_instance (GTypeInstance *instance)
/* See comment in g_type_create_instance() about what's going on here.
* We're basically unwinding what we put into motion there.
*/
if (private_size && RUNNING_ON_VALGRIND)
if (G_UNLIKELY (private_size && RUNNING_ON_VALGRIND))
{
private_size += ALIGN_STRUCT (1);
allocated -= ALIGN_STRUCT (1);
@ -3577,8 +3577,8 @@ type_node_conforms_to_U (TypeNode *node,
* Returns: %TRUE if @type is a @is_a_type
*/
gboolean
g_type_is_a (GType type,
GType iface_type)
(g_type_is_a) (GType type,
GType iface_type)
{
TypeNode *node, *iface_node;
gboolean is_a;

View File

@ -728,6 +728,10 @@ GType g_type_next_base (GType leaf_type
GLIB_AVAILABLE_IN_ALL
gboolean g_type_is_a (GType type,
GType is_a_type);
/* Hoist exact GType comparisons into the caller */
#define g_type_is_a(a,b) ((a) == (b) || (g_type_is_a) ((a), (b)))
GLIB_AVAILABLE_IN_ALL
gpointer g_type_class_ref (GType type);
GLIB_AVAILABLE_IN_ALL

View File

@ -201,6 +201,20 @@ test_next_base (void)
g_assert (type == G_TYPE_INITIALLY_UNOWNED);
}
/* Test that the macro an function versions of g_type_is_a
* work the same
*/
static void
test_is_a (void)
{
g_assert_true (g_type_is_a (G_TYPE_OBJECT, G_TYPE_OBJECT));
g_assert_true ((g_type_is_a) (G_TYPE_OBJECT, G_TYPE_OBJECT));
g_assert_true (g_type_is_a (bar_get_type (), G_TYPE_OBJECT));
g_assert_true ((g_type_is_a) (bar_get_type (), G_TYPE_OBJECT));
g_assert_false (g_type_is_a (bar_get_type (), bibi_get_type ()));
g_assert_false ((g_type_is_a) (bar_get_type (), bibi_get_type ()));
}
int
main (int argc, char *argv[])
{
@ -210,6 +224,7 @@ main (int argc, char *argv[])
g_test_add_func ("/type/interface-prerequisite", test_interface_prerequisite);
g_test_add_func ("/type/interface-check", test_interface_check);
g_test_add_func ("/type/next-base", test_next_base);
g_test_add_func ("/type/is-a", test_is_a);
return g_test_run ();
}