Skip to content

Commit

Permalink
Staging: batman-adv: Remove hashdata_choose_cb from hash
Browse files Browse the repository at this point in the history
Function pointers cannot be inlined by a compiler and thus always has
the overhead of an call. hashdata_choose_cb's are one of the most often
called function pointers and its overhead must kept relative low.

As first step, every function which uses this function pointer takes it
as parameter instead of storing it inside the hash abstraction
structure.

This not generate any performance gain right now. The called functions
must also be able to be inlined by the calling functions to enable
inlining of the function pointer.

Reported-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Sven Eckelmann <sven.eckelmann@gmx.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
  • Loading branch information
Sven Eckelmann authored and Greg Kroah-Hartman committed Nov 29, 2010
1 parent 51f3d8a commit 6d5e654
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 73 deletions.
1 change: 0 additions & 1 deletion drivers/staging/batman-adv/TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
* remove own list functionality from hash
* use hlist_head, hlist_node in hash
* don't use callbacks for choose in hash
* think about more efficient ways instead of abstraction of hash
* Request a new review
* Process the comments from the review
Expand Down
24 changes: 12 additions & 12 deletions drivers/staging/batman-adv/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct hash_it_t *hash_iterate(struct hashtable_t *hash,
}

/* allocates and clears the hash */
struct hashtable_t *hash_new(int size, hashdata_choose_cb choose)
struct hashtable_t *hash_new(int size)
{
struct hashtable_t *hash;

Expand All @@ -156,21 +156,20 @@ struct hashtable_t *hash_new(int size, hashdata_choose_cb choose)

hash_init(hash);

hash->choose = choose;

return hash;
}

