]> granicus.if.org Git - graphviz/commitdiff
common point_gencode: tighter scope and stack-allocate 'AF'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 18 Aug 2022 01:52:13 +0000 (18:52 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Tue, 23 Aug 2022 04:38:16 +0000 (21:38 -0700)
`AF` was being dynamically allocated as if it needed to contain an arbitrary
(potentially large) number of points. However its only use is in being passed to
`gvrender_ellipse` which only looks at the first two points. So we can simplify
and optimize this code by simply stack-allocating 2 entries.

lib/common/shapes.c

index 15f0e4bed5abd0fff9b908032334f87f7c46b123..3854e3ca8deb60d24eee08c89c4e0f0893b8fe2f 100644 (file)
@@ -3081,8 +3081,6 @@ static void point_gencode(GVJ_t * job, node_t * n)
     polygon_t *poly;
     int i, j, sides, peripheries, style;
     pointf P, *vertices;
-    static pointf *AF;
-    static int A_size;
     bool filled;
     char *color;
     int doMap = obj->url || obj->explicit_tooltip;
@@ -3096,10 +3094,6 @@ static void point_gencode(GVJ_t * job, node_t * n)
     vertices = poly->vertices;
     sides = poly->sides;
     peripheries = poly->peripheries;
-    if (A_size < sides) {
-       A_size = sides + 2;
-       AF = ALLOC(A_size, AF, pointf);
-    }
 
     checkStyle(n, &style);
     if (style & INVISIBLE)
@@ -3152,10 +3146,14 @@ static void point_gencode(GVJ_t * job, node_t * n)
     }
 
     for (j = 0; j < peripheries; j++) {
+       enum {A_size = 2};
+       pointf AF[A_size] = {{0}};
        for (i = 0; i < sides; i++) {
            P = vertices[i + j * sides];
-           AF[i].x = P.x + ND_coord(n).x;
-           AF[i].y = P.y + ND_coord(n).y;
+           if (i < A_size) {
+             AF[i].x = P.x + ND_coord(n).x;
+             AF[i].y = P.y + ND_coord(n).y;
+           }
        }
        gvrender_ellipse(job, AF, filled);
        /* fill innermost periphery only */