builder: Avoid allocating things we don't use

We pretty often have no parameters at all, so avoid allocating
ObjectProperties+GArray+GPtrArray in that case.
This commit is contained in:
Timm Bäder 2020-06-11 15:07:21 +02:00
parent 0b13e68bca
commit 7c9f2949ce

View File

@ -443,58 +443,69 @@ typedef struct
guint len; guint len;
} ObjectProperties; } ObjectProperties;
static ObjectProperties *
object_properties_new (void) static void
object_properties_init (ObjectProperties *self)
{ {
ObjectProperties *res = g_new (ObjectProperties, 1); self->names = NULL;
self->values = NULL;
res->names = g_ptr_array_new (); self->len = 0;
res->values = g_array_new (FALSE, FALSE, sizeof (GValue));
g_array_set_clear_func (res->values, (GDestroyNotify) g_value_unset);
res->len = 0;
return res;
} }
static void static void
object_properties_free (ObjectProperties *properties) object_properties_destroy (ObjectProperties *self)
{ {
if (properties == NULL) if (self == NULL)
return; return;
g_ptr_array_unref (properties->names); if (self->names)
g_array_unref (properties->values); g_ptr_array_unref (self->names);
g_free (properties); if (self->values)
g_array_unref (self->values);
self->len = 0;
} }
static void static void
object_properties_add (ObjectProperties *properties, object_properties_add (ObjectProperties *self,
const char *name, const char *name,
const GValue *value) const GValue *value)
{ {
g_ptr_array_add (properties->names, (char *) name); if (!self->names)
g_array_append_vals (properties->values, value, 1); self->names = g_ptr_array_new ();
g_assert (properties->names->len == properties->values->len); if (!self->values)
{
self->values = g_array_new (FALSE, FALSE, sizeof (GValue));
g_array_set_clear_func (self->values, (GDestroyNotify) g_value_unset);
}
properties->len += 1; g_ptr_array_add (self->names, (char *) name);
g_array_append_vals (self->values, value, 1);
g_assert (self->names->len == self->values->len);
self->len += 1;
} }
static const char * static const char *
object_properties_get_name (ObjectProperties *properties, object_properties_get_name (const ObjectProperties *self,
guint idx) guint idx)
{ {
return g_ptr_array_index (properties->names, idx); g_assert (self->names);
return g_ptr_array_index (self->names, idx);
} }
static GValue * static GValue *
object_properties_get_value (ObjectProperties *properties, object_properties_get_value (const ObjectProperties *self,
guint idx) guint idx)
{ {
return &g_array_index (properties->values, GValue, idx); g_assert (self->values);
return &g_array_index (self->values, GValue, idx);
} }
static void static void
@ -503,19 +514,14 @@ gtk_builder_get_parameters (GtkBuilder *builder,
const gchar *object_name, const gchar *object_name,
GSList *properties, GSList *properties,
GParamFlags filter_flags, GParamFlags filter_flags,
ObjectProperties **parameters, ObjectProperties *parameters,
ObjectProperties **filtered_parameters) ObjectProperties *filtered_parameters)
{ {
GtkBuilderPrivate *priv = gtk_builder_get_instance_private (builder); GtkBuilderPrivate *priv = gtk_builder_get_instance_private (builder);
GSList *l; GSList *l;
DelayedProperty *property; DelayedProperty *property;
GError *error = NULL; GError *error = NULL;
if (parameters)
*parameters = object_properties_new ();
if (filtered_parameters)
*filtered_parameters = object_properties_new ();
for (l = properties; l; l = l->next) for (l = properties; l; l = l->next)
{ {
PropertyInfo *prop = (PropertyInfo*)l->data; PropertyInfo *prop = (PropertyInfo*)l->data;
@ -591,12 +597,12 @@ gtk_builder_get_parameters (GtkBuilder *builder,
if (prop->pspec->flags & filter_flags) if (prop->pspec->flags & filter_flags)
{ {
if (filtered_parameters) if (filtered_parameters)
object_properties_add (*filtered_parameters, property_name, &property_value); object_properties_add (filtered_parameters, property_name, &property_value);
} }
else else
{ {
if (parameters) if (parameters)
object_properties_add (*parameters, property_name, &property_value); object_properties_add (parameters, property_name, &property_value);
} }
} }
} }
@ -721,7 +727,7 @@ _gtk_builder_construct (GtkBuilder *builder,
GError **error) GError **error)
{ {
GtkBuilderPrivate *priv = gtk_builder_get_instance_private (builder); GtkBuilderPrivate *priv = gtk_builder_get_instance_private (builder);
ObjectProperties *parameters, *construct_parameters; ObjectProperties parameters, construct_parameters;
GObject *obj; GObject *obj;
int i; int i;
GtkBuildableIface *iface; GtkBuildableIface *iface;
@ -763,6 +769,9 @@ _gtk_builder_construct (GtkBuilder *builder,
else else
param_filter_flags = G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY; param_filter_flags = G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY;
object_properties_init (&parameters);
object_properties_init (&construct_parameters);
gtk_builder_get_parameters (builder, info->type, gtk_builder_get_parameters (builder, info->type,
info->id, info->id,
info->properties, info->properties,
@ -783,15 +792,15 @@ _gtk_builder_construct (GtkBuilder *builder,
"Unknown object constructor for %s: %s", "Unknown object constructor for %s: %s",
info->id, info->id,
info->constructor); info->constructor);
object_properties_free (parameters); object_properties_destroy (&parameters);
object_properties_free (construct_parameters); object_properties_destroy (&construct_parameters);
return NULL; return NULL;
} }
obj = gtk_buildable_construct_child (GTK_BUILDABLE (constructor), obj = gtk_buildable_construct_child (GTK_BUILDABLE (constructor),
builder, builder,
info->id); info->id);
g_assert (obj != NULL); g_assert (obj != NULL);
if (construct_parameters->len) if (construct_parameters.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 &&
@ -802,22 +811,25 @@ _gtk_builder_construct (GtkBuilder *builder,
obj = gtk_builder_get_internal_child (builder, info, childname, error); obj = gtk_builder_get_internal_child (builder, info, childname, error);
if (!obj) if (!obj)
{ {
object_properties_free (parameters); object_properties_destroy (&parameters);
object_properties_free (construct_parameters); object_properties_destroy (&construct_parameters);
return NULL; return NULL;
} }
if (construct_parameters->len) if (construct_parameters.len > 0)
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);
} }
else else
{ {
ensure_special_construct_parameters (builder, info->type, construct_parameters); ensure_special_construct_parameters (builder, info->type, &construct_parameters);
obj = g_object_new_with_properties (info->type, if (construct_parameters.len > 0)
construct_parameters->len, obj = g_object_new_with_properties (info->type,
(const char **) construct_parameters->names->pdata, construct_parameters.len,
(GValue *) construct_parameters->values->data); (const char **) construct_parameters.names->pdata,
(GValue *) construct_parameters.values->data);
else
obj = g_object_new (info->type, NULL);
/* No matter what, make sure we have a reference. /* No matter what, make sure we have a reference.
* *
@ -834,7 +846,7 @@ _gtk_builder_construct (GtkBuilder *builder,
GTK_NOTE (BUILDER, GTK_NOTE (BUILDER,
g_message ("created %s of type %s", info->id, g_type_name (info->type))); g_message ("created %s of type %s", info->id, g_type_name (info->type)));
} }
object_properties_free (construct_parameters); object_properties_destroy (&construct_parameters);
custom_set_property = FALSE; custom_set_property = FALSE;
buildable = NULL; buildable = NULL;
@ -847,10 +859,10 @@ _gtk_builder_construct (GtkBuilder *builder,
custom_set_property = TRUE; custom_set_property = TRUE;
} }
for (i = 0; i < parameters->len; i++) for (i = 0; i < parameters.len; i++)
{ {
const char *name = object_properties_get_name (parameters, i); const char *name = object_properties_get_name (&parameters, i);
const GValue *value = object_properties_get_value (parameters, i); const GValue *value = object_properties_get_value (&parameters, i);
if (custom_set_property) if (custom_set_property)
iface->set_buildable_property (buildable, builder, name, value); iface->set_buildable_property (buildable, builder, name, value);
@ -866,7 +878,7 @@ _gtk_builder_construct (GtkBuilder *builder,
} }
#endif #endif
} }
object_properties_free (parameters); object_properties_destroy (&parameters);
if (info->bindings) if (info->bindings)
gtk_builder_take_bindings (builder, obj, info->bindings); gtk_builder_take_bindings (builder, obj, info->bindings);
@ -885,7 +897,7 @@ _gtk_builder_apply_properties (GtkBuilder *builder,
ObjectInfo *info, ObjectInfo *info,
GError **error) GError **error)
{ {
ObjectProperties *parameters; ObjectProperties parameters;
GtkBuildableIface *iface; GtkBuildableIface *iface;
GtkBuildable *buildable; GtkBuildable *buildable;
gboolean custom_set_property; gboolean custom_set_property;
@ -894,6 +906,8 @@ _gtk_builder_apply_properties (GtkBuilder *builder,
g_assert (info->object != NULL); g_assert (info->object != NULL);
g_assert (info->type != G_TYPE_INVALID); g_assert (info->type != G_TYPE_INVALID);
object_properties_init (&parameters);
/* Fetch all properties that are not construct-only */ /* Fetch all properties that are not construct-only */
gtk_builder_get_parameters (builder, info->type, gtk_builder_get_parameters (builder, info->type,
info->id, info->id,
@ -901,6 +915,7 @@ _gtk_builder_apply_properties (GtkBuilder *builder,
G_PARAM_CONSTRUCT_ONLY, G_PARAM_CONSTRUCT_ONLY,
&parameters, NULL); &parameters, NULL);
custom_set_property = FALSE; custom_set_property = FALSE;
buildable = NULL; buildable = NULL;
iface = NULL; iface = NULL;
@ -912,10 +927,10 @@ _gtk_builder_apply_properties (GtkBuilder *builder,
custom_set_property = TRUE; custom_set_property = TRUE;
} }
for (i = 0; i < parameters->len; i++) for (i = 0; i < parameters.len; i++)
{ {
const char *name = object_properties_get_name (parameters, i); const char *name = object_properties_get_name (&parameters, i);
const GValue *value = object_properties_get_value (parameters, i); const GValue *value = object_properties_get_value (&parameters, i);
if (custom_set_property) if (custom_set_property)
iface->set_buildable_property (buildable, builder, name, value); iface->set_buildable_property (buildable, builder, name, value);
else else
@ -930,7 +945,7 @@ _gtk_builder_apply_properties (GtkBuilder *builder,
} }
#endif #endif
} }
object_properties_free (parameters); object_properties_destroy (&parameters);
} }
void void