diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c index 447303dfd..2cdca0971 100644 --- a/gtk/gtkwidget.c +++ b/gtk/gtkwidget.c @@ -380,6 +380,20 @@ static guint composite_child_stack = 0; static GtkTextDirection gtk_default_direction = GTK_TEXT_DIR_LTR; static GParamSpecPool *style_property_spec_pool = NULL; +/* Widgets ask for the same few style properties of the same few types + * at every size request and expose, and a lookup by name walks the + * class tree with a hash lookup per level. The answers are kept here, + * by type and name pointer, until a style property is added or removed. */ +#define STYLE_PROPERTY_CACHE_SIZE 128 +typedef struct +{ + GType type; + const gchar *name; + GParamSpec *pspec; + GtkRcPropertyParser parser; +} StylePropertyCacheEntry; +static StylePropertyCacheEntry style_property_cache[STYLE_PROPERTY_CACHE_SIZE]; + static GQuark quark_property_parser = 0; static GQuark quark_aux_info = 0; static GQuark quark_accel_path = 0; @@ -2638,6 +2652,8 @@ gtk_widget_base_class_finalize (GtkWidgetClass *klass) GList *list, *node; list = g_param_spec_pool_list_owned (style_property_spec_pool, G_OBJECT_CLASS_TYPE (klass)); + if (list) + memset (style_property_cache, 0, sizeof (style_property_cache)); for (node = list; node; node = node->next) { GParamSpec *pspec = node->data; @@ -9842,6 +9858,7 @@ gtk_widget_class_install_style_property_parser (GtkWidgetClass *klass, g_param_spec_ref_sink (pspec); g_param_spec_set_qdata (pspec, quark_property_parser, (gpointer) parser); g_param_spec_pool_insert (style_property_spec_pool, pspec, G_OBJECT_CLASS_TYPE (klass)); + memset (style_property_cache, 0, sizeof (style_property_cache)); } /** @@ -9917,6 +9934,39 @@ gtk_widget_class_list_style_properties (GtkWidgetClass *klass, return pspecs; } +static GParamSpec * +style_property_lookup (GType type, + const gchar *name, + GtkRcPropertyParser *parser) +{ + StylePropertyCacheEntry *entry; + GParamSpec *pspec; + + entry = &style_property_cache[(((gsize) name >> 3) ^ ((gsize) type >> 2)) % STYLE_PROPERTY_CACHE_SIZE]; + if (entry->type == type && entry->name == name && + strcmp (name, entry->pspec->name) == 0) + { + *parser = entry->parser; + return entry->pspec; + } + + pspec = g_param_spec_pool_lookup (style_property_spec_pool, name, type, TRUE); + if (!pspec) + return NULL; + + *parser = (GtkRcPropertyParser) g_param_spec_get_qdata (pspec, quark_property_parser); + /* names in another spelling or with a type prefix are looked up again */ + if (strcmp (name, pspec->name) == 0) + { + entry->type = type; + entry->name = name; + entry->pspec = pspec; + entry->parser = *parser; + } + + return pspec; +} + /** * gtk_widget_style_get_property: * @widget: a #GtkWidget @@ -9931,16 +9981,13 @@ gtk_widget_style_get_property (GtkWidget *widget, GValue *value) { GParamSpec *pspec; + GtkRcPropertyParser parser; g_return_if_fail (GTK_IS_WIDGET (widget)); g_return_if_fail (property_name != NULL); g_return_if_fail (G_IS_VALUE (value)); - g_object_ref (widget); - pspec = g_param_spec_pool_lookup (style_property_spec_pool, - property_name, - G_OBJECT_TYPE (widget), - TRUE); + pspec = style_property_lookup (G_OBJECT_TYPE (widget), property_name, &parser); if (!pspec) g_warning ("%s: widget class `%s' has no property named `%s'", G_STRLOC, @@ -9953,7 +10000,7 @@ gtk_widget_style_get_property (GtkWidget *widget, peek_value = _gtk_style_peek_property_value (widget->style, G_OBJECT_TYPE (widget), pspec, - (GtkRcPropertyParser) g_param_spec_get_qdata (pspec, quark_property_parser)); + parser); /* auto-conversion of the caller's value type */ @@ -9967,7 +10014,6 @@ gtk_widget_style_get_property (GtkWidget *widget, g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)), G_VALUE_TYPE_NAME (value)); } - g_object_unref (widget); } /** @@ -9990,19 +10036,15 @@ gtk_widget_style_get_valist (GtkWidget *widget, g_return_if_fail (GTK_IS_WIDGET (widget)); - g_object_ref (widget); - name = first_property_name; while (name) { const GValue *peek_value; GParamSpec *pspec; + GtkRcPropertyParser parser; gchar *error; - pspec = g_param_spec_pool_lookup (style_property_spec_pool, - name, - G_OBJECT_TYPE (widget), - TRUE); + pspec = style_property_lookup (G_OBJECT_TYPE (widget), name, &parser); if (!pspec) { g_warning ("%s: widget class `%s' has no property named `%s'", @@ -10016,7 +10058,7 @@ gtk_widget_style_get_valist (GtkWidget *widget, peek_value = _gtk_style_peek_property_value (widget->style, G_OBJECT_TYPE (widget), pspec, - (GtkRcPropertyParser) g_param_spec_get_qdata (pspec, quark_property_parser)); + parser); G_VALUE_LCOPY (peek_value, var_args, 0, &error); if (error) { @@ -10027,8 +10069,6 @@ gtk_widget_style_get_valist (GtkWidget *widget, name = va_arg (var_args, gchar*); } - - g_object_unref (widget); } /**