[gtk/matthiasc/css-change-tracking: 5/5] css: Avoid computing change too often



commit 27c73570a30968219686d4ce170de70dcc20e015
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jan 15 02:35:21 2020 -0500

    css: Avoid computing change too often
    
    Most of the time when styles need to be recreated, the name and classes
    of the css node haven't changed. In this case, the change value will not
    change either, since we are computing change under the assumption that
    name and classes are unchanged.
    
    So don't recompute the change. This avoids the second match we do to
    find the superset, cutting down the number of times we consult the
    selector tree.

 gtk/gtkcssnode.c               | 29 ++++++++++++++++++++++++-----
 gtk/gtkcssstaticstyle.c        |  9 +++++----
 gtk/gtkcssstaticstyleprivate.h |  3 ++-
 3 files changed, 31 insertions(+), 10 deletions(-)
---
diff --git a/gtk/gtkcssnode.c b/gtk/gtkcssnode.c
index 16faabe0df..b921b8ba73 100644
--- a/gtk/gtkcssnode.c
+++ b/gtk/gtkcssnode.c
@@ -87,6 +87,11 @@
  * if we need to change things. */
 #define GTK_CSS_RADICAL_CHANGE (GTK_CSS_CHANGE_ID | GTK_CSS_CHANGE_NAME | GTK_CSS_CHANGE_CLASS | 
GTK_CSS_CHANGE_SOURCE | GTK_CSS_CHANGE_PARENT_STYLE)
 
+/* When these change, we need to recompute the change flags for the new style
+ * since they may have changed.
+ */
+#define GTK_CSS_CHANGE_NEEDS_RECOMPUTE (GTK_CSS_RADICAL_CHANGE & ~GTK_CSS_CHANGE_PARENT_STYLE)
+
 G_DEFINE_TYPE (GtkCssNode, gtk_css_node, G_TYPE_OBJECT)
 
 enum {
@@ -348,12 +353,14 @@ store_in_global_parent_cache (GtkCssNode                  *node,
 }
 
 static GtkCssStyle *
-gtk_css_node_create_style (GtkCssNode *cssnode)
+gtk_css_node_create_style (GtkCssNode   *cssnode,
+                           GtkCssChange  change)
 {
   const GtkCssNodeDeclaration *decl;
   GtkCssMatcher matcher;
   GtkCssStyle *parent;
   GtkCssStyle *style;
+  GtkCssChange style_change;
 
   decl = gtk_css_node_get_declaration (cssnode);
 
@@ -363,14 +370,26 @@ gtk_css_node_create_style (GtkCssNode *cssnode)
 
   parent = cssnode->parent ? cssnode->parent->style : NULL;
 
+  if (change & GTK_CSS_CHANGE_NEEDS_RECOMPUTE)
+    {
+      /* Need to recompute the change flags */
+      style_change = 0;
+    }
+  else
+    {
+      style_change = gtk_css_static_style_get_change (gtk_css_style_get_static_style (cssnode->style));
+    }
+
   if (gtk_css_node_init_matcher (cssnode, &matcher))
     style = gtk_css_static_style_new_compute (gtk_css_node_get_style_provider (cssnode),
                                               &matcher,
-                                              parent);
+                                              parent,
+                                              style_change);
   else
     style = gtk_css_static_style_new_compute (gtk_css_node_get_style_provider (cssnode),
                                               NULL,
-                                              parent);
+                                              parent,
+                                              style_change);
 
   store_in_global_parent_cache (cssnode, decl, style);
 
@@ -410,7 +429,7 @@ gtk_css_node_real_update_style (GtkCssNode   *cssnode,
   static_style = GTK_CSS_STYLE (gtk_css_style_get_static_style (style));
 
   if (gtk_css_style_needs_recreation (static_style, change))
-    new_static_style = gtk_css_node_create_style (cssnode);
+    new_static_style = gtk_css_node_create_style (cssnode, change);
   else
     new_static_style = g_object_ref (static_style);
 
@@ -429,7 +448,7 @@ gtk_css_node_real_update_style (GtkCssNode   *cssnode,
     }
   else if (static_style != style && (change & GTK_CSS_CHANGE_TIMESTAMP))
     {
-      new_style = gtk_css_animated_style_new_advance ((GtkCssAnimatedStyle *)style,
+      new_style = gtk_css_animated_style_new_advance (GTK_CSS_ANIMATED_STYLE (style),
                                                       static_style,
                                                       timestamp);
     }
diff --git a/gtk/gtkcssstaticstyle.c b/gtk/gtkcssstaticstyle.c
index 6cb835d6e2..96fb3200cf 100644
--- a/gtk/gtkcssstaticstyle.c
+++ b/gtk/gtkcssstaticstyle.c
@@ -158,7 +158,8 @@ gtk_css_static_style_get_default (void)
       settings = gtk_settings_get_default ();
       default_style = gtk_css_static_style_new_compute (GTK_STYLE_PROVIDER (settings),
                                                         NULL,
-                                                        NULL);
+                                                        NULL,
+                                                        TRUE);
       g_object_set_data_full (G_OBJECT (settings), I_("gtk-default-style"),
                               default_style, clear_default_style);
     }
@@ -169,11 +170,11 @@ gtk_css_static_style_get_default (void)
 GtkCssStyle *
 gtk_css_static_style_new_compute (GtkStyleProvider    *provider,
                                   const GtkCssMatcher *matcher,
-                                  GtkCssStyle         *parent)
+                                  GtkCssStyle         *parent,
+                                  GtkCssChange         change)
 {
   GtkCssStaticStyle *result;
   GtkCssLookup lookup;
-  GtkCssChange change = GTK_CSS_CHANGE_ANY_SELF | GTK_CSS_CHANGE_ANY_SIBLING | GTK_CSS_CHANGE_ANY_PARENT;
 
   _gtk_css_lookup_init (&lookup);
 
@@ -181,7 +182,7 @@ gtk_css_static_style_new_compute (GtkStyleProvider    *provider,
     gtk_style_provider_lookup (provider,
                                matcher,
                                &lookup,
-                               &change);
+                               change == 0 ? &change : NULL);
 
   result = g_object_new (GTK_TYPE_CSS_STATIC_STYLE, NULL);
 
diff --git a/gtk/gtkcssstaticstyleprivate.h b/gtk/gtkcssstaticstyleprivate.h
index 7ef2b67a4d..81c97bbdc0 100644
--- a/gtk/gtkcssstaticstyleprivate.h
+++ b/gtk/gtkcssstaticstyleprivate.h
@@ -54,7 +54,8 @@ GType                   gtk_css_static_style_get_type           (void) G_GNUC_CO
 GtkCssStyle *           gtk_css_static_style_get_default        (void);
 GtkCssStyle *           gtk_css_static_style_new_compute        (GtkStyleProvider       *provider,
                                                                  const GtkCssMatcher    *matcher,
-                                                                 GtkCssStyle            *parent);
+                                                                 GtkCssStyle            *parent,
+                                                                 GtkCssChange            change);
 
 void                    gtk_css_static_style_compute_value      (GtkCssStaticStyle      *style,
                                                                  GtkStyleProvider       *provider,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]