/* adds data to the hashtable. returns 0 on success, -1 on error */
int hash_add(struct hashtable_t *hash, hashdata_compare_cb compare, void *data)
int hash_add(struct hashtable_t *hash, hashdata_compare_cb compare,
hashdata_choose_cb choose, void *data)
{
int index;
struct element_t *bucket, *prev_bucket = NULL;

if (!hash)
return -1;

index = hash->choose(data, hash->size);
index = choose(data, hash->size);
bucket = hash->table[index];

while (bucket != NULL) {
Expand Down Expand Up @@ -203,15 +202,15 @@ int hash_add(struct hashtable_t *hash, hashdata_compare_cb compare, void *data)
/* finds data, based on the key in keydata. returns the found data on success,
* or NULL on error */
void *hash_find(struct hashtable_t *hash, hashdata_compare_cb compare,
void *keydata)
hashdata_choose_cb choose, void *keydata)
{
int index;
struct element_t *bucket;

if (!hash)
return NULL;

index = hash->choose(keydata , hash->size);
index = choose(keydata , hash->size);
bucket = hash->table[index];

while (bucket != NULL) {
Expand Down Expand Up @@ -250,11 +249,11 @@ void *hash_remove_bucket(struct hashtable_t *hash, struct hash_it_t *hash_it_t)
* structure you use with just the key filled, we just need the key for
* comparing. */
void *hash_remove(struct hashtable_t *hash, hashdata_compare_cb compare,
void *data)
hashdata_choose_cb choose, void *data)
{
struct hash_it_t hash_it_t;

hash_it_t.index = hash->choose(data, hash->size);
hash_it_t.index = choose(data, hash->size);
hash_it_t.bucket = hash->table[hash_it_t.index];
hash_it_t.prev_bucket = NULL;

Expand All @@ -277,14 +276,15 @@ void *hash_remove(struct hashtable_t *hash, hashdata_compare_cb compare,
/* resize the hash, returns the pointer to the new hash or NULL on
* error. removes the old hash on success. */
struct hashtable_t *hash_resize(struct hashtable_t *hash,
hashdata_compare_cb compare, int size)
hashdata_compare_cb compare,
hashdata_choose_cb choose, int size)
{
struct hashtable_t *new_hash;
struct element_t *bucket;
int i;

/* initialize a new hash with the new size */
new_hash = hash_new(size, hash->choose);
new_hash = hash_new(size);

if (new_hash == NULL)
return NULL;
Expand All @@ -294,7 +294,7 @@ struct hashtable_t *hash_resize(struct hashtable_t *hash,
bucket = hash->table[i];

while (bucket != NULL) {
hash_add(new_hash, compare, bucket->data);
hash_add(new_hash, compare, choose, bucket->data);
bucket = bucket->next;
}
}
Expand Down
19 changes: 11 additions & 8 deletions drivers/staging/batman-adv/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
* return 0 if same and not 0 if not
* same */
typedef int (*hashdata_compare_cb)(void *, void *);

/* the hashfunction, should return an index
* based on the key in the data of the first
* argument and the size the second */
typedef int (*hashdata_choose_cb)(void *, int);
typedef void (*hashdata_free_cb)(void *, void *);

Expand All @@ -51,13 +55,10 @@ struct hashtable_t {
struct element_t **table; /* the hashtable itself, with the buckets */
int elements; /* number of elements registered */
int size; /* size of hashtable */
hashdata_choose_cb choose; /* the hashfunction, should return an index
* based on the key in the data of the first
* argument and the size the second */
};

/* allocates and clears the hash */
struct hashtable_t *hash_new(int size, hashdata_choose_cb choose);
struct hashtable_t *hash_new(int size);

/* remove bucket (this might be used in hash_iterate() if you already found the
* bucket you want to delete and don't need the overhead to find it again with
Expand All @@ -74,24 +75,26 @@ void hash_delete(struct hashtable_t *hash, hashdata_free_cb free_cb, void *arg);
void hash_destroy(struct hashtable_t *hash);

/* adds data to the hashtable. returns 0 on success, -1 on error */
int hash_add(struct hashtable_t *hash, hashdata_compare_cb compare, void *data);
int hash_add(struct hashtable_t *hash, hashdata_compare_cb compare,
hashdata_choose_cb choose, void *data);

/* removes data from hash, if found. returns pointer do data on success, so you
* can remove the used structure yourself, or NULL on error . data could be the
* structure you use with just the key filled, we just need the key for
* comparing. */
void *hash_remove(struct hashtable_t *hash, hashdata_compare_cb compare,
void *data);
hashdata_choose_cb choose, void *data);

/* finds data, based on the key in keydata. returns the found data on success,
* or NULL on error */
void *hash_find(struct hashtable_t *hash, hashdata_compare_cb compare,
void *keydata);
hashdata_choose_cb choose, void *keydata);

/* resize the hash, returns the pointer to the new hash or NULL on
* error. removes the old hash on success */
struct hashtable_t *hash_resize(struct hashtable_t *hash,
hashdata_compare_cb compare, int size);
hashdata_compare_cb compare,
hashdata_choose_cb choose, int size);

/* iterate though the hash. first element is selected with iter_in NULL. use
* the returned iterator to access the elements until hash_it_t returns NULL. */
Expand Down
2 changes: 1 addition & 1 deletion drivers/staging/batman-adv/icmp_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,

spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
compare_orig,
compare_orig, choose_orig,
icmp_packet->dst));

if (!orig_node)
Expand Down
21 changes: 0 additions & 21 deletions drivers/staging/batman-adv/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,6 @@ void dec_module_count(void)
module_put(THIS_MODULE);
}

/* hashfunction to choose an entry in a hash table of given size */
/* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
int choose_orig(void *data, int32_t size)
{
unsigned char *key = data;
uint32_t hash = 0;
size_t i;

for (i = 0; i < 6; i++) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}

hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);

return hash % size;
}

int is_my_mac(uint8_t *addr)
{
struct batman_if *batman_if;
Expand Down
1 change: 0 additions & 1 deletion drivers/staging/batman-adv/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ int mesh_init(struct net_device *soft_iface);
void mesh_free(struct net_device *soft_iface);
void inc_module_count(void);
void dec_module_count(void);
int choose_orig(void *data, int32_t size);
int is_my_mac(uint8_t *addr);
int is_bcast(uint8_t *addr);
int is_mcast(uint8_t *addr);
Expand Down
11 changes: 8 additions & 3 deletions drivers/staging/batman-adv/originator.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int originator_init(struct bat_priv *bat_priv)
return 1;

spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
bat_priv->orig_hash = hash_new(128, choose_orig);
bat_priv->orig_hash = hash_new(128);

if (!bat_priv->orig_hash)
goto err;
Expand Down Expand Up @@ -128,9 +128,11 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
struct orig_node *orig_node;
struct hashtable_t *swaphash;
int size;
int hash_added;

orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
compare_orig, addr));
compare_orig, choose_orig,
addr));

if (orig_node)
return orig_node;
Expand Down Expand Up @@ -167,11 +169,14 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
if (!orig_node->bcast_own_sum)
goto free_bcast_own;

if (hash_add(bat_priv->orig_hash, compare_orig, orig_node) < 0)
hash_added = hash_add(bat_priv->orig_hash, compare_orig, choose_orig,
orig_node);
if (hash_added < 0)
goto free_bcast_own_sum;

if (bat_priv->orig_hash->elements * 4 > bat_priv->orig_hash->size) {
swaphash = hash_resize(bat_priv->orig_hash, compare_orig,
choose_orig,
bat_priv->orig_hash->size * 2);

if (!swaphash)
Expand Down
21 changes: 21 additions & 0 deletions drivers/staging/batman-adv/originator.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,25 @@ static inline int compare_orig(void *data1, void *data2)
return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
}

/* hashfunction to choose an entry in a hash table of given size */
/* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
static inline int choose_orig(void *data, int32_t size)
{
unsigned char *key = data;
uint32_t hash = 0;
size_t i;

for (i = 0; i < 6; i++) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}

hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);

return hash % size;
}

#endif /* _NET_BATMAN_ADV_ORIGINATOR_H_ */
13 changes: 7 additions & 6 deletions drivers/staging/batman-adv/routing.c
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ static int recv_my_icmp_packet(struct bat_priv *bat_priv,
/* get routing information */
spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
compare_orig,
compare_orig, choose_orig,
icmp_packet->orig));
ret = NET_RX_DROP;

Expand Down Expand Up @@ -874,7 +874,7 @@ static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
/* get routing information */
spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
orig_node = ((struct orig_node *)
hash_find(bat_priv->orig_hash, compare_orig,
hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
icmp_packet->orig));
ret = NET_RX_DROP;

Expand Down Expand Up @@ -969,7 +969,7 @@ int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
/* get routing information */
spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
orig_node = ((struct orig_node *)
hash_find(bat_priv->orig_hash, compare_orig,
hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
icmp_packet->dst));

if ((orig_node != NULL) &&
Expand Down Expand Up @@ -1042,7 +1042,8 @@ struct neigh_node *find_router(struct bat_priv *bat_priv,
primary_orig_node = router_orig;
} else {
primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig,
router_orig->primary_addr);
choose_orig,
router_orig->primary_addr);

if (!primary_orig_node)
return orig_node->router;
Expand Down Expand Up @@ -1147,7 +1148,7 @@ int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
/* get routing information */
spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
orig_node = ((struct orig_node *)
hash_find(bat_priv->orig_hash, compare_orig,
hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
unicast_packet->dest));

router = find_router(bat_priv, orig_node, recv_if);
Expand Down Expand Up @@ -1294,7 +1295,7 @@ int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)

spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
orig_node = ((struct orig_node *)
hash_find(bat_priv->orig_hash, compare_orig,
hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
bcast_packet->orig));

if (orig_node == NULL) {
Expand Down
Loading

0 comments on commit 6d5e654

Please sign in to comment.