netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Graf <tgraf@suug.ch>
To: "David S. Miller" <davem@davemloft.net>
Cc: hadi@cyberus.ca, lark@linux.net.cn, netdev@oss.sgi.com
Subject: [RFC] dynamic hash table size & xor hash function for cls_fw
Date: Thu, 7 Apr 2005 02:55:19 +0200	[thread overview]
Message-ID: <20050407005519.GS26731@postel.suug.ch> (raw)
In-Reply-To: <20050406183134.GR26731@postel.suug.ch>

* Thomas Graf <20050406183134.GR26731@postel.suug.ch> 2005-04-06 20:31
> Yes, it sounds pretty good. I can't find any scenario where it
> would perform unacceptable, it's not perfect but fair enough
> for everyone I guess.

Changes the hashtable size to PAGE_SIZE/sizeof(unsigned long) and
adapts the hashing algorithm to xor various buckets in order to
maintain collision free hashing for the most common use case
while providing some kind of distribution for more rare cases.

The hashing function looks really ugly, it is, but gcc will optimize
away all the uneeded branches which will make it short again. The hash
case for 10bits hash table ignores the most significant 2 bits, I
don't think this has any impact but would rather be wasted cycles.
It looks inefficient but the manual shifting & xoring only takes one
instruction more than the HTSIZE == 256 case on my x86 box so the
increased hashing area should be worth it.

Thoughts?


===== net/sched/cls_fw.c 1.21 vs edited =====
--- 1.21/net/sched/cls_fw.c	2005-03-29 02:45:46 +02:00
+++ edited/net/sched/cls_fw.c	2005-04-07 02:41:46 +02:00
@@ -46,9 +46,11 @@
 #include <net/act_api.h>
 #include <net/pkt_cls.h>
 
+#define HTSIZE (PAGE_SIZE/sizeof(struct fw_filter *))
+
 struct fw_head
 {
-	struct fw_filter *ht[256];
+	struct fw_filter *ht[HTSIZE];
 };
 
 struct fw_filter
@@ -69,7 +71,28 @@
 
 static __inline__ int fw_hash(u32 handle)
 {
-	return handle&0xFF;
+	if (HTSIZE == 4096)
+		return ((handle >> 24) & 0xFFF) ^
+		       ((handle >> 12) & 0xFFF) ^
+		       (handle & 0xFFF);
+	else if (HTSIZE == 2048)
+		return ((handle >> 22) & 0x7FF) ^
+		       ((handle >> 11) & 0x7FF) ^
+		       (handle & 0x7FF);
+	else if (HTSIZE == 1024)
+		return ((handle >> 20) & 0x3FF) ^
+		       ((handle >> 10) & 0x3FF) ^
+		       (handle & 0x3FF);
+	else if (HTSIZE == 512)
+		return (handle >> 27) ^
+		       ((handle >> 18) & 0x1FF) ^
+		       ((handle >> 9) & 0x1FF) ^
+		       (handle & 0x1FF);
+	else if (HTSIZE == 256) {
+		u8 *t = (u8 *) &handle;
+		return t[0] ^ t[1] ^ t[2] ^ t[3];
+	} else 
+		return handle & (HTSIZE - 1);
 }
 
 static int fw_classify(struct sk_buff *skb, struct tcf_proto *tp,
@@ -152,7 +175,7 @@
 	if (head == NULL)
 		return;
 
-	for (h=0; h<256; h++) {
+	for (h=0; h<HTSIZE; h++) {
 		while ((f=head->ht[h]) != NULL) {
 			head->ht[h] = f->next;
 			fw_delete_filter(tp, f);
@@ -291,7 +314,7 @@
 	if (arg->stop)
 		return;
 
-	for (h = 0; h < 256; h++) {
+	for (h = 0; h < HTSIZE; h++) {
 		struct fw_filter *f;
 
 		for (f = head->ht[h]; f; f = f->next) {

  reply	other threads:[~2005-04-07  0:55 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-05  5:35 [PATCH] improvement on net/sched/cls_fw.c's hash function Wang Jian
2005-04-05  5:37 ` David S. Miller
2005-04-05  6:05   ` Wang Jian
2005-04-05 10:25     ` jamal
2005-04-05 10:38     ` Thomas Graf
2005-04-05 11:25       ` Wang Jian
2005-04-05 12:16         ` Thomas Graf
2005-04-05 12:39           ` Wang Jian
2005-04-05 12:52             ` Thomas Graf
2005-04-05 13:29               ` Wang Jian
2005-04-05 12:54             ` jamal
2005-04-05 14:18               ` Wang Jian
2005-04-05 16:11                 ` jamal
2005-04-06  6:45                   ` Wang Jian
2005-04-06 12:16                     ` jamal
2005-04-06 12:30                     ` Thomas Graf
2005-04-06 13:01                       ` Wang Jian
2005-04-06 13:34                       ` jamal
2005-04-06 13:45                         ` Thomas Graf
2005-04-06 14:10                           ` Thomas Graf
2005-04-06 18:15                             ` David S. Miller
2005-04-06 18:31                               ` Thomas Graf
2005-04-07  0:55                                 ` Thomas Graf [this message]
2005-04-07 10:38                                   ` [RFC] dynamic hash table size & xor hash function for cls_fw jamal
2005-04-07 10:47                                     ` Wang Jian
2005-04-07 10:51                                     ` Thomas Graf
2005-04-07 11:07                                       ` jamal
2005-04-07 13:09                                         ` [PATCH] [PKT_SCHED]: improve hashing performance of cls_fw Thomas Graf
2005-04-07 13:31                                           ` Wang Jian
2005-04-07 13:52                                             ` Thomas Graf
2005-04-07 14:03                                               ` Wang Jian
2005-04-06 13:36                       ` [PATCH] improvement on net/sched/cls_fw.c's hash function Eran Mann
2005-04-06 13:53                         ` Wang Jian

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=20050407005519.GS26731@postel.suug.ch \
    --to=tgraf@suug.ch \
    --cc=davem@davemloft.net \
    --cc=hadi@cyberus.ca \
    --cc=lark@linux.net.cn \
    --cc=netdev@oss.sgi.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;
as well as URLs for NNTP newsgroup(s).