[dia] [cleanup] Make DiaObject::enclosing_box an optional pointer



commit 0f365dd9a91dd7861ddeef760428ab40d0f55d54
Author: Hans Breuer <hans breuer org>
Date:   Tue Sep 9 20:48:44 2014 +0200

    [cleanup] Make DiaObject::enclosing_box an optional pointer
    
    Only three objects are currently using the extra storage, but it did
    occupy four double values (32 bytes) in every single DiaObject.
    Now it's only one pointer and some indirection with the three objects,
    but faster dia_object_get_enclosing_box() for all objects and less
    memory usage for most.
    
    test-sizeof (before)
    DiaObject: 136
    Connection: 264
    Element: 432
    UMLClass: 992
    Handle: 32
    ConnectionPoint: 32
    
    test-sizeof (after)
    DiaObject: 104
    Connection: 232
    Element: 400
    UMLClass: 960
    Handle: 32

 lib/object.c                 |   16 ++++++----------
 lib/object.h                 |    2 +-
 lib/plug-ins.h               |    2 +-
 objects/standard/arc.c       |   12 +++++++++---
 objects/standard/bezier.c    |   12 +++++++++---
 objects/standard/beziergon.c |   12 +++++++++---
 6 files changed, 35 insertions(+), 21 deletions(-)
