diff options
author | Eric Blake <ebb9@byu.net> | 2009-06-18 06:56:13 -0600 |
---|---|---|
committer | Eric Blake <ebb9@byu.net> | 2009-06-18 07:20:14 -0600 |
commit | 16908fc83e83a08e4f0e8c004141ff01ae12ca86 (patch) | |
tree | b24d1eaae052a92d4299ca133ec249b7b76c5c75 | |
parent | 9f950c184f8b242fb134472f862ba2a99347b8b3 (diff) | |
download | gnulib-16908fc83e83a08e4f0e8c004141ff01ae12ca86.tar.gz |
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>
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | lib/hash.c | 20 | ||||
-rw-r--r-- | tests/test-hash.c | 1 |
3 files changed, 20 insertions, 10 deletions
@@ -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 diff --git a/lib/hash.c b/lib/hash.c index 4de106960c..e464ce3904 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -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; diff --git a/tests/test-hash.c b/tests/test-hash.c index 2266545cba..73a3512068 100644 --- 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; } |