BPF List
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Hao Peng" <flyingpenghao@gmail.com>
Cc: "Mykyta Yatsenko" <mykyta.yatsenko5@gmail.com>,
	<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, 04 Dec 2026 01:35:17 +0000	[thread overview]
Message-ID: <DNBK9GX56BRZ.16TYM14YSNPSF@gmail.com> (raw)
In-Reply-To: <CAPm50aK9vG0dzsn4J-LeC5BFt2d5R_Yy=wnwgnwZqpTUaa2ckA@mail.gmail.com>

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 = {

      reply	other threads:[~2026-12-04  1:35 UTC|newest]

Thread overview: 7+ 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 [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=DNBK9GX56BRZ.16TYM14YSNPSF@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=flyingpenghao@gmail.com \
    --cc=mykyta.yatsenko5@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