Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rhashtable: consolidate hash computation in rht_key_get_hash()

The else-if and else branches in rht_key_get_hash() both compute a hash
using either params.hashfn or jhash, differing only in the source of
key_len (params.key_len vs ht->p.key_len). Merge the two branches into
one by using the ternary `params.key_len ?: ht->p.key_len` to select
the key length, removing the duplicated logic.

This also improves the performance of the else branch which previously
always used jhash and never fell through to jhash2. This branch is going
to be used by BPF resizable hashmap, which wraps rhashtable:
https://lore.kernel.org/bpf/20260205-rhash-v1-0-30dd6d63c462@meta.com/

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Mykyta Yatsenko and committed by
Herbert Xu
c9429bf5 68e96c97

+3 -10
+3 -10
include/linux/rhashtable.h
··· 129 129 unsigned int hash; 130 130 131 131 /* params must be equal to ht->p if it isn't constant. */ 132 - if (!__builtin_constant_p(params.key_len)) 132 + if (!__builtin_constant_p(params.key_len)) { 133 133 hash = ht->p.hashfn(key, ht->key_len, hash_rnd); 134 - else if (params.key_len) { 135 - unsigned int key_len = params.key_len; 134 + } else { 135 + unsigned int key_len = params.key_len ? : ht->p.key_len; 136 136 137 137 if (params.hashfn) 138 138 hash = params.hashfn(key, key_len, hash_rnd); ··· 140 140 hash = jhash(key, key_len, hash_rnd); 141 141 else 142 142 hash = jhash2(key, key_len / sizeof(u32), hash_rnd); 143 - } else { 144 - unsigned int key_len = ht->p.key_len; 145 - 146 - if (params.hashfn) 147 - hash = params.hashfn(key, key_len, hash_rnd); 148 - else 149 - hash = jhash(key, key_len, hash_rnd); 150 143 } 151 144 152 145 return hash;