Received: from eggs.gnu.org ([2001:4830:134:3::10]:35934) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yq2P9-00073j-Ix for qemu-devel@nongnu.org; Wed, 06 May 2015 12:42:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yq2P4-0004oE-HU for qemu-devel@nongnu.org; Wed, 06 May 2015 12:42:39 -0400 Date: Wed, 6 May 2015 17:42:30 +0100 From: Stefan Hajnoczi Message-ID: <20150506164230.GF2386@stefanha-thinkpad.redhat.com> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="H4SyuGOnfnj3aJqJ" Content-Disposition: inline In-Reply-To: Subject: Re: [Qemu-devel] [Qemu-block] [PATCH 5/6] qcow2: use a hash to look for entries in the L2 cache List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Alberto Garcia Cc: Stefan Hajnoczi , qemu-devel@nongnu.org, qemu-block@nongnu.org --H4SyuGOnfnj3aJqJ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Apr 30, 2015 at 01:11:44PM +0300, Alberto Garcia wrote: > By using a hash of the offset as the starting point, lookups are > faster and independent from the array size. >=20 > The hash is computed using the cluster number of the table, multiplied > by 4 to make it perform better when there are collisions. =2E.. > + i =3D lookup_index =3D (offset / c->table_size * 4) % c->size; The hash function is very weak. It's better than always starting with i=3D0 but mixes input bits very poorly. Have you considered an integer hash function so that more input bits affect the output? http://burtleburtle.net/bob/hash/integer.html Regarding collisions, the multiply-by-4 effectively reduces the hash space by a factor of 4. This *increases* the chance of collisions in the hope that the extra slots will prevent chains from forming. Using an integer hash function ought to be more resistant to input value patterns and eliminate the need for multiply-by-4, because chains only form when the table load is high. Finally, the hash optimizes for the sparsely occupied table scenario. If the table is loaded (dense) then there will be chains in any case. A full search will be necessary. Feel free to leave the hash function as-is, I don't think it's a huge issue either way. Stefan --H4SyuGOnfnj3aJqJ Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQEcBAEBAgAGBQJVSkR2AAoJEJykq7OBq3PIwvIH/14xLcBRXV6gMbOdsnN8KsyK qiGwqHMbUf9VW8ZVwe296vSXrx/FCbIw4KCuuqO5vJXRDpgwqRYqd6jc6qfuYfwa PilZ+pEspHgeJHpAH5wLD9PbwNO+8aUDGFf0894SURS/VbWe1LwlxMWev0W393OM 56TuCBXNa1KUDmBrG4f3bI2bVdwONU6KhvQEsFErwjUTK8PIfyXeOyjAwA9mAVbJ y5jyKkIKOwO8HdaBfK//xbCJOmV5lkwg2SPaTu+QCn35sqoLHG/vnt5x7ch19EKX wCYwiXSM7GenirQNnPEpA5OdnXcYLe3B+luYYw8ahR25QH/JH+3TrS1xBdjGfRk= =FvWg -----END PGP SIGNATURE----- --H4SyuGOnfnj3aJqJ--