[gtk/css-lookup-caching] Revamp lookup memory handling



commit 2a6dfafb6d9b93960564b545e4c20ef43cf2fa6b
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Feb 10 23:04:29 2020 -0500

    Revamp lookup memory handling
    
    Make GtkCssLookup keep an an array of pointers to the
    PropertyValue structs in the rulesets, instead of copying
    things. Then make GtkCssStaticStyle find sections in the
    lookup, instead of keeping a separate array for those.

 gtk/gtkcsslookup.c             | 48 +++++++++++++++++++-------------------
 gtk/gtkcsslookupprivate.h      | 15 ++++++------
 gtk/gtkcssprovider.c           | 23 +++++++++----------
 gtk/gtkcssstaticstyle.c        | 52 +++++++-----------------------------------
 gtk/gtkcssstaticstyleprivate.h |  4 +---
 5 files changed, 52 insertions(+), 90 deletions(-)
---
diff --git a/gtk/gtkcsslookup.c b/gtk/gtkcsslookup.c
index d621604957..bcce8d2a49 100644
--- a/gtk/gtkcsslookup.c
+++ b/gtk/gtkcsslookup.c
@@ -24,15 +24,6 @@
 #include "gtkprivatetypebuiltins.h"
 #include "gtkprivate.h"
 
-static void
-free_value (gpointer data)
-{
-  GtkCssLookupValue *value = data;
-
-  gtk_css_value_unref (value->value);
-  if (value->section)
-    gtk_css_section_unref (value->section);
-}
 
 GtkCssLookup *
 gtk_css_lookup_new (void)
@@ -50,7 +41,7 @@ gtk_css_lookup_free (GtkCssLookup *lookup)
 {
   _gtk_bitmask_free (lookup->set_values);
   if (lookup->values)
-    g_array_unref (lookup->values);
+    g_ptr_array_unref (lookup->values);
   g_free (lookup);
 }
 
@@ -100,26 +91,35 @@ gtk_css_lookup_is_missing (const GtkCssLookup *lookup,
  * to ensure they are kept alive until _gtk_css_lookup_free() is called.
  **/
 void
-gtk_css_lookup_set (GtkCssLookup  *lookup,
-                    guint          id,
-                    GtkCssSection *section,
-                    GtkCssValue   *value)
+gtk_css_lookup_set (GtkCssLookup      *lookup,
+                    guint              id,
+                    GtkCssLookupValue *value)
 {
-  GtkCssLookupValue v;
-
   gtk_internal_return_if_fail (lookup != NULL);
   gtk_internal_return_if_fail (value != NULL);
 
-  v.value = gtk_css_value_ref (value);
-  v.section = section ? gtk_css_section_ref (section) : NULL;
-  v.id = id;
-
   if (!lookup->values)
+    lookup->values = g_ptr_array_sized_new (16);
+
+  g_ptr_array_add (lookup->values, value);
+  lookup->set_values = _gtk_bitmask_set (lookup->set_values, id, TRUE);
+}
+
+GtkCssSection *
+gtk_css_lookup_get_section (GtkCssLookup *lookup,
+                            guint         id)
+{
+  if (_gtk_bitmask_get (lookup->set_values, id))
     {
-      lookup->values = g_array_sized_new (FALSE, FALSE, sizeof (GtkCssLookupValue), 16);
-      g_array_set_clear_func (lookup->values, free_value);
+      int i;
+
+      for (i = 0; i < lookup->values->len; i++)
+        {
+          GtkCssLookupValue *value = g_ptr_array_index (lookup->values, i);
+          if (value->id == id)
+            return value->section;
+        }
     }
 
-  g_array_append_val (lookup->values, v);
-  lookup->set_values = _gtk_bitmask_set (lookup->set_values, id, TRUE);
+  return NULL;
 }
diff --git a/gtk/gtkcsslookupprivate.h b/gtk/gtkcsslookupprivate.h
index 7fcd530d7e..2d61d9b2c0 100644
--- a/gtk/gtkcsslookupprivate.h
+++ b/gtk/gtkcsslookupprivate.h
@@ -31,15 +31,15 @@ G_BEGIN_DECLS
 typedef struct _GtkCssLookup GtkCssLookup;
 
 typedef struct {
-  GtkCssSection     *section;
-  GtkCssValue       *value;
-  guint              id;
+  guint                id;
+  GtkCssValue         *value;
+  GtkCssSection       *section;
 } GtkCssLookupValue;
 
 struct _GtkCssLookup {
   int ref_count;
   GtkBitmask *set_values;
-  GArray *values;
+  GPtrArray *values;
 };
 
 GtkCssLookup *           gtk_css_lookup_new (void);
@@ -50,8 +50,9 @@ gboolean                 gtk_css_lookup_is_missing              (const GtkCssLoo
                                                                  guint                       id);
 void                     gtk_css_lookup_set                     (GtkCssLookup               *lookup,
                                                                  guint                       id,
-                                                                 GtkCssSection              *section,
-                                                                 GtkCssValue                *value);
+                                                                 GtkCssLookupValue          *value);
+GtkCssSection *          gtk_css_lookup_get_section             (GtkCssLookup               *lookup,
+                                                                 guint                       id);
 
 static inline const GtkBitmask *
 gtk_css_lookup_get_set_values (const GtkCssLookup *lookup)
@@ -69,7 +70,7 @@ gtk_css_lookup_get (GtkCssLookup *lookup,
 
       for (i = 0; i < lookup->values->len; i++)
         {
-          GtkCssLookupValue *value = &g_array_index (lookup->values, GtkCssLookupValue, i);
+          GtkCssLookupValue *value = g_ptr_array_index (lookup->values, i);
           if (value->id == id)
             return value;
         }
diff --git a/gtk/gtkcssprovider.c b/gtk/gtkcssprovider.c
index 72d4ba41c2..ceea9057f5 100644
--- a/gtk/gtkcssprovider.c
+++ b/gtk/gtkcssprovider.c
@@ -90,7 +90,7 @@ typedef enum ParserScope ParserScope;
 typedef enum ParserSymbol ParserSymbol;
 
 struct _PropertyValue {
-  GtkCssStyleProperty *property;
+  guint                id;
   GtkCssValue         *value;
   GtkCssSection       *section;
 };
@@ -267,14 +267,17 @@ gtk_css_ruleset_add (GtkCssRuleset       *ruleset,
                      GtkCssSection       *section)
 {
   guint i;
+  guint id;
 
   g_return_if_fail (ruleset->owns_styles || ruleset->n_styles == 0);
 
   ruleset->owns_styles = TRUE;
 
+  id = _gtk_css_style_property_get_id (property);
+
   for (i = 0; i < ruleset->n_styles; i++)
     {
-      if (ruleset->styles[i].property == property)
+      if (ruleset->styles[i].id == id)
         {
           _gtk_css_value_unref (ruleset->styles[i].value);
          ruleset->styles[i].value = NULL;
@@ -288,7 +291,7 @@ gtk_css_ruleset_add (GtkCssRuleset       *ruleset,
       ruleset->n_styles++;
       ruleset->styles = g_realloc (ruleset->styles, ruleset->n_styles * sizeof (PropertyValue));
       ruleset->styles[i].value = NULL;
-      ruleset->styles[i].property = property;
+      ruleset->styles[i].id = id;
     }
 
   ruleset->styles[i].value = value;
@@ -477,16 +480,12 @@ gtk_css_style_provider_lookup (GtkStyleProvider             *provider,
 
           for (j = 0; j < ruleset->n_styles; j++)
             {
-              GtkCssStyleProperty *prop = ruleset->styles[j].property;
-              guint id = _gtk_css_style_property_get_id (prop);
+              guint id = ruleset->styles[j].id;
 
               if (!gtk_css_lookup_is_missing (lookup, id))
                 continue;
 
-              gtk_css_lookup_set (lookup,
-                                  id,
-                                  ruleset->styles[j].section,
-                                  ruleset->styles[j].value);
+              gtk_css_lookup_set (lookup, id, (GtkCssLookupValue *)&ruleset->styles[j]);
             }
         }
 
@@ -1396,8 +1395,8 @@ compare_properties (gconstpointer a, gconstpointer b, gpointer style)
   const guint *ub = b;
   PropertyValue *styles = style;
 
-  return strcmp (_gtk_style_property_get_name (GTK_STYLE_PROPERTY (styles[*ua].property)),
-                 _gtk_style_property_get_name (GTK_STYLE_PROPERTY (styles[*ub].property)));
+  return strcmp (_gtk_style_property_get_name (GTK_STYLE_PROPERTY (_gtk_css_style_property_lookup_by_id 
(styles[*ua].id))),
+                 _gtk_style_property_get_name (GTK_STYLE_PROPERTY (_gtk_css_style_property_lookup_by_id 
(styles[*ub].id))));
 }
 
 static void
@@ -1424,7 +1423,7 @@ gtk_css_ruleset_print (const GtkCssRuleset *ruleset,
         {
           PropertyValue *prop = &ruleset->styles[sorted[i]];
           g_string_append (str, "  ");
-          g_string_append (str, _gtk_style_property_get_name (GTK_STYLE_PROPERTY (prop->property)));
+          g_string_append (str, _gtk_style_property_get_name (GTK_STYLE_PROPERTY 
(_gtk_css_style_property_lookup_by_id (prop->id))));
           g_string_append (str, ": ");
           _gtk_css_value_print (prop->value, str);
           g_string_append (str, ";\n");
diff --git a/gtk/gtkcssstaticstyle.c b/gtk/gtkcssstaticstyle.c
index 04c4984183..e1db1a3858 100644
--- a/gtk/gtkcssstaticstyle.c
+++ b/gtk/gtkcssstaticstyle.c
@@ -43,8 +43,7 @@ static void gtk_css_static_style_compute_value (GtkCssStaticStyle *style,
                                                 GtkStyleProvider  *provider,
                                                 GtkCssStyle       *parent_style,
                                                 guint              id,
-                                                GtkCssValue       *specified,
-                                                GtkCssSection     *section);
+                                                GtkCssValue       *specified);
 
 static const int core_props[] = {
   GTK_CSS_PROPERTY_COLOR,
@@ -212,8 +211,7 @@ gtk_css_ ## NAME ## _values_new_compute (GtkCssStaticStyle *sstyle, \
                                           provider, \
                                           parent_style, \
                                           id, \
-                                          value ? value->value : NULL, \
-                                          value ? value->section : NULL); \
+                                          value ? value->value : NULL); \
     } \
 } \
 static GtkBitmask * gtk_css_ ## NAME ## _values_mask; \
@@ -300,15 +298,11 @@ G_DEFINE_TYPE (GtkCssStaticStyle, gtk_css_static_style, GTK_TYPE_CSS_STYLE)
 
 static GtkCssSection *
 gtk_css_static_style_get_section (GtkCssStyle *style,
-                                    guint        id)
+                                  guint        id)
 {
   GtkCssStaticStyle *sstyle = GTK_CSS_STATIC_STYLE (style);
 
-  if (sstyle->sections == NULL ||
-      id >= sstyle->sections->len)
-    return NULL;
-
-  return g_ptr_array_index (sstyle->sections, id);
+  return gtk_css_lookup_get_section (sstyle->lookup, id);
 }
 
 static void
@@ -316,12 +310,6 @@ gtk_css_static_style_dispose (GObject *object)
 {
   GtkCssStaticStyle *style = GTK_CSS_STATIC_STYLE (object);
 
-  if (style->sections)
-    {
-      g_ptr_array_unref (style->sections);
-      style->sections = NULL;
-    }
-
   g_clear_pointer (&style->lookup, gtk_css_lookup_unref);
 
   G_OBJECT_CLASS (gtk_css_static_style_parent_class)->dispose (object);
@@ -364,13 +352,6 @@ gtk_css_static_style_init (GtkCssStaticStyle *style)
 {
 }
 
-static void
-maybe_unref_section (gpointer section)
-{
-  if (section)
-    gtk_css_section_unref (section);
-}
-
 static inline void
 gtk_css_take_value (GtkCssValue **variable,
                     GtkCssValue  *value)
@@ -383,8 +364,7 @@ gtk_css_take_value (GtkCssValue **variable,
 static void
 gtk_css_static_style_set_value (GtkCssStaticStyle *sstyle,
                                 guint              id,
-                                GtkCssValue       *value,
-                                GtkCssSection     *section)
+                                GtkCssValue       *value)
 {
   GtkCssStyle *style = (GtkCssStyle *)sstyle;
 
@@ -665,21 +645,6 @@ gtk_css_static_style_set_value (GtkCssStaticStyle *sstyle,
       g_assert_not_reached ();
       break;
     }
-
-  if (sstyle->sections && sstyle->sections->len > id && g_ptr_array_index (sstyle->sections, id))
-    {
-      gtk_css_section_unref (g_ptr_array_index (sstyle->sections, id));
-      g_ptr_array_index (sstyle->sections, id) = NULL;
-    }
-
-  if (section)
-    {
-      if (sstyle->sections == NULL)
-        sstyle->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
-      if (sstyle->sections->len <= id)
-        g_ptr_array_set_size (sstyle->sections, id + 1);
-      g_ptr_array_index (sstyle->sections, id) = gtk_css_section_ref (section);
-    }
 }
 
 static GtkCssStyle *default_style;
@@ -1044,8 +1009,7 @@ gtk_css_static_style_compute_value (GtkCssStaticStyle *style,
                                     GtkStyleProvider  *provider,
                                     GtkCssStyle       *parent_style,
                                     guint              id,
-                                    GtkCssValue       *specified,
-                                    GtkCssSection     *section)
+                                    GtkCssValue       *specified)
 {
   GtkCssValue *value;
   GtkBorderStyle border_style;
@@ -1068,7 +1032,7 @@ gtk_css_static_style_compute_value (GtkCssStaticStyle *style,
         border_style = _gtk_css_border_style_value_get (gtk_css_style_get_value ((GtkCssStyle *)style, id - 
1));
         if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
           {
-            gtk_css_static_style_set_value (style, id, gtk_css_dimension_value_new (0, GTK_CSS_NUMBER), 
section);
+            gtk_css_static_style_set_value (style, id, gtk_css_dimension_value_new (0, GTK_CSS_NUMBER));
             return;
           }
         break;
@@ -1097,7 +1061,7 @@ gtk_css_static_style_compute_value (GtkCssStaticStyle *style,
       value = _gtk_css_initial_value_new_compute (id, provider, (GtkCssStyle *)style, parent_style);
     }
 
-  gtk_css_static_style_set_value (style, id, value, section);
+  gtk_css_static_style_set_value (style, id, value);
 }
 
 GtkCssChange
diff --git a/gtk/gtkcssstaticstyleprivate.h b/gtk/gtkcssstaticstyleprivate.h
index a3b62f59b1..a95e4a2e3a 100644
--- a/gtk/gtkcssstaticstyleprivate.h
+++ b/gtk/gtkcssstaticstyleprivate.h
@@ -41,9 +41,7 @@ struct _GtkCssStaticStyle
 {
   GtkCssStyle parent;
 
-  GPtrArray             *sections;             /* sections the values are defined in */
-
-  GtkCssChange           change;               /* change as returned by value lookup */
+  GtkCssChange           change;              /* change as returned by value lookup */
   GtkCssLookup          *lookup;
 };
 


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