Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752286AbcD3NE2 (ORCPT ); Sat, 30 Apr 2016 09:04:28 -0400 Received: from www.linutronix.de ([62.245.132.108]:37367 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752149AbcD3NE0 (ORCPT ); Sat, 30 Apr 2016 09:04:26 -0400 Date: Sat, 30 Apr 2016 15:02:47 +0200 (CEST) From: Thomas Gleixner To: Linus Torvalds cc: 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 Subject: Re: [patch 2/7] lib/hashmod: Add modulo based hash mechanism In-Reply-To: Message-ID: References: <20160428161742.363543816@linutronix.de> <20160428163525.495514517@linutronix.de> User-Agent: Alpine 2.11 (DEB 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1641 Lines: 57 On Thu, 28 Apr 2016, Linus Torvalds wrote: > It's the hashes that _look_ like they might be good hashes, but > there's not a lot of analysis behind it, that I would worry about. The > simple prime modulus _should_ be fine, but at the same time I kind of > suspect we can do better. Especially since it has two multiplications. > > Looking around, there's > > http://burtleburtle.net/bob/hash/integer.html > > and that 32-bit "full avalanche" hash in six shifts looks like it > could be better. You wouldn't want to inline it, but the point of a > full avalanche bit mixing _should_ be that you could avoid the whole > "upper bits" part, and it should work independently of the target set > size. 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. Thanks, tglx