---
diff --git a/lib/object.c b/lib/object.c
index 20fe414..a4e8d68 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1061,17 +1061,13 @@ dia_object_get_bounding_box(const DiaObject *obj) {
  * @return A pointer to a Rectangle object.  This object should *not*
  *  be freed after use, as it belongs to the object.
  */
-const Rectangle *dia_object_get_enclosing_box(const DiaObject *obj) {
-  /* I believe we can do this comparison, as it is only to compare for cases
-   * where it would be set explicitly to 0.
-   */
-  if (obj->enclosing_box.top == 0.0 &&
-      obj->enclosing_box.bottom == 0.0 &&
-      obj->enclosing_box.left == 0.0 &&
-      obj->enclosing_box.right == 0.0) {
+const Rectangle *
+dia_object_get_enclosing_box(const DiaObject *obj)
+{
+  if (!obj->enclosing_box)
     return &obj->bounding_box;
-  } else {
-  } return &obj->enclosing_box;
+  else
+    return obj->enclosing_box;
 }
 
 void
diff --git a/lib/object.h b/lib/object.h
index fb57e5d..555fafb 100644
--- a/lib/object.h
+++ b/lib/object.h
@@ -503,7 +503,7 @@ struct _DiaObject {
    *  Since handles and CPs are not in the BB, that will be the case for most
    *  objects.
    */
-  Rectangle         enclosing_box;
+  Rectangle        *enclosing_box;
   /*! Metainfo of the object, should not be manipulated directy. Use dia_object_set_meta() */
   GHashTable       *meta;
 };
diff --git a/lib/plug-ins.h b/lib/plug-ins.h
index 3fa6665..7e92a09 100644
--- a/lib/plug-ins.h
+++ b/lib/plug-ins.h
@@ -52,7 +52,7 @@ G_BEGIN_DECLS
  *
  * \ingroup Plugins
  */
-#define DIA_PLUGIN_API_VERSION 19
+#define DIA_PLUGIN_API_VERSION 20
 
 typedef enum {
   DIA_PLUGIN_INIT_OK,
diff --git a/objects/standard/arc.c b/objects/standard/arc.c
index 0b276fb..425a1d6 100644
--- a/objects/standard/arc.c
+++ b/objects/standard/arc.c
@@ -696,7 +696,8 @@ arc_create(Point *startpoint,
   Point defaultlen = { 1.0, 1.0 };
 
   arc = g_malloc0(sizeof(Arc));
-
+  arc->connection.object.enclosing_box = g_new0 (Rectangle, 1);
+  
   arc->line_width =  attributes_get_default_linewidth();
   arc->curve_distance = 1.0;
   arc->arc_color = attributes_get_foreground(); 
@@ -729,6 +730,8 @@ arc_create(Point *startpoint,
 static void
 arc_destroy(Arc *arc)
 {
+  g_free (arc->connection.object.enclosing_box);
+  arc->connection.object.enclosing_box = NULL;
   connection_destroy(&arc->connection);
 }
 
@@ -742,6 +745,7 @@ arc_copy(Arc *arc)
   conn = &arc->connection;
   
   newarc = g_malloc0(sizeof(Arc));
+  newarc->connection.object.enclosing_box = g_new0 (Rectangle, 1);
   newconn = &newarc->connection;
   newobj = &newconn->object;
 
@@ -926,8 +930,9 @@ arc_update_data(Arc *arc)
     rectangle_union(&obj->bounding_box, &bbox);
   }
   /* if selected put the centerpoint in the box, too. */
-  obj->enclosing_box = obj->bounding_box;
-  rectangle_add_point(&obj->enclosing_box, &arc->center);
+  g_assert (obj->enclosing_box != NULL);
+  *obj->enclosing_box = obj->bounding_box;
+  rectangle_add_point(obj->enclosing_box, &arc->center);
 
   obj->position = conn->endpoints[0];
 }
@@ -982,6 +987,7 @@ arc_load(ObjectNode obj_node, int version,DiaContext *ctx)
   AttributeNode attr;
 
   arc = g_malloc0(sizeof(Arc));
+  arc->connection.object.enclosing_box = g_new0 (Rectangle, 1);
 
   conn = &arc->connection;
   obj = &conn->object;
diff --git a/objects/standard/bezier.c b/objects/standard/bezier.c
index 51c90a0..caa64a1 100644
--- a/objects/standard/bezier.c
+++ b/objects/standard/bezier.c
@@ -441,6 +441,7 @@ bezierline_create(Point *startpoint,
   Point defaultlen = { .3, .3 };
 
   bezierline = g_new0(Bezierline, 1);
+  bezierline->bez.object.enclosing_box = g_new0 (Rectangle, 1);
   bez = &bezierline->bez;
   obj = &bez->object;
   
@@ -484,6 +485,8 @@ bezierline_create(Point *startpoint,
 static void
 bezierline_destroy(Bezierline *bezierline)
 {
+  g_free (bezierline->bez.object.enclosing_box);
+  bezierline->bez.object.enclosing_box = NULL;
   bezierconn_destroy(&bezierline->bez);
 }
 
@@ -496,6 +499,7 @@ bezierline_copy(Bezierline *bezierline)
   bez = &bezierline->bez;
  
   newbezierline = g_new0(Bezierline, 1);
+  newbezierline->bez.object.enclosing_box = g_new0 (Rectangle, 1);
   newbez = &newbezierline->bez;
 
   bezierconn_copy(bez, newbez);
@@ -573,13 +577,14 @@ bezierline_update_data(Bezierline *bezierline)
       * and to remove traces from them */
   {
     int i, num_points = bez->bezier.num_points;
-    obj->enclosing_box = obj->bounding_box;
+    g_assert (obj->enclosing_box != NULL);
+    *obj->enclosing_box = obj->bounding_box;
     /* starting with the second point, the first one is MOVE_TO */
     for (i = 1; i < num_points; ++i) {
       if (bez->bezier.points[i].type != BEZ_CURVE_TO)
         continue;
-      rectangle_add_point(&obj->enclosing_box, &bez->bezier.points[i].p1);      
-      rectangle_add_point(&obj->enclosing_box, &bez->bezier.points[i].p2);      
+      rectangle_add_point(obj->enclosing_box, &bez->bezier.points[i].p1);      
+      rectangle_add_point(obj->enclosing_box, &bez->bezier.points[i].p2);      
     }
   }
 }
@@ -650,6 +655,7 @@ bezierline_load(ObjectNode obj_node, int version, DiaContext *ctx)
   AttributeNode attr;
 
   bezierline = g_new0(Bezierline, 1);
+  bezierline->bez.object.enclosing_box = g_new0 (Rectangle, 1);
 
   bez = &bezierline->bez;
   obj = &bez->object;
diff --git a/objects/standard/beziergon.c b/objects/standard/beziergon.c
index 8212334..0ae7456 100644
--- a/objects/standard/beziergon.c
+++ b/objects/standard/beziergon.c
@@ -263,6 +263,7 @@ beziergon_create(Point *startpoint,
   Point defaulty = { 0.0, 1.0 };
 
   beziergon = g_new0(Beziergon, 1);
+  beziergon->bezier.object.enclosing_box = g_new0 (Rectangle, 1);
   bez = &beziergon->bezier;
   obj = &bez->object;
 
@@ -313,6 +314,8 @@ beziergon_destroy(Beziergon *beziergon)
 {
   if (beziergon->pattern)
     g_object_unref (beziergon->pattern);
+  g_free (beziergon->bezier.object.enclosing_box);
+  beziergon->bezier.object.enclosing_box = NULL;
   beziershape_destroy(&beziergon->bezier);
 }
 
@@ -325,6 +328,7 @@ beziergon_copy(Beziergon *beziergon)
   bezier = &beziergon->bezier;
  
   newbeziergon = g_malloc0(sizeof(Beziergon));
+  newbeziergon->bezier.object.enclosing_box = g_new0 (Rectangle, 1);
   newbezier = &newbeziergon->bezier;
 
   beziershape_copy(bezier, newbezier);
@@ -357,12 +361,13 @@ beziergon_update_data(Beziergon *beziergon)
   /* update the enclosing box using the control points */
   {
     int i, num_points = bez->bezier.num_points;
-    obj->enclosing_box = obj->bounding_box;
+    g_assert (obj->enclosing_box != NULL);
+    *obj->enclosing_box = obj->bounding_box;
     for (i = 0; i < num_points; ++i) {
       if (bez->bezier.points[i].type != BEZ_CURVE_TO)
         continue;
-      rectangle_add_point(&obj->enclosing_box, &bez->bezier.points[i].p1);      
-      rectangle_add_point(&obj->enclosing_box, &bez->bezier.points[i].p2);      
+      rectangle_add_point(obj->enclosing_box, &bez->bezier.points[i].p1);      
+      rectangle_add_point(obj->enclosing_box, &bez->bezier.points[i].p2);      
     }
   }
   obj->position = bez->bezier.points[0].p1;
@@ -416,6 +421,7 @@ beziergon_load(ObjectNode obj_node, int version, DiaContext *ctx)
   AttributeNode attr;
 
   beziergon = g_malloc0(sizeof(Beziergon));
+  beziergon->bezier.object.enclosing_box = g_new0 (Rectangle, 1);
 
   bez = &beziergon->bezier;
   obj = &bez->object;


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