[gtk/wip/baedert/for-master: 63/64] css: short-circuit gtk_css_dimension_value_try_add



commit 5750ef0b0cb34244b0abd88e64cfe1aa09d67ee1
Author: Timm Bäder <mail baedert org>
Date:   Thu Nov 7 07:32:03 2019 +0100

    css: short-circuit gtk_css_dimension_value_try_add
    
    No need to allocate a new css value for something that we already have,
    because one of the two values is 0

 gtk/gtkcsscalcvalue.c          |  4 ++--
 gtk/gtkcssdimensionvalue.c     | 10 ++++++++--
 gtk/gtkcssnumbervalue.c        |  4 ++--
 gtk/gtkcssnumbervalueprivate.h |  8 ++++----
 4 files changed, 16 insertions(+), 10 deletions(-)
---
diff --git a/gtk/gtkcsscalcvalue.c b/gtk/gtkcsscalcvalue.c
index 929d971bd7..8a5ea6b065 100644
--- a/gtk/gtkcsscalcvalue.c
+++ b/gtk/gtkcsscalcvalue.c
@@ -228,8 +228,8 @@ gtk_css_value_calc_multiply (const GtkCssValue *value,
 }
 
 static GtkCssValue *
-gtk_css_value_calc_try_add (const GtkCssValue *value1,
-                            const GtkCssValue *value2)
+gtk_css_value_calc_try_add (GtkCssValue *value1,
+                            GtkCssValue *value2)
 {
   return NULL;
 }
diff --git a/gtk/gtkcssdimensionvalue.c b/gtk/gtkcssdimensionvalue.c
index d545b65e20..1e9e758e63 100644
--- a/gtk/gtkcssdimensionvalue.c
+++ b/gtk/gtkcssdimensionvalue.c
@@ -235,12 +235,18 @@ gtk_css_value_dimension_multiply (const GtkCssValue *value,
 }
 
 static GtkCssValue *
-gtk_css_value_dimension_try_add (const GtkCssValue *value1,
-                                 const GtkCssValue *value2)
+gtk_css_value_dimension_try_add (GtkCssValue *value1,
+                                 GtkCssValue *value2)
 {
   if (value1->unit != value2->unit)
     return NULL;
 
+  if (value1->value == 0)
+    return _gtk_css_value_ref (value2);
+
+  if (value2->value == 0)
+    return _gtk_css_value_ref (value1);
+
   return gtk_css_dimension_value_new (value1->value + value2->value, value1->unit);
 }
 
diff --git a/gtk/gtkcssnumbervalue.c b/gtk/gtkcssnumbervalue.c
index e8543bea28..f5e574c81d 100644
--- a/gtk/gtkcssnumbervalue.c
+++ b/gtk/gtkcssnumbervalue.c
@@ -66,8 +66,8 @@ gtk_css_number_value_add (GtkCssValue *value1,
 }
 
 GtkCssValue *
-gtk_css_number_value_try_add (const GtkCssValue *value1,
-                              const GtkCssValue *value2)
+gtk_css_number_value_try_add (GtkCssValue *value1,
+                              GtkCssValue *value2)
 {
   GtkCssNumberValueClass *number_value_class;
   
diff --git a/gtk/gtkcssnumbervalueprivate.h b/gtk/gtkcssnumbervalueprivate.h
index faf7dad5c9..e18b69afcb 100644
--- a/gtk/gtkcssnumbervalueprivate.h
+++ b/gtk/gtkcssnumbervalueprivate.h
@@ -46,8 +46,8 @@ struct _GtkCssNumberValueClass {
   gboolean              (* has_percent)             (const GtkCssValue      *value);
   GtkCssValue *         (* multiply)                (const GtkCssValue      *value,
                                                      double                  factor);
-  GtkCssValue *         (* try_add)                 (const GtkCssValue      *value1,
-                                                     const GtkCssValue      *value2);
+  GtkCssValue *         (* try_add)                 (GtkCssValue            *value1,
+                                                     GtkCssValue            *value2);
   gint                  (* get_calc_term_order)     (const GtkCssValue      *value);
 };
 
@@ -67,8 +67,8 @@ GtkCssValue *   gtk_css_number_value_multiply       (const GtkCssValue      *val
                                                      double                  factor);
 GtkCssValue *   gtk_css_number_value_add            (GtkCssValue            *value1,
                                                      GtkCssValue            *value2);
-GtkCssValue *   gtk_css_number_value_try_add        (const GtkCssValue      *value1,
-                                                     const GtkCssValue      *value2);
+GtkCssValue *   gtk_css_number_value_try_add        (GtkCssValue            *value1,
+                                                     GtkCssValue            *value2);
 gint            gtk_css_number_value_get_calc_term_order (const GtkCssValue *value);
 
 double          _gtk_css_number_value_get           (const GtkCssValue      *number,


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