From 16908fc83e83a08e4f0e8c004141ff01ae12ca86 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 18 Jun 2009 06:56:13 -0600 Subject: [PATCH] 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 --- ChangeLog | 9 +++++++++ lib/hash.c | 20 ++++++++++---------- tests/test-hash.c | 1 + 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1b7cd7865..09cc027ab 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2009-06-18 Eric Blake + + 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 * m4/strstr.m4 (gl_FUNC_STRSTR): Skip linear time test if strstr is diff --git a/lib/hash.c b/lib/hash.c index 4de106960..e464ce390 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 2266545cb..73a351206 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; } -- 2.11.0