changeset 11636:95fd221d763c

hash: minor optimization * lib/hash.c (hash_lookup, hash_find_entry): Avoid function call when possible. (hash_initialize): Document this promise. (hash_do_for_each, hash_clear, hash_free): Use C89 syntax. * tests/test-hash.c (hash_compare_strings): Test this. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Thu, 18 Jun 2009 06:56:13 -0600
parents a395edd0f5a4
children 0b026e877c96
files ChangeLog lib/hash.c tests/test-hash.c
diffstat 3 files changed, 20 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2009-06-18  Eric Blake  <ebb9@byu.net>
+
+	hash: minor optimization
+	* lib/hash.c (hash_lookup, hash_find_entry): Avoid function call
+	when possible.
+	(hash_initialize): Document this promise.
+	(hash_do_for_each, hash_clear, hash_free): Use C89 syntax.
+	* tests/test-hash.c (hash_compare_strings): Test this.
+
 2009-06-18  Bruno Haible  <bruno@clisp.org>
 
 	* m4/strstr.m4 (gl_FUNC_STRSTR): Skip linear time test if strstr is
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -263,7 +263,7 @@
     return NULL;
 
   for (cursor = bucket; cursor; cursor = cursor->next)
-    if (table->comparator (entry, cursor->data))
+    if (entry == cursor->data || table->comparator (entry, cursor->data))
       return cursor->data;
 
   return NULL;
@@ -373,7 +373,7 @@
 	{
 	  for (cursor = bucket; cursor; cursor = cursor->next)
 	    {
-	      if (!(*processor) (cursor->data, processor_data))
+	      if (! processor (cursor->data, processor_data))
 		return counter;
 	      counter++;
 	    }
@@ -535,7 +535,8 @@
    The user-supplied COMPARATOR function should be provided.  It accepts two
    arguments pointing to user data, it then returns true for a pair of entries
    that compare equal, or false otherwise.  This function is internally called
-   on entries which are already known to hash to the same bucket index.
+   on entries which are already known to hash to the same bucket index,
+   but which are distinct pointers.
 
    The user-supplied DATA_FREER function, when not NULL, may be later called
    with the user data as an argument, just before the entry containing the
@@ -628,7 +629,7 @@
 	  for (cursor = bucket->next; cursor; cursor = next)
 	    {
 	      if (table->data_freer)
-		(*table->data_freer) (cursor->data);
+		table->data_freer (cursor->data);
 	      cursor->data = NULL;
 
 	      next = cursor->next;
@@ -640,7 +641,7 @@
 
 	  /* Free the bucket head.  */
 	  if (table->data_freer)
-	    (*table->data_freer) (bucket->data);
+	    table->data_freer (bucket->data);
 	  bucket->data = NULL;
 	  bucket->next = NULL;
 	}
@@ -670,9 +671,7 @@
 	  if (bucket->data)
 	    {
 	      for (cursor = bucket; cursor; cursor = cursor->next)
-		{
-		  (*table->data_freer) (cursor->data);
-		}
+		table->data_freer (cursor->data);
 	    }
 	}
     }
@@ -769,7 +768,7 @@
     return NULL;
 
   /* See if the entry is the first in the bucket.  */
-  if ((*table->comparator) (entry, bucket->data))
+  if (entry == bucket->data || table->comparator (entry, bucket->data))
     {
       void *data = bucket->data;
 
@@ -796,7 +795,8 @@
   /* Scan the bucket overflow.  */
   for (cursor = bucket; cursor->next; cursor = cursor->next)
     {
-      if ((*table->comparator) (entry, cursor->next->data))
+      if (entry == cursor->next->data
+	  || table->comparator (entry, cursor->next->data))
 	{
 	  void *data = cursor->next->data;
 
--- a/tests/test-hash.c
+++ b/tests/test-hash.c
@@ -46,6 +46,7 @@
 static bool
 hash_compare_strings (void const *x, void const *y)
 {
+  ASSERT (x != y);
   return STREQ (x, y) ? true : false;
 }