csscalcvalue: Avoid allocating memory in compute()

We already know how many values we're going to have, it's
value->n_terms.
This commit is contained in:
Timm Bäder 2020-05-06 08:23:17 +02:00
parent 90b3105d99
commit df08387e35

View File

@ -50,22 +50,21 @@ gtk_css_value_calc_free (GtkCssValue *value)
static GtkCssValue *gtk_css_calc_value_new (gsize n_terms); static GtkCssValue *gtk_css_calc_value_new (gsize n_terms);
static GtkCssValue * static GtkCssValue *
gtk_css_value_new_from_array (GPtrArray *array) gtk_css_value_new_from_array (GtkCssValue **values,
guint n_values)
{ {
GtkCssValue *result; GtkCssValue *result;
if (array->len > 1) if (n_values > 1)
{ {
result = gtk_css_calc_value_new (array->len); result = gtk_css_calc_value_new (n_values);
memcpy (result->terms, array->pdata, array->len * sizeof (GtkCssValue *)); memcpy (result->terms, values, n_values * sizeof (GtkCssValue *));
} }
else else
{ {
result = g_ptr_array_index (array, 0); result = values[0];
} }
g_ptr_array_free (array, TRUE);
return result; return result;
} }
@ -105,26 +104,28 @@ gtk_css_value_calc_compute (GtkCssValue *value,
GtkCssStyle *parent_style) GtkCssStyle *parent_style)
{ {
GtkCssValue *result; GtkCssValue *result;
GPtrArray *array;
gboolean changed = FALSE; gboolean changed = FALSE;
gsize i; gsize i;
GtkCssValue **new_values;
new_values = g_alloca (sizeof (GtkCssValue *) * value->n_terms);
array = g_ptr_array_new ();
for (i = 0; i < value->n_terms; i++) for (i = 0; i < value->n_terms; i++)
{ {
GtkCssValue *computed = _gtk_css_value_compute (value->terms[i], property_id, provider, style, parent_style); GtkCssValue *computed = _gtk_css_value_compute (value->terms[i], property_id, provider, style, parent_style);
changed |= computed != value->terms[i]; changed |= computed != value->terms[i];
gtk_css_calc_array_add (array, computed); new_values[i] = computed;
} }
if (changed) if (changed)
{ {
result = gtk_css_value_new_from_array (array); result = gtk_css_value_new_from_array (new_values, value->n_terms);
} }
else else
{ {
g_ptr_array_set_free_func (array, (GDestroyNotify) _gtk_css_value_unref); for (i = 0; i < value->n_terms; i++)
g_ptr_array_free (array, TRUE); gtk_css_value_unref (new_values[i]);
result = _gtk_css_value_ref (value); result = _gtk_css_value_ref (value);
} }
@ -279,6 +280,7 @@ gtk_css_calc_value_new_sum (GtkCssValue *value1,
GtkCssValue *value2) GtkCssValue *value2)
{ {
GPtrArray *array; GPtrArray *array;
GtkCssValue *result;
gsize i; gsize i;
array = g_ptr_array_new (); array = g_ptr_array_new ();
@ -307,7 +309,10 @@ gtk_css_calc_value_new_sum (GtkCssValue *value1,
gtk_css_calc_array_add (array, _gtk_css_value_ref (value2)); gtk_css_calc_array_add (array, _gtk_css_value_ref (value2));
} }
return gtk_css_value_new_from_array (array); result = gtk_css_value_new_from_array ((GtkCssValue **)array->pdata, array->len);
g_ptr_array_free (array, TRUE);
return result;
} }
GtkCssValue * gtk_css_calc_value_parse_sum (GtkCssParser *parser, GtkCssValue * gtk_css_calc_value_parse_sum (GtkCssParser *parser,