[gtk/wip/baedert/for-master: 4/12] builderparser: Only allocate subparser stack when needed




commit 12958e5a49e4d57e96b789e1748ce24897225f83
Author: Timm Bäder <mail baedert org>
Date:   Wed Jan 6 17:54:31 2021 +0100

    builderparser: Only allocate subparser stack when needed
    
    Out of the 3.3k possibly_finish_subparser calls when opening the
    widget-factory, only 300 need one.

 gtk/gtkbuilderparser.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
---
diff --git a/gtk/gtkbuilderparser.c b/gtk/gtkbuilderparser.c
index 41935df8e1..b22cb1dfe5 100644
--- a/gtk/gtkbuilderparser.c
+++ b/gtk/gtkbuilderparser.c
@@ -32,7 +32,8 @@
 #include <string.h>
 
 
-typedef struct {
+typedef struct
+{
   const GtkBuildableParser *last_parser;
   gpointer last_user_data;
   int last_depth;
@@ -41,9 +42,8 @@ typedef struct {
 static void
 pop_subparser_stack (GtkBuildableParseContext *context)
 {
-  GtkBuildableParserStack *stack =
-    &g_array_index (context->subparser_stack, GtkBuildableParserStack,
-                    context->subparser_stack->len - 1);
+  GtkBuildableParserStack *stack = &g_array_index (context->subparser_stack, GtkBuildableParserStack,
+                                                   context->subparser_stack->len - 1);
 
   context->awaiting_pop = TRUE;
   context->held_user_data = context->user_data;
@@ -57,15 +57,17 @@ pop_subparser_stack (GtkBuildableParseContext *context)
 static void
 possibly_finish_subparser (GtkBuildableParseContext *context)
 {
-  if (context->subparser_stack->len > 0)
-    {
-      GtkBuildableParserStack *stack =
-        &g_array_index (context->subparser_stack, GtkBuildableParserStack,
-                        context->subparser_stack->len - 1);
+  GtkBuildableParserStack *stack;
 
-      if (stack->last_depth ==  context->tag_stack->len)
-        pop_subparser_stack (context);
-    }
+  if (!context->subparser_stack ||
+      context->subparser_stack->len == 0)
+    return;
+
+  stack = &g_array_index (context->subparser_stack, GtkBuildableParserStack,
+                          context->subparser_stack->len - 1);
+
+  if (stack->last_depth == context->tag_stack->len)
+    pop_subparser_stack (context);
 }
 
 static void
@@ -128,6 +130,9 @@ proxy_error (GMarkupParseContext *gm_context,
 
   /* report the error all the way up to free all the user-data */
 
+  if (!context->subparser_stack)
+    return;
+
   while (context->subparser_stack->len > 0)
     {
       pop_subparser_stack (context);
@@ -157,7 +162,7 @@ gtk_buildable_parse_context_init (GtkBuildableParseContext *context,
   context->parser = parser;
   context->user_data = user_data;
 
-  context->subparser_stack = g_array_new (FALSE, FALSE, sizeof (GtkBuildableParserStack));
+  context->subparser_stack = NULL;
   context->tag_stack = g_ptr_array_new ();
   context->held_user_data = NULL;
   context->awaiting_pop = FALSE;
@@ -166,7 +171,9 @@ gtk_buildable_parse_context_init (GtkBuildableParseContext *context,
 static void
 gtk_buildable_parse_context_free (GtkBuildableParseContext *context)
 {
-  g_array_unref (context->subparser_stack);
+  if (context->subparser_stack)
+    g_array_unref (context->subparser_stack);
+
   g_ptr_array_unref (context->tag_stack);
 }
 
@@ -245,6 +252,9 @@ gtk_buildable_parse_context_push (GtkBuildableParseContext *context,
   context->parser = parser;
   context->user_data = user_data;
 
+  if (!context->subparser_stack)
+    context->subparser_stack = g_array_new (FALSE, FALSE, sizeof (GtkBuildableParserStack));
+
   g_array_append_val (context->subparser_stack, stack);
 }
 


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