]> granicus.if.org Git - graphviz/commitdiff
use a C99 bool array for checked in beautify_leaves
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 17 Jun 2021 00:54:02 +0000 (17:54 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Wed, 23 Jun 2021 00:06:12 +0000 (17:06 -0700)
This change not only reduces the heap usage for this function (sizeof(bool) is
typically less than sizeof(int)), but also makes both allocation and
initialization more efficient.

lib/sfdpgen/spring_electrical.c

index b062818eecdfdfa13e3005889d2eb79b157cc0fa..b5a200d52cd982937b790e26fa5cf2d24cd348d3 100644 (file)
@@ -22,6 +22,7 @@
 #include <common/logic.h>
 #include <math.h>
 #include <common/globals.h>
+#include <stdbool.h>
 #include <stddef.h>
 #include <string.h>
 #include <time.h>
@@ -373,7 +374,7 @@ static void set_leaves(real *x, int dim, real dist, real ang, int i, int j){
 
 static void beautify_leaves(int dim, SparseMatrix A, real *x){
   int m = A->m, i, j, *ia = A->ia, *ja = A->ja, k;
-  int *checked, p;
+  int p;
   real dist;
   int nleaves, nleaves_max = 10;
   real *angles, maxang, ang1 = 0, ang2 = 0, pad, step;
@@ -381,19 +382,17 @@ static void beautify_leaves(int dim, SparseMatrix A, real *x){
 
   assert(!SparseMatrix_has_diagonal(A));
 
-  checked = MALLOC(sizeof(int)*m);
+  bool *checked = gcalloc(sizeof(bool), m);
   angles = MALLOC(sizeof(real)*nangles_max);
   leaves = MALLOC(sizeof(int)*nleaves_max);
 
 
-  for (i = 0; i < m; i++) checked[i] = FALSE;
-
   for (i = 0; i < m; i++){
     if (ia[i+1] - ia[i] != 1) continue;
     if (checked[i]) continue;
     p = ja[ia[i]];
     if (!checked[p]){
-      checked[p] = TRUE;
+      checked[p] = true;
       dist = 0; nleaves = 0; nangles = 0;
       for (j = ia[p]; j < ia[p+1]; j++){
        if (node_degree(ja[j]) == 1){