From 6bac5daf258e413a9c4790ba13edca025710b537 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Wed, 16 Jun 2021 17:54:02 -0700 Subject: [PATCH] use a C99 bool array for checked in beautify_leaves 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 | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/sfdpgen/spring_electrical.c b/lib/sfdpgen/spring_electrical.c index b062818ee..b5a200d52 100644 --- a/lib/sfdpgen/spring_electrical.c +++ b/lib/sfdpgen/spring_electrical.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -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){ -- 2.40.0