[Xfce4-commits] <garcon:master> Optimize some functions that are called a lot.

Nick Schermer noreply at xfce.org
Sun Jun 26 09:20:02 CEST 2011


Updating branch refs/heads/master
         to 42c7fdfde47a1f4ba79c70f28e9615cf7aee5164 (commit)
       from 9ebab4a5b31448d4acafeafbe58c27acbbd7801a (commit)

commit 42c7fdfde47a1f4ba79c70f28e9615cf7aee5164
Author: Nick Schermer <nick at xfce.org>
Date:   Sun Jun 26 08:46:42 2011 +0200

    Optimize some functions that are called a lot.
    
    Avoid the reverse check on garcon_menu_node_tree_get_child_node(),
    which is called a bunch of times.
    
    And post-reverse the lists that are generated with
    garcon_menu_node_tree_get_string_children() and
    garcon_menu_node_tree_get_child_nodes().

 garcon/garcon-menu-node.c |   42 +++++++++++++++++++++---------------------
 1 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/garcon/garcon-menu-node.c b/garcon/garcon-menu-node.c
index a62fad0..6991eed 100644
--- a/garcon/garcon-menu-node.c
+++ b/garcon/garcon-menu-node.c
@@ -420,7 +420,6 @@ typedef struct
 {
   GarconMenuNodeType type;
   GNode             *self;
-  gboolean           reverse;
   gpointer           value;
 } Pair;
 
@@ -434,12 +433,7 @@ collect_children (GNode *node,
     return FALSE;
 
   if (garcon_menu_node_tree_get_node_type (node) == pair->type)
-    {
-      if (pair->reverse)
-        pair->value = g_list_prepend (pair->value, node);
-      else
-        pair->value = g_list_append (pair->value, node);
-    }
+    pair->value = g_list_prepend (pair->value, node);
 
   return FALSE;
 }
@@ -451,18 +445,22 @@ garcon_menu_node_tree_get_child_node (GNode             *tree,
                                       GarconMenuNodeType type,
                                       gboolean           reverse)
 {
-  GNode *node = NULL;
   GNode *child;
 
-  for (child = reverse ? g_node_last_child (tree) : g_node_first_child (tree);
-       node == NULL && child != NULL;
-       child = reverse ? g_node_prev_sibling (child) : g_node_next_sibling (child))
+  if (reverse)
+    {
+      for (child = g_node_last_child (tree); child != NULL; child = g_node_prev_sibling (child))
+        if (garcon_menu_node_tree_get_node_type (child) == type)
+          return child;
+    }
+  else
     {
-      if (garcon_menu_node_tree_get_node_type (child) == type)
-        node = child;
+      for (child = g_node_first_child (tree); child != NULL; child = g_node_next_sibling (child))
+        if (garcon_menu_node_tree_get_node_type (child) == type)
+          return child;
     }
 
-  return node;
+  return NULL;
 }
 
 
@@ -475,13 +473,16 @@ garcon_menu_node_tree_get_child_nodes (GNode             *tree,
   Pair pair;
 
   pair.type = type;
-  pair.reverse = reverse;
   pair.value = NULL;
   pair.self = tree;
 
   g_node_traverse (tree, G_IN_ORDER, G_TRAVERSE_ALL, 2,
                    (GNodeTraverseFunc) collect_children, &pair);
 
+  /* Return the list as if we appended */
+  if (!reverse && pair.value != NULL)
+    pair.value = g_list_reverse (pair.value);
+
   return pair.value;
 }
 
@@ -499,11 +500,7 @@ collect_strings (GNode *node,
   if (garcon_menu_node_tree_get_node_type (node) == pair->type)
     {
       string = (gpointer) garcon_menu_node_tree_get_string (node);
-
-      if (pair->reverse)
-        pair->value = g_list_prepend (pair->value, string);
-      else
-        pair->value = g_list_append (pair->value, string);
+      pair->value = g_list_prepend (pair->value, string);
     }
 
   return FALSE;
@@ -519,13 +516,16 @@ garcon_menu_node_tree_get_string_children (GNode             *tree,
   Pair pair;
 
   pair.type = type;
-  pair.reverse = reverse;
   pair.value = NULL;
   pair.self = tree;
 
   g_node_traverse (tree, G_IN_ORDER, G_TRAVERSE_ALL, 2,
                    (GNodeTraverseFunc) collect_strings, &pair);
 
+  /* Return the list as if we appended */
+  if (!reverse && pair.value != NULL)
+    pair.value = g_list_reverse (pair.value);
+
   return pair.value;
 }
 



More information about the Xfce4-commits mailing list