BPF List
 help / color / mirror / Atom feed
From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
	Hao Peng <flyingpenghao@gmail.com>
Cc: andrii@kernel.org, bpf@vger.kernel.org, yatsenko@meta.com
Subject: Re: [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding
Date: Fri, 4 Dec 2026 05:55:44 +0000	[thread overview]
Message-ID: <b90f47a7-4547-4656-9b72-be3dbfc1b2dd@gmail.com> (raw)
In-Reply-To: <DNBK9GX56BRZ.16TYM14YSNPSF@gmail.com>



On 12/4/26 1:35 AM, Alexei Starovoitov wrote:
> On Thu, Dec 03, 2026 at 05:11 PM Hao Peng <flyingpenghao@gmail.com> wrote:
>>   │ keys max nr   │ fold.     │ gr (new)   │ gr vs fold     │
> 
>>   │ seq 1M 750K│ 82.4    │ 20.1         │ −75.7%        │
>>   ├──────── ┼─────┼──────┼─────────┤
>>   │ seq 1M 1M    │ 79.1   │ 18.2         │ −77.0%        │
> 
> Same as jhash. gr scatters sequential keys over buckets[] too.
> 
>>   │ rand 1K 750  │ 53.6   │ 47.8         │ −10.8%        │
>>   ├────────┼─────┼──────┼─────────┤
>>   │ rand 1K 1K   │ 61.6    │ 53.5         │ −13.2%        │
>>   ├─────────┼────┼──────┼─────────┤
>>   │ rand 1M 750K │ 20.2  │ 18.7         │ −7.8%         │
>>   ├─────────┼────┼──────┼─────────┤
>>   │ rand 1M 1M   │ 19.4   │ 18.3         │ −5.3%         │
> 
> That's about half of the jhash cost.
> 
> Pls rerun the table with the diff below.
> 
> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> index b849110..d882de5 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
> @@ -4,6 +4,7 @@
>   */
>  #include <linux/bpf.h>
>  #include <linux/btf.h>
> +#include <linux/hash.h>
>  #include <linux/jhash.h>
>  #include <linux/filter.h>
>  #include <linux/rculist_nulls.h>
> @@ -3025,11 +3026,18 @@ static __always_inline int rhtab_key_cmp_long(struct rhashtable_compare_arg *arg
>  	return key1 != *(const unsigned long *)key2->data;
>  }
>  
> +/*
> + * rht_bucket_index() takes the low bits of the hash, so fold the top bits of
> + * the product into them. The low 3 bits of the key keep 8 sequential keys in
> + * one cache line of buckets.
> + */
>  static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed)
>  {
>  	u64 k = *(const unsigned long *)data;
> +	u64 x = (k >> 3) ^ seed;
>  
> -	return (u32)(k ^ (k >> 32)) ^ seed;
> +	x = (x ^ (x >> 33)) * GOLDEN_RATIO_64;
> +	return (x >> 32) ^ (x >> 48) ^ (k & 7);
>  }
>  
>  static const struct rhashtable_params rhtab_params_long = {


I tried this variant:
static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed)
{
	u64 k = *(const unsigned long *)data;
	u64 x = ((k >> 3) ^ seed) * GOLDEN_RATIO_64;

	return (u32)swab64(x) ^ (k & 7);
}

which performed a bit better on my setup:
keys  max/nr   xor     jhash            GR Alexei        GR swap
seq   1K/750   81.86   52.90 (-35.4%)   63.49 (-22.4%)   72.34 (-11.6%)
seq   1K/1K    81.32   51.61 (-36.5%)   65.86 (-19.0%)   72.40 (-11.0%)
seq   1M/750K  76.91   19.07 (-75.2%)   48.72 (-36.7%)   48.70 (-36.7%)
seq   1M/1M    76.12   16.52 (-78.3%)   44.74 (-41.2%)   47.19 (-38.0%)
rand  1K/750   67.45   51.09 (-24.3%)   59.08 (-12.4%)   61.22 (-9.2%)
rand  1K/1K    65.89   49.44 (-25.0%)   58.20 (-11.7%)   58.83 (-10.7%)
rand  1M/750K  20.56   18.57 (-9.7%)    20.12 (-2.1%)    19.97 (-2.9%)
rand  1M/1M    17.55   15.88 (-9.5%)    16.92 (-3.6%)    17.18 (-2.1%)

      reply	other threads:[~2026-12-04  5:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 14:03 [PATCH] bpf: Mix the seed into 8-byte resizable hashtab keys before folding Peng Hao
2026-12-01  3:17 ` Alexei Starovoitov
2026-12-02  4:09   ` Hao Peng
2026-12-02  6:29     ` Mykyta Yatsenko
2026-12-03  4:03       ` Alexei Starovoitov
2026-12-04  1:11         ` Hao Peng
2026-12-04  1:35           ` Alexei Starovoitov
2026-12-04  5:55             ` Mykyta Yatsenko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b90f47a7-4547-4656-9b72-be3dbfc1b2dd@gmail.com \
    --to=mykyta.yatsenko5@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=flyingpenghao@gmail.com \
    --cc=yatsenko@meta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox