[gtk/matthiasc/for-master: 3/15] textbtree: Avoid malloc in one place




commit 214e2d14be42302872dbd7e2cacf324a25d976b6
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Apr 3 20:25:35 2021 -0400

    textbtree: Avoid malloc in one place

 gtk/gtktextbtree.c | 25 ++++++-------------------
 1 file changed, 6 insertions(+), 19 deletions(-)
---
diff --git a/gtk/gtktextbtree.c b/gtk/gtktextbtree.c
index 5f87ae70ad..8167366109 100644
--- a/gtk/gtktextbtree.c
+++ b/gtk/gtktextbtree.c
@@ -2469,16 +2469,12 @@ _gtk_text_btree_char_count (GtkTextBTree *tree)
   return tree->root_node->num_chars - 2;
 }
 
-#define LOTSA_TAGS 1000
 gboolean
 _gtk_text_btree_char_is_invisible (const GtkTextIter *iter)
 {
   gboolean invisible = FALSE;  /* if nobody says otherwise, it's visible */
-
-  int deftagCnts[LOTSA_TAGS] = { 0, };
-  int *tagCnts = deftagCnts;
-  GtkTextTag *deftags[LOTSA_TAGS];
-  GtkTextTag **tags = deftags;
+  int *tagCnts;
+  GtkTextTag **tags;
   int numTags;
   GtkTextBTreeNode *node;
   GtkTextLine *siblingline;
@@ -2489,7 +2485,6 @@ _gtk_text_btree_char_is_invisible (const GtkTextIter *iter)
   GtkTextBTree *tree;
   int byte_index;
 
-  line = _gtk_text_iter_get_text_line (iter);
   tree = _gtk_text_iter_get_btree (iter);
 
   /* Short-circuit if we've never seen a visibility tag within the
@@ -2498,16 +2493,14 @@ _gtk_text_btree_char_is_invisible (const GtkTextIter *iter)
   if G_LIKELY (!_gtk_text_tag_table_affects_visibility (tree->table))
     return FALSE;
 
+  line = _gtk_text_iter_get_text_line (iter);
+
   byte_index = gtk_text_iter_get_line_index (iter);
 
   numTags = gtk_text_tag_table_get_size (tree->table);
 
-  /* almost always avoid malloc, so stay out of system calls */
-  if (LOTSA_TAGS < numTags)
-    {
-      tagCnts = g_new0 (int, numTags);
-      tags = g_new (GtkTextTag*, numTags);
-    }
+  tagCnts = g_alloca (sizeof (int) * numTags);
+  tags = g_alloca (sizeof (GtkTextTag *) * numTags);
 
   /*
    * Record tag toggles within the line of indexPtr but preceding
@@ -2610,12 +2603,6 @@ _gtk_text_btree_char_is_invisible (const GtkTextIter *iter)
         }
     }
 
-  if (LOTSA_TAGS < numTags)
-    {
-      g_free (tagCnts);
-      g_free (tags);
-    }
-
   return invisible;
 }
 


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