builder: Small optimization

Use g_object_setv where we can.

It would be much nicer if we could pass the
pspecs we already have, and avoid having GObject
look them up again.
This commit is contained in:
Matthias Clasen 2021-09-19 22:30:34 -04:00
parent 5c3bb42612
commit 138fd6f0f3

View File

@ -459,7 +459,6 @@ typedef struct
{ {
GPtrArray *names; GPtrArray *names;
GArray *values; GArray *values;
guint len;
} ObjectProperties; } ObjectProperties;
@ -468,8 +467,6 @@ object_properties_init (ObjectProperties *self)
{ {
self->names = NULL; self->names = NULL;
self->values = NULL; self->values = NULL;
self->len = 0;
} }
static void static void
@ -483,8 +480,6 @@ object_properties_destroy (ObjectProperties *self)
if (self->values) if (self->values)
g_array_unref (self->values); g_array_unref (self->values);
self->len = 0;
} }
static void static void
@ -493,10 +488,8 @@ object_properties_add (ObjectProperties *self,
const GValue *value) const GValue *value)
{ {
if (!self->names) if (!self->names)
self->names = g_ptr_array_sized_new (8);
if (!self->values)
{ {
self->names = g_ptr_array_sized_new (8);
self->values = g_array_sized_new (FALSE, FALSE, sizeof (GValue), 8); self->values = g_array_sized_new (FALSE, FALSE, sizeof (GValue), 8);
g_array_set_clear_func (self->values, (GDestroyNotify) g_value_unset); g_array_set_clear_func (self->values, (GDestroyNotify) g_value_unset);
} }
@ -505,8 +498,6 @@ object_properties_add (ObjectProperties *self,
g_array_append_vals (self->values, value, 1); g_array_append_vals (self->values, value, 1);
g_assert (self->names->len == self->values->len); g_assert (self->names->len == self->values->len);
self->len += 1;
} }
static const char * static const char *
@ -566,12 +557,12 @@ gtk_builder_get_parameters (GtkBuilder *builder,
continue; continue;
} }
else if (G_IS_PARAM_SPEC_OBJECT (prop->pspec) && else if (G_IS_PARAM_SPEC_OBJECT (prop->pspec) &&
(G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != GDK_TYPE_PIXBUF) && (G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != GDK_TYPE_PIXBUF) &&
(G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != GDK_TYPE_TEXTURE) && (G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != GDK_TYPE_TEXTURE) &&
(G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != GDK_TYPE_PAINTABLE) && (G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != GDK_TYPE_PAINTABLE) &&
(G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != GTK_TYPE_SHORTCUT_TRIGGER) && (G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != GTK_TYPE_SHORTCUT_TRIGGER) &&
(G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != GTK_TYPE_SHORTCUT_ACTION) && (G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != GTK_TYPE_SHORTCUT_ACTION) &&
(G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != G_TYPE_FILE)) (G_PARAM_SPEC_VALUE_TYPE (prop->pspec) != G_TYPE_FILE))
{ {
GObject *object = g_hash_table_lookup (priv->objects, GObject *object = g_hash_table_lookup (priv->objects,
g_strstrip (prop->text->str)); g_strstrip (prop->text->str));
@ -751,9 +742,6 @@ _gtk_builder_construct (GtkBuilder *builder,
ObjectProperties parameters, construct_parameters; ObjectProperties parameters, construct_parameters;
GObject *obj; GObject *obj;
int i; int i;
GtkBuildableIface *iface;
gboolean custom_set_property;
GtkBuildable *buildable;
GParamFlags param_filter_flags; GParamFlags param_filter_flags;
g_assert (info->type != G_TYPE_INVALID); g_assert (info->type != G_TYPE_INVALID);
@ -821,7 +809,7 @@ _gtk_builder_construct (GtkBuilder *builder,
builder, builder,
info->id); info->id);
g_assert (obj != NULL); g_assert (obj != NULL);
if (construct_parameters.len > 0) if (construct_parameters.names->len > 0)
g_warning ("Can't pass in construct-only parameters to %s", info->id); g_warning ("Can't pass in construct-only parameters to %s", info->id);
} }
else if (info->parent && else if (info->parent &&
@ -836,7 +824,7 @@ _gtk_builder_construct (GtkBuilder *builder,
object_properties_destroy (&construct_parameters); object_properties_destroy (&construct_parameters);
return NULL; return NULL;
} }
if (construct_parameters.len > 0) if (construct_parameters.names)
g_warning ("Can't pass in construct-only parameters to %s", childname); g_warning ("Can't pass in construct-only parameters to %s", childname);
g_object_ref (obj); g_object_ref (obj);
} }
@ -844,9 +832,9 @@ _gtk_builder_construct (GtkBuilder *builder,
{ {
ensure_special_construct_parameters (builder, info->type, &construct_parameters); ensure_special_construct_parameters (builder, info->type, &construct_parameters);
if (construct_parameters.len > 0) if (construct_parameters.names)
obj = g_object_new_with_properties (info->type, obj = g_object_new_with_properties (info->type,
construct_parameters.len, construct_parameters.names->len,
(const char **) construct_parameters.names->pdata, (const char **) construct_parameters.names->pdata,
(GValue *) construct_parameters.values->data); (GValue *) construct_parameters.values->data);
else else
@ -869,36 +857,60 @@ _gtk_builder_construct (GtkBuilder *builder,
} }
object_properties_destroy (&construct_parameters); object_properties_destroy (&construct_parameters);
custom_set_property = FALSE; if (parameters.names)
buildable = NULL;
iface = NULL;
if (GTK_IS_BUILDABLE (obj))
{ {
buildable = GTK_BUILDABLE (obj); GtkBuildableIface *iface = NULL;
iface = GTK_BUILDABLE_GET_IFACE (obj); gboolean custom_set_property = FALSE;
if (iface->set_buildable_property) GtkBuildable *buildable = NULL;
custom_set_property = TRUE;
}
for (i = 0; i < parameters.len; i++) if (GTK_IS_BUILDABLE (obj))
{ {
const char *name = object_properties_get_name (&parameters, i); buildable = GTK_BUILDABLE (obj);
const GValue *value = object_properties_get_value (&parameters, i); iface = GTK_BUILDABLE_GET_IFACE (obj);
if (iface->set_buildable_property)
custom_set_property = TRUE;
}
if (custom_set_property) if (custom_set_property)
iface->set_buildable_property (buildable, builder, name, value);
else
g_object_set_property (obj, name, value);
#ifdef G_ENABLE_DEBUG
if (GTK_DEBUG_CHECK (BUILDER))
{ {
char *str = g_strdup_value_contents (value); for (i = 0; i < parameters.names->len; i++)
g_message ("set %s: %s = %s", info->id, name, str); {
g_free (str); const char *name = object_properties_get_name (&parameters, i);
} const GValue *value = object_properties_get_value (&parameters, i);
iface->set_buildable_property (buildable, builder, name, value);
#ifdef G_ENABLE_DEBUG
if (GTK_DEBUG_CHECK (BUILDER))
{
char *str = g_strdup_value_contents (value);
g_message ("set %s: %s = %s", info->id, name, str);
g_free (str);
}
#endif #endif
}
}
else
{
g_object_setv (obj,
parameters.names->len,
(const char **) parameters.names->pdata,
(GValue *) parameters.values->data);
#ifdef G_ENABLE_DEBUG
if (GTK_DEBUG_CHECK (BUILDER))
{
for (i = 0; i < parameters.names->len; i++)
{
const char *name = object_properties_get_name (&parameters, i);
const GValue *value = object_properties_get_value (&parameters, i);
char *str = g_strdup_value_contents (value);
g_message ("set %s: %s = %s", info->id, name, str);
g_free (str);
}
}
#endif
}
} }
object_properties_destroy (&parameters); object_properties_destroy (&parameters);
if (info->bindings) if (info->bindings)
@ -919,10 +931,6 @@ _gtk_builder_apply_properties (GtkBuilder *builder,
GError **error) GError **error)
{ {
ObjectProperties parameters; ObjectProperties parameters;
GtkBuildableIface *iface;
GtkBuildable *buildable;
gboolean custom_set_property;
int i;
g_assert (info->object != NULL); g_assert (info->object != NULL);
g_assert (info->type != G_TYPE_INVALID); g_assert (info->type != G_TYPE_INVALID);
@ -936,36 +944,60 @@ _gtk_builder_apply_properties (GtkBuilder *builder,
G_PARAM_CONSTRUCT_ONLY, G_PARAM_CONSTRUCT_ONLY,
&parameters, NULL); &parameters, NULL);
if (parameters.names)
custom_set_property = FALSE;
buildable = NULL;
iface = NULL;
if (GTK_IS_BUILDABLE (info->object))
{ {
buildable = GTK_BUILDABLE (info->object); GtkBuildableIface *iface = NULL;
iface = GTK_BUILDABLE_GET_IFACE (info->object); GtkBuildable *buildable = NULL;
if (iface->set_buildable_property) gboolean custom_set_property = FALSE;
custom_set_property = TRUE; int i;
}
for (i = 0; i < parameters.len; i++) if (GTK_IS_BUILDABLE (info->object))
{
const char *name = object_properties_get_name (&parameters, i);
const GValue *value = object_properties_get_value (&parameters, i);
if (custom_set_property)
iface->set_buildable_property (buildable, builder, name, value);
else
g_object_set_property (info->object, name, value);
#ifdef G_ENABLE_DEBUG
if (GTK_DEBUG_CHECK (BUILDER))
{ {
char *str = g_strdup_value_contents (value); buildable = GTK_BUILDABLE (info->object);
g_message ("set %s: %s = %s", info->id, name, str); iface = GTK_BUILDABLE_GET_IFACE (info->object);
g_free (str); if (iface->set_buildable_property)
custom_set_property = TRUE;
} }
if (custom_set_property)
{
for (i = 0; i < parameters.names->len; i++)
{
const char *name = object_properties_get_name (&parameters, i);
const GValue *value = object_properties_get_value (&parameters, i);
iface->set_buildable_property (buildable, builder, name, value);
#ifdef G_ENABLE_DEBUG
if (GTK_DEBUG_CHECK (BUILDER))
{
char *str = g_strdup_value_contents (value);
g_message ("set %s: %s = %s", info->id, name, str);
g_free (str);
}
#endif #endif
}
}
else
{
g_object_setv (info->object,
parameters.names->len,
(const char **) parameters.names->pdata,
(GValue *) parameters.values->data);
#ifdef G_ENABLE_DEBUG
if (GTK_DEBUG_CHECK (BUILDER))
{
for (i = 0; i < parameters.names->len; i++)
{
const char *name = object_properties_get_name (&parameters, i);
const GValue *value = object_properties_get_value (&parameters, i);
char *str = g_strdup_value_contents (value);
g_message ("set %s: %s = %s", info->id, name, str);
g_free (str);
}
}
#endif
}
} }
object_properties_destroy (&parameters); object_properties_destroy (&parameters);
} }