hash: minor optimization
authorEric Blake <ebb9@byu.net>
Thu, 18 Jun 2009 12:56:13 +0000 (06:56 -0600)
committerEric Blake <ebb9@byu.net>
Thu, 18 Jun 2009 13:20:14 +0000 (07:20 -0600)
* 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>
ChangeLog
lib/hash.c
tests/test-hash.c

index 1b7cd78..09cc027 100644 (file)
--- 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
index 4de1069..e464ce3 100644 (file)
@@ -263,7 +263,7 @@ hash_lookup (const Hash_table *table, const void *entry)
     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 @@ hash_do_for_each (const Hash_table *table, Hash_processor processor,
        {
          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 @@ check_tuning (Hash_table *table)
    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 @@ hash_clear (Hash_table *table)
          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 @@ hash_clear (Hash_table *table)
 
          /* 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 @@ hash_free (Hash_table *table)
          if (bucket->data)
            {
              for (cursor = bucket; cursor; cursor = cursor->next)
-               {
-                 (*table->data_freer) (cursor->data);
-               }
+               table->data_freer (cursor->data);
            }
        }
     }
@@ -769,7 +768,7 @@ hash_find_entry (Hash_table *table, const void *entry,
     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 @@ hash_find_entry (Hash_table *table, const void *entry,
   /* 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;
 
index 2266545..73a3512 100644 (file)
@@ -46,6 +46,7 @@
 static bool
 hash_compare_strings (void const *x, void const *y)
 {
+  ASSERT (x != y);
   return STREQ (x, y) ? true : false;
 }