]> git.puffer.fish Git - matthieu/frr.git/commitdiff
hash: force size to be a power of 2
authorStephen Hemminger <shemminger@vyatta.com>
Fri, 4 Jan 2013 22:29:21 +0000 (22:29 +0000)
committerDavid Lamparter <equinox@opensourcerouting.org>
Sun, 24 Feb 2013 19:42:40 +0000 (20:42 +0100)
By forcing the hash table size to be a power of 2, a potentially
expensive divide can be replaced by a mask operation. Almost all
usage of the hash table was using default size of 1024. Only places
with different size was thread library (1011) and bgp aspath.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
bgpd/bgp_aspath.c
lib/hash.c
lib/hash.h
lib/thread.c

index c37a8897f2e84c9f1e30e511983ab30df3b46e5a..a8b078ff363b1982699f47d57aa54c8381aab013 100644 (file)
@@ -1856,7 +1856,7 @@ aspath_cmp (const void *arg1, const void *arg2)
 void
 aspath_init (void)
 {
-  ashash = hash_create_size (32767, aspath_key_make, aspath_cmp);
+  ashash = hash_create_size (32768, aspath_key_make, aspath_cmp);
 }
 
 void
index 6db79ea77a6ba7c9d1b27702c9d755e69c766830..1e097f284871c6b9145496961b7f34f49931fcb6 100644 (file)
@@ -31,6 +31,7 @@ hash_create_size (unsigned int size, unsigned int (*hash_key) (void *),
 {
   struct hash *hash;
 
+  assert ((size & (size-1)) == 0);
   hash = XMALLOC (MTYPE_HASH, sizeof (struct hash));
   hash->index = XCALLOC (MTYPE_HASH_INDEX,
                         sizeof (struct hash_backet *) * size);
@@ -71,7 +72,7 @@ hash_get (struct hash *hash, void *data, void * (*alloc_func) (void *))
   struct hash_backet *backet;
 
   key = (*hash->hash_key) (data);
-  index = key % hash->size;
+  index = key & (hash->size - 1);
 
   for (backet = hash->index[index]; backet != NULL; backet = backet->next) 
     if (backet->key == key && (*hash->hash_cmp) (backet->data, data))
@@ -125,7 +126,7 @@ hash_release (struct hash *hash, void *data)
   struct hash_backet *pp;
 
   key = (*hash->hash_key) (data);
-  index = key % hash->size;
+  index = key & (hash->size - 1);
 
   for (backet = pp = hash->index[index]; backet; backet = backet->next)
     {
index 4cb772e5797ee6ff209da2ccc73476dee03a7572..a6dd531904e69cc98c4b7729ea7680e016a5bf57 100644 (file)
@@ -41,7 +41,7 @@ struct hash
   /* Hash backet. */
   struct hash_backet **index;
 
-  /* Hash table size. */
+  /* Hash table size. Must be power of 2 */
   unsigned int size;
 
   /* Key make function. */
index 16c92c2467779f68543573d01d6456f3c68c6726..27c29d6c8e37f6971925cfb5e057b8a46c85d08d 100644 (file)
@@ -531,8 +531,8 @@ thread_master_create ()
 {
   if (cpu_record == NULL) 
     cpu_record 
-      = hash_create_size (1011, (unsigned int (*) (void *))cpu_record_hash_key, 
-                          (int (*) (const void *, const void *))cpu_record_hash_cmp);
+      = hash_create ((unsigned int (*) (void *))cpu_record_hash_key,
+                    (int (*) (const void *, const void *))cpu_record_hash_cmp);
     
   return (struct thread_master *) XCALLOC (MTYPE_THREAD_MASTER,
                                           sizeof (struct thread_master));