Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752188AbcD3QpS (ORCPT ); Sat, 30 Apr 2016 12:45:18 -0400 Received: from mail-qg0-f52.google.com ([209.85.192.52]:35631 "EHLO mail-qg0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750760AbcD3QpQ (ORCPT ); Sat, 30 Apr 2016 12:45:16 -0400 Message-ID: <1462034711.5535.169.camel@edumazet-glaptop3.roam.corp.google.com> Subject: Re: [patch 2/7] lib/hashmod: Add modulo based hash mechanism From: Eric Dumazet To: Thomas Gleixner Cc: Linus Torvalds , LKML , Peter Zijlstra , Ingo Molnar , Andrew Morton , Sebastian Andrzej Siewior , Darren Hart , Michael Kerrisk , Davidlohr Bueso , Chris Mason , "Carlos O'Donell" , Torvald Riegel , Eric Dumazet Date: Sat, 30 Apr 2016 09:45:11 -0700 In-Reply-To: References: <20160428161742.363543816@linutronix.de> <20160428163525.495514517@linutronix.de> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.10.4-0ubuntu2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1834 Lines: 64 On Sat, 2016-04-30 at 15:02 +0200, Thomas Gleixner wrote: > Yes. So I tested those two: > > u32 hash_64(u64 key) > { > key = ~key + (key << 18); > key ^= key >> 31; > key += (key << 2)) + (key << 4); > key ^= key >> 11; > key += key << 6; > key ^= key >> 22; > return (u32) key; > } > > u32 hash_32(u32 key) > { > key = (key + 0x7ed55d16) + (key << 12); > key = (key ^ 0xc761c23c) ^ (key >> 19); > key = (key + 0x165667b1) + (key << 5); > key = (key + 0xd3a2646c) ^ (key << 9); > key = (key + 0xfd7046c5) + (key << 3); > key = (key ^ 0xb55a4f09) ^ (key >> 16); > return key; > } > > They are really good and the results are similar to the simple modulo prime > hash. hash64 is slightly faster as the modulo prime as it does not have the > multiplication. > > I'll send a patch to replace hash_64 and hash_32. > > Text size: > x86_64 i386 arm > hash_64 88 148 128 > hash_32 88 84 112 > > So probably slightly too large to inline. I use hash_32() in net/sched/sch_fq.c, for all packets sent by Google servers. (Note that I did _not_ use hash_ptr()) That's gazillions of packets per second, and the current multiply worked just fine in term of hash spreading. Are you really going to use something which looks much slower ? u32 hash_32(u32 key) { key = (key + 0x7ed55d16) + (key << 12); key = (key ^ 0xc761c23c) ^ (key >> 19); key = (key + 0x165667b1) + (key << 5); key = (key + 0xd3a2646c) ^ (key << 9); key = (key + 0xfd7046c5) + (key << 3); key = (key ^ 0xb55a4f09) ^ (key >> 16); return key; } Probably having a simple multiple when ARCH_HAS_FAST_MULTIPLIER is defined might be good enough, eventually by choosing a better GOLDEN_RATIO_PRIME_32