]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: A small optimization for the hash iterate and walk functions 2474/head
authorDonald Sharp <sharpd@cumulusnetworks.com>
Sat, 16 Jun 2018 23:27:41 +0000 (19:27 -0400)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Sun, 17 Jun 2018 00:09:45 +0000 (20:09 -0400)
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>
lib/hash.c
vtysh/vtysh.c

index ee5401b2367a4816111439863c3b67f1d8df51d7..37f6cdcc8f86a4c3d65e405131942fa2d1fe1367 100644 (file)
@@ -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;
        }
 }
 
index 4ac7448394a3ca70411956ff87b187c06005704f..65223d02bd4652cdb1d819de65dcd5f6580970c1 100644 (file)
@@ -2144,7 +2144,6 @@ DEFUN (vtysh_show_poll,
        "Thread Poll Information\n")
 {
        unsigned int i;
-       int idx = 0;
        int ret = CMD_SUCCESS;
        char line[100];