This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gfortran, committed] Cleanup gfc_find_gsymbol



Hi,


my previous patch reminded me that we're still using strcmp to establish the ordering in our binary tree of symbols instead of pointer comparisons. I refrained from fixing this to use pointer comparisons, because I didn't want to dive into the wondrous world that is the C standard to find out, if --or how-- one can assume any ordering relation between pointers, necessary to build up a binary tree.

Anyway, looking over the code I spotted that gfc_find_gsymbol unnecessarily descends into both branches of the global symbol tree, so I fixed it to do The Right Thing.

Committed as r123355 under the obviously correct rule after the obligatory testing on i386-darwin.

Cheers,
- Tobi

2007-03-30  Tobias Schlüter  <tobi@gcc.gnu.org>

	* symbol.c (gfc_find_gsymbol): Simplify, don't unconditionally
	descend into all branches.

Index: symbol.c
===================================================================
--- symbol.c	(revision 123338)
+++ symbol.c	(working copy)
@@ -2770,20 +2770,19 @@ gfc_symbol_state(void) {
 gfc_gsymbol *
 gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
 {
-  gfc_gsymbol *s;
+  int c;
 
   if (symbol == NULL)
     return NULL;
-  if (strcmp (symbol->name, name) == 0)
-    return symbol;
 
-  s = gfc_find_gsymbol (symbol->left, name);
-  if (s != NULL)
-    return s;
+  while (symbol)
+    {
+      c = strcmp (name, symbol->name);
+      if (!c)
+	return symbol;
 
-  s = gfc_find_gsymbol (symbol->right, name);
-  if (s != NULL)
-    return s;
+      symbol = (c < 0) ? symbol->left : symbol->right;
+    }
 
   return NULL;
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]