[glib/gobject-speedups2: 2/2] Avoid g_type_peek_value_table
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/gobject-speedups2: 2/2] Avoid g_type_peek_value_table
- Date: Sat, 21 May 2022 12:58:50 +0000 (UTC)
commit 462d5138f62cbb9ab4cc330363c020faf32b4733
Author: Matthias Clasen <mclasen redhat com>
Date: Fri May 20 07:54:35 2022 -0400
Avoid g_type_peek_value_table
In several places we do paired calls of g_value_init
and g_value_unset, both of which peek the value table.
We can avoid half of that cost by remembering the value
table, instead of looking it up again.
This uses the new G_VALUE_COLLECT_INIT2 macro.
gobject/gobject.c | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)
---
diff --git a/gobject/gobject.c b/gobject/gobject.c
index 5c34deb9a4..004d519162 100644
--- a/gobject/gobject.c
+++ b/gobject/gobject.c
@@ -2329,9 +2329,11 @@ g_object_new_valist (GType object_type,
{
GObjectConstructParam params_stack[16];
GValue values_stack[G_N_ELEMENTS (params_stack)];
+ GTypeValueTable *vtabs_stack[G_N_ELEMENTS (params_stack)];
const gchar *name;
GObjectConstructParam *params = params_stack;
GValue *values = values_stack;
+ GTypeValueTable **vtabs = vtabs_stack;
guint n_params = 0;
guint n_params_alloc = G_N_ELEMENTS (params_stack);
@@ -2356,14 +2358,17 @@ g_object_new_valist (GType object_type,
n_params_alloc = G_N_ELEMENTS (params_stack) * 2u;
params = g_new (GObjectConstructParam, n_params_alloc);
values = g_new (GValue, n_params_alloc);
+ vtabs = g_new (GTypeValueTable *, n_params_alloc);
memcpy (params, params_stack, sizeof (GObjectConstructParam) * n_params);
memcpy (values, values_stack, sizeof (GValue) * n_params);
+ memcpy (vtabs, vtabs_stack, sizeof (GTypeValueTable *) * n_params);
}
else
{
n_params_alloc *= 2u;
params = g_realloc (params, sizeof (GObjectConstructParam) * n_params_alloc);
values = g_realloc (values, sizeof (GValue) * n_params_alloc);
+ vtabs = g_realloc (vtabs, sizeof (GTypeValueTable *) * n_params_alloc);
}
for (i = 0; i < n_params; i++)
@@ -2374,7 +2379,7 @@ g_object_new_valist (GType object_type,
params[n_params].value = &values[n_params];
memset (&values[n_params], 0, sizeof (GValue));
- G_VALUE_COLLECT_INIT (&values[n_params], pspec->value_type, var_args, G_VALUE_NOCOPY_CONTENTS,
&error);
+ G_VALUE_COLLECT_INIT2 (&values[n_params], vtabs[n_params], pspec->value_type, var_args,
G_VALUE_NOCOPY_CONTENTS, &error);
if (error)
{
@@ -2391,12 +2396,19 @@ g_object_new_valist (GType object_type,
object = g_object_new_internal (class, params, n_params);
while (n_params--)
- g_value_unset (params[n_params].value);
+ {
+ /* We open-code g_value_unset() here to avoid the
+ * cost of looking up the GTypeValueTable again.
+ */
+ if (vtabs[n_params]->value_free)
+ vtabs[n_params]->value_free (params[n_params].value);
+ }
if (G_UNLIKELY (n_params_alloc != G_N_ELEMENTS (params_stack)))
{
g_free (params);
g_free (values);
+ g_free (vtabs);
}
}
else
@@ -2557,6 +2569,7 @@ g_object_set_valist (GObject *object,
GValue value = G_VALUE_INIT;
GParamSpec *pspec;
gchar *error = NULL;
+ GTypeValueTable *vtab;
pspec = g_param_spec_pool_lookup (pspec_pool,
name,
@@ -2566,7 +2579,7 @@ g_object_set_valist (GObject *object,
if (!g_object_set_is_valid_property (object, pspec, name))
break;
- G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args, G_VALUE_NOCOPY_CONTENTS, &error);
+ G_VALUE_COLLECT_INIT2 (&value, vtab, pspec->value_type, var_args, G_VALUE_NOCOPY_CONTENTS, &error);
if (error)
{
g_warning ("%s: %s", G_STRFUNC, error);
@@ -2576,7 +2589,12 @@ g_object_set_valist (GObject *object,
}
object_set_property (object, pspec, &value, nqueue);
- g_value_unset (&value);
+
+ /* We open-code g_value_unset() here to avoid the
+ * cost of looking up the GTypeValueTable again.
+ */
+ if (vtab->value_free)
+ vtab->value_free (&value);
name = va_arg (var_args, gchar*);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]