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.
This commit is contained in:
Matthias Clasen
2020-02-10 23:04:29 -05:00
parent 91120d3f46
commit a112c3ff45
5 changed files with 53 additions and 96 deletions

View File

@@ -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_array_sized_new (FALSE, FALSE, sizeof (GtkCssLookupValue), 16);
g_array_set_clear_func (lookup->values, free_value);
}
lookup->values = g_ptr_array_sized_new (16);
g_array_append_val (lookup->values, v);
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))
{
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;
}
}
return NULL;
}

View File

@@ -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;
}

View File

@@ -86,15 +86,10 @@ struct _GtkCssProviderClass
typedef struct GtkCssRuleset GtkCssRuleset;
typedef struct _GtkCssScanner GtkCssScanner;
typedef struct _PropertyValue PropertyValue;
typedef GtkCssLookupValue PropertyValue;
typedef enum ParserScope ParserScope;
typedef enum ParserSymbol ParserSymbol;
struct _PropertyValue {
GtkCssStyleProperty *property;
GtkCssValue *value;
GtkCssSection *section;
};
struct GtkCssRuleset
{
@@ -268,14 +263,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;
@@ -289,7 +287,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;
@@ -481,16 +479,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]);
}
}
@@ -1400,8 +1394,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
@@ -1428,7 +1422,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");

View File

@@ -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,
@@ -211,8 +210,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; \
@@ -299,15 +297,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
@@ -315,12 +309,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);
@@ -363,13 +351,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)
@@ -382,8 +363,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;
@@ -661,21 +641,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;
@@ -1040,8 +1005,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;
@@ -1064,7 +1028,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;
@@ -1093,7 +1057,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

View File

@@ -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;
};