summaryrefslogtreecommitdiffstats
path: root/lib/hash.c
diff options
context:
space:
mode:
authorDonald Sharp <sharpd@cumulusnetworks.com>2018-06-17 01:27:41 +0200
committerDonald Sharp <sharpd@cumulusnetworks.com>2018-06-17 02:09:45 +0200
commit626e8d0a9b4830438276947d0c76d1fbf4c7884c (patch)
tree5cce0ff596a34a13e2c813c8a936eb693b56fcef /lib/hash.c
parentlib, vtysh: Add 'show thread poll' command (diff)
downloadfrr-626e8d0a9b4830438276947d0c76d1fbf4c7884c.tar.xz
frr-626e8d0a9b4830438276947d0c76d1fbf4c7884c.zip
lib: A small optimization for the hash iterate and walk functions
When we are iterating through the hash, keep count of how many we've called and if we have finished calling the hash->size iterator times, then short-circuit and stop looping over the entire array. Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
Diffstat (limited to 'lib/hash.c')
-rw-r--r--lib/hash.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/lib/hash.c b/lib/hash.c
index ee5401b23..37f6cdcc8 100644
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -241,15 +241,21 @@ void hash_iterate(struct hash *hash, void (*func)(struct hash_backet *, void *),
unsigned int i;
struct hash_backet *hb;
struct hash_backet *hbnext;
+ uint32_t count = 0;
- for (i = 0; i < hash->size; i++)
+ for (i = 0; i < hash->size; i++) {
for (hb = hash->index[i]; hb; hb = hbnext) {
/* get pointer to next hash backet here, in case (*func)
* decides to delete hb by calling hash_release
*/
hbnext = hb->next;
(*func)(hb, arg);
+ count++;
+
}
+ if (count == hash->count)
+ return;
+ }
}
void hash_walk(struct hash *hash, int (*func)(struct hash_backet *, void *),
@@ -259,6 +265,7 @@ void hash_walk(struct hash *hash, int (*func)(struct hash_backet *, void *),
struct hash_backet *hb;
struct hash_backet *hbnext;
int ret = HASHWALK_CONTINUE;
+ uint32_t count = 0;
for (i = 0; i < hash->size; i++) {
for (hb = hash->index[i]; hb; hb = hbnext) {
@@ -269,7 +276,10 @@ void hash_walk(struct hash *hash, int (*func)(struct hash_backet *, void *),
ret = (*func)(hb, arg);
if (ret == HASHWALK_ABORT)
return;
+ count++;
}
+ if (count == hash->count)
+ return;
}